From 0b65c591e36a1a47bd4c051220e0cf0cf6df5d91 Mon Sep 17 00:00:00 2001 From: nishi Date: Wed, 17 Jan 2024 10:44:43 +0000 Subject: [PATCH] http git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@17 d27a3e52-49c5-7645-884c-6793ebffc270 --- Library/Core.c | 2 ++ Library/DNS.c | 2 ++ Library/HTTP.c | 12 ++++++++++++ Library/Util.c | 30 ++++++++++++++++++++++++++++++ Library/W3Core.h | 1 + Library/W3Util.h | 4 ++++ 6 files changed, 51 insertions(+) diff --git a/Library/Core.c b/Library/Core.c index 9becfa7..f115f00 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -53,6 +53,7 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port){ w3->method = NULL; w3->path = NULL; w3->protocol = __W3_Strdup(protocol); + w3->hostname = __W3_Strdup(hostname); if(ssl) __W3_Debug("Protocol", "Enabled SSL"); w3->sock = __W3_DNS_Connect(hostname, ssl, port #ifdef SSL_SUPPORT @@ -85,5 +86,6 @@ void W3_Free(struct W3* w3){ if(w3->method != NULL) free(w3->method); if(w3->path != NULL) free(w3->path); if(w3->protocol != NULL) free(w3->protocol); + if(w3->hostname != NULL) free(w3->hostname); free(w3); } diff --git a/Library/DNS.c b/Library/DNS.c index 9e47dac..d74135e 100644 --- a/Library/DNS.c +++ b/Library/DNS.c @@ -58,6 +58,8 @@ int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port for(rp = result; rp != NULL; rp = rp->ai_next){ sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if(sock == -1) continue; + int nzero = 0; + setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &nzero, sizeof(nzero)); if(connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) break; close(sock); } diff --git a/Library/HTTP.c b/Library/HTTP.c index 690e379..4be8a05 100644 --- a/Library/HTTP.c +++ b/Library/HTTP.c @@ -4,6 +4,18 @@ #include "W3Core.h" #include "W3Util.h" +#include + void __W3_HTTP_Request(struct W3* w3){ __W3_Debug("LibW3-HTTP", "Sending the request"); + __W3_Auto_Write(w3, w3->method, strlen(w3->method)); + __W3_Auto_Write(w3, " ", 1); + __W3_Auto_Write(w3, w3->path, strlen(w3->path)); + __W3_Auto_Write(w3, " ", 1); + __W3_Auto_Write(w3, "HTTP/1.1", 8); + __W3_Auto_Write(w3, "\r\n", 2); + __W3_Auto_Write(w3, "Host: ", 6); + __W3_Auto_Write(w3, w3->hostname, strlen(w3->hostname)); + __W3_Auto_Write(w3, "\r\n", 2); + __W3_Auto_Write(w3, "\r\n", 2); } diff --git a/Library/Util.c b/Library/Util.c index de0055d..fba72f2 100644 --- a/Library/Util.c +++ b/Library/Util.c @@ -1,6 +1,8 @@ /* $Id$ */ #include "W3Util.h" +#include "W3Core.h" + #include #include #include @@ -14,6 +16,10 @@ #include #endif +#ifdef SSL_SUPPORT +#include +#endif + #define __DEBUG_LEN 12 void __W3_Debug(const char* title, const char* message){ @@ -45,3 +51,27 @@ char* __W3_Strdup(const char* str){ memcpy(result, str, strlen(str) + 1); return result; } + +unsigned long __W3_Auto_Write(struct W3* w3, char* data, unsigned long length){ +#ifdef SSL_SUPPORT + if(w3->ssl != NULL){ + return SSL_write(w3->ssl, data, length); + }else{ + return send(w3->sock, data, length, 0); + } +#else + return send(w3->sock, data, length, 0); +#endif +} + +unsigned long __W3_Auto_Read(struct W3* w3, char* data, unsigned long length){ +#ifdef SSL_SUPPORT + if(w3->ssl != NULL){ + return SSL_read(w3->ssl, data, length); + }else{ + return recv(w3->sock, data, length, 0); + } +#else + return recv(w3->sock, data, length, 0); +#endif +} diff --git a/Library/W3Core.h b/Library/W3Core.h index a232ae4..b0c2c1a 100644 --- a/Library/W3Core.h +++ b/Library/W3Core.h @@ -11,6 +11,7 @@ struct W3 { char* protocol; /* As you can read from its name */ char* method; /* Used in HTTP */ char* path; /* As you can read from its name */ + char* hostname; /* As you can read from its name */ #ifdef SSL_SUPPORT void* ssl; /* Actually SSL*, NULL if no SSL */ void* ssl_ctx; /* Actually SSL_CTX* */ diff --git a/Library/W3Util.h b/Library/W3Util.h index 1156655..37c99da 100644 --- a/Library/W3Util.h +++ b/Library/W3Util.h @@ -2,9 +2,13 @@ #ifndef __W3UTIL_H__ #define __W3UTIL_H__ +#include "W3Core.h" + void __W3_Debug(const char* title, const char* message); char* __W3_Concat(const char* str1, const char* str2); char* __W3_Concat3(const char* str1, const char* str2, const char* str3); 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); #endif -- 2.43.0