]> Nishi Git Mirror - libw3.git/commitdiff
better url parser
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Tue, 13 Feb 2024 03:53:59 +0000 (03:53 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Tue, 13 Feb 2024 03:53:59 +0000 (03:53 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@207 d27a3e52-49c5-7645-884c-6793ebffc270

Library/Core.c
Library/URL.c
Library/W3URL.h
W3Version.h.p

index b466570a5abfd91f4080d00c539bfe6bf3f966b7..d02028542993b2df4fed4ab9adbb3d411dfee091 100644 (file)
@@ -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
index 45a579adbb103173e81bed260723459c9f7aa807..c80566e0fdf4dfa889b06b3ab9db5a460ac73b36 100644 (file)
@@ -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);
 }
index 56d2557dbddcd6c196f504bab113cdd7d81f4919..d628b32cf5a66d2918303fd53f0a587ec8f0c3c3 100644 (file)
@@ -7,6 +7,8 @@ struct W3URL {
        char* host;
        int port;
        char* path;
+       char* username;
+       char* password;
 };
 
 struct W3URL* W3_Parse_URL(const char* url);
index 4bcd77a66b8b8e4602e06673d6233e168239e0fe..0f2e398c346122ae6dabfa4d00b444275f67283b 100644 (file)
@@ -6,7 +6,7 @@
 extern "C" {
 #endif
 
-#define LIBW3_VERSION "2.8" \
+#define LIBW3_VERSION "2.9" \
 SUFFIX
 
 #ifdef __cplusplus