From 655f7fd4df74cce9ca417d986b0635b2237d0b38 Mon Sep 17 00:00:00 2001 From: nishi Date: Thu, 28 Mar 2024 00:44:46 +0000 Subject: [PATCH] adding spartan support git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@265 d27a3e52-49c5-7645-884c-6793ebffc270 --- Library/Core.c | 11 +++++ Library/Makefile | 4 ++ Library/Spartan.c | 99 +++++++++++++++++++++++++++++++++++++++++++++ Library/URL.c | 4 +- Library/W3Spartan.h | 17 ++++++++ Library/protocol.mk | 1 + Makefile | 4 ++ W3Version.h.m4 | 3 +- 8 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 Library/Spartan.c create mode 100644 Library/W3Spartan.h diff --git a/Library/Core.c b/Library/Core.c index de40505..9793373 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -45,6 +45,10 @@ #include "W3IRC.h" #endif +#ifdef SPARTAN_SUPPORT +#include "W3Spartan.h" +#endif + #include #include #include @@ -156,6 +160,9 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port) { #endif #ifdef IRC_SUPPORT } else if(strcmp(protocol, "irc") == 0) { +#endif +#ifdef SPARTAN_SUPPORT + } else if(strcmp(protocol, "spartan") == 0) { #endif } else { __W3_Debug("Protocol", "Not suppported"); @@ -259,6 +266,10 @@ void W3_Send_Request(struct W3* w3) { } else if(strcmp(w3->protocol, "file") == 0) { __W3_File_Request(w3); #endif +#ifdef SPARTAN_SUPPORT + } else if(strcmp(w3->protocol, "spartan") == 0) { + __W3_Spartan_Request(w3); +#endif #ifdef IRC_SUPPORT } else if(strcmp(w3->protocol, "irc") == 0 #ifdef SSL_SUPPORT diff --git a/Library/Makefile b/Library/Makefile index dda6b21..bae59aa 100644 --- a/Library/Makefile +++ b/Library/Makefile @@ -50,6 +50,10 @@ ifneq ($(IRC),NO) OBJS += ./IRC.o endif +ifneq ($(Spartan),NO) +OBJS += ./Spartan.o +endif + ifeq ($(WINDOWS),YES) ./w3.dll: $(OBJS) $(CC) $(LDFLAGS) -fstack-protector -L../openssl/lib/mingw/$(WINARCH) -L . -shared -Wl,--out-implib,./w3.lib -o $@ $^ $(LIBS) diff --git a/Library/Spartan.c b/Library/Spartan.c new file mode 100644 index 0000000..0c4c7c5 --- /dev/null +++ b/Library/Spartan.c @@ -0,0 +1,99 @@ +/* $Id$ */ +#include "W3Spartan.h" + +#include "W3Util.h" + +#include +#include +#include +#include + +void __W3_Spartan_Request(struct W3* w3) { + __W3_Debug("LibW3-", "Sending the request"); + __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, " ", 1); + __W3_Auto_Write(w3, w3->path, strlen(w3->path)); + __W3_Auto_Write(w3, " ", 1); + char* conlen = malloc(65); + sprintf(conlen, "%d", w3->size); + __W3_Auto_Write(w3, conlen, strlen(conlen)); + free(conlen); + __W3_Auto_Write(w3, "\r\n", 2); + if(w3->data != NULL){ + __W3_Auto_Write(w3, w3->data, w3->size); + } + 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) == 2) { + 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(atoi(code) == 3) { + void* funcptr = __W3_Get_Event(w3, "header"); + if(funcptr != NULL) { + void (*func)(struct W3*, char*, char*) = (void (*)(struct W3*, char*, char*))funcptr; + func(w3, "Location", 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 c57d2db..633c95e 100644 --- a/Library/URL.c +++ b/Library/URL.c @@ -132,8 +132,10 @@ struct W3URL* W3_Parse_URL(const char* _url) { r->port = 119; } else if(strcmp(r->protocol, "irc") == 0) { r->port = 6667; - } else if(strcmp(r->protocol, "ircs") == 0) { + } else if(strcmp(r->protocol, "ircs") == 0) { r->port = 6697; + }else if(strcmp(r->protocol, "spartan") == 0) { + r->port = 300; } } r->host = __W3_Strdup(url + start + (atmark == 0 ? 0 : (atmark - 1))); diff --git a/Library/W3Spartan.h b/Library/W3Spartan.h new file mode 100644 index 0000000..6339a4c --- /dev/null +++ b/Library/W3Spartan.h @@ -0,0 +1,17 @@ +/* $Id$ */ +#ifndef __W3SPARTAN_H__ +#define __W3SPARTAN_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "W3Core.h" + +void __W3_Spartan_Request(struct W3* w3); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Library/protocol.mk b/Library/protocol.mk index d7ce213..cd81fae 100644 --- a/Library/protocol.mk +++ b/Library/protocol.mk @@ -9,3 +9,4 @@ #FILE=NO #NNTP=NO #IRC=NO +#SPARTAN=NO diff --git a/Makefile b/Makefile index 0d350e3..ae18a68 100644 --- a/Makefile +++ b/Makefile @@ -89,6 +89,10 @@ ifneq ($(IRC),NO) FLAGS += -DIRC_SUPPORT endif +ifneq ($(SPARTAN),NO) +FLAGS += -DSPARTAN_SUPPORT +endif + CFLAGS += $(FLAGS) ifeq ($(WIN32),YES) diff --git a/W3Version.h.m4 b/W3Version.h.m4 index 239edf6..fc9644e 100644 --- a/W3Version.h.m4 +++ b/W3Version.h.m4 @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "2.20G" \ +#define LIBW3_VERSION "2.21" \ SUFFIX ifdef(`HTTP_SUPPORT', `#define LIBW3_HTTP_SUPPORT', `') @@ -18,6 +18,7 @@ ifdef(`NEX_SUPPORT', `#define LIBW3_NEX_SUPPORT', `') ifdef(`FILE_SUPPORT', `#define LIBW3_FILE_SUPPORT', `') ifdef(`IRC_SUPPORT', `#define LIBW3_IRC_SUPPORT', `') ifdef(`NNTP_SUPPORT', `#define LIBW3_NNTP_SUPPORT', `') +ifdef(`SPARTAN_SUPPORT', `#define LIBW3_SPARTAN_SUPPORT', `') #ifdef __cplusplus } -- 2.43.0