From: nishi Date: Sun, 21 Jan 2024 16:21:48 +0000 (+0000) Subject: post works X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=3af2410e906b5cd9aae619e47b7a35c74860e7aa;p=libw3.git post works git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@42 d27a3e52-49c5-7645-884c-6793ebffc270 --- diff --git a/Example/fetch.c b/Example/fetch.c index 26ea393..11b2e46 100644 --- a/Example/fetch.c +++ b/Example/fetch.c @@ -32,17 +32,20 @@ int main(int argc, char** argv){ return 0; } if(argc < 3){ - fprintf(stderr, "Usage: %s URL Path\n", argv[0]); + fprintf(stderr, "Usage: %s URL Path [method [data]]\n", argv[0]); return 1; } W3_Library_Init(); struct W3* w3 = W3_Create("http", argv[1], 80); if(w3 != NULL){ - W3_Set_Method(w3, "GET"); + W3_Set_Method(w3, argv[3] == NULL ? "GET" : argv[3]); 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); + if(argv[3] != NULL && strcmp(argv[3], "POST") == 0 && argv[4] != NULL){ + W3_Set_Data(w3, argv[4], strlen(argv[4])); + } W3_Send_Request(w3); W3_Free(w3); }else{ diff --git a/Library/Core.c b/Library/Core.c index 02fbc80..5e284a3 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -56,6 +56,8 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port){ w3->headers = NULL; w3->protocol = __W3_Strdup(protocol); w3->hostname = __W3_Strdup(hostname); + w3->size = 0; + w3->data = NULL; if(ssl) __W3_Debug("Protocol", "Enabled SSL"); w3->sock = __W3_DNS_Connect(hostname, ssl, port #ifdef SSL_SUPPORT @@ -87,6 +89,11 @@ void W3_Send_Request(struct W3* w3){ } } +void W3_Set_Data(struct W3* w3, char* data, size_t size){ + w3->data = data; + w3->size = size; +} + void W3_Set_Header(struct W3* w3, const char* key, const char* value){ char* tmp = __W3_Concat3("Setting the header `", key, "` to `"); char* str = __W3_Concat3(tmp, value, "`"); diff --git a/Library/HTTP.c b/Library/HTTP.c index e8f5ec5..3d697af 100644 --- a/Library/HTTP.c +++ b/Library/HTTP.c @@ -6,6 +6,7 @@ #include #include +#include void __W3_HTTP_Request(struct W3* w3){ __W3_Debug("LibW3-HTTP", "Sending the request"); @@ -36,14 +37,26 @@ void __W3_HTTP_Request(struct W3* w3){ int i; for(i = 0; w3->headers[i] != NULL; i += 2){ if(strcmp(w3->headers[i], "connection") == 0) continue; + if(strcmp(w3->headers[i], "content-length") == 0) continue; __W3_Auto_Write(w3, w3->headers[i], strlen(w3->headers[i])); __W3_Auto_Write(w3, ": ", 2); __W3_Auto_Write(w3, w3->headers[i + 1], strlen(w3->headers[i + 1])); __W3_Auto_Write(w3, "\r\n", 2); } } + if(w3->data != NULL){ + char* len = malloc(129); + memset(len, 0, 129); + sprintf(len, "%d", w3->size); + __W3_Auto_Write(w3, "content-length: ", 16); + __W3_Auto_Write(w3, len, strlen(len)); + __W3_Auto_Write(w3, "\r\n", 2); + free(len); + } __W3_Auto_Write(w3, "\r\n", 2); - __W3_Auto_Write(w3, "\r\n", 2); + if(w3->data != NULL){ + __W3_Auto_Write(w3, w3->data, w3->size); + } char* buf = malloc(512); char* statusbuf = malloc(1); statusbuf[0] = 0; diff --git a/Library/W3Core.h b/Library/W3Core.h index 3927f55..b422039 100644 --- a/Library/W3Core.h +++ b/Library/W3Core.h @@ -6,6 +6,7 @@ extern "C" { #endif +#include #include #include "W3Version.h" @@ -19,6 +20,8 @@ struct W3 { char** headers; /* As you can read from its name */ void** events; /* As you can read from its name */ int status; /* As you can read from its name */ + char* data; /* As you can read from its name */ + size_t size; /* Size of the data */ #ifdef SSL_SUPPORT void* ssl; /* Actually SSL*, NULL if no SSL */ void* ssl_ctx; /* Actually SSL_CTX* */ @@ -34,6 +37,7 @@ void W3_Send_Request(struct W3* w3); /* Send the request */ void W3_Set_Header(struct W3* w3, const char* key, const char* value); /* Set the header */ void W3_Free(struct W3* w3); /* Free the struct */ void W3_On(struct W3* w3, const char* eventname, void* func); /* Set Handlers */ +void W3_Set_Data(struct W3* w3, char* data, size_t size); /* Send the data - LibW3 won't free the data */ #ifdef __cplusplus } diff --git a/W3Version.h.p b/W3Version.h.p index b2622b0..7b001b1 100644 --- a/W3Version.h.p +++ b/W3Version.h.p @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "1.0" \ +#define LIBW3_VERSION "1.1" \ SUFFIX #ifdef __cplusplus