From c951f95780bb9ebbe50b08938d11890ac74a60a7 Mon Sep 17 00:00:00 2001 From: nishi Date: Mon, 22 Jan 2024 02:58:01 +0000 Subject: [PATCH] file protocol git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@46 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/fetch.c | 2 +- Library/Core.c | 28 +++++++++++++++++----------- Library/File.c | 38 ++++++++++++++++++++++++++++++++++++++ Library/HTTP.c | 3 ++- Library/Makefile | 2 +- Library/W3File.h | 19 +++++++++++++++++++ W3Version.h.p | 2 +- 7 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 Library/File.c create mode 100644 Library/W3File.h diff --git a/Example/fetch.c b/Example/fetch.c index 11b2e46..c0a4698 100644 --- a/Example/fetch.c +++ b/Example/fetch.c @@ -36,7 +36,7 @@ int main(int argc, char** argv){ return 1; } W3_Library_Init(); - struct W3* w3 = W3_Create("http", argv[1], 80); + struct W3* w3 = W3_Create("file", argv[1], 80); if(w3 != NULL){ W3_Set_Method(w3, argv[3] == NULL ? "GET" : argv[3]); W3_Set_Path(w3, argv[2]); diff --git a/Library/Core.c b/Library/Core.c index 5e284a3..83ff326 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -3,7 +3,9 @@ #include "W3DNS.h" #include "W3Util.h" + #include "W3HTTP.h" +#include "W3File.h" #include #include @@ -54,21 +56,23 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port){ w3->path = NULL; w3->events = NULL; 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 + w3->protocol = __W3_Strdup(protocol); + if(strcmp(protocol, "file") != 0){ + w3->hostname = __W3_Strdup(hostname); + if(ssl) __W3_Debug("Protocol", "Enabled SSL"); + w3->sock = __W3_DNS_Connect(hostname, ssl, port #ifdef SSL_SUPPORT - , - &w3->ssl, - &w3->ssl_ctx + , + &w3->ssl, + &w3->ssl_ctx #endif - ); - if(w3->sock == -1){ - W3_Free(w3); - w3 = NULL; + ); + if(w3->sock == -1){ + W3_Free(w3); + w3 = NULL; + } } return w3; } @@ -86,6 +90,8 @@ void W3_Set_Path(struct W3* w3, const char* path){ void W3_Send_Request(struct W3* w3){ if(strcmp(w3->protocol, "http") == 0 || strcmp(w3->protocol, "https") == 0){ __W3_HTTP_Request(w3); + }else if(strcmp(w3->protocol, "file") == 0){ + __W3_File_Request(w3); } } diff --git a/Library/File.c b/Library/File.c new file mode 100644 index 0000000..996e5d7 --- /dev/null +++ b/Library/File.c @@ -0,0 +1,38 @@ +/* $Id$ */ +#include "W3File.h" + +#include "W3Core.h" +#include "W3Util.h" + +#include +#include +#include + +void __W3_File_Request(struct W3* w3){ + __W3_Debug("LibW3-File", "Sending the request"); + FILE* f = fopen(w3->path, "r"); + if(f == NULL){ + void* funcptr = __W3_Get_Event(w3, "status"); + if(funcptr != NULL){ + void(*func)(struct W3*, int) = (void(*)(struct W3*, int))funcptr; + func(w3, LIBW3_FILE_NOT_FOUND); + } + }else{ + void* funcptr = __W3_Get_Event(w3, "status"); + if(funcptr != NULL){ + void(*func)(struct W3*, int) = (void(*)(struct W3*, int))funcptr; + func(w3, LIBW3_FILE_FOUND); + } + char* buf = malloc(512); + while(true){ + int len = fread(buf, 1, 512, 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; + func(w3, buf, len); + } + if(feof(f)) break; + } + free(buf); + } +} diff --git a/Library/HTTP.c b/Library/HTTP.c index 3d697af..bfa87aa 100644 --- a/Library/HTTP.c +++ b/Library/HTTP.c @@ -7,6 +7,7 @@ #include #include #include +#include void __W3_HTTP_Request(struct W3* w3){ __W3_Debug("LibW3-HTTP", "Sending the request"); @@ -63,7 +64,7 @@ void __W3_HTTP_Request(struct W3* w3){ char* headerbuf = malloc(1); headerbuf[0] = 0; int phase = 0; - while(1){ + while(true){ int l = __W3_Auto_Read(w3, buf, 512); if(l <= 0) break; int i; diff --git a/Library/Makefile b/Library/Makefile index f6be740..977810d 100644 --- a/Library/Makefile +++ b/Library/Makefile @@ -1,7 +1,7 @@ # $Id$ .PHONY: clean install -OBJS = ./Core.o ./Util.o ./DNS.o ./HTTP.o +OBJS = ./Core.o ./Util.o ./DNS.o ./HTTP.o ./File.o ifeq ($(WINDOWS),YES) ./w3.dll: $(OBJS) diff --git a/Library/W3File.h b/Library/W3File.h new file mode 100644 index 0000000..370745e --- /dev/null +++ b/Library/W3File.h @@ -0,0 +1,19 @@ +/* $Id$ */ +#ifndef __W3FILE_H__ +#define __W3FILE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "W3Core.h" +void __W3_File_Request(struct W3* w3); + +#define LIBW3_FILE_FOUND 0 +#define LIBW3_FILE_NOT_FOUND 1 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/W3Version.h.p b/W3Version.h.p index 7b001b1..f3388c8 100644 --- a/W3Version.h.p +++ b/W3Version.h.p @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "1.1" \ +#define LIBW3_VERSION "1.2" \ SUFFIX #ifdef __cplusplus -- 2.43.0