]> Nishi Git Mirror - libw3.git/commitdiff
file protocol
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Mon, 22 Jan 2024 02:58:01 +0000 (02:58 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Mon, 22 Jan 2024 02:58:01 +0000 (02:58 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@46 d27a3e52-49c5-7645-884c-6793ebffc270

Example/fetch.c
Library/Core.c
Library/File.c [new file with mode: 0644]
Library/HTTP.c
Library/Makefile
Library/W3File.h [new file with mode: 0644]
W3Version.h.p

index 11b2e46b8a8bb40d94a6a7f9f33933695f1bcf01..c0a46986e36b0f6760d08a618c5ecd05e10992ba 100644 (file)
@@ -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]);
index 5e284a3a092cdf749c04fd444ccb373619bbdc99..83ff326e5e791547c1fe60639deb7fd65c2d3906 100644 (file)
@@ -3,7 +3,9 @@
 
 #include "W3DNS.h"
 #include "W3Util.h"
+
 #include "W3HTTP.h"
+#include "W3File.h"
 
 #include <stdio.h>
 #include <string.h>
@@ -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 (file)
index 0000000..996e5d7
--- /dev/null
@@ -0,0 +1,38 @@
+/* $Id$ */
+#include "W3File.h"
+
+#include "W3Core.h"
+#include "W3Util.h"
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+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);
+       }
+}
index 3d697af1a0b69b845328af0fc895b7f3bdfe9a82..bfa87aaa79293ad494ca63e97b17c5a8e82e1056 100644 (file)
@@ -7,6 +7,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdbool.h>
 
 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;
index f6be7408cfadcaa3e1d39c5b118af1b2833a43f6..977810dafd42e742e310723a2f236683c4951aa0 100644 (file)
@@ -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 (file)
index 0000000..370745e
--- /dev/null
@@ -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
index 7b001b1267e51b52c81e5c76f7b414a458f66f60..f3388c8674efb8dfed6cd295bb7eac3729cef84e 100644 (file)
@@ -6,7 +6,7 @@
 extern "C" {
 #endif
 
-#define LIBW3_VERSION "1.1" \
+#define LIBW3_VERSION "1.2" \
 SUFFIX
 
 #ifdef __cplusplus