#include <W3Core.h>
#include <stdio.h>
-#include <stdbool.h>
#include <string.h>
+#include <unistd.h>
+
+void fetch_data(struct W3* w3, size_t size, char* data){
+ write(1, data, size);
+}
+
+void status(struct W3* w3, int status){
+ printf("Response code is %d\n", status);
+}
int main(int argc, char** argv){
if(argv[1] != NULL && strcmp(argv[1], "--version") == 0){
if(w3 != NULL){
W3_Set_Method(w3, "GET");
W3_Set_Path(w3, argv[2]);
+ W3_On(w3, "status", (void*)status);
+ W3_On(w3, "data", (void*)fetch_data);
W3_Send_Request(w3);
W3_Free(w3);
}else{
}
w3->method = NULL;
w3->path = NULL;
+ w3->events = NULL;
w3->headers = NULL;
w3->protocol = __W3_Strdup(protocol);
w3->hostname = __W3_Strdup(hostname);
w3->headers[len] = __W3_Strdup(key);
w3->headers[len + 1] = __W3_Strdup(value);
int i;
- for(i = 0; w3->headers[len + 1][i] != 0; i++){
- w3->headers[len + 1][i] = tolower(w3->headers[len + 1][i]);
+ for(i = 0; w3->headers[len][i] != 0; i++){
+ w3->headers[len][i] = tolower(w3->headers[len][i]);
}
w3->headers[len + 2] = NULL;
}else{
w3->headers[len + 1] = __W3_Strdup(value);
w3->headers[len + 2] = NULL;
int i;
- for(i = 0; w3->headers[len + 1][i] != 0; i++){
- w3->headers[len + 1][i] = tolower(w3->headers[len + 1][i]);
+ for(i = 0; w3->headers[len][i] != 0; i++){
+ w3->headers[len][i] = tolower(w3->headers[len][i]);
}
free(headers);
}
}
+void W3_On(struct W3* w3, const char* eventname, void* func){
+ int len = 0;
+ if(w3->events == NULL){
+ w3->events = malloc(sizeof(*w3->events) * (len + 3));
+ w3->events[len] = __W3_Strdup(eventname);
+ w3->events[len + 1] = func;
+ int i;
+ for(i = 0; ((char*)w3->events[len])[i] != 0; i++){
+ ((char*)w3->events[len])[i] = tolower(((char*)w3->events[len])[i]);
+ }
+ w3->events[len + 2] = NULL;
+ }else{
+ for(len = 0; w3->events[len] != NULL; len++);
+ void** events = w3->events;
+ w3->events = malloc(sizeof(*w3->events) * (len + 3));
+ for(len = 0; events[len] != NULL; len++){
+ w3->events[len] = events[len];
+ }
+ w3->events[len] = __W3_Strdup(eventname);
+ w3->events[len + 1] = func;
+ w3->events[len + 2] = NULL;
+ int i;
+ for(i = 0; ((char*)w3->events[len])[i] != 0; i += 2){
+ ((char*)w3->events[len])[i] = tolower(((char*)w3->events[len])[i]);
+ }
+ free(events);
+ }
+}
+
void W3_Free(struct W3* w3){
__W3_Debug("LibW3", "Freeing");
if(w3->method != NULL) free(w3->method);
for(i = 0; w3->headers[i] != 0; i++) free(w3->headers[i]);
free(w3->headers);
}
+ if(w3->events != NULL){
+ int i;
+ for(i = 0; w3->events[i] != 0; i += 2) free(w3->events[i]);
+ free(w3->events);
+ }
free(w3);
}
}
__W3_Auto_Write(w3, "\r\n", 2);
__W3_Auto_Write(w3, "\r\n", 2);
+ char* buf = malloc(512);
+ char* statusbuf = malloc(1);
+ statusbuf[0] = 0;
+ char* headerbuf = malloc(1);
+ headerbuf[0] = 0;
+ int phase = 0;
+ while(1){
+ int l = __W3_Auto_Read(w3, buf, 512);
+ if(l <= 0) break;
+ int i;
+ for(i = 0; i < l; i++){
+ if(phase == 0){
+ if(buf[i] == '\r'){
+ int phase2 = 0;
+ int j = 0;
+ int start_status = 0;
+ for(j = 0; statusbuf[j] != 0; j++){
+ if(phase2 == 0){
+ if(statusbuf[j] == ' '){
+ phase2++;
+ start_status = j + 1;
+ }
+ }else if(phase2 == 1){
+ if(statusbuf[j] == ' '){
+ char* code = malloc(j - start_status + 1);
+ code[j - start_status] = 0;
+ memcpy(code, statusbuf + start_status, j - start_status);
+ w3->status = atoi(code);
+ void* funcptr = __W3_Get_Event(w3, "status");
+ if(funcptr != NULL){
+ void(*func)(struct W3*, int) = (void(*)(struct W3*, int))funcptr;
+ func(w3, w3->status);
+ }
+ free(code);
+ break;
+ }
+ }
+ }
+ phase++;
+ break;
+ }else{
+ char* oldbuf = statusbuf;
+ statusbuf = malloc(strlen(oldbuf) + 2);
+ strcpy(statusbuf, oldbuf);
+ statusbuf[strlen(oldbuf)] = buf[i];
+ statusbuf[strlen(oldbuf) + 1] = 0;
+ free(oldbuf);
+ }
+ }
+ }
+ }
+ free(headerbuf);
+ free(statusbuf);
+ free(buf);
}
# $Id$
.PHONY: clean install
-ifdef WINDOWS
+ifeq ($(WINDOWS),YES)
./w3.dll: ./Core.o ./Util.o ./DNS.o ./HTTP.o
$(CC) $(LDFLAGS) -shared -Wl,--out-implib,./w3.lib -o $@ $^ $(LIBS)
else
#endif
}
+void* __W3_Get_Event(struct W3* w3, const char* eventname){
+ if(w3->events == NULL) return NULL;
+ int i;
+ for(i = 0; w3->events[i] != NULL; i += 2){
+ if(strcmp(w3->events[i], eventname) == 0){
+ return w3->events[i + 1];
+ }
+ }
+ return NULL;
+}
+
bool __W3_Have_Header(struct W3* w3, const char* name){
if(w3->headers == NULL) return false;
int i;
char* path; /* As you can read from its name */
char* hostname; /* As you can read from its name */
char** headers; /* As you can read from its name */
+ void** events; /* As you can read from its name */
+ int status; /* As you can read from its name */
#ifdef SSL_SUPPORT
void* ssl; /* Actually SSL*, NULL if no SSL */
void* ssl_ctx; /* Actually SSL_CTX* */
void W3_Send_Request(struct W3* w3); /* Send the request */
void W3_Set_Header(struct W3* w3, const char* key, const char* value); /* Set the header */
void W3_Free(struct W3* w3); /* Free the struct */
+void W3_On(struct W3* w3, const char* eventname, void* func); /* Set Handlers */
#ifdef __cplusplus
}
unsigned long __W3_Auto_Write(struct W3* w3, char* data, unsigned long length);
unsigned long __W3_Auto_Read(struct W3* w3, char* data, unsigned long length);
bool __W3_Have_Header(struct W3* w3, const char* name);
+void* __W3_Get_Event(struct W3* w3, const char* eventname);
char* __W3_Get_Platform(void);
#ifdef __cplusplus
VERSION = $(shell cat Library/W3Version.h | grep -m 1 LIBW3_VERSION | sed -E "s/.+\"([^\"]+)\".+/\1/g")$(shell cat Library/W3Version.h | grep -A 1 -Eo "LIBW3_VERSION" | tail -n1 | grep -Eo "W")
-ifdef SSL
+ifeq ($(SSL),YES)
CFLAGS += -DSSL_SUPPORT
LIBS += -lssl -lcrypto
endif
-ifdef WIN32
+ifeq ($(WIN32),YES)
CC := i686-w64-mingw32-gcc
WINDOWS := YES
endif
-ifdef WIN64
+ifeq ($(WIN64),YES)
CC := x86_64-w64-mingw32-gcc
WINDOWS := YES
endif
-ifdef WINDOWS
+ifeq ($(WINDOWS),YES)
LIBS += -lws2_32
endif
-ifdef DEBUG
+ifeq ($(DEBUG),YES)
CFLAGS += -g -D__DEBUG__
endif
-ifdef WINDOWS
+ifeq ($(WINDOWS),YES)
.PHONY: all clean ./Library/w3.dll ./Example/fetch
ALL := ./Library/w3.dll ./Example/fetch.exe