*/
#include <W3Core.h>
+#include <W3HTTP.h>
+
#include <W3URL.h>
#include <W3Util.h> /* It has some useful functions, you know */
int datalen;
void status_handler(struct W3* w3, int status) { printf("Response code: %d\n", status); }
+void header_handler(struct W3* w3, char* key, char* value) { printf("Header: %s: %s\n", key, value); }
void data_handler(struct W3* w3, char* data, size_t size) {
- if(databuf == NULL){
+ if(databuf == NULL) {
databuf = malloc(size);
datalen = size;
memcpy(databuf, data, size);
- }else{
+ } else {
char* oldbuf = databuf;
databuf = malloc(datalen + size);
memcpy(databuf, oldbuf, datalen);
if(u != NULL) {
struct W3* w3 = W3_Create(u->protocol, u->host, u->port);
if(w3 != NULL) {
- if(databuf != NULL){
+ if(databuf != NULL) {
free(databuf);
}
databuf = NULL;
datalen = 0;
W3_Set_Method(w3, "GET");
W3_Set_Path(w3, u->path);
+ W3_Enable_Redirect(w3);
W3_On(w3, "status", (void*)status_handler);
+ W3_On(w3, "header", (void*)header_handler);
W3_On(w3, "data", (void*)data_handler);
W3_Send_Request(w3);
W3_Free(w3);
printf("%d bytes\n", datalen);
- }else{
+ } else {
fprintf(stderr, "Failed to connect\n");
}
W3_Free_URL(u);
- }else{
+ } else {
fprintf(stderr, "Failed to parse\n");
}
}
acc = false;
break;
case 'p':
- if(acc){
+ if(acc) {
write(1, databuf, datalen);
}
break;
if(strcmp(protocol, "https") == 0) {
ssl = true;
}
+ w3->props = NULL;
w3->method = NULL;
w3->path = NULL;
w3->events = NULL;
for(i = 0; w3->headers[i] != 0; i++) free(w3->headers[i]);
free(w3->headers);
}
+ if(w3->props != NULL) {
+ int i;
+ for(i = 0; w3->props[i] != 0; i++) free(w3->props[i]);
+ free(w3->props);
+ }
if(w3->events != NULL) {
int i;
for(i = 0; w3->events[i] != 0; i += 2) free(w3->events[i]);
free(w3->events);
}
+ W3_Disconnect(w3);
free(w3);
}
+
+void W3_Disconnect(struct W3* w3) {
+#ifdef SSL_SUPPORT
+ if(w3->ssl != NULL) {
+ SSL_shutdown(w3->ssl);
+ SSL_CTX_free(w3->ssl_ctx);
+ SSL_free(w3->ssl);
+ w3->ssl = NULL;
+ }
+#endif
+ if(w3->sock != -1) close(w3->sock);
+}
#include "W3Core.h"
#include "W3Util.h"
+#include "W3URL.h"
#include <stdbool.h>
#include <stdio.h>
bool breakchunk = false;
int chunksize;
size_t allsize = -1;
+ char* redir = NULL;
while(true) {
int l = __W3_Auto_Read(w3, buf, w3->readsize);
if(l <= 0) break;
chunked = true;
} else if(strcasecmp(data, "content-length") == 0) {
allsize = atoi(data + k + 1);
+ } else if(strcasecmp(data, "location") == 0) {
+ if(redir != NULL) free(redir);
+ redir = __W3_Strdup(data + k + 1);
}
}
break;
free(buf);
if(chunklen != NULL) free(chunklen);
if(chunk != NULL) free(chunk);
+ if(redir != NULL && __W3_Have_Prop(w3, "HTTP_REDIRECT")){
+ W3_Disconnect(w3);
+ struct W3URL* u = W3_Parse_URL(redir);
+ if(u != NULL){
+ w3->sock = __W3_DNS_Connect(u->host, strcmp(u->protocol, "https") == 0 ? true : false, u->port
+#ifdef SSL_SUPPORT
+ ,
+ &w3->ssl, &w3->ssl_ctx
+#endif
+ );
+ free(w3->protocol);
+ w3->protocol = __W3_Strdup(u->protocol);
+
+ free(w3->hostname);
+ w3->hostname = __W3_Strdup(u->host);
+ W3_Set_Path(w3, u->path);
+ W3_Free_URL(u);
+ W3_Send_Request(w3);
+ }
+ }
+ if(redir != NULL) free(redir);
}
+
+void W3_Enable_Redirect(struct W3* w3) { __W3_Add_Prop(w3, "HTTP_REDIRECT"); }
__W3_Debug("URL", str);
free(str);
}
- }else{
+ } else {
W3_Free_URL(r);
r = NULL;
}
return __W3_Concat3(un.sysname, "/", un.release);
#endif
}
+
+bool __W3_Have_Prop(struct W3* w3, const char* name) {
+ if(w3->props == NULL) return false;
+ int i;
+ for(i = 0; w3->props[i] != NULL; i++) {
+ if(strcmp(w3->props[i], name) == 0) return true;
+ }
+ return false;
+}
+
+void __W3_Add_Prop(struct W3* w3, const char* name) {
+ char* str = __W3_Concat3("Adding a prop `", name, "`");
+ __W3_Debug("Prop", str);
+ free(str);
+ int len = 0;
+ if(w3->props == NULL) {
+ w3->props = malloc(sizeof(*w3->props) * 2);
+ } else {
+ char** oldprops = w3->props;
+ for(len = 0; oldprops[len] != NULL; len++)
+ ;
+ w3->props = malloc(sizeof(*w3->props) * (len + 2));
+ for(len = 0; oldprops[len] != NULL; len++) {
+ w3->props[len] = oldprops[len];
+ }
+ free(oldprops);
+ }
+ w3->props[len] = __W3_Strdup(name);
+ w3->props[len + 1] = NULL;
+}
char* data; /* As you can read from its name */
size_t size; /* Size of the data */
size_t readsize; /* Read buffer size, default is 512 */
+ char** props; /* Properties */
#ifdef SSL_SUPPORT
void* ssl; /* Actually SSL*, NULL if no SSL */
void* ssl_ctx; /* Actually SSL_CTX* */
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 */
+void W3_Disconnect(struct W3* w3); /* Disconnect */
void W3_Set_Data(struct W3* w3, char* data, size_t size); /* Send the data - LibW3 won't free the data */
void W3_Set_Read_Size(struct W3* w3, size_t size); /* Change the read buffer size */
#endif
#include "W3Core.h"
+
void __W3_HTTP_Request(struct W3* w3);
+void W3_Enable_Redirect(struct W3* w3);
#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);
+bool __W3_Have_Prop(struct W3* w3, const char* name);
+void __W3_Add_Prop(struct W3* w3, const char* name);
void* __W3_Get_Event(struct W3* w3, const char* eventname);
char* __W3_Get_Platform(void);
# $Id$
include config.mk
+ifeq ($(shell uname -s),SunOS)
+GREP = ggrep
+else
+GREP = grep
+endif
+
CC := cc
CFLAGS := -g -std=c99 -fPIC -D_BSD_SOURCE
LDFLAGS :=
LIBS :=
PREFIX := /usr/local
-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")
+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")
ifeq ($(SSL),YES)
extern "C" {
#endif
-#define LIBW3_VERSION "1.4A" \
+#define LIBW3_VERSION "1.5" \
SUFFIX
#ifdef __cplusplus