]> Nishi Git Mirror - libw3.git/commitdiff
url support
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Fri, 26 Jan 2024 04:18:45 +0000 (04:18 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Fri, 26 Jan 2024 04:18:45 +0000 (04:18 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@78 d27a3e52-49c5-7645-884c-6793ebffc270

Example/W3B/w3b.c
Library/Makefile
Library/URL.c
Library/W3URL.h

index 1f2af9a3aa94f145cebb73506eb6b551bc81a918..dc27d3f1443ae8a683b202266591b0b3e3546908 100644 (file)
@@ -5,6 +5,7 @@
  */
 
 #include <W3Core.h>
+#include <W3URL.h>
 #include <W3Util.h> /* It has some useful functions, you know */
 
 #include <stdio.h>
 #include <stdbool.h>
 
 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++){
index fa5afa6f670c7cfa820999cfa878baf3a14dbd2e..aba32481813b226ecdb8f139c8995acd9e669a71 100644 (file)
@@ -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)
index 37b1356ea0627d676741ff4d2b4c999d64fa3e40..579844cefbcc6a2ace865969a36a597526eaec3f 100644 (file)
@@ -1,6 +1,69 @@
 /* $Id$ */
 #include "W3URL.h"
 
-struct W3URL* W3_Parse_URL(const char* url){
-       return NULL;
+#include "W3Util.h"
+
+#include <string.h>
+#include <stdio.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+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);
 }
index 352c64211c2c0596795ec507c5c185167e64ac14..56d2557dbddcd6c196f504bab113cdd7d81f4919 100644 (file)
@@ -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