#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;
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");
r->protocol = NULL;
r->host = NULL;
r->path = NULL;
+ r->port = -1;
if(strlen(url) > 3){
int i;
bool found = false;
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);
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;