#include <stdio.h>
#include <stdlib.h>
#include "http.h"
int
main(int argc,
char *argv[])
{
int i;
http_t *http;
http_status_t status;
char buffer[8192];
long bytes;
FILE *out;
char host[HTTP_MAX_URI],
method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
long length, total;
time_t start, current;
http = NULL;
out = stdout;
for (i = 1; i < argc; i ++)
{
if (strcmp(argv[i], "-o") == 0)
{
i ++;
out = fopen(argv[i], "wb");
continue;
}
httpSeparate(argv[i], method, username, host, &port, resource);
http = httpConnect(host, port);
if (http == NULL)
{
perror(host);
continue;
}
printf("Requesting file \"%s\"...\n", resource);
httpClearFields(http);
httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
httpGet(http, resource);
while ((status = httpUpdate(http)) == HTTP_CONTINUE);
if (status == HTTP_OK)
puts("GET OK:");
else
printf("GET failed with status %d...\n", status);
start = time(NULL);
length = atoi(httpGetField(http, HTTP_FIELD_CONTENT_LENGTH));
total = 0;
while ((bytes = httpRead(http, buffer, sizeof(buffer))) > 0)
{
total += bytes;
fwrite(buffer, bytes, 1, out);
if (out != stdout)
{
current = time(NULL);
if (current == start) current ++;
printf("\r%ld/%ld bytes (%ld bytes/sec) ", total, length,
total / (current - start));
fflush(stdout);
}
}
}
puts("Closing connection to server...");
httpClose(http);
if (out != stdout)
fclose(out);
return (0);
}