#include <W3URL.h>
#include <W3Util.h> /* It has some useful functions, you know */
+#include <ctype.h>
+#include <stdbool.h>
#include <stdio.h>
-#include <string.h>
#include <stdlib.h>
-#include <stdbool.h>
+#include <string.h>
#include <unistd.h>
-void status_handler(struct W3* w3, int status){
- printf("Response code: %d\n", status);
-}
+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 data_handler(struct W3* w3, char* data, size_t size) {}
-void access_site(const char* url){
+void access_site(const char* url) {
struct W3URL* u = W3_Parse_URL(url);
- if(u != NULL){
+ if(u != NULL) {
struct W3* w3 = W3_Create(u->protocol, u->host, u->port);
- if(w3 != NULL){
+ if(w3 != NULL) {
W3_Set_Method(w3, "GET");
W3_Set_Path(w3, u->path);
W3_On(w3, "status", (void*)status_handler);
int main(int argc, char** argv) {
int i;
char* url = NULL;
- for(i = 1; i < argc; i++){
- if(strcmp(argv[i], "--version") == 0){
+ for(i = 1; i < argc; i++) {
+ if(strcmp(argv[i], "--version") == 0) {
printf("LibW3 %s\n", LIBW3_VERSION);
return 0;
- }else if(argv[i][0] == '-'){
+ } else if(argv[i][0] == '-') {
fprintf(stderr, "%s: unknown option: %s\n", argv[0], argv[i]);
return 1;
- }else{
- if(url != NULL){
+ } else {
+ if(url != NULL) {
free(url);
fprintf(stderr, "%s: garbage argument found\n", argv[0]);
return 1;
int phase = 0;
char c = 0;
bool acc = false;
- if(url != NULL){
+ if(url != NULL) {
access_site(url);
acc = true;
}
- while(true){ /* Loop */
- if(c != '\n' && c != '\r'){
+ while(true) { /* Loop */
+ 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(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;
+ switch(tolower(c)) {
+ case 'q':
+ goto exitnow;
+ case 'o':
+ printf("URL: ");
+ fflush(stdout);
+ 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){
+ if(!acc) {
access_site(url);
acc = true;
}
#include "W3Util.h"
-#include <string.h>
-#include <stdio.h>
+#include <stdbool.h>
#include <stddef.h>
+#include <stdio.h>
#include <stdlib.h>
-#include <stdbool.h>
+#include <string.h>
-struct W3URL* W3_Parse_URL(const char* _url){
+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->host = NULL;
r->path = NULL;
r->port = -1;
- if(strlen(url) > 3){
+ 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] == '/'){
+ for(i = 0; i < strlen(url) - 3; i++) {
+ if(url[i] == ':' && url[i + 1] == '/' && url[i + 2] == '/') {
found = true;
break;
}
}
- if(!found){
+ if(!found) {
__W3_Debug("URL", "Failed to parse");
W3_Free_URL(r);
r = NULL;
- }else{
+ } else {
url[i] = 0;
char* str = malloc(strlen(url) + 64);
sprintf(str, "Protocol is %s", url);
i += 3;
int start = i;
int port_start = -1;
- for(; url[i] != 0; i++){
- if(url[i] == '/'){
+ for(; url[i] != 0; i++) {
+ if(url[i] == '/') {
r->path = __W3_Strdup(url + i);
url[i] = 0;
break;
- }else if(url[i] == ':'){
+ } else if(url[i] == ':') {
port_start = i + 1;
url[i] = 0;
}
}
- if(port_start != -1){
+ if(port_start != -1) {
r->port = atoi(url + port_start);
}
- if(r->port == -1){
- if(strcmp(r->protocol, "http") == 0){
+ if(r->port == -1) {
+ if(strcmp(r->protocol, "http") == 0) {
r->port = 80;
- }else if(strcmp(r->protocol, "https") == 0){
+ } else if(strcmp(r->protocol, "https") == 0) {
r->port = 443;
}
}
return r;
}
-void W3_Free_URL(struct W3URL* url){
+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);