From af5b5e9486f5b30396a1302d07f09d2848804163 Mon Sep 17 00:00:00 2001 From: Fufu Fang Date: Thu, 19 Jul 2018 10:07:13 +0100 Subject: [PATCH] finished writing header parser --- README => README.md | 0 http.c | 19 ++++++++++++++++--- test.c | 7 ++++++- 3 files changed, 22 insertions(+), 4 deletions(-) rename README => README.md (100%) diff --git a/README b/README.md similarity index 100% rename from README rename to README.md diff --git a/http.c b/http.c index 9dbb127..4322fa0 100644 --- a/http.c +++ b/http.c @@ -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 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; } diff --git a/test.c b/test.c index 27c0fc0..30d55cd 100644 --- a/test.c +++ b/test.c @@ -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);