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

Example/W3B/w3b.c
Library/URL.c

index a9a152564f5f4ba27dc6d2dc4584f1fbe286221f..d88ed8389948d74fccbeaa31763b04c5938d1207 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include <unistd.h>
+
+void status_handler(struct W3* w3, int status){
+       printf("Response code: %d\n", status);
+}
+
+void data_handler(struct W3* w3, char* data, size_t size){
+}
+
+void access_site(const char* url){
+       struct W3URL* u = W3_Parse_URL(url);
+       if(u != NULL){
+               struct W3* w3 = W3_Create(u->protocol, u->host, u->port);
+               if(w3 != NULL){
+                       W3_Set_Method(w3, "GET");
+                       W3_Set_Path(w3, u->path);
+                       W3_On(w3, "status", (void*)status_handler);
+                       W3_On(w3, "data", (void*)data_handler);
+                       W3_Send_Request(w3);
+                       W3_Free(w3);
+               }
+               W3_Free_URL(u);
+       }
+}
 
 int main(int argc, char** argv) {
        int i;
@@ -32,32 +56,41 @@ int main(int argc, char** argv) {
                        url = __W3_Strdup(argv[i]);
                }
        }
-       int phase = url == NULL ? 0 : 1;
+       W3_Library_Init();
+       int phase = 0;
        char c = 0;
+       bool acc = false;
+       if(url != NULL){
+               access_site(url);
+               acc = true;
+       }
        while(true){    /* Loop */
-               if(phase == 0){
-                       if(c != '\n' && c != '\r'){
-                               printf("(O)pen, (Q)uit? ");
+               if(c != '\n' && c != '\r'){
+                       printf("(O)pen, (Q)uit? ");
+                       fflush(stdout);
+               }
+               if(scanf("%c", &c) < 0) break;
+               switch(tolower(c)){
+                       case 'q':
+                               goto exitnow;
+                       case 'o':
+                               printf("URL: ");
                                fflush(stdout);
-                       }
-                       if(scanf("%c", &c) < 0) break;
-                       switch(tolower(c)){
-                               case 'q':
-                                       goto exitnow;
-                               case 'o':
-                                       printf("URL: ");
-                                       fflush(stdout);
-                                       char* url = malloc(2049);
-                                       scanf("%s", url);
-                                       free(url);
-                                       break;
-                               case '\n':
-                               case '\r':
-                                       break;
-                               default:
-                                       printf("What do you mean?\n");
-                                       break;
-                       }
+                               if(url != NULL) free(url);
+                               url = malloc(2049);
+                               scanf("%s", url);
+                               acc = false;
+                               break;
+                       case '\n':
+                       case '\r':
+                               break;
+                       default:
+                               printf("What do you mean?\n");
+                               break;
+               }
+               if(!acc){
+                       access_site(url);
+                       acc = true;
                }
        }
        printf("\n");
index 579844cefbcc6a2ace865969a36a597526eaec3f..18e8da9645c7003ac87d1aeda45965ffbb650c3c 100644 (file)
@@ -16,6 +16,7 @@ struct W3URL* W3_Parse_URL(const char* _url){
        r->protocol = NULL;
        r->host = NULL;
        r->path = NULL;
+       r->port = -1;
        if(strlen(url) > 3){
                int i;
                bool found = false;
@@ -38,11 +39,25 @@ struct W3URL* W3_Parse_URL(const char* _url){
                        r->protocol = __W3_Strdup(url);
                        i += 3;
                        int start = i;
+                       int port_start = -1;
                        for(; url[i] != 0; i++){
                                if(url[i] == '/'){
                                        r->path = __W3_Strdup(url + i);
                                        url[i] = 0;
                                        break;
+                               }else if(url[i] == ':'){
+                                       port_start = i + 1;
+                                       url[i] = 0;
+                               }
+                       }
+                       if(port_start != -1){
+                               r->port = atoi(url + port_start);
+                       }
+                       if(r->port == -1){
+                               if(strcmp(r->protocol, "http") == 0){
+                                       r->port = 80;
+                               }else if(strcmp(r->protocol, "https") == 0){
+                                       r->port = 443;
                                }
                        }
                        r->host = __W3_Strdup(url + start);
@@ -55,6 +70,10 @@ struct W3URL* W3_Parse_URL(const char* _url){
                        sprintf(str, "Path is %s", r->path);
                        __W3_Debug("URL", str);
                        free(str);
+                       str = malloc(64);
+                       sprintf(str, "Port is %d", r->port);
+                       __W3_Debug("URL", str);
+                       free(str);
                }
        }
        return r;