From ed9157bfab0a2692a1820310a84c825f3c8445d7 Mon Sep 17 00:00:00 2001 From: nishi Date: Tue, 13 Feb 2024 00:49:20 +0000 Subject: [PATCH] gemini git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@201 d27a3e52-49c5-7645-884c-6793ebffc270 --- Library/Core.c | 10 ++++++ Library/Gemini.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++- Library/URL.c | 2 ++ W3Version.h.p | 2 +- 4 files changed, 94 insertions(+), 2 deletions(-) diff --git a/Library/Core.c b/Library/Core.c index d148ad6..628b615 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -9,6 +9,9 @@ #include "W3Gopher.h" #include "W3HTTP.h" #include "W3POP3.h" +#ifdef SSL_SUPPORT +#include "W3Gemini.h" +#endif #include #include @@ -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); } diff --git a/Library/Gemini.c b/Library/Gemini.c index d7e6857..0d6fa82 100644 --- a/Library/Gemini.c +++ b/Library/Gemini.c @@ -1,5 +1,85 @@ /* $Id$ */ #include "W3Gemini.h" -void __W3_Gemini_Request(struct W3* w3){ +#include "W3Util.h" + +#include +#include +#include +#include + +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); } diff --git a/Library/URL.c b/Library/URL.c index 59ebdc9..a3f8ec6 100644 --- a/Library/URL.c +++ b/Library/URL.c @@ -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); diff --git a/W3Version.h.p b/W3Version.h.p index d9d9722..927ed55 100644 --- a/W3Version.h.p +++ b/W3Version.h.p @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "2.5" \ +#define LIBW3_VERSION "2.5A" \ SUFFIX #ifdef __cplusplus -- 2.43.0