#include <stdio.h>
#include <stdlib.h>
#include "http.h"
#include <config.h>
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
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;
off_t 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 = strtoimax(httpGetField(http, HTTP_FIELD_CONTENT_LENGTH), (char **)NULL, 10);
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%" PRIdMAX "/%" PRIdMAX " bytes (%ld bytes/sec) ",
(intmax_t)total, (intmax_t)length,
(long)(total / (current - start)));
fflush(stdout);
}
}
}
puts("Closing connection to server...");
httpClose(http);
if (out != stdout)
fclose(out);
return (0);
}