From 57b44f3c33c133f1cc39935534aac34a7ff34c55 Mon Sep 17 00:00:00 2001 From: nishi Date: Mon, 22 Jan 2024 03:03:27 +0000 Subject: [PATCH] W3_Set_Read_Size git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@49 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/fetch.c | 1 + Library/Core.c | 5 +++++ Library/File.c | 4 ++-- Library/HTTP.c | 4 ++-- Library/W3Core.h | 26 ++++++++++++++------------ W3Version.h.p | 2 +- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/Example/fetch.c b/Example/fetch.c index 4e5c4ea..4728b18 100644 --- a/Example/fetch.c +++ b/Example/fetch.c @@ -38,6 +38,7 @@ int main(int argc, char** argv){ W3_Library_Init(); struct W3* w3 = W3_Create("http", argv[1], 80); if(w3 != NULL){ + W3_Set_Read_Size(w3, 1024); W3_Set_Method(w3, argv[3] == NULL ? "GET" : argv[3]); W3_Set_Path(w3, argv[2]); W3_On(w3, "status", (void*)status); diff --git a/Library/Core.c b/Library/Core.c index 83ff326..8c61d0a 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -58,6 +58,7 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port){ w3->headers = NULL; w3->size = 0; w3->data = NULL; + w3->readsize = 512; w3->protocol = __W3_Strdup(protocol); if(strcmp(protocol, "file") != 0){ w3->hostname = __W3_Strdup(hostname); @@ -77,6 +78,10 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port){ return w3; } +void W3_Set_Read_Size(struct W3* w3, size_t size){ + w3->readsize = size; +} + void W3_Set_Method(struct W3* w3, const char* method){ if(w3->method != NULL) free(w3->method); w3->method = __W3_Strdup(method); diff --git a/Library/File.c b/Library/File.c index 996e5d7..29a6b33 100644 --- a/Library/File.c +++ b/Library/File.c @@ -23,9 +23,9 @@ void __W3_File_Request(struct W3* w3){ void(*func)(struct W3*, int) = (void(*)(struct W3*, int))funcptr; func(w3, LIBW3_FILE_FOUND); } - char* buf = malloc(512); + char* buf = malloc(w3->readsize); while(true){ - int len = fread(buf, 1, 512, f); + int len = fread(buf, 1, w3->readsize, f); void* funcptr = __W3_Get_Event(w3, "data"); if(funcptr != NULL){ void(*func)(struct W3*, char*, size_t) = (void(*)(struct W3*, char*, size_t))funcptr; diff --git a/Library/HTTP.c b/Library/HTTP.c index bfa87aa..1795aff 100644 --- a/Library/HTTP.c +++ b/Library/HTTP.c @@ -58,14 +58,14 @@ void __W3_HTTP_Request(struct W3* w3){ if(w3->data != NULL){ __W3_Auto_Write(w3, w3->data, w3->size); } - char* buf = malloc(512); + char* buf = malloc(w3->readsize); char* statusbuf = malloc(1); statusbuf[0] = 0; char* headerbuf = malloc(1); headerbuf[0] = 0; int phase = 0; while(true){ - int l = __W3_Auto_Read(w3, buf, 512); + int l = __W3_Auto_Read(w3, buf, w3->readsize); if(l <= 0) break; int i; for(i = 0; i < l; i++){ diff --git a/Library/W3Core.h b/Library/W3Core.h index b422039..305fb6c 100644 --- a/Library/W3Core.h +++ b/Library/W3Core.h @@ -12,19 +12,20 @@ extern "C" { #include "W3Version.h" struct W3 { - int sock; /* Socket */ - char* protocol; /* As you can read from its name */ - char* method; /* Used in HTTP */ - char* path; /* As you can read from its name */ - char* hostname; /* As you can read from its name */ - 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 */ + int sock; /* Socket */ + char* protocol; /* As you can read from its name */ + char* method; /* Used in HTTP */ + char* path; /* As you can read from its name */ + char* hostname; /* As you can read from its name */ + 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 */ + size_t readsize; /* Read buffer size, default is 512 */ #ifdef SSL_SUPPORT - void* ssl; /* Actually SSL*, NULL if no SSL */ - void* ssl_ctx; /* Actually SSL_CTX* */ + void* ssl; /* Actually SSL*, NULL if no SSL */ + void* ssl_ctx; /* Actually SSL_CTX* */ #endif }; @@ -38,6 +39,7 @@ void W3_Set_Header(struct W3* w3, const char* key, const char* value); /* Set t 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 */ +void W3_Set_Read_Size(struct W3* w3, size_t size); /* Change the read buffer size */ #ifdef __cplusplus } diff --git a/W3Version.h.p b/W3Version.h.p index f3388c8..70a6689 100644 --- a/W3Version.h.p +++ b/W3Version.h.p @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "1.2" \ +#define LIBW3_VERSION "1.2A" \ SUFFIX #ifdef __cplusplus -- 2.43.0