From 0724c3b3e7eee70dfe6d380a04b1c7f648f345ba Mon Sep 17 00:00:00 2001 From: nishi Date: Sun, 21 Jan 2024 14:27:51 +0000 Subject: [PATCH] header support git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@38 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/fetch.c | 5 +++++ Library/HTTP.c | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/Example/fetch.c b/Example/fetch.c index 64afe18..ac17c18 100644 --- a/Example/fetch.c +++ b/Example/fetch.c @@ -18,6 +18,10 @@ void status(struct W3* w3, int status){ printf("Response code is %d\n", status); } +void header(struct W3* w3, char* key, char* value){ + printf("Header: %s is `%s'\n", key, value); +} + int main(int argc, char** argv){ if(argv[1] != NULL && strcmp(argv[1], "--version") == 0){ printf("LibW3 %s\n", LIBW3_VERSION); @@ -34,6 +38,7 @@ int main(int argc, char** argv){ W3_Set_Path(w3, argv[2]); W3_On(w3, "status", (void*)status); W3_On(w3, "data", (void*)fetch_data); + W3_On(w3, "header", (void*)header); W3_Send_Request(w3); W3_Free(w3); }else{ diff --git a/Library/HTTP.c b/Library/HTTP.c index 39e379b..e8f5ec5 100644 --- a/Library/HTTP.c +++ b/Library/HTTP.c @@ -114,6 +114,19 @@ void __W3_HTTP_Request(struct W3* w3){ char* data = __W3_Strdup(headers + start); int k; for(k = 0; data[k] != 0; k++){ + if(data[k] == ':'){ + data[k] = 0; + k++; + for(; data[k] != 0 && data[k] != ' ' && data[k] != '\t'; k++) data[k] = 0; + if(data[k] == ' ' || data[k] == '\t'){ + void* funcptr = __W3_Get_Event(w3, "header"); + if(funcptr != NULL){ + void(*func)(struct W3*, char*, char*) = (void(*)(struct W3*, char*, char*))funcptr; + func(w3, data, data + k + 1); + } + } + break; + } } free(data); start = incr + 1; -- 2.43.0