From 45031b25458934f085a35e931a72b50548936f32 Mon Sep 17 00:00:00 2001 From: nishi Date: Tue, 13 Feb 2024 03:53:59 +0000 Subject: [PATCH] better url parser git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@207 d27a3e52-49c5-7645-884c-6793ebffc270 --- Library/Core.c | 2 +- Library/URL.c | 75 ++++++++++++++++++++++++++++++++++++++++++++----- Library/W3URL.h | 2 ++ W3Version.h.p | 2 +- 4 files changed, 72 insertions(+), 9 deletions(-) diff --git a/Library/Core.c b/Library/Core.c index b466570..d020285 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -6,10 +6,10 @@ #include "W3Version.h" #include "W3File.h" +#include "W3Finger.h" #include "W3Gopher.h" #include "W3HTTP.h" #include "W3POP3.h" -#include "W3Finger.h" #ifdef SSL_SUPPORT #include "W3Gemini.h" #endif diff --git a/Library/URL.c b/Library/URL.c index 45a579a..c80566e 100644 --- a/Library/URL.c +++ b/Library/URL.c @@ -16,6 +16,8 @@ struct W3URL* W3_Parse_URL(const char* _url) { r->protocol = NULL; r->host = NULL; r->path = NULL; + r->username = NULL; + r->password = NULL; r->port = -1; if(strlen(url) > 3) { int i; @@ -32,14 +34,72 @@ struct W3URL* W3_Parse_URL(const char* _url) { r = NULL; } else { url[i] = 0; - char* str = malloc(strlen(url) + 64); - sprintf(str, "Protocol is %s", url); - __W3_Debug("URL", str); - free(str); + { + 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; int port_start = -1; + bool auth = false; + int atmark = 0; + for(; url[i] != 0; i++) { + if(url[i] == '@') { + auth = true; + atmark = i; + break; + } + } + if(auth) { + __W3_Debug("URL", "This URL has an authentication field"); + i = start; + bool password = false; + char* cbuf = malloc(2); + cbuf[1] = 0; + for(; i < atmark; i++) { + if(url[i] == ':' && !password) { + password = true; + continue; + } + cbuf[0] = url[i]; + if(!password) { + if(r->username == NULL) { + r->username = malloc(1); + r->username[0] = 0; + } + char* tmp = r->username; + r->username = __W3_Concat(tmp, cbuf); + free(tmp); + } else { + if(r->password == NULL) { + r->password = malloc(1); + r->password[0] = 0; + } + char* tmp = r->password; + r->password = __W3_Concat(tmp, cbuf); + free(tmp); + } + } + free(cbuf); + if(r->username != NULL){ + char* str = malloc(64 + strlen(r->username)); + sprintf(str, "Username is %s", r->username); + __W3_Debug("URL", str); + free(str); + } + if(r->password != NULL){ + char* str = malloc(64 + strlen(r->password)); + sprintf(str, "Password is %s", r->password); + __W3_Debug("URL", str); + free(str); + } + i = atmark + 1; + } else { + i = start; + } for(; url[i] != 0; i++) { if(url[i] == '/') { r->path = __W3_Strdup(url + i); @@ -65,10 +125,9 @@ struct W3URL* W3_Parse_URL(const char* _url) { } else if(strcmp(r->protocol, "finger") == 0) { r->port = 79; } - } - r->host = __W3_Strdup(url + start); - str = malloc(strlen(r->host) + 64); + r->host = __W3_Strdup(url + start + (atmark == 0 ? 0 : (atmark - 1))); + char* str = malloc(strlen(r->host) + 64); sprintf(str, "Host is %s", r->host); __W3_Debug("URL", str); free(str); @@ -94,5 +153,7 @@ void W3_Free_URL(struct W3URL* url) { if(url->protocol != NULL) free(url->protocol); if(url->host != NULL) free(url->host); if(url->path != NULL) free(url->path); + if(url->username != NULL) free(url->username); + if(url->password != NULL) free(url->password); free(url); } diff --git a/Library/W3URL.h b/Library/W3URL.h index 56d2557..d628b32 100644 --- a/Library/W3URL.h +++ b/Library/W3URL.h @@ -7,6 +7,8 @@ struct W3URL { char* host; int port; char* path; + char* username; + char* password; }; struct W3URL* W3_Parse_URL(const char* url); diff --git a/W3Version.h.p b/W3Version.h.p index 4bcd77a..0f2e398 100644 --- a/W3Version.h.p +++ b/W3Version.h.p @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "2.8" \ +#define LIBW3_VERSION "2.9" \ SUFFIX #ifdef __cplusplus -- 2.43.0