finished writing header parser

This commit is contained in:
Fufu Fang 2018-07-19 10:07:13 +01:00
parent 63925a2832
commit af5b5e9486
3 changed files with 22 additions and 4 deletions

View File

19
http.c
View File

@ -10,9 +10,6 @@
* instead of (only) local files. Local files (ie those that can be directly
* fopened) will drop back to using the underlying clib implementations
*
* See the main() function at the bottom that shows an app that retrieves from
* a specified url using fgets() and fread() and saves as two output files.
*
* Copyright (c) 2003, 2017 Simtec Electronics
*
* Re-implemented by Vincent Sanders <vince@kyllikki.org> with extensive
@ -118,6 +115,22 @@ static size_t header_callback(char *buffer, size_t size,
memcpy(&url->header[url->header_pos], buffer, size);
url->header_pos += size;
char *hf;
hf = "Accept-Ranges:";
if (!strncasecmp(buffer, hf, strlen(hf))) {
url->accept_range = 1;
}
hf = "Content-Length: ";
if (!strncasecmp(buffer, hf, strlen(hf))) {
/*
* We are doing this, because libcurl documentation says
*"Do not assume that the header line is zero terminated!"
*/
char *tmp = malloc((nitems) * sizeof(char));
tmp[nitems] = '\0';
strncpy(tmp, buffer, nitems);
url->content_length = atoi(strchr(tmp, ' ')+1);
}
return size;
}

7
test.c
View File

@ -22,7 +22,7 @@ int http_test()
/* ---------------------------Test fgets ----------------------------*/
/* open the input file */
handle = url_fopen(url, "rh");
handle = url_fopen(url, "hr");
if(!handle) {
printf("couldn't url_fopen() %s\n", url);
return 2;
@ -40,7 +40,12 @@ int http_test()
url_fgets(buffer, sizeof(buffer), handle);
fwrite(buffer, 1, strlen(buffer), outf);
}
/* Print the header */
printf(handle->header);
printf("\n");
printf("accept-range: %d\n", handle->accept_range);
printf("filesize: %d\n", handle->content_length);
/* close the handles for the fgets test*/
url_fclose(handle);