]> Nishi Git Mirror - libw3.git/commitdiff
gemini
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Tue, 13 Feb 2024 00:49:20 +0000 (00:49 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Tue, 13 Feb 2024 00:49:20 +0000 (00:49 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@201 d27a3e52-49c5-7645-884c-6793ebffc270

Library/Core.c
Library/Gemini.c
Library/URL.c
W3Version.h.p

index d148ad6b71266e58a2fad78f0298bd3f98e39289..628b61520bcbc3ff09aae90ccef6bfc35ef06716 100644 (file)
@@ -9,6 +9,9 @@
 #include "W3Gopher.h"
 #include "W3HTTP.h"
 #include "W3POP3.h"
+#ifdef SSL_SUPPORT
+#include "W3Gemini.h"
+#endif
 
 #include <ctype.h>
 #include <stdbool.h>
@@ -55,6 +58,8 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port) {
        bool ssl = false;
        if(strcmp(protocol, "https") == 0) {
                ssl = true;
+       }else if(strcmp(protocol, "gemini") == 0) {
+               ssl = true;
        }
        w3->props = NULL;
        w3->method = NULL;
@@ -76,6 +81,7 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port) {
 #ifdef SSL_SUPPORT
                } else if(strcmp(protocol, "https") == 0) {
                } else if(strcmp(protocol, "pop3s") == 0) {
+               } else if(strcmp(protocol, "gemini") == 0) {
 #endif
                } else if(strcmp(protocol, "gopher") == 0) {
                } else if(strcmp(protocol, "pop3") == 0) {
@@ -136,6 +142,10 @@ void W3_Send_Request(struct W3* w3) {
 #endif
        ) {
                __W3_POP3_Request(w3);
+#ifdef SSL_SUPPORT
+       } else if(strcmp(w3->protocol, "gemini") == 0) {
+               __W3_Gemini_Request(w3);
+#endif
        } else if(strcmp(w3->protocol, "file") == 0) {
                __W3_File_Request(w3);
        }
index d7e6857b61da7228613b6da7dd0531d61ee4a0c2..0d6fa828f6a349e42f222c1b5dd181735a24f7db 100644 (file)
@@ -1,5 +1,85 @@
 /* $Id$ */
 #include "W3Gemini.h"
 
-void __W3_Gemini_Request(struct W3* w3){
+#include "W3Util.h"
+
+#include <stdbool.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+void __W3_Gemini_Request(struct W3* w3) {
+       __W3_Debug("LibW3-Gemini", "Sending the request");
+       __W3_Auto_Write(w3, "gemini://", 9);
+       __W3_Auto_Write(w3, w3->hostname, strlen(w3->hostname));
+       char* portstr = malloc(10);
+       memset(portstr, 0, 10);
+       sprintf(portstr, ":%d", w3->port);
+       __W3_Auto_Write(w3, portstr, strlen(portstr));
+       __W3_Auto_Write(w3, w3->path, strlen(w3->path));
+       __W3_Auto_Write(w3, "\r\n", 2);
+       char* buf = malloc(w3->readsize);
+       bool status = true;
+       char* code = malloc(1);
+       code[0] = 0;
+       char* meta = malloc(1);
+       meta[0] = 0;
+       bool bcode = true;
+       while(true) {
+               int len = __W3_Auto_Read(w3, buf, w3->readsize);
+               if(len <= 0) break;
+               int i = 0;
+               if(status){
+                       for(i = 0; i < len; i++){
+                               if(buf[i] == '\n'){
+                                       status = false;
+                                       break;
+                               }else if(buf[i] == '\r'){
+                                       if(!bcode){
+                                               if(atoi(code) == 20){
+                                                       void* funcptr = __W3_Get_Event(w3, "header");
+                                                       if(funcptr != NULL) {
+                                                               void (*func)(struct W3*, char*, char*) = (void (*)(struct W3*, char*, char*))funcptr;
+                                                               func(w3, "Content-Type", meta);
+                                                       }
+                                               }
+                                       }
+                               }else if(!bcode){
+                                       char* tmp = meta;
+                                       char* cbuf = malloc(2);
+                                       cbuf[0] = buf[i];
+                                       cbuf[1] = 0;
+                                       meta = __W3_Concat(tmp, cbuf);
+                                       free(tmp);
+                                       free(cbuf);
+                               }else if(bcode){
+                                       if(buf[i] == ' '){
+                                               bcode = false;
+                                               void* funcptr = __W3_Get_Event(w3, "status");
+                                               if(funcptr != NULL) {
+                                                       void (*func)(struct W3*, int) = (void (*)(struct W3*, int))funcptr;
+                                                       func(w3, atoi(code));
+                                               }
+                                       }else{
+                                               char* tmp = code;
+                                               char* cbuf = malloc(2);
+                                               cbuf[0] = buf[i];
+                                               cbuf[1] = 0;
+                                               code = __W3_Concat(tmp, cbuf);
+                                               free(tmp);
+                                               free(cbuf);
+                                       }
+                               }
+                       }
+                       i++;
+               }
+               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 + i, len - i);
+               }
+       }
+       free(meta);
+       free(code);
+       free(buf);
 }
index 59ebdc993d3b545d88cb712f882abbba84d3e6b4..a3f8ec656b5f666c675092cd13ff890abbcd22a4 100644 (file)
@@ -60,6 +60,8 @@ struct W3URL* W3_Parse_URL(const char* _url) {
                                        r->port = 443;
                                } else if(strcmp(r->protocol, "gopher") == 0) {
                                        r->port = 70;
+                               } else if(strcmp(r->protocol, "gemini") == 0) {
+                                       r->port = 1965;
                                }
                        }
                        r->host = __W3_Strdup(url + start);
index d9d972211b37d11d8f7626462729376c1258edb9..927ed55be45d0c94a8fffd4e117bcbaca29bef07 100644 (file)
@@ -6,7 +6,7 @@
 extern "C" {
 #endif
 
-#define LIBW3_VERSION "2.5" \
+#define LIBW3_VERSION "2.5A" \
 SUFFIX
 
 #ifdef __cplusplus