return 0;
}
if(argc < 3){
- fprintf(stderr, "Usage: %s URL Path\n", argv[0]);
+ fprintf(stderr, "Usage: %s URL Path [method [data]]\n", argv[0]);
return 1;
}
W3_Library_Init();
struct W3* w3 = W3_Create("http", argv[1], 80);
if(w3 != NULL){
- W3_Set_Method(w3, "GET");
+ W3_Set_Method(w3, argv[3] == NULL ? "GET" : argv[3]);
W3_Set_Path(w3, argv[2]);
W3_On(w3, "status", (void*)status);
W3_On(w3, "data", (void*)fetch_data);
W3_On(w3, "header", (void*)header);
+ if(argv[3] != NULL && strcmp(argv[3], "POST") == 0 && argv[4] != NULL){
+ W3_Set_Data(w3, argv[4], strlen(argv[4]));
+ }
W3_Send_Request(w3);
W3_Free(w3);
}else{
w3->headers = NULL;
w3->protocol = __W3_Strdup(protocol);
w3->hostname = __W3_Strdup(hostname);
+ w3->size = 0;
+ w3->data = NULL;
if(ssl) __W3_Debug("Protocol", "Enabled SSL");
w3->sock = __W3_DNS_Connect(hostname, ssl, port
#ifdef SSL_SUPPORT
}
}
+void W3_Set_Data(struct W3* w3, char* data, size_t size){
+ w3->data = data;
+ w3->size = size;
+}
+
void W3_Set_Header(struct W3* w3, const char* key, const char* value){
char* tmp = __W3_Concat3("Setting the header `", key, "` to `");
char* str = __W3_Concat3(tmp, value, "`");
#include <string.h>
#include <stdlib.h>
+#include <stdio.h>
void __W3_HTTP_Request(struct W3* w3){
__W3_Debug("LibW3-HTTP", "Sending the request");
int i;
for(i = 0; w3->headers[i] != NULL; i += 2){
if(strcmp(w3->headers[i], "connection") == 0) continue;
+ if(strcmp(w3->headers[i], "content-length") == 0) continue;
__W3_Auto_Write(w3, w3->headers[i], strlen(w3->headers[i]));
__W3_Auto_Write(w3, ": ", 2);
__W3_Auto_Write(w3, w3->headers[i + 1], strlen(w3->headers[i + 1]));
__W3_Auto_Write(w3, "\r\n", 2);
}
}
+ if(w3->data != NULL){
+ char* len = malloc(129);
+ memset(len, 0, 129);
+ sprintf(len, "%d", w3->size);
+ __W3_Auto_Write(w3, "content-length: ", 16);
+ __W3_Auto_Write(w3, len, strlen(len));
+ __W3_Auto_Write(w3, "\r\n", 2);
+ free(len);
+ }
__W3_Auto_Write(w3, "\r\n", 2);
- __W3_Auto_Write(w3, "\r\n", 2);
+ if(w3->data != NULL){
+ __W3_Auto_Write(w3, w3->data, w3->size);
+ }
char* buf = malloc(512);
char* statusbuf = malloc(1);
statusbuf[0] = 0;
extern "C" {
#endif
+#include <stddef.h>
#include <stdbool.h>
#include "W3Version.h"
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 */
+ char* data; /* As you can read from its name */
+ size_t size; /* Size of the data */
#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_Set_Data(struct W3* w3, char* data, size_t size); /* Send the data - LibW3 won't free the data */
#ifdef __cplusplus
}
extern "C" {
#endif
-#define LIBW3_VERSION "1.0" \
+#define LIBW3_VERSION "1.1" \
SUFFIX
#ifdef __cplusplus