]> Nishi Git Mirror - libw3.git/commitdiff
redirect support
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Fri, 26 Jan 2024 12:27:54 +0000 (12:27 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Fri, 26 Jan 2024 12:27:54 +0000 (12:27 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@90 d27a3e52-49c5-7645-884c-6793ebffc270

Example/interactive/interactive.c
Library/Core.c
Library/HTTP.c
Library/URL.c
Library/Util.c
Library/W3Core.h
Library/W3HTTP.h
Library/W3Util.h
Makefile
W3Version.h.p

index c7a65cd840a1f845fb5f54e24c34dfe325bc9aaa..2c3c906a151309a758fcd5b8adacb3ffa2f20645 100644 (file)
@@ -5,6 +5,8 @@
  */
 
 #include <W3Core.h>
+#include <W3HTTP.h>
+
 #include <W3URL.h>
 #include <W3Util.h> /* It has some useful functions, you know */
 
@@ -19,13 +21,14 @@ char* databuf;
 int datalen;
 
 void status_handler(struct W3* w3, int status) { printf("Response code: %d\n", status); }
+void header_handler(struct W3* w3, char* key, char* value) { printf("Header: %s: %s\n", key, value); }
 
 void data_handler(struct W3* w3, char* data, size_t size) {
-       if(databuf == NULL){
+       if(databuf == NULL) {
                databuf = malloc(size);
                datalen = size;
                memcpy(databuf, data, size);
-       }else{
+       } else {
                char* oldbuf = databuf;
                databuf = malloc(datalen + size);
                memcpy(databuf, oldbuf, datalen);
@@ -40,23 +43,25 @@ void access_site(const char* url) {
        if(u != NULL) {
                struct W3* w3 = W3_Create(u->protocol, u->host, u->port);
                if(w3 != NULL) {
-                       if(databuf != NULL){
+                       if(databuf != NULL) {
                                free(databuf);
                        }
                        databuf = NULL;
                        datalen = 0;
                        W3_Set_Method(w3, "GET");
                        W3_Set_Path(w3, u->path);
+                       W3_Enable_Redirect(w3);
                        W3_On(w3, "status", (void*)status_handler);
+                       W3_On(w3, "header", (void*)header_handler);
                        W3_On(w3, "data", (void*)data_handler);
                        W3_Send_Request(w3);
                        W3_Free(w3);
                        printf("%d bytes\n", datalen);
-               }else{
+               } else {
                        fprintf(stderr, "Failed to connect\n");
                }
                W3_Free_URL(u);
-       }else{
+       } else {
                fprintf(stderr, "Failed to parse\n");
        }
 }
@@ -110,7 +115,7 @@ int main(int argc, char** argv) {
                        acc = false;
                        break;
                case 'p':
-                       if(acc){
+                       if(acc) {
                                write(1, databuf, datalen);
                        }
                        break;
index 4dc71599eafe851f21530e7ccc9a5f89ee93c459..d04569d0c7c32e467d80e601570dc8678a1dab8e 100644 (file)
@@ -50,6 +50,7 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port) {
        if(strcmp(protocol, "https") == 0) {
                ssl = true;
        }
+       w3->props = NULL;
        w3->method = NULL;
        w3->path = NULL;
        w3->events = NULL;
@@ -190,10 +191,28 @@ void W3_Free(struct W3* w3) {
                for(i = 0; w3->headers[i] != 0; i++) free(w3->headers[i]);
                free(w3->headers);
        }
+       if(w3->props != NULL) {
+               int i;
+               for(i = 0; w3->props[i] != 0; i++) free(w3->props[i]);
+               free(w3->props);
+       }
        if(w3->events != NULL) {
                int i;
                for(i = 0; w3->events[i] != 0; i += 2) free(w3->events[i]);
                free(w3->events);
        }
+       W3_Disconnect(w3);
        free(w3);
 }
+
+void W3_Disconnect(struct W3* w3) {
+#ifdef SSL_SUPPORT
+       if(w3->ssl != NULL) {
+               SSL_shutdown(w3->ssl);
+               SSL_CTX_free(w3->ssl_ctx);
+               SSL_free(w3->ssl);
+               w3->ssl = NULL;
+       }
+#endif
+       if(w3->sock != -1) close(w3->sock);
+}
index f4166e54bba07fc094a604c79703d4b17b373271..5a7848c05520831806173f5abfbce6ef21016836 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "W3Core.h"
 #include "W3Util.h"
+#include "W3URL.h"
 
 #include <stdbool.h>
 #include <stdio.h>
@@ -75,6 +76,7 @@ void __W3_HTTP_Request(struct W3* w3) {
        bool breakchunk = false;
        int chunksize;
        size_t allsize = -1;
+       char* redir = NULL;
        while(true) {
                int l = __W3_Auto_Read(w3, buf, w3->readsize);
                if(l <= 0) break;
@@ -153,6 +155,9 @@ void __W3_HTTP_Request(struct W3* w3) {
                                                                                                chunked = true;
                                                                                        } else if(strcasecmp(data, "content-length") == 0) {
                                                                                                allsize = atoi(data + k + 1);
+                                                                                       } else if(strcasecmp(data, "location") == 0) {
+                                                                                               if(redir != NULL) free(redir);
+                                                                                               redir = __W3_Strdup(data + k + 1);
                                                                                        }
                                                                                }
                                                                                break;
@@ -249,4 +254,27 @@ void __W3_HTTP_Request(struct W3* w3) {
        free(buf);
        if(chunklen != NULL) free(chunklen);
        if(chunk != NULL) free(chunk);
+       if(redir != NULL && __W3_Have_Prop(w3, "HTTP_REDIRECT")){
+               W3_Disconnect(w3);
+               struct W3URL* u = W3_Parse_URL(redir);
+               if(u != NULL){
+                       w3->sock = __W3_DNS_Connect(u->host, strcmp(u->protocol, "https") == 0 ? true : false, u->port
+#ifdef SSL_SUPPORT
+                                                   ,
+                                                   &w3->ssl, &w3->ssl_ctx
+#endif
+                       );
+                       free(w3->protocol);
+                       w3->protocol = __W3_Strdup(u->protocol);
+
+                       free(w3->hostname);
+                       w3->hostname = __W3_Strdup(u->host);
+                       W3_Set_Path(w3, u->path);
+                       W3_Free_URL(u);
+                       W3_Send_Request(w3);
+               }
+       }
+       if(redir != NULL) free(redir);
 }
+
+void W3_Enable_Redirect(struct W3* w3) { __W3_Add_Prop(w3, "HTTP_REDIRECT"); }
index 48b1f406976df31dee5a078cb6d0e423917b0b32..21767d213de4c8dee0ac50dc25d273bcff92187e 100644 (file)
@@ -75,7 +75,7 @@ struct W3URL* W3_Parse_URL(const char* _url) {
                        __W3_Debug("URL", str);
                        free(str);
                }
-       }else{
+       } else {
                W3_Free_URL(r);
                r = NULL;
        }
index 4d0e92facef2a5ee0c43b91a10afa8cb2727f533..d0297c2825c10cd11af9af665cae62934fcc9259 100644 (file)
@@ -109,3 +109,33 @@ char* __W3_Get_Platform(void) {
        return __W3_Concat3(un.sysname, "/", un.release);
 #endif
 }
+
+bool __W3_Have_Prop(struct W3* w3, const char* name) {
+       if(w3->props == NULL) return false;
+       int i;
+       for(i = 0; w3->props[i] != NULL; i++) {
+               if(strcmp(w3->props[i], name) == 0) return true;
+       }
+       return false;
+}
+
+void __W3_Add_Prop(struct W3* w3, const char* name) {
+       char* str = __W3_Concat3("Adding a prop `", name, "`");
+       __W3_Debug("Prop", str);
+       free(str);
+       int len = 0;
+       if(w3->props == NULL) {
+               w3->props = malloc(sizeof(*w3->props) * 2);
+       } else {
+               char** oldprops = w3->props;
+               for(len = 0; oldprops[len] != NULL; len++)
+                       ;
+               w3->props = malloc(sizeof(*w3->props) * (len + 2));
+               for(len = 0; oldprops[len] != NULL; len++) {
+                       w3->props[len] = oldprops[len];
+               }
+               free(oldprops);
+       }
+       w3->props[len] = __W3_Strdup(name);
+       w3->props[len + 1] = NULL;
+}
index 338fc7d265deef8528ddcb420e6170f1499f6c6e..3eb67f29b0cf24dfaa5c57eab7ffc50483ddfa2a 100644 (file)
@@ -23,6 +23,7 @@ struct W3 {
        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 */
+       char** props;    /* Properties */
 #ifdef SSL_SUPPORT
        void* ssl;     /* Actually SSL*, NULL if no SSL */
        void* ssl_ctx; /* Actually SSL_CTX* */
@@ -38,6 +39,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_Disconnect(struct W3* w3);                                    /* Disconnect */
 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 */
 
index 2000259032acd352a54eb6dc39f510564eea9511..a91583997288bb4ef14b25db0914c2b8a4607ad2 100644 (file)
@@ -7,7 +7,9 @@ extern "C" {
 #endif
 
 #include "W3Core.h"
+
 void __W3_HTTP_Request(struct W3* w3);
+void W3_Enable_Redirect(struct W3* w3);
 
 #ifdef __cplusplus
 }
index 500b24bd21369b348813bc7a4e6c57f97b059c23..4bcadde906fe72d3d36dd0dc3b8b67aba8732b99 100644 (file)
@@ -17,6 +17,8 @@ char* __W3_Strdup(const char* str);
 unsigned long __W3_Auto_Write(struct W3* w3, char* data, unsigned long length);
 unsigned long __W3_Auto_Read(struct W3* w3, char* data, unsigned long length);
 bool __W3_Have_Header(struct W3* w3, const char* name);
+bool __W3_Have_Prop(struct W3* w3, const char* name);
+void __W3_Add_Prop(struct W3* w3, const char* name);
 void* __W3_Get_Event(struct W3* w3, const char* eventname);
 char* __W3_Get_Platform(void);
 
index 829928b7e0546a67e866b3b6726c0f256e91a79d..29252488cd2b7496121c0951de83002537a91fbb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,18 @@
 # $Id$
 include config.mk
 
+ifeq ($(shell uname -s),SunOS)
+GREP = ggrep
+else
+GREP = grep
+endif
+
 CC := cc
 CFLAGS := -g -std=c99 -fPIC -D_BSD_SOURCE
 LDFLAGS :=
 LIBS :=
 PREFIX := /usr/local
-VERSION = $(shell cat Library/W3Version.h | grep -m 1 LIBW3_VERSION | sed -E "s/.+\"([^\"]+)\".+/\1/g")$(shell cat Library/W3Version.h | grep -A 1 -Eo "LIBW3_VERSION" | tail -n1 | grep -Eo "W")
+VERSION = $(shell cat Library/W3Version.h | $(GREP) -m 1 LIBW3_VERSION | sed -E "s/.+\"([^\"]+)\".+/\1/g")$(shell cat Library/W3Version.h | grep -A 1 -Eo "LIBW3_VERSION" | tail -n1 | grep -Eo "W")
 
 
 ifeq ($(SSL),YES)
index b835f8ebf23022174316318161fa7c5fe1f15a5c..26fe8686d33603e3193eae8bd004339885839274 100644 (file)
@@ -6,7 +6,7 @@
 extern "C" {
 #endif
 
-#define LIBW3_VERSION "1.4A" \
+#define LIBW3_VERSION "1.5" \
 SUFFIX
 
 #ifdef __cplusplus