From: nishi Date: Fri, 26 Jan 2024 04:18:45 +0000 (+0000) Subject: url support X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=e7f83073426cd705ea668d1eb26087019d86ac89;p=libw3.git url support git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@78 d27a3e52-49c5-7645-884c-6793ebffc270 --- diff --git a/Example/W3B/w3b.c b/Example/W3B/w3b.c index 1f2af9a..dc27d3f 100644 --- a/Example/W3B/w3b.c +++ b/Example/W3B/w3b.c @@ -5,6 +5,7 @@ */ #include +#include #include /* It has some useful functions, you know */ #include @@ -13,6 +14,10 @@ #include int main(int argc, char** argv) { + W3_Parse_URL("http://nishi.boats"); + W3_Parse_URL("http://nishi.boats/abc"); + W3_Parse_URL("nishi.boats"); + W3_Parse_URL("nishi.boats/abc"); int i; char* url = NULL; for(i = 1; i < argc; i++){ diff --git a/Library/Makefile b/Library/Makefile index fa5afa6..aba3248 100644 --- a/Library/Makefile +++ b/Library/Makefile @@ -1,7 +1,7 @@ # $Id$ .PHONY: clean install -OBJS = ./Core.o ./Util.o ./DNS.o ./HTTP.o ./File.o +OBJS = ./Core.o ./Util.o ./DNS.o ./HTTP.o ./File.o ./URL.o ifeq ($(WINDOWS),YES) ./w3.dll: $(OBJS) diff --git a/Library/URL.c b/Library/URL.c index 37b1356..579844c 100644 --- a/Library/URL.c +++ b/Library/URL.c @@ -1,6 +1,69 @@ /* $Id$ */ #include "W3URL.h" -struct W3URL* W3_Parse_URL(const char* url){ - return NULL; +#include "W3Util.h" + +#include +#include +#include +#include +#include + +struct W3URL* W3_Parse_URL(const char* _url){ + char* url = __W3_Strdup(_url); + __W3_Debug("URL", "Parsing URL"); + struct W3URL* r = malloc(sizeof(*r)); + r->protocol = NULL; + r->host = NULL; + r->path = NULL; + if(strlen(url) > 3){ + int i; + bool found = false; + for(i = 0; i < strlen(url) - 3; i++){ + if(url[i] == ':' && url[i + 1] == '/' && url[i + 2] == '/'){ + found = true; + break; + } + } + if(!found){ + __W3_Debug("URL", "Failed to parse"); + W3_Free_URL(r); + r = NULL; + }else{ + url[i] = 0; + char* str = malloc(strlen(url) + 64); + sprintf(str, "Protocol is %s", url); + __W3_Debug("URL", str); + free(str); + r->protocol = __W3_Strdup(url); + i += 3; + int start = i; + for(; url[i] != 0; i++){ + if(url[i] == '/'){ + r->path = __W3_Strdup(url + i); + url[i] = 0; + break; + } + } + r->host = __W3_Strdup(url + start); + str = malloc(strlen(r->host) + 64); + sprintf(str, "Host is %s", r->host); + __W3_Debug("URL", str); + free(str); + if(r->path == NULL) r->path = __W3_Strdup("/"); + str = malloc(strlen(r->path) + 64); + sprintf(str, "Path is %s", r->path); + __W3_Debug("URL", str); + free(str); + } + } + return r; +} + +void W3_Free_URL(struct W3URL* url){ + if(url == NULL) return; + if(url->protocol != NULL) free(url->protocol); + if(url->host != NULL) free(url->host); + if(url->path != NULL) free(url->path); + free(url); } diff --git a/Library/W3URL.h b/Library/W3URL.h index 352c642..56d2557 100644 --- a/Library/W3URL.h +++ b/Library/W3URL.h @@ -2,6 +2,14 @@ #ifndef __W3URL_H__ #define __W3URL_H__ +struct W3URL { + char* protocol; + char* host; + int port; + char* path; +}; + struct W3URL* W3_Parse_URL(const char* url); +void W3_Free_URL(struct W3URL* url); #endif