From a00a15b8a841490ab35ecd42aa1e13cabb42190a Mon Sep 17 00:00:00 2001 From: nishi Date: Fri, 26 Jan 2024 04:37:49 +0000 Subject: [PATCH] get works git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@80 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/W3B/w3b.c | 79 +++++++++++++++++++++++++++++++++-------------- Library/URL.c | 19 ++++++++++++ 2 files changed, 75 insertions(+), 23 deletions(-) diff --git a/Example/W3B/w3b.c b/Example/W3B/w3b.c index a9a1525..d88ed83 100644 --- a/Example/W3B/w3b.c +++ b/Example/W3B/w3b.c @@ -12,6 +12,30 @@ #include #include #include +#include + +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"); diff --git a/Library/URL.c b/Library/URL.c index 579844c..18e8da9 100644 --- a/Library/URL.c +++ b/Library/URL.c @@ -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; -- 2.43.0