summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--nanohttp/nanohttp-client.c465
-rw-r--r--nanohttp/nanohttp-client.h11
-rw-r--r--nanohttp/nanohttp-common.c156
-rw-r--r--nanohttp/nanohttp-common.h30
4 files changed, 380 insertions, 282 deletions
diff --git a/nanohttp/nanohttp-client.c b/nanohttp/nanohttp-client.c
index cd6b802..14b024f 100644
--- a/nanohttp/nanohttp-client.c
+++ b/nanohttp/nanohttp-client.c
@@ -1,5 +1,5 @@
/******************************************************************
- * $Id: nanohttp-client.c,v 1.4 2003/12/17 12:55:02 snowdrop Exp $
+ * $Id: nanohttp-client.c,v 1.5 2003/12/18 11:14:37 snowdrop Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -28,25 +28,15 @@
#endif
#include <time.h>
-
#include <stdio.h>
#include <stdlib.h>
/*--------------------------------------------------
- STRUCTURES
-----------------------------------------------------*/
-typedef struct httpc_cb_userdata
-{
- httpc_conn_t *conn;
- void *userdata;
- httpc_response_callback callback;
- int counter;
-}httpc_cb_userdata_t;
-
-
-/*--------------------------------------------------
FUNCTION: httpc_new
+ DESC: Creates a new http client connection object
+ You need to create at least 1 http client connection
+ to communicate via http.
----------------------------------------------------*/
httpc_conn_t* httpc_new()
{
@@ -62,17 +52,15 @@ httpc_conn_t* httpc_new()
/*--------------------------------------------------
FUNCTION: httpc_free
+ DESC: Free the given http client object.
----------------------------------------------------*/
void httpc_free(httpc_conn_t* conn)
{
- const char *FUNC = "httpc_free";
hpair_t *tmp;
if (conn != NULL) {
hsocket_free(conn->sock);
-
-
while (conn->header != NULL) {
tmp = conn->header;
conn->header = conn->header->next;
@@ -85,31 +73,17 @@ void httpc_free(httpc_conn_t* conn)
/*--------------------------------------------------
- FUNCTION: httpc_add_header
-----------------------------------------------------*/
-void httpc_add_header(httpc_conn_t *conn, const char* key, const char* value)
-{
- const char *FUNC = "httpc_add_header";
-
- if (conn == NULL) {
- log_warn(FUNC, "Connection object is NULL");
- return;
- }
-
- conn->header = hpairnode_new(key, value, conn->header);
-}
-
-
-/*--------------------------------------------------
FUNCTION: httpc_set_header
+ DESC: Adds a new (key, value) pair to the header
+ or modifies the old pair if this function will
+ finds another pair with the same 'key' value.
----------------------------------------------------*/
int httpc_set_header(httpc_conn_t *conn, const char* key, const char* value)
{
- const char *FUNC = "httpc_set_header";
hpair_t *p;
if (conn == NULL) {
- log_warn(FUNC, "Connection object is NULL");
+ log_warn1("Connection object is NULL");
return 0;
}
@@ -131,12 +105,12 @@ int httpc_set_header(httpc_conn_t *conn, const char* key, const char* value)
}
-
/*--------------------------------------------------
- FUNCTION: httpc_build_header
+ FUNCTION: httpc_header_add_date
+ DESC: Adds the current date to the header.
----------------------------------------------------*/
static
-void httpc_build_header(httpc_conn_t *conn)
+void httpc_header_add_date(httpc_conn_t *conn)
{
char buffer[255];
time_t nw;
@@ -150,99 +124,11 @@ void httpc_build_header(httpc_conn_t *conn)
}
-/*--------------------------------------------------
- FUNCTION: httpc_get
-----------------------------------------------------*/
-hresponse_t *httpc_get(httpc_conn_t *conn, const char* urlstr)
-{
- const char *FUNC = "httpc_get";
-
- hurl_t *url;
- char buffer[255];
- int status;
- char *response;
- int rsize;
- hresponse_t *res;
- char *rest;
- int restsize;
-
- if (conn == NULL) {
- log_error(FUNC, "Connection object is NULL");
- return NULL;
- }
-
- /* set response to 0 to allocate
- it in hsocket_recv */
- response = 0;
-
- /* Build request header */
- httpc_build_header(conn);
-
- /* Create url */
- url = hurl_new(urlstr);
- if (url == NULL) {
- log_error(FUNC, "Can not parse URL '%s'", SAVE_STR(urlstr));
- return NULL;
- }
-
- /* Set hostname */
- httpc_set_header(conn, HEADER_HOST, url->host);
-
- /* Open connection */
- status = hsocket_open(&conn->sock, url->host, url->port);
- if (status != HSOCKET_OK) {
- log_error(FUNC, "Can not open connection to '%s' (status:%d)",
- SAVE_STR(url->host), status);
- return NULL;
- }
-
- /* Send GET */
- sprintf(buffer, "GET %s HTTP/1.1\r\n",
- (url->context)?url->context:("/"));
- status = hsocket_send(conn->sock, buffer);
- if (status != HSOCKET_OK) {
- log_error(FUNC, "Can not send GET (status:%d)", status);
- hsocket_close(conn->sock);
- return NULL;
- }
-
- /* Send Header */
- status = httpc_send_header(conn);
- if (status != HSOCKET_OK) {
- log_error(FUNC, "Can not send header (status:%d)", status);
- hsocket_close(conn->sock);
- return NULL;
- }
-
- /* Receive Response */
- status = hsocket_recv(conn->sock, &response, &rsize);
- if (status != HSOCKET_OK) {
- log_error(FUNC, "Can not receive response (status:%d)", status);
- return NULL;
- }
-
-
- /*status = hsocket_recv_limit(conn->sock, &response,
- "\r\n\r\n", &rest, &rsize, &restsize);
- if (status != HSOCKET_OK) {
- log_error(FUNC, "Can not receive response (status:%d)", status);
- return NULL;
- }
- */
- res = hresponse_new(response);
-
- if (res == NULL) {
- free(response);
- return NULL;
- }
-
- /*res->body = rest;*/
- return res;
-}
-
/*--------------------------------------------------
FUNCTION: httpc_send_header
+ DESC: Sends the current header information stored
+ in conn through conn->sock.
----------------------------------------------------*/
int httpc_send_header(httpc_conn_t *conn)
{
@@ -265,121 +151,57 @@ int httpc_send_header(httpc_conn_t *conn)
return status;
}
-/*--------------------------------------------------
- FUNCTION: httpc_post
-----------------------------------------------------*/
-hresponse_t *httpc_post(httpc_conn_t *conn, const char *urlstr, const char *content)
-{
- const char *FUNC = "httpc_post";
- int content_length;
- hurl_t *url;
- char buffer[255];
- int status;
- char *response;
- hresponse_t *res;
- int rsize;
-
- if (conn == NULL) {
- log_error(FUNC, "Connection object is NULL");
- return NULL;
- }
-
- if (content == NULL) {
- log_error(FUNC, "Content is NULL");
- return NULL;
- }
-
- /* set response to 0 to allocate
- it in hsocket_recv */
- response = 0;
- /* Build request header */
- httpc_build_header(conn);
-
- /* Create url */
- url = hurl_new(urlstr);
- if (url == NULL) {
- log_error(FUNC, "Can not parse URL '%s'", SAVE_STR(urlstr));
- return NULL;
- }
-
- /* Set content length */
- content_length = strlen(content);
- sprintf(buffer, "%d", content_length);
- httpc_set_header(conn, HEADER_CONTENT_LENGTH, buffer);
-
- /* Set hostname */
- httpc_set_header(conn, HEADER_HOST, url->host);
-
- /* Open connection */
- status = hsocket_open(&conn->sock, url->host, url->port);
- if (status != HSOCKET_OK) {
- log_error(FUNC, "Can not open connection to '%s' (status:%d)",
- SAVE_STR(url->host), status);
- return NULL;
- }
+/*--------------------------------------------------
+ FUNCTION: httpc_talk_to_server
+ DESC: This function is the heart of the httpc
+ module. It will send the request and process the
+ response.
- /* Send POST */
- sprintf(buffer, "POST %s HTTP/1.1\r\n",
- (url->context)?url->context:("/"));
- status = hsocket_send(conn->sock, buffer);
- if (status != HSOCKET_OK) {
- log_error(FUNC, "Can not send POST (status:%d)", status);
- hsocket_close(conn->sock);
- return NULL;
- }
+ Here the parameters:
- /* Send Header */
- status = httpc_send_header(conn);
- if (status != HSOCKET_OK) {
- log_error(FUNC, "Can not send header (status:%d)", status);
- hsocket_close(conn->sock);
- return NULL;
- }
-
- /* Send Content */
- status = hsocket_send(conn->sock, content);
- if (status != HSOCKET_OK) {
- log_error(FUNC, "Can not send content (status:%d)", status);
- return NULL;
- }
+ method:
+ the request method. This can be HTTP_REQUEST_POST and
+ HTTP_REQUEST_GET.
- /* Receive Response */
- status = hsocket_recv(conn->sock, &response, &rsize);
- if (status != HSOCKET_OK) {
- log_error(FUNC, "Can not receive response (status:%d)", status);
- return NULL;
- }
+ conn:
+ the connection object (created with httpc_new())
+
+ urlstr:
+ the complete url in string format.
+ http://<host>:<port>/<context>
+ where <port> is not mendatory.
- res = hresponse_new(response);
- if (res == NULL) {
- free(response);
- return NULL;
- }
+ start_cb:
+ a callback function, which will be called when
+ the response header is completely arrives.
- return res;
-}
+ cb:
+ a callback function, which will be called everytime
+ when data arrives.
+ content_size:
+ size of content to send.
+ (only if method is HTTP_REQUEST_POST)
-/*--------------------------------------------------
- FUNCTION: httpc_recv_cb_callback
-----------------------------------------------------*/
-int httpc_recv_cb_callback(hsocket_t sock, char *buffer,
- int size, void *userdata)
-{
- httpc_cb_userdata_t *cbdata = (httpc_cb_userdata_t*)userdata;
+ content:
+ the content data to send.
+ (only if method is HTTP_REQUEST_POST)
- cbdata->callback(cbdata->counter++, cbdata->conn,
- cbdata->userdata, size, buffer);
- return 1;
-}
+ userdata:
+ a user define data, which will be passed to the
+ start_cb and cb callbacks as a parameter. This
+ can also be NULL.
-/*--------------------------------------------------
- FUNCTION: httpc_get_cb
+
+ If success, this function will return 0.
+ >0 otherwise.
----------------------------------------------------*/
-int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
- httpc_response_start_callback start_cb,
- httpc_response_callback cb, void *userdata)
+static
+int httpc_talk_to_server(hreq_method method, httpc_conn_t *conn, const char *urlstr,
+ httpc_response_start_callback start_cb,
+ httpc_response_callback cb, int content_size,
+ char* content, void *userdata)
{
hurl_t *url;
char buffer[4096];
@@ -389,7 +211,6 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
hresponse_t *res;
char *rest;
int restsize;
- httpc_cb_userdata_t cbdata;
int i, done;
/* content-length */
@@ -403,12 +224,10 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
/* chunked encoding */
char *transfer_encoding;
char *chunk_buffer;
- int chunk_size, cs;
+ int chunk_size;
hbufsocket_t bufsock;
- char tmpch;
char chunk_size_str[25];
int chunk_size_cur;
- char chunk_ch[2];
/* connection closed */
char *connection_status;
@@ -424,7 +243,7 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
response = 0;
/* Build request header */
- httpc_build_header(conn);
+ httpc_header_add_date(conn);
/* Create url */
url = hurl_new(urlstr);
@@ -444,12 +263,27 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
return 3;
}
- /* Send GET */
- sprintf(buffer, "GET %s HTTP/1.1\r\n",
- (url->context)?url->context:("/"));
+ /* check method */
+ if (method == HTTP_REQUEST_GET) {
+
+ /* Set GET Header */
+ sprintf(buffer, "GET %s HTTP/1.1\r\n",
+ (url->context)?url->context:("/"));
+
+ } else if (method == HTTP_REQUEST_POST) {
+
+ /* Set POST Header */
+ sprintf(buffer, "POST %s HTTP/1.1\r\n",
+ (url->context)?url->context:("/"));
+ } else {
+
+ log_error1("Unknown method type!");
+ return 15;
+ }
+
status = hsocket_send(conn->sock, buffer);
if (status != HSOCKET_OK) {
- log_error2("Can not send GET (status:%d)", status);
+ log_error2("Can not send request (status:%d)", status);
hsocket_close(conn->sock);
return 4;
}
@@ -525,7 +359,7 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
return 13;
}
- res = hresponse_new(response);
+ res = hresponse_new_from_buffer(response);
if (res == NULL) {
log_error2("Can't create response (url:'%s')", urlstr);
return 7;
@@ -698,16 +532,157 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
/*--------------------------------------------------
+ FUNCTION: httpc_get_cb
+ DESC: Wraps the httpc_talk_to_server() function
+ to communicate with GET method.
+
+ See the documentation of httpc_talk_to_server()
+ for more information.
+----------------------------------------------------*/
+int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
+ httpc_response_start_callback start_cb,
+ httpc_response_callback cb, void *userdata)
+{
+ int status;
+
+ status = httpc_talk_to_server(HTTP_REQUEST_GET, conn, urlstr,
+ start_cb, cb, 0, NULL, userdata);
+ return status;
+}
+
+
+
+/*--------------------------------------------------
FUNCTION: httpc_post_cb
+ DESC: Wraps the httpc_talk_to_server() function
+ to communicate with POST method.
+
+ See the documentation of httpc_talk_to_server()
+ for more information.
----------------------------------------------------*/
-int httpc_post_cb(httpc_conn_t *conn, const char *url,
+int httpc_post_cb(httpc_conn_t *conn, const char *urlstr,
httpc_response_start_callback start_cb,
- httpc_response_callback cb, void *userdata)
+ httpc_response_callback cb, int content_size,
+ char *content, void *userdata)
{
- return 1;
+ int status;
+
+ status = httpc_talk_to_server(HTTP_REQUEST_POST, conn, urlstr,
+ start_cb, cb, content_size, content,
+ userdata);
+ return status;
}
+/*======================================================
+ The following functions are used internally to wrap
+ the httpc_x_cb (x = get|post) functions.
+ ======================================================*/
+static
+void httpc_custom_res_callback(int counter, httpc_conn_t* conn,
+ void *userdata, int size, char *buffer)
+{
+ hresponse_t *res;
+
+ /* get response object */
+ res = (hresponse_t*)userdata;
+
+ /* allocate buffersize */
+
+ res->bodysize += size;
+ res->body = (unsigned char*)realloc(res->body, res->bodysize + 1);
+
+ memcpy(&(res->body[res->bodysize-size]), buffer, size);
+
+}
+
+
+static
+void httpc_custom_start_callback(httpc_conn_t *conn, void *userdata,
+ hpair_t *header, const char *spec,
+ int errcode, const char *desc)
+{
+ hresponse_t *res;
+
+ /* get response object */
+ res = (hresponse_t*)userdata;
+
+ /* set spec */
+ if (spec != NULL) {
+ strcpy(res->spec, spec);
+ }
+
+ /* set errcode */
+ res->errcode = errcode;
+
+ /* set desc */
+ if (desc != NULL) {
+ res->desc = (char*)malloc(strlen(desc)+1);
+ strcpy(res->desc, desc);
+ }
+
+ /* set header */
+ if (header == NULL) {
+ log_warn1("header is NULL!");
+ }
+
+ res->header = hpairnode_copy_deep(header);
+}
+
+
+/*--------------------------------------------------
+ FUNCTION: httpc_get
+----------------------------------------------------*/
+hresponse_t *httpc_get(httpc_conn_t *conn, const char *url)
+{
+ int status;
+ hresponse_t *res;
+
+ res = hresponse_new();
+ status = httpc_get_cb(conn, url, httpc_custom_start_callback,
+ httpc_custom_res_callback, res);
+
+ if (status != 0) {
+ hresponse_free(res);
+ return NULL;
+ }
+
+ return res;
+}
+
+
+/*--------------------------------------------------
+ FUNCTION: httpc_post
+----------------------------------------------------*/
+hresponse_t *httpc_post(httpc_conn_t *conn, const char *url,
+ int content_size, const char *content)
+{
+ int status;
+ hresponse_t *res;
+
+ res = hresponse_new();
+ status = httpc_post_cb(conn, url, httpc_custom_start_callback,
+ httpc_custom_res_callback, content_size,
+ content, res);
+
+ if (status != 0) {
+ hresponse_free(res);
+ return NULL;
+ }
+
+ return res;
+}
+
+
+
+
+
+
+
+
+
+
+
diff --git a/nanohttp/nanohttp-client.h b/nanohttp/nanohttp-client.h
index 60abc57..7fad690 100644
--- a/nanohttp/nanohttp-client.h
+++ b/nanohttp/nanohttp-client.h
@@ -1,5 +1,5 @@
/******************************************************************
- * $Id: nanohttp-client.h,v 1.1 2003/12/11 14:51:04 snowdrop Exp $
+ * $Id: nanohttp-client.h,v 1.2 2003/12/18 11:14:37 snowdrop Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -54,19 +54,22 @@ typedef void (*httpc_response_start_callback)(httpc_conn_t*, void*, hpair_t*,
httpc_conn_t* httpc_new();
void httpc_free(httpc_conn_t* conn);
-void httpc_add_header(httpc_conn_t *conn, const char* key, const char* value);
int httpc_set_header(httpc_conn_t *conn, const char* key, const char* value);
hresponse_t *httpc_get(httpc_conn_t *conn, const char *url);
-hresponse_t *httpc_post(httpc_conn_t *conn, const char *url, const char *content);
+hresponse_t *httpc_post(httpc_conn_t *conn, const char *url,
+ int conten_size, const char *content);
int httpc_get_cb(httpc_conn_t *conn, const char *url,
httpc_response_start_callback start_cb,
httpc_response_callback cb, void *userdata);
+
int httpc_post_cb(httpc_conn_t *conn, const char *url,
httpc_response_start_callback start_cb,
- httpc_response_callback cb, void *userdata);
+ httpc_response_callback cb, int content_size,
+ char *content, void *userdata);
+
#endif
diff --git a/nanohttp/nanohttp-common.c b/nanohttp/nanohttp-common.c
index e13276d..4c196a9 100644
--- a/nanohttp/nanohttp-common.c
+++ b/nanohttp/nanohttp-common.c
@@ -1,5 +1,5 @@
/******************************************************************
- * $Id: nanohttp-common.c,v 1.3 2003/12/17 13:48:37 snowdrop Exp $
+ * $Id: nanohttp-common.c,v 1.4 2003/12/18 11:14:37 snowdrop Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -60,6 +60,15 @@ void log_write(log_level_t level, const char *prefix,
fflush(stdout);
}
+void log_verbose(const char* FUNC, const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ log_write(HLOG_VERBOSE, "VERBOSE", FUNC, format, ap);
+ va_end(ap);
+}
+
void log_debug(const char* FUNC, const char *format, ...)
{
va_list ap;
@@ -101,13 +110,24 @@ hpair_t *hpairnode_new(const char* key, const char* value, hpair_t *next)
{
hpair_t *pair;
+ log_verbose3("new pair ('%s','%s')", SAVE_STR(key), SAVE_STR(value));
pair = (hpair_t*)malloc(sizeof(hpair_t));
- pair->key = (char*)malloc(strlen(key)+1);
- pair->value = (char*)malloc(strlen(value)+1);
- pair->next = next;
- strcpy(pair->key, key);
- strcpy(pair->value, value);
+ if (key != NULL) {
+ pair->key = (char*)malloc(strlen(key)+1);
+ strcpy(pair->key, key);
+ } else {
+ pair->key = NULL;
+ }
+
+ if (value != NULL) {
+ pair->value = (char*)malloc(strlen(value)+1);
+ strcpy(pair->value, value);
+ } else {
+ pair->value = NULL;
+ }
+
+ pair->next = next;
return pair;
}
@@ -139,6 +159,68 @@ hpair_t *hpairnode_parse(const char *str, const char *delim, hpair_t *next)
return pair;
}
+
+hpair_t* hpairnode_copy(const hpair_t *src)
+{
+ hpair_t *pair;
+
+ if (src == NULL) return NULL;
+
+ pair = hpairnode_new(src->key, src->value, NULL);
+ return pair;
+}
+
+
+hpair_t* hpairnode_copy_deep(const hpair_t *src)
+{
+ hpair_t *pair, *result, *next;
+
+ if (src == NULL) return NULL;
+
+ result = hpairnode_copy(src);
+
+ next = src->next;
+ pair = result;
+
+ while (next != NULL) {
+ pair->next = hpairnode_copy(next);
+ pair = pair->next;
+ next = next->next;
+ }
+
+ return result;
+}
+
+
+void hpairnode_dump(hpair_t *pair)
+{
+ if (pair == NULL) {
+ log_verbose1("(NULL)[]");
+ return;
+ }
+
+ log_verbose5("(%p)['%s','%s','%p']", pair,
+ SAVE_STR(pair->key), SAVE_STR(pair->value),
+ pair->next);
+}
+
+
+void hpairnode_dump_deep(hpair_t *pair)
+{
+ hpair_t *p;
+ p = pair;
+
+ log_verbose1("-- BEGIN dump hpairnode_t --");
+
+ while (p != NULL) {
+ hpairnode_dump(p);
+ p = p->next;
+ }
+
+ log_verbose1("-- END dump hpairnode_t --\n");
+}
+
+
void hpairnode_free(hpair_t *pair)
{
if (pair == NULL) return;
@@ -172,17 +254,16 @@ char *hpairnode_get(hpair_t *pair, const char* key)
static
void hurl_dump(const hurl_t *url)
{
- const char *FUNC = "hurl_dump";
if (url == NULL) {
- printf("(null)\n");
+ log_error1("url is NULL!");
return ;
}
- log_debug(FUNC, "PROTOCOL : %s", url->protocol?url->protocol:"(null)");
- log_debug(FUNC, " HOST : %s", url->host?url->host:"(null)");
- log_debug(FUNC, " PORT : %d", url->port);
- log_debug(FUNC, " CONTEXT : %s", url->context?url->context:"(null)");
+ log_verbose2("PROTOCOL : %s", SAVE_STR(url->protocol));
+ log_verbose2(" HOST : %s", SAVE_STR(url->host));
+ log_verbose2(" PORT : %d", url->port);
+ log_verbose2(" CONTEXT : %s", SAVE_STR(url->context));
}
@@ -195,7 +276,6 @@ hurl_t* hurl_new(const char* urlstr)
int size;
hurl_t *url;
char tmp[8];
- const char *FUNC = "hurl_create";
iprotocol = 0;
len = strlen(urlstr);
@@ -207,12 +287,12 @@ hurl_t* hurl_new(const char* urlstr)
}
if (iprotocol == 0) {
- log_error(FUNC, "no protocol");
+ log_error1("no protocol");
return NULL;
}
if (iprotocol + 3 >= len) {
- log_error(FUNC, "no host");
+ log_error1("no host");
return NULL;
}
@@ -220,7 +300,7 @@ hurl_t* hurl_new(const char* urlstr)
&& urlstr[iprotocol+1] != '/'
&& urlstr[iprotocol+2] != '/')
{
- log_error(FUNC, "no protocol");
+ log_error1("no protocol");
return NULL;
}
@@ -234,7 +314,7 @@ hurl_t* hurl_new(const char* urlstr)
}
if (ihost == iprotocol + 1) {
- log_error(FUNC, "no host");
+ log_error1("no host");
return NULL;
}
@@ -299,22 +379,38 @@ void hurl_free(hurl_t *url)
/* response stuff */
+
/* -------------------------------------------
FUNCTION: hresponse_new
---------------------------------------------*/
-hresponse_t *hresponse_new(const char* buffer)
+hresponse_t *hresponse_new()
{
hresponse_t *res;
- char *s1, *s2, *str;
- hpair_t *pair;
-
- const char *FUNC = "hresponse_new";
/* create response object */
res = (hresponse_t*)malloc(sizeof(hresponse_t));
- res->desc = "";
+ res->spec[0] = '\0';
+ res->errcode = -1;
+ res->desc = NULL;
res->header = NULL;
- res->body = "";
+ res->body = NULL;
+ res->bodysize = 0;
+
+ return res;
+}
+
+
+/* -------------------------------------------
+ FUNCTION: hresponse_new_from_buffer
+ ---------------------------------------------*/
+hresponse_t *hresponse_new_from_buffer(const char* buffer)
+{
+ hresponse_t *res;
+ char *s1, *s2, *str;
+ hpair_t *pair;
+
+ /* create response object */
+ res = hresponse_new();
/* *** parse spec *** */
/* [HTTP/1.1 | 1.2] [CODE] [DESC] */
@@ -322,21 +418,21 @@ hresponse_t *hresponse_new(const char* buffer)
/* stage 1: HTTP spec */
str = (char*)strtok_r(buffer, " ", &s2);
s1 = s2;
- if (str == NULL) { log_error(FUNC, "Parse error"); return NULL; }
+ if (str == NULL) { log_error1("Parse error"); return NULL; }
strncpy(res->spec, str, 10);
/* stage 2: http code */
str = (char*)strtok_r(s1, " ", &s2);
s1 = s2;
- if (str == NULL) { log_error(FUNC, "Parse error"); return NULL; }
+ if (str == NULL) { log_error1("Parse error"); return NULL; }
res->errcode = atoi(str);
/* stage 3: description text */
str = (char*)strtok_r(s1, "\r\n", &s2);
s1 = s2;
- if (str == NULL) { log_error(FUNC, "Parse error"); return NULL; }
+ if (str == NULL) { log_error1("Parse error"); return NULL; }
res->desc = (char*)malloc(strlen(str)+1);
strcpy(res->desc, str);
@@ -376,3 +472,9 @@ void hresponse_free(hresponse_t *res)
}
+
+
+
+
+
+
diff --git a/nanohttp/nanohttp-common.h b/nanohttp/nanohttp-common.h
index 9e32368..326a4e4 100644
--- a/nanohttp/nanohttp-common.h
+++ b/nanohttp/nanohttp-common.h
@@ -1,5 +1,5 @@
/******************************************************************
- * $Id: nanohttp-common.h,v 1.3 2003/12/17 12:55:02 snowdrop Exp $
+ * $Id: nanohttp-common.h,v 1.4 2003/12/18 11:14:37 snowdrop Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -54,12 +54,15 @@ struct hpair
hpair_t *hpairnode_new(const char* key, const char* value, hpair_t* next);
void hpairnode_free(hpair_t *pair);
char *hpairnode_get(hpair_t *pair, const char* key);
-
+hpair_t* hpairnode_copy(const hpair_t *src);
+hpair_t* hpairnode_copy_deep(const hpair_t *src);
+void hpairnode_dump_deep(hpair_t *pair);
+void hpairnode_dump(hpair_t *pair);
typedef enum hreq_method
{
- HTTP_REQUESET_POST,
- HTTP_REQUESET_GET
+ HTTP_REQUEST_POST,
+ HTTP_REQUEST_GET
}hreq_method ;
@@ -84,19 +87,22 @@ typedef struct hresponse
int errcode;
char *desc;
hpair_t *header;
- char *body;
+ unsigned char *body;
+ long bodysize;
}hresponse_t;
/*
PARAMS
buffer: The hole received data from socket.
*/
-hresponse_t *hresponse_new(const char* buffer);
+hresponse_t *hresponse_new_from_buffer(const char* buffer);
+hresponse_t *hresponse_new();
void hresponse_free(hresponse_t *res);
/* logging stuff*/
typedef enum log_level
{
+ HLOG_VERBOSE,
HLOG_DEBUG,
HLOG_INFO,
HLOG_WARN,
@@ -104,9 +110,17 @@ typedef enum log_level
HLOG_FATAL
}log_level_t;
+
+
log_level_t log_set_level(log_level_t level);
log_level_t log_get_level();
+#define log_verbose1(a1) log_verbose(__FUNCTION__, a1)
+#define log_verbose2(a1,a2) log_verbose(__FUNCTION__, a1,a2)
+#define log_verbose3(a1,a2,a3) log_verbose(__FUNCTION__, a1,a2,a3)
+#define log_verbose4(a1,a2,a3,a4) log_verbose(__FUNCTION__, a1,a2,a3,a4)
+#define log_verbose5(a1,a2,a3,a4,a5) log_verbose(__FUNCTION__, a1,a2,a3,a4,a5)
+
#define log_debug1(a1) log_debug(__FUNCTION__, a1)
#define log_debug2(a1,a2) log_debug(__FUNCTION__, a1,a2)
#define log_debug3(a1,a2,a3) log_debug(__FUNCTION__, a1,a2,a3)
@@ -131,11 +145,15 @@ log_level_t log_get_level();
#define log_error4(a1,a2,a3,a4) log_error(__FUNCTION__, a1,a2,a3,a4)
#define log_error5(a1,a2,a3,a4,a5) log_error(__FUNCTION__, a1,a2,a3,a4,a5)
+void log_verbose(const char* FUNC, const char *format, ...);
void log_debug(const char* FUNC, const char *format, ...);
void log_info(const char* FUNC, const char *format, ...);
void log_warn(const char* FUNC, const char *format, ...);
void log_error(const char* FUNC, const char *format, ...);
+
+
+
#endif