diff options
author | snowdrop | 2004-10-28 10:30:41 +0000 |
---|---|---|
committer | snowdrop | 2004-10-28 10:30:41 +0000 |
commit | d24d6ab7bbfda8e302af3a5cf8be62299d543c1a (patch) | |
tree | d5da54889ad987c216d6767e39c65607b4b678e5 /nanohttp | |
parent | ebde083c541b5d202014c41450a3519f4d08e3c9 (diff) | |
download | csoap-d24d6ab7bbfda8e302af3a5cf8be62299d543c1a.tar.gz csoap-d24d6ab7bbfda8e302af3a5cf8be62299d543c1a.tar.bz2 |
changed hstatus_t to herror_t and chagend the API function to return herror_t. Added herror_*() functions and fixed a bug in the socket receive comm.
Diffstat (limited to 'nanohttp')
-rw-r--r-- | nanohttp/nanohttp-client.c | 151 | ||||
-rw-r--r-- | nanohttp/nanohttp-client.h | 22 | ||||
-rw-r--r-- | nanohttp/nanohttp-common.c | 70 | ||||
-rw-r--r-- | nanohttp/nanohttp-common.h | 27 | ||||
-rwxr-xr-x | nanohttp/nanohttp-mime.c | 58 | ||||
-rwxr-xr-x | nanohttp/nanohttp-mime.h | 4 | ||||
-rwxr-xr-x | nanohttp/nanohttp-request.c | 27 | ||||
-rwxr-xr-x | nanohttp/nanohttp-request.h | 4 | ||||
-rwxr-xr-x | nanohttp/nanohttp-response.c | 25 | ||||
-rwxr-xr-x | nanohttp/nanohttp-response.h | 4 | ||||
-rw-r--r-- | nanohttp/nanohttp-server.c | 71 | ||||
-rw-r--r-- | nanohttp/nanohttp-server.h | 21 | ||||
-rw-r--r-- | nanohttp/nanohttp-socket.c | 104 | ||||
-rw-r--r-- | nanohttp/nanohttp-socket.h | 22 | ||||
-rwxr-xr-x | nanohttp/nanohttp-stream.c | 113 | ||||
-rwxr-xr-x | nanohttp/nanohttp-stream.h | 10 |
16 files changed, 450 insertions, 283 deletions
diff --git a/nanohttp/nanohttp-client.c b/nanohttp/nanohttp-client.c index de55427..c393573 100644 --- a/nanohttp/nanohttp-client.c +++ b/nanohttp/nanohttp-client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-client.c,v 1.22 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-client.c,v 1.23 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -48,7 +48,7 @@ FUNCTION: httpc_init DESC: Initialize http client connection NOTE: This will be called from soap_client_init_args() ----------------------------------------------------*/ -int +herror_t httpc_init(int argc, char *argv[]) {
int i;
@@ -63,7 +63,7 @@ httpc_init(int argc, char *argv[]) log_set_file(argv[i+1]);
}
}
- return 0; + return H_OK; } /*-------------------------------------------------- @@ -198,11 +198,11 @@ FUNCTION: httpc_send_header DESC: Sends the current header information stored in conn through conn->sock. ----------------------------------------------------*/ -int +herror_t httpc_send_header(httpc_conn_t * conn) { hpair_t *p; - int status; + herror_t status; char buffer[1024]; p = conn->header; @@ -266,17 +266,20 @@ If success, this function will return 0. >0 otherwise. ----------------------------------------------------*/ static -int +herror_t httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, const char *urlstr) -{ +{
+ hurl_t url; char buffer[4096]; - int status; + herror_t status; if (conn == NULL) { - log_error1("Connection object is NULL"); - return 1; + return herror_new(
+ "httpc_talk_to_server",
+ GENERAL_INVALID_PARAM,
+ "httpc_conn_t param is NULL"); } /* Build request header */ httpc_header_add_date(conn); @@ -295,9 +298,7 @@ httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, /* Open connection */ status = hsocket_open(&conn->sock, url.host, url.port); if (status != H_OK) { - log_error3("Can not open connection to '%s' (status:%d)", - SAVE_STR(url.host), status); - return 3; + return status; } status = hsocket_block(conn->sock, conn->block); if (status != H_OK) { @@ -321,23 +322,27 @@ httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, } else { log_error1("Unknown method type!"); - return 15; + return herror_new(
+ "httpc_talk_to_server",
+ GENERAL_INVALID_PARAM,
+ "hreq_method_t must be HTTP_REQUEST_GET or HTTP_REQUEST_POST");
} status = hsocket_send(conn->sock, buffer); if (status != H_OK) { log_error2("Can not send request (status:%d)", status); hsocket_close(conn->sock); - return 4; + return status;
} /* Send Header */ status = httpc_send_header(conn); if (status != H_OK) { log_error2("Can not send header (status:%d)", status); hsocket_close(conn->sock); - return 5; - } - return status; + return status; + }
+ + return H_OK; } @@ -345,28 +350,26 @@ httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, FUNCTION: httpc_get DESC: ----------------------------------------------------*/ -hresponse_t * -httpc_get(httpc_conn_t *conn, const char *urlstr) +herror_t +httpc_get(httpc_conn_t *conn, hresponse_t** out, const char *urlstr) { - int status; - hresponse_t *res; + herror_t status; status = httpc_talk_to_server(HTTP_REQUEST_GET, conn, urlstr); if (status != H_OK) { - _httpc_set_error(conn, status, "Can not comunicate: %s", urlstr); - return NULL; + return status; } - res = hresponse_new_from_socket(conn->sock); - if (res == NULL) + status = hresponse_new_from_socket(conn->sock, out); + if (status != H_OK) { - _httpc_set_error(conn, -1, "Can not get response from '%s'", urlstr); - return NULL; + return status; } - - return res; +
+ + return H_OK; } @@ -374,10 +377,10 @@ httpc_get(httpc_conn_t *conn, const char *urlstr) FUNCTION: httpc_post_begin DESC: Returns H_OK if success ----------------------------------------------------*/ -int httpc_post_begin(httpc_conn_t *conn, const char *url) +herror_t httpc_post_begin(httpc_conn_t *conn, const char *url) { - int status; + herror_t status; status = httpc_talk_to_server(HTTP_REQUEST_POST, conn, url); if (status != H_OK) @@ -394,27 +397,24 @@ FUNCTION: httpc_post_begin DESC: End a "POST" method and receive the response. You MUST call httpc_post_end() before! ----------------------------------------------------*/ -hresponse_t *httpc_post_end(httpc_conn_t *conn) +herror_t httpc_post_end(httpc_conn_t *conn, hresponse_t** out) { - int status; - hresponse_t *res; + herror_t status; status = http_output_stream_flush(conn->out); if (status != H_OK) { - _httpc_set_error(conn, status, "Can not flush output stream"); - return NULL; + return status; } - res = hresponse_new_from_socket(conn->sock); - if (res == NULL) - { - _httpc_set_error(conn, -1, "Can not get response "); - return NULL; + status = hresponse_new_from_socket(conn->sock, out); + if (status != H_OK) + {
+ return status; } - - return res; +
+ return H_OK; } @@ -422,6 +422,7 @@ hresponse_t *httpc_post_end(httpc_conn_t *conn) /* --------------------------------------------------- DIME support functions httpc_dime_* function set -----------------------------------------------------*/ +/*
int httpc_dime_begin(httpc_conn_t *conn, const char *url) { int status; @@ -545,7 +546,7 @@ hresponse_t* httpc_dime_end(httpc_conn_t *conn) int status; hresponse_t *res; - /* Flush put stream */ + Flush put stream status = http_output_stream_flush(conn->out); if (status != H_OK) @@ -564,7 +565,7 @@ hresponse_t* httpc_dime_end(httpc_conn_t *conn) return res; } - +*/ /* --------------------------------------------------- MIME support functions httpc_mime_* function set -----------------------------------------------------*/ @@ -576,12 +577,12 @@ void _httpc_mime_get_boundary(httpc_conn_t *conn, char *dest) log_verbose2("boundary= \"%s\"", dest); } -int httpc_mime_begin(httpc_conn_t *conn, const char *url, +herror_t httpc_mime_begin(httpc_conn_t *conn, const char *url, const char* related_start, const char* related_start_info, const char* related_type) { - int status; + herror_t status; char buffer[300]; char temp[75]; char boundary[75]; @@ -626,12 +627,12 @@ int httpc_mime_begin(httpc_conn_t *conn, const char *url, } -int httpc_mime_next(httpc_conn_t *conn, +herror_t httpc_mime_next(httpc_conn_t *conn, const char* content_id, const char* content_type, const char* transfer_encoding) { - int status; + herror_t status; char buffer[512]; char boundary[75]; @@ -659,10 +660,9 @@ int httpc_mime_next(httpc_conn_t *conn, } -hresponse_t *httpc_mime_end(httpc_conn_t *conn) +herror_t httpc_mime_end(httpc_conn_t *conn, hresponse_t** out) { - hresponse_t *res; - int status; + herror_t status; char buffer[512]; char boundary[75]; @@ -675,25 +675,23 @@ hresponse_t *httpc_mime_end(httpc_conn_t *conn) (const byte_t*)buffer, strlen(buffer)); if (status != H_OK) - return NULL; + return status; /* Flush put stream */ status = http_output_stream_flush(conn->out); if (status != H_OK) { - _httpc_set_error(conn, status, "Can not flush output stream"); - return NULL; + return status; } - res = hresponse_new_from_socket(conn->sock); - if (res == NULL) + status = hresponse_new_from_socket(conn->sock, out); + if (status != H_OK) { - _httpc_set_error(conn, -1, "Can not get response "); - return NULL; + return status; } - - return res; +
+ return H_OK; } @@ -702,19 +700,20 @@ hresponse_t *httpc_mime_end(httpc_conn_t *conn) Send boundary and part header and continue with next part */ -int +herror_t httpc_mime_send_file (httpc_conn_t * conn, const char *content_id, const char *content_type, const char *transfer_encoding, const char *filename) { - int status; + herror_t status; FILE *fd = fopen (filename, "rb"); byte_t buffer[MAX_FILE_BUFFER_SIZE]; size_t size; if (fd == NULL) - return FILE_ERROR_OPEN; + return herror_new("httpc_mime_send_file", FILE_ERROR_OPEN,
+ "Can not open file '%s'", filename); status = httpc_mime_next(conn, content_id, content_type, transfer_encoding); @@ -730,17 +729,23 @@ httpc_mime_send_file (httpc_conn_t * conn, if (size == -1) { fclose (fd); - return FILE_ERROR_READ; - } - - status = http_output_stream_write (conn->out, buffer, size); - if (status != H_OK) { - fclose (fd); - return status; + return herror_new("httpc_mime_send_file", FILE_ERROR_READ,
+ "Can not read from file '%s'", filename); } +
+ if (size>0)
+ {
+ /*DEBUG: fwrite(buffer, 1, size, stdout);*/ + status = http_output_stream_write (conn->out, buffer, size); + if (status != H_OK) { + fclose (fd); + return status; + }
+ } } - fclose (fd); + fclose (fd);
+ log_verbose1("file sent!"); return H_OK; } diff --git a/nanohttp/nanohttp-client.h b/nanohttp/nanohttp-client.h index 6b54a03..b03c0a0 100644 --- a/nanohttp/nanohttp-client.h +++ b/nanohttp/nanohttp-client.h @@ -1,5 +1,5 @@ /******************************************************************
- * $Id: nanohttp-client.h,v 1.11 2004/10/20 14:17:41 snowdrop Exp $
+ * $Id: nanohttp-client.h,v 1.12 2004/10/28 10:30:46 snowdrop Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -58,7 +58,7 @@ typedef struct httpc_conn /**
initialize the httpc_* module
*/
-hstatus_t httpc_init(int argc, char *argv[]);
+herror_t httpc_init(int argc, char *argv[]);
/**
Creates a new connection
@@ -78,19 +78,20 @@ int httpc_set_header(httpc_conn_t *conn, const char* key, const char* value); /**
Invoke a "GET" method request and receive the response
*/
-hresponse_t *httpc_get(httpc_conn_t *conn, const char *url);
+herror_t
+httpc_get(httpc_conn_t *conn, hresponse_t** out, const char *urlstr);
/**
Start a "POST" method request
Returns: HSOCKET_OK or error flag
*/
-int httpc_post_begin(httpc_conn_t *conn, const char *url);
+herror_t httpc_post_begin(httpc_conn_t *conn, const char *url);
/**
End a "POST" method and receive the response.
You MUST call httpc_post_end() before!
*/
-hresponse_t *httpc_post_end(httpc_conn_t *conn);
+herror_t httpc_post_end(httpc_conn_t *conn, hresponse_t **out);
/* --------------------------------------------------------------
@@ -100,12 +101,13 @@ hresponse_t *httpc_post_end(httpc_conn_t *conn); /*
DIME support httpc_dime_* function set
*/
+/*
int httpc_dime_begin(httpc_conn_t *conn, const char *url);
int httpc_dime_next(httpc_conn_t* conn, long content_length,
const char *content_type, const char *id,
const char *dime_options, int last);
hresponse_t* httpc_dime_end(httpc_conn_t *conn);
-
+*/
/* --------------------------------------------------------------
MIME RELATED FUNCTIONS
@@ -118,7 +120,7 @@ hresponse_t* httpc_dime_end(httpc_conn_t *conn); Begin MIME multipart/related POST request
Returns: HSOCKET_OK or error flag
*/
-int httpc_mime_begin(httpc_conn_t *conn, const char *url,
+herror_t httpc_mime_begin(httpc_conn_t *conn, const char *url,
const char* related_start,
const char* related_start_info,
const char* related_type);
@@ -127,7 +129,7 @@ int httpc_mime_begin(httpc_conn_t *conn, const char *url, Send boundary and part header and continue
with next part
*/
-int httpc_mime_next(httpc_conn_t *conn,
+herror_t httpc_mime_next(httpc_conn_t *conn,
const char* content_id,
const char* content_type,
const char* transfer_encoding);
@@ -135,14 +137,14 @@ int httpc_mime_next(httpc_conn_t *conn, /**
Finish MIME request and get the response
*/
-hresponse_t *httpc_mime_end(httpc_conn_t *conn);
+herror_t httpc_mime_end(httpc_conn_t *conn, hresponse_t** out);
/**
Send boundary and part header and continue
with next part
*/
-int httpc_mime_send_file (httpc_conn_t * conn,
+herror_t httpc_mime_send_file (httpc_conn_t * conn,
const char *content_id,
const char *content_type,
const char *transfer_encoding,
diff --git a/nanohttp/nanohttp-common.c b/nanohttp/nanohttp-common.c index df968c3..819c98b 100644 --- a/nanohttp/nanohttp-common.c +++ b/nanohttp/nanohttp-common.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-common.c,v 1.14 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-common.c,v 1.15 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -33,6 +33,61 @@ #ifdef MEM_DEBUG #include <utils/alloc.h> #endif +
+
+
+typedef struct _herror_impl_t
+{
+ int errcode;
+ char message[250];
+ char func[100];
+}herror_impl_t;
+
+
+herror_t herror_new(const char* func, int errcode, const char* format, ...)
+{
+ va_list ap;
+
+ herror_impl_t *impl = (herror_impl_t*)malloc(sizeof(herror_impl_t));
+ impl->errcode = errcode;
+ strcpy(impl->func, func);
+ va_start(ap, format);
+ vsprintf(impl->message, format, ap);
+ va_end(ap);
+
+ return (herror_t)impl;
+
+}
+
+int herror_code(herror_t err)
+{
+ herror_impl_t* impl = (herror_impl_t*)err;
+ if (!err) return H_OK;
+ return impl->errcode;
+}
+
+char* herror_func(herror_t err)
+{
+ herror_impl_t* impl = (herror_impl_t*)err;
+ if (!err) return "";
+ return impl->func;
+}
+
+char* herror_message(herror_t err)
+{
+ herror_impl_t* impl = (herror_impl_t*)err;
+ if (!err) return "";
+ return impl->message;
+}
+
+void herror_release(herror_t err)
+{
+ herror_impl_t* impl = (herror_impl_t*)err;
+ if (!err) return;
+ free(impl);
+}
+
+
static log_level_t loglevel = HLOG_DEBUG;
static char logfile[75] = {'\0'}; @@ -372,7 +427,7 @@ hurl_dump(const hurl_t * url) } -int hurl_parse(hurl_t* url, const char *urlstr) +herror_t hurl_parse(hurl_t* url, const char *urlstr) { int iprotocol; int ihost; @@ -392,17 +447,17 @@ int hurl_parse(hurl_t* url, const char *urlstr) if (iprotocol == 0) { log_error1("no protocol"); - return URL_ERROR_NO_PROTOCOL; + return herror_new("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol"); } if (iprotocol + 3 >= len) { log_error1("no host"); - return URL_ERROR_NO_HOST; + return herror_new("hurl_parse", URL_ERROR_NO_HOST, "No host");
} if (urlstr[iprotocol] != ':' && urlstr[iprotocol + 1] != '/' && urlstr[iprotocol + 2] != '/') { log_error1("no protocol"); - return URL_ERROR_NO_PROTOCOL; + return herror_new("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol");
} /* find host */ ihost = iprotocol + 3; @@ -414,7 +469,7 @@ int hurl_parse(hurl_t* url, const char *urlstr) if (ihost == iprotocol + 1) { log_error1("no host"); - return URL_ERROR_NO_HOST; + return herror_new("hurl_parse", URL_ERROR_NO_HOST, "No host");
} /* find port */ iport = ihost; @@ -435,7 +490,8 @@ int hurl_parse(hurl_t* url, const char *urlstr) url->protocol = PROTOCOL_HTTPS; else if (strcmpigcase(protocol, "ftp")) url->protocol = PROTOCOL_FTP; - else return URL_ERROR_UNKNOWN_PROTOCOL; + else return herror_new("hurl_parse",
+ URL_ERROR_UNKNOWN_PROTOCOL, "Unknown protocol '%s'", protocol);
/* TODO (#1#): add max of size and URL_MAX_HOST_SIZE */ size = ihost - iprotocol - 3; diff --git a/nanohttp/nanohttp-common.h b/nanohttp/nanohttp-common.h index f7b6c10..79ca625 100644 --- a/nanohttp/nanohttp-common.h +++ b/nanohttp/nanohttp-common.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-common.h,v 1.13 2004/10/20 14:17:41 snowdrop Exp $ + * $Id: nanohttp-common.h,v 1.14 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -118,8 +118,18 @@ #define MIME_ERROR_NO_ROOT_PART 1304 #define MIME_ERROR_NOT_MIME_MESSAGE 1305 +
+/* General errors */
+#define GENERAL_INVALID_PARAM 1400
+#define GENERAL_HEADER_PARSE_ERROR 1401
- +/* Thread errors */
+#define THREAD_BEGIN_ERROR 1500
+
+/* XML Errors */
+#define XML_ERROR_EMPTY_DOCUMENT 1600
+#define XML_ERROR_PARSE 1601
+
/* Set Sleep function platform depended */ @@ -136,7 +146,7 @@ struct tm *localtime_r(const time_t *const timep, struct tm *p_tm); #endif typedef unsigned char byte_t; -typedef int hstatus_t; +typedef void* herror_t; @@ -159,7 +169,14 @@ typedef enum _hreq_method HTTP_REQUEST_GET }hreq_method_t ; - +
+herror_t herror_new(const char* func,
+ int errcode, const char* format, ...);
+int herror_code(herror_t err);
+char* herror_func(herror_t err);
+char* herror_message(herror_t err);
+void herror_release(herror_t err);
+
/* string function to compare strings ignoring case @@ -350,7 +367,7 @@ typedef struct _hurl URL_ERROR_NO_PROTOCOL URL_ERROR_NO_HOST */ -hstatus_t hurl_parse(hurl_t *obj, const char* url); +herror_t hurl_parse(hurl_t *obj, const char* url); /* Object representation of the content-type field diff --git a/nanohttp/nanohttp-mime.c b/nanohttp/nanohttp-mime.c index 952cc22..2b6794d 100755 --- a/nanohttp/nanohttp-mime.c +++ b/nanohttp/nanohttp-mime.c @@ -3,7 +3,7 @@ * | \/ | | | | \/ | | _/ * |_''_| |_| |_''_| |_'/ PARSER * -* $Id: nanohttp-mime.c,v 1.2 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-mime.c,v 1.3 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -509,13 +509,21 @@ MIME_read_status mime_streamreader_function(void* userdata, if (!http_input_stream_is_ready(in)) return MIME_READ_EOF; - readed = http_input_stream_read(in, dest, *size); + readed = http_input_stream_read(in, dest, *size);
+ /*
+ log_info1("http_input_stream_read() returned 0");
+ */
+ if (readed == -1) {
+ log_error4("[%d] %s():%s ", herror_code(in->err), herror_func(in->err), herror_message(in->err));
+ }
+ *size = readed; - if (*size!=-1) { - _log_str("reader.log", dest, *size); + if (*size!=-1) {
+ /* + _log_str("reader.log", dest, *size);
+ */ return MIME_READ_OK; - } - + } return MIME_READ_ERROR; } @@ -654,7 +662,7 @@ static void _mime_received_bytes(void *data, const unsigned char* bytes, int size) { int i=0; - char *id; + char *id, *type; mime_callback_data_t *cbdata = (mime_callback_data_t*)data; if (!cbdata ) { @@ -720,13 +728,18 @@ void _mime_received_bytes(void *data, const unsigned char* bytes, int size) cbdata->current_part->header = _mime_process_header(cbdata->header); hpairnode_dump_deep(cbdata->current_part->header); /* set id */ - id = hpairnode_get(cbdata->current_part->header, HEADER_CONTENT_ID); - if (id != NULL) - { - strcpy(cbdata->current_part->id, id); - if (!strcmp(id, cbdata->root_id)) - cbdata->message->root_part = cbdata->current_part; - } + id = hpairnode_get(cbdata->current_part->header, HEADER_CONTENT_ID);
+ if (id != NULL)
+ {
+ strcpy(cbdata->current_part->id, id);
+ if (!strcmp(id, cbdata->root_id))
+ cbdata->message->root_part = cbdata->current_part;
+ }
+ type = hpairnode_get(cbdata->current_part->header, HEADER_CONTENT_TYPE);
+ if (type != NULL)
+ {
+ strcpy(cbdata->current_part->content_type, type);
+ }
i++; break; } @@ -854,7 +867,7 @@ mime_message_parse_from_file(FILE *in, const char* root_id, -hstatus_t mime_get_attachments(content_type_t *ctype, http_input_stream_t *in, attachments_t **dest) +herror_t mime_get_attachments(content_type_t *ctype, http_input_stream_t *in, attachments_t **dest) { /* MIME variables */ attachments_t *mimeMessage; @@ -864,7 +877,8 @@ hstatus_t mime_get_attachments(content_type_t *ctype, http_input_stream_t *in, a /* Check for MIME message */ if (!(ctype && !strcmp(ctype->type, "multipart/related"))) - return MIME_ERROR_NOT_MIME_MESSAGE; + return herror_new("mime_get_attachments", MIME_ERROR_NOT_MIME_MESSAGE,
+ "Not a MIME message '%s'", ctype->type); boundary = hpairnode_get(ctype->params, "boundary"); root_id = hpairnode_get(ctype->params, "start"); @@ -872,14 +886,16 @@ hstatus_t mime_get_attachments(content_type_t *ctype, http_input_stream_t *in, a { /* TODO (#1#): Handle Error in http form */ log_error1("'boundary' not set for multipart/related"); - return MIME_ERROR_NO_BOUNDARY_PARAM; + return herror_new("mime_get_attachments", MIME_ERROR_NO_BOUNDARY_PARAM,
+ "'boundary' not set for multipart/related"); } if (root_id == NULL) { /* TODO (#1#): Handle Error in http form */ log_error1("'start' not set for multipart/related"); - return MIME_ERROR_NO_START_PARAM; + return herror_new("mime_get_attachments", MIME_ERROR_NO_START_PARAM,
+ "'start' not set for multipart/related");
} /* TODO (#1#): Set this not to working directory @@ -890,14 +906,16 @@ hstatus_t mime_get_attachments(content_type_t *ctype, http_input_stream_t *in, a { /* TODO (#1#): Handle Error in http form */ log_error1("MIME Parse Error"); - return MIME_ERROR_PARSE_ERROR; + return herror_new("mime_get_attachments", MIME_ERROR_PARSE_ERROR,
+ "MIME Parse Error"); } /* Find root */ if (!mimeMessage->root_part) { attachments_free(mimeMessage); - return MIME_ERROR_NO_ROOT_PART; + return herror_new("mime_get_attachments", MIME_ERROR_NO_ROOT_PART,
+ "No root part found!"); } /* delete root_part from list */ diff --git a/nanohttp/nanohttp-mime.h b/nanohttp/nanohttp-mime.h index f78efa1..b51e44d 100755 --- a/nanohttp/nanohttp-mime.h +++ b/nanohttp/nanohttp-mime.h @@ -3,7 +3,7 @@ * | \/ | | | | \/ | | _/ * |_''_| |_| |_''_| |_'/ PARSER * -* $Id: nanohttp-mime.h,v 1.2 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-mime.h,v 1.3 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -40,7 +40,7 @@ -hstatus_t mime_get_attachments(content_type_t *ctype, +herror_t mime_get_attachments(content_type_t *ctype, http_input_stream_t *in, attachments_t **dest); diff --git a/nanohttp/nanohttp-request.c b/nanohttp/nanohttp-request.c index 0eb5a22..7661592 100755 --- a/nanohttp/nanohttp-request.c +++ b/nanohttp/nanohttp-request.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-request.c,v 1.2 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-request.c,v 1.3 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -217,10 +217,11 @@ hrequest_free(hrequest_t * req) } -hrequest_t * -hrequest_new_from_socket(hsocket_t sock) +herror_t +hrequest_new_from_socket(hsocket_t sock, hrequest_t **out) { - int i=0, status; + int i=0, readed;
+ herror_t status; hrequest_t *req; char buffer[MAX_HEADER_SIZE+1]; attachments_t *mimeMessage; @@ -228,11 +229,11 @@ hrequest_new_from_socket(hsocket_t sock) /* Read header */ while (i<MAX_HEADER_SIZE) { - status = hsocket_read(sock, &(buffer[i]), 1, 1); - if (status == -1) + status = hsocket_read(sock, &(buffer[i]), 1, 1, &readed); + if (status != H_OK) { log_error1("Socket read error"); - return NULL; + return status; } buffer[i+1] = '\0'; /* for strmp */ @@ -248,11 +249,7 @@ hrequest_new_from_socket(hsocket_t sock) /* Create response */ req = _hrequest_parse_header(buffer); - if (req == NULL) - { - log_error1("Header parse error"); - return NULL; - } + /* Create input stream */ req->in = http_input_stream_new(sock, req->header); @@ -266,7 +263,7 @@ hrequest_new_from_socket(hsocket_t sock) { /* TODO (#1#): Handle error */ hrequest_free(req); - return NULL; + return status; } else { @@ -275,8 +272,8 @@ hrequest_new_from_socket(hsocket_t sock) } } - - return req; + *out = req; + return H_OK; } diff --git a/nanohttp/nanohttp-request.h b/nanohttp/nanohttp-request.h index c445596..8989247 100755 --- a/nanohttp/nanohttp-request.h +++ b/nanohttp/nanohttp-request.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-request.h,v 1.1 2004/10/15 13:30:42 snowdrop Exp $ + * $Id: nanohttp-request.h,v 1.2 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -45,7 +45,7 @@ typedef struct hrequest char root_part_id[150]; }hrequest_t; -hrequest_t *hrequest_new_from_socket(hsocket_t sock); +herror_t hrequest_new_from_socket(hsocket_t sock, hrequest_t **out); void hrequest_free(hrequest_t *req); #endif diff --git a/nanohttp/nanohttp-response.c b/nanohttp/nanohttp-response.c index 8f55856..f2e13c5 100755 --- a/nanohttp/nanohttp-response.c +++ b/nanohttp/nanohttp-response.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-response.c,v 1.1 2004/10/15 13:30:42 snowdrop Exp $ +* $Id: nanohttp-response.c,v 1.2 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -121,10 +121,12 @@ _hresponse_parse_header(const char *buffer) return res; } -hresponse_t * -hresponse_new_from_socket(hsocket_t sock) +
+herror_t +hresponse_new_from_socket(hsocket_t sock, hresponse_t **out) { - int i=0, status; + int i=0, readed;
+ herror_t status; hresponse_t *res; attachments_t *mimeMessage; char buffer[MAX_HEADER_SIZE+1]; @@ -133,11 +135,11 @@ read_header: /* for errorcode: 100 (continue) */ /* Read header */ while (i<MAX_HEADER_SIZE) { - status = hsocket_read(sock, &(buffer[i]), 1, 1); - if (status == -1) + status = hsocket_read(sock, &(buffer[i]), 1, 1, &readed); + if (status != H_OK) { log_error1("Socket read error"); - return NULL; + return status; } buffer[i+1] = '\0'; /* for strmp */ @@ -156,7 +158,8 @@ read_header: /* for errorcode: 100 (continue) */ if (res == NULL) { log_error1("Header parse error"); - return NULL; + return herror_new("hresponse_new_from_socket",
+ GENERAL_HEADER_PARSE_ERROR, "Can not parse response header"); } /* Chec for Errorcode: 100 (continue) */ @@ -179,7 +182,7 @@ read_header: /* for errorcode: 100 (continue) */ { /* TODO (#1#): Handle error */ hresponse_free(res); - return NULL; + return status; } else { @@ -187,8 +190,8 @@ read_header: /* for errorcode: 100 (continue) */ res->in = http_input_stream_new_from_file(mimeMessage->root_part->filename); } } - - return res; + *out = res; + return H_OK; } diff --git a/nanohttp/nanohttp-response.h b/nanohttp/nanohttp-response.h index 7607eb7..63dd463 100755 --- a/nanohttp/nanohttp-response.h +++ b/nanohttp/nanohttp-response.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-response.h,v 1.1 2004/10/15 13:30:42 snowdrop Exp $ + * $Id: nanohttp-response.h,v 1.2 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -43,7 +43,7 @@ typedef struct hresponse char root_part_id[150]; }hresponse_t; -hresponse_t *hresponse_new_from_socket(hsocket_t sock); +herror_t hresponse_new_from_socket(hsocket_t sock, hresponse_t **out); void hresponse_free(hresponse_t *res); diff --git a/nanohttp/nanohttp-server.c b/nanohttp/nanohttp-server.c index 2ee15c7..71dbba7 100644 --- a/nanohttp/nanohttp-server.c +++ b/nanohttp/nanohttp-server.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-server.c,v 1.27 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-server.c,v 1.28 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -94,12 +94,13 @@ static void WSAReaper(void *x); * NOTE: This will be called from soap_server_init_args() * ----------------------------------------------------- */ -int +herror_t httpd_init (int argc, char *argv[]) { - int i, status; + int i;
+ herror_t status; status = hsocket_module_init (); - if (status != 0) + if (status != H_OK) return status; /* write argument information */ @@ -136,12 +137,15 @@ httpd_init (int argc, char *argv[]) memset ((char *) &_httpd_connection[i], 0, sizeof (_httpd_connection[i])); } -#ifdef WIN32 +#ifdef WIN32
+ /* if (_beginthread (WSAReaper, 0, NULL) == -1) { log_error1 ("Winsock reaper thread failed to start"); - return (-1); - } + return herror_new("httpd_init", THREAD_BEGIN_ERROR,
+ "_beginthread() failed while starting WSAReaper"); + }
+ */ #endif /* create socket */ @@ -244,7 +248,7 @@ httpd_response_set_content_type (httpd_conn_t * res, const char *content_type) * FUNCTION: httpd_response_send_header * ----------------------------------------------------- */ -int +herror_t httpd_send_header (httpd_conn_t * res, int code, const char *text) { struct tm stm; @@ -252,7 +256,7 @@ httpd_send_header (httpd_conn_t * res, int code, const char *text) char buffer[255]; char header[1024]; hpair_t *cur; - int status; + herror_t status; /* set status code */ sprintf (header, "HTTP/1.1 %d %s\r\n", code, text); @@ -291,9 +295,11 @@ httpd_send_header (httpd_conn_t * res, int code, const char *text) /* send header */ status = hsocket_nsend (res->sock, header, strlen (header)); + if (status != H_OK)
+ return status;
res->out = http_output_stream_new (res->sock, res->header); - return status; + return H_OK; } @@ -381,10 +387,12 @@ httpd_session_main (void *data) hrequest_t *req = NULL; /* only for test */ httpd_conn_t *rconn; hservice_t *service = NULL; - long content_length = 0; + long content_length = 0;
+ herror_t status; header[0] = '\0'; - len = 0; + len = 0;
+ log_verbose1 ("starting httpd_session_main()"); @@ -393,10 +401,11 @@ httpd_session_main (void *data) /* req = hrequest_new_from_buffer (header);*/ rconn = httpd_new(conn->sock); - req = hrequest_new_from_socket (conn->sock); - if (req == NULL) + status = hrequest_new_from_socket (conn->sock, &req); + if (status != H_OK) { - httpd_send_internal_error (rconn, "Request parse error!"); + httpd_send_internal_error (rconn, herror_message(status)/*"Request parse error!"*/);
+ herror_release(status); } else { @@ -597,10 +606,10 @@ _httpd_start_thread (conndata_t * conn) * ----------------------------------------------------- */ -int +herror_t httpd_run () { - int err; + herror_t err; conndata_t *conn; fd_set fds; struct timeval timeout; @@ -614,7 +623,7 @@ httpd_run () err = hsocket_listen (_httpd_socket); if (err != H_OK) { - log_error2 ("httpd_run(): '%d'", err); + log_error2 ("httpd_run(): '%d'", herror_message(err)); return err; } log_verbose2 ("listening to port '%d'", _httpd_port); @@ -626,7 +635,7 @@ httpd_run () err = hsocket_block (_httpd_socket, 0); if (err != H_OK) { - log_error2 ("httpd_run(): '%d'", err); + log_error2 ("httpd_run(): '%s'", herror_message(err)); return err; } @@ -673,8 +682,8 @@ httpd_run () err = hsocket_accept (_httpd_socket, &(conn->sock)); if (err != H_OK) { - log_error2 ("Can not accept socket: %d", err); - return -1; /* this is hard core! */ + log_error2 ("Can not accept socket: %s", herror_message(err)); + return err; /* this is hard core! */ } /* Now start a thread */ @@ -805,7 +814,7 @@ _httpd_mime_get_boundary (httpd_conn_t * conn, char *dest) Begin MIME multipart/related POST Returns: H_OK or error flag */ -int +herror_t httpd_mime_send_header (httpd_conn_t * conn, const char *related_start, const char *related_start_info, @@ -861,12 +870,12 @@ httpd_mime_send_header (httpd_conn_t * conn, Send boundary and part header and continue with next part */ -int +herror_t httpd_mime_next (httpd_conn_t * conn, const char *content_id, const char *content_type, const char *transfer_encoding) { - int status; + herror_t status; char buffer[512]; char boundary[75]; @@ -901,19 +910,20 @@ httpd_mime_next (httpd_conn_t * conn, Send boundary and part header and continue with next part */ -int +herror_t httpd_mime_send_file (httpd_conn_t * conn, const char *content_id, const char *content_type, const char *transfer_encoding, const char *filename) { - int status; + herror_t status; FILE *fd = fopen (filename, "rb"); byte_t buffer[MAX_FILE_BUFFER_SIZE]; size_t size; if (fd == NULL) - return FILE_ERROR_OPEN; + return herror_new("httpd_mime_send_file", FILE_ERROR_OPEN,
+ "Can not open file '%d'", filename); status = httpd_mime_next (conn, content_id, content_type, transfer_encoding); @@ -929,7 +939,8 @@ httpd_mime_send_file (httpd_conn_t * conn, if (size == -1) { fclose (fd); - return FILE_ERROR_READ; + return herror_new("httpd_mime_send_file", FILE_ERROR_READ,
+ "Can not read from file '%d'", filename);
} status = http_output_stream_write (conn->out, buffer, size); @@ -947,10 +958,10 @@ httpd_mime_send_file (httpd_conn_t * conn, Finish MIME request Returns: H_OK or error flag */ -int +herror_t httpd_mime_end (httpd_conn_t * conn) { - int status; + herror_t status; char buffer[512]; char boundary[75]; diff --git a/nanohttp/nanohttp-server.h b/nanohttp/nanohttp-server.h index 513f4de..e344eba 100644 --- a/nanohttp/nanohttp-server.h +++ b/nanohttp/nanohttp-server.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-server.h,v 1.7 2004/10/20 14:17:41 snowdrop Exp $ + * $Id: nanohttp-server.h,v 1.8 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -62,22 +62,23 @@ typedef struct tag_hservice /* Begin httpd_* function set */ -int httpd_init(int argc, char *argv[]); +herror_t httpd_init(int argc, char *argv[]); int httpd_register(const char* ctx, httpd_service service); -int httpd_run(); +herror_t httpd_run(); void httpd_destroy(); hservice_t *httpd_services(); -int httpd_send_header(httpd_conn_t *res, +herror_t httpd_send_header(httpd_conn_t *res, int code, const char* text); int httpd_set_header(httpd_conn_t *conn, const char *key, const char* value); void httpd_set_headers(httpd_conn_t *conn, hpair_t *header); - +
+/* unsigned char *httpd_get_postdata(httpd_conn_t *conn, hrequest_t *req, long *received, long max); - +*/ /* -------------------------------------------------------------- MIME RELATED FUNCTIONS ---------------------------------------------------------------*/ @@ -89,7 +90,7 @@ unsigned char *httpd_get_postdata(httpd_conn_t *conn, Begin MIME multipart/related POST Returns: HSOCKET_OK or error flag */ -int httpd_mime_send_header(httpd_conn_t *conn, +herror_t httpd_mime_send_header(httpd_conn_t *conn, const char* related_start, const char* related_start_info, const char* related_type, int code, const char* text); @@ -98,7 +99,7 @@ int httpd_mime_send_header(httpd_conn_t *conn, Send boundary and part header and continue with next part */ -int httpd_mime_next(httpd_conn_t *conn, +herror_t httpd_mime_next(httpd_conn_t *conn, const char* content_id, const char* content_type, const char* transfer_encoding); @@ -107,7 +108,7 @@ int httpd_mime_next(httpd_conn_t *conn, Send boundary and part header and continue with next part */ -int httpd_mime_send_file(httpd_conn_t *conn, +herror_t httpd_mime_send_file(httpd_conn_t *conn, const char* content_id, const char* content_type, const char* transfer_encoding, @@ -117,7 +118,7 @@ int httpd_mime_send_file(httpd_conn_t *conn, Finish MIME request Returns: HSOCKET_OK or error flag */ -int httpd_mime_end(httpd_conn_t *conn); +herror_t httpd_mime_end(httpd_conn_t *conn); #endif diff --git a/nanohttp/nanohttp-socket.c b/nanohttp/nanohttp-socket.c index d7a1d59..7b378fc 100644 --- a/nanohttp/nanohttp-socket.c +++ b/nanohttp/nanohttp-socket.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-socket.c,v 1.29 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-socket.c,v 1.30 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -73,7 +73,7 @@ FUNCTION: hsocket_module_init NOTE: This will be called from httpd_init() for server and from httpc_init() for client ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_module_init () { #ifdef WIN32 @@ -103,7 +103,7 @@ hsocket_module_destroy () /*-------------------------------------------------- FUNCTION: hsocket_init ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_init (hsocket_t * sock) { /* just set the descriptor to -1 */ @@ -123,7 +123,7 @@ hsocket_free (hsocket_t sock) /*-------------------------------------------------- FUNCTION: hsocket_open ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_open (hsocket_t * dsock, const char *hostname, int port) { hsocket_t sock; @@ -133,12 +133,12 @@ hsocket_open (hsocket_t * dsock, const char *hostname, int port) sock = socket (AF_INET, SOCK_STREAM, 0); if (sock <= 0) - return HSOCKET_ERROR_CREATE; + return herror_new("hsocket_open", HSOCKET_ERROR_CREATE, strerror (errno));
/* Get host data */ host = gethostbyname (hostname); if (host == NULL) - return HSOCKET_ERROR_GET_HOSTNAME; + return herror_new("hsocket_open", HSOCKET_ERROR_GET_HOSTNAME, strerror (errno));
ip = inet_ntoa (*(struct in_addr *) *host->h_addr_list); address.sin_addr.s_addr = inet_addr (ip); @@ -149,7 +149,7 @@ hsocket_open (hsocket_t * dsock, const char *hostname, int port) /* connect to the server */ if (connect (sock, (struct sockaddr *) &address, sizeof (address)) != 0) - return HSOCKET_ERROR_CONNECT; + return herror_new("hsocket_open", HSOCKET_ERROR_CONNECT, "Connect to '%s:%d' failed", hostname, port);
*dsock = sock; return H_OK; @@ -158,7 +158,7 @@ hsocket_open (hsocket_t * dsock, const char *hostname, int port) /*-------------------------------------------------- FUNCTION: hsocket_bind ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_bind (hsocket_t * dsock, int port) { hsocket_t sock; @@ -169,7 +169,7 @@ hsocket_bind (hsocket_t * dsock, int port) if (sock == -1) { log_error2 ("Can not create socket: '%s'", strerror (errno)); - return HSOCKET_ERROR_CREATE; + return herror_new("hsocket_bind", HSOCKET_ERROR_CREATE, strerror (errno));
} /* bind socket */ addr.sin_family = AF_INET; @@ -181,7 +181,7 @@ hsocket_bind (hsocket_t * dsock, int port) if (bind (sock, (struct sockaddr *) &addr, sizeof (struct sockaddr)) == -1) { log_error2 ("Can not bind: '%s'", strerror (errno)); - return HSOCKET_ERROR_BIND; + return herror_new("hsocket_bind", HSOCKET_ERROR_BIND, strerror (errno));
} *dsock = sock; return H_OK; @@ -190,7 +190,7 @@ hsocket_bind (hsocket_t * dsock, int port) /*---------------------------------------------------------- FUNCTION: hsocket_accept ----------------------------------------------------------*/ -hstatus_t +herror_t hsocket_accept (hsocket_t sock, hsocket_t * dest) { socklen_t asize; @@ -198,7 +198,8 @@ hsocket_accept (hsocket_t sock, hsocket_t * dest) struct sockaddr_in addr; if (sock <= 0) - return HSOCKET_ERROR_NOT_INITIALIZED; + return herror_new("hsocket_accept", HSOCKET_ERROR_NOT_INITIALIZED,
+ "Called hsocket_listen() before initializing!");
asize = sizeof (struct sockaddr_in); #ifdef WIN32 @@ -207,7 +208,7 @@ hsocket_accept (hsocket_t sock, hsocket_t * dest) sockfd = accept (sock, (struct sockaddr *) &addr, &asize); if (sockfd == INVALID_SOCKET) { if (WSAGetLastError () != WSAEWOULDBLOCK) - return HSOCKET_ERROR_ACCEPT; + return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT, strerror (errno));
} else { break; } @@ -216,7 +217,7 @@ hsocket_accept (hsocket_t sock, hsocket_t * dest) /* TODO (#1#): why not a loop like in win32? */ sockfd = accept (sock, (struct sockaddr *) &addr, &asize); if (sockfd == -1) { - return HSOCKET_ERROR_ACCEPT; + return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT, strerror (errno));
} #endif /* TODO (#1#): Write to access.log file */ @@ -231,16 +232,17 @@ hsocket_accept (hsocket_t sock, hsocket_t * dest) /*-------------------------------------------------- FUNCTION: hsocket_listen ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_listen (hsocket_t sock) { if (sock <= 0) - return HSOCKET_ERROR_NOT_INITIALIZED; + return herror_new("hsocket_listen", HSOCKET_ERROR_NOT_INITIALIZED,
+ "Called hsocket_listen() before initializing!");
if (listen (sock, 15) == -1) { log_error2 ("Can not listen: '%s'", strerror (errno)); - return HSOCKET_ERROR_LISTEN; + return herror_new("hsocket_listen", HSOCKET_ERROR_LISTEN, strerror (errno));
} return H_OK; @@ -315,34 +317,49 @@ hsocket_close (hsocket_t sock) #endif log_verbose1 ("closed"); } +
+
+static int _test_send_to_file(const char* filename, const byte_t* bytes,int n)
+{
+ int size;
+ FILE *f = fopen(filename, "ab");
+ if (!f) f = fopen(filename, "wb");
+ size= fwrite(bytes, 1, n, f);
+ fclose(f);
+ return size;
+}
/*-------------------------------------------------- FUNCTION: hsocket_send ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_nsend (hsocket_t sock, const byte_t *bytes, int n) { int size; - + int total=0; if (sock <= 0) - return HSOCKET_ERROR_NOT_INITIALIZED; + return herror_new("hsocket_nsend", HSOCKET_ERROR_NOT_INITIALIZED,
+ "Called hsocket_listen() before initializing!");
+ /* TODO (#1#): check return value and send again until n bytes sent */ while (1) { - size = send((int) sock, bytes, n, 0); + size = send((int) sock, bytes + total, n, 0);
+ /* size = _test_send_to_file(filename, bytes, n);*/ #ifdef WIN32 if (size == INVALID_SOCKET) if (WSAGetLastError () == WSAEWOULDBLOCK) continue; else - return HSOCKET_ERROR_SEND; + return herror_new("hsocket_nsend", HSOCKET_ERROR_SEND, strerror (errno));
#else if (size == -1) - return HSOCKET_ERROR_SEND; + return herror_new("hsocket_nsend", HSOCKET_ERROR_SEND, strerror (errno));
#endif - n -= size; + n -= size;
+ total += size; if (n<=0) break; } return H_OK; @@ -351,7 +368,7 @@ hsocket_nsend (hsocket_t sock, const byte_t *bytes, int n) /*-------------------------------------------------- FUNCTION: hsocket_send ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_send (hsocket_t sock, const char *str) { return hsocket_nsend (sock, str, strlen (str)); @@ -360,14 +377,16 @@ hsocket_send (hsocket_t sock, const char *str) /* return: -1 is error. read bytes otherwise */ -int -hsocket_read (hsocket_t sock, byte_t *buffer, int total, int force) +herror_t +hsocket_read (hsocket_t sock, byte_t *buffer, int total, int force, int *received) { int status; int totalRead; int wsa_error; totalRead = 0; - +/*
+ log_verbose3("Entering hsocket_read(total=%d,force=%d)", total, force);
+*/ do { status = recv(sock, &buffer[totalRead], total - totalRead, 0); @@ -379,12 +398,12 @@ hsocket_read (hsocket_t sock, byte_t *buffer, int total, int force) switch (wsa_error) { case WSAEWOULDBLOCK: - case WSAEALREADY: - case WSAEINPROGRESS: + /* case WSAEALREADY: + case WSAEINPROGRESS: */ continue; default: log_error2("WSAGetLastError()=%d", wsa_error); - return -1; + return herror_new("hsocket_read", HSOCKET_ERROR_RECEIVE, strerror (errno));
} } @@ -398,24 +417,32 @@ hsocket_read (hsocket_t sock, byte_t *buffer, int total, int force) } */ if (status == -1) - return -1; + return herror_new("hsocket_read", HSOCKET_ERROR_RECEIVE, strerror (errno));
#endif - if (!force) { - return status; + if (!force) {
+ *received = status;
+ /*
+ log_verbose3("Leaving !force (received=%d)(status=%d)", *received, status);
+ */ + return H_OK; } totalRead += status; if (totalRead == total) { - return totalRead; + *received = totalRead;
+ /*
+ log_verbose4("Leaving totalRead == total (received=%d)(status=%d)(totalRead=%d)", *received, status, totalRead);
+ */
+ return H_OK;
} } while (1); } -hstatus_t +herror_t hsocket_block(hsocket_t sock, int block) { #ifdef WIN32 @@ -423,7 +450,8 @@ hsocket_block(hsocket_t sock, int block) #endif if (sock <= 0) - return HSOCKET_ERROR_NOT_INITIALIZED; + return herror_new("hsocket_block", HSOCKET_ERROR_NOT_INITIALIZED,
+ "Called hsocket_listen() before initializing!");
#ifdef WIN32 /*#define HSOCKET_BLOCKMODE 0 @@ -434,7 +462,7 @@ hsocket_block(hsocket_t sock, int block) if (ioctlsocket (sock, FIONBIO, (u_long FAR *) & iMode) == INVALID_SOCKET) { log_error1 ("ioctlsocket error"); - return HSOCKET_ERROR_IOCTL; + return herror_new("hsocket_block", HSOCKET_ERROR_IOCTL, strerror (errno));
} #else /* fcntl(sock, F_SETFL, O_NONBLOCK); */ /* TODO (#1#): check for *nix the non blocking sockets */ diff --git a/nanohttp/nanohttp-socket.h b/nanohttp/nanohttp-socket.h index 8deb273..83d8f72 100644 --- a/nanohttp/nanohttp-socket.h +++ b/nanohttp/nanohttp-socket.h @@ -1,5 +1,5 @@ /******************************************************************
- * $Id: nanohttp-socket.h,v 1.13 2004/10/15 13:29:37 snowdrop Exp $
+ * $Id: nanohttp-socket.h,v 1.14 2004/10/28 10:30:47 snowdrop Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -48,7 +48,7 @@ @returns This function should always return H_OK.
*/
-hstatus_t hsocket_module_init();
+herror_t hsocket_module_init();
/**
@@ -66,7 +66,7 @@ void hsocket_module_destroy(); @returns This function should always return H_OK.
*/
-hstatus_t hsocket_init(hsocket_t *sock);
+herror_t hsocket_init(hsocket_t *sock);
/**
@@ -90,7 +90,7 @@ void hsocket_free(hsocket_t sock); <BR>HSOCKET_ERROR_GET_HOSTNAME
<BR>HSOCKET_ERROR_CONNECT
*/
-hstatus_t hsocket_open(hsocket_t *sock, const char* host, int port);
+herror_t hsocket_open(hsocket_t *sock, const char* host, int port);
/**
@@ -114,7 +114,7 @@ void hsocket_close(hsocket_t sock); @see hsocket_listen
*/
-hstatus_t hsocket_bind(hsocket_t *sock, int port);
+herror_t hsocket_bind(hsocket_t *sock, int port);
/**
@@ -128,7 +128,7 @@ hstatus_t hsocket_bind(hsocket_t *sock, int port); <BR>HSOCKET_ERROR_NOT_INITIALIZED
<BR>HSOCKET_ERROR_LISTEN
*/
-hstatus_t hsocket_listen(hsocket_t sock);
+herror_t hsocket_listen(hsocket_t sock);
/**
@@ -142,7 +142,7 @@ hstatus_t hsocket_listen(hsocket_t sock); <BR>HSOCKET_ERROR_NOT_INITIALIZED
<BR>HSOCKET_ERROR_ACCEPT
*/
-hstatus_t hsocket_accept(hsocket_t sock, hsocket_t *dest);
+herror_t hsocket_accept(hsocket_t sock, hsocket_t *dest);
/**
@@ -156,7 +156,7 @@ hstatus_t hsocket_accept(hsocket_t sock, hsocket_t *dest); <BR>HSOCKET_ERROR_NOT_INITIALIZED
<BR>HSOCKET_ERROR_SEND
*/
-hstatus_t hsocket_nsend(hsocket_t sock, const byte_t* bytes, int size);
+herror_t hsocket_nsend(hsocket_t sock, const byte_t* bytes, int size);
/**
@@ -169,7 +169,7 @@ hstatus_t hsocket_nsend(hsocket_t sock, const byte_t* bytes, int size); <BR>HSOCKET_ERROR_NOT_INITIALIZED
<BR>HSOCKET_ERROR_SEND
*/
-hstatus_t hsocket_send(hsocket_t sock, const char* str);
+herror_t hsocket_send(hsocket_t sock, const char* str);
/**
@@ -188,7 +188,7 @@ hstatus_t hsocket_send(hsocket_t sock, const char* str); the socket.
*/
-int hsocket_read(hsocket_t sock, byte_t* buffer, int size, int force);
+herror_t hsocket_read(hsocket_t sock, byte_t* buffer, int size, int force, int *readed);
/**
Sets the goven socket to non-blocking socket mode.
@@ -199,7 +199,7 @@ int hsocket_read(hsocket_t sock, byte_t* buffer, int size, int force); <BR>HSOCKET_ERROR_NOT_INITIALIZED
<BR>HSOCKET_ERROR_IOCTL
*/
-hstatus_t hsocket_block(hsocket_t sock, int block);
+herror_t hsocket_block(hsocket_t sock, int block);
#endif
diff --git a/nanohttp/nanohttp-stream.c b/nanohttp/nanohttp-stream.c index 039eadc..991d2ab 100755 --- a/nanohttp/nanohttp-stream.c +++ b/nanohttp/nanohttp-stream.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-stream.c,v 1.3 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-stream.c,v 1.4 2004/10/28 10:30:47 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -180,21 +180,22 @@ static int _http_input_stream_content_length_read( http_input_stream_t *stream, byte_t *dest, int size) { - int status; + herror_t status;
+ int read; /* check limit */ if (stream->content_length - stream->received < size) size = stream->content_length - stream->received; /* read from socket */ - status = hsocket_read(stream->sock, dest, size, 1); - if (status == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; + status = hsocket_read(stream->sock, dest, size, 1, &read); + if (status != H_OK) { + stream->err = status; return -1; } - stream->received += status; - return status; + stream->received += read; + return read; } static @@ -203,14 +204,22 @@ int _http_input_stream_chunked_read_chunk_size( { char chunk[25]; int status, i = 0; - int chunk_size; + int chunk_size;
+ herror_t err; while (1) { - status = hsocket_read(stream->sock, &(chunk[i]), 1, 1); - - if (status == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; + err = hsocket_read(stream->sock, &(chunk[i]), 1, 1, &status);
+ if (status != 1) {
+ stream->err = herror_new("_http_input_stream_chunked_read_chunk_size",
+ GENERAL_INVALID_PARAM, "This should never happens!");
+ return -1;
+ } + + if (err != H_OK) {
+ log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), herror_message(err));
+ + stream->err = err; return -1; } @@ -221,13 +230,17 @@ int _http_input_stream_chunked_read_chunk_size( else if (chunk[i] == '\n') { chunk[i] = '\0'; /* double check*/ - chunk_size = strtol(chunk, (char **) NULL, 16); /* hex to dec */ -/* log_verbose3("chunk_size: '%s' as dec: '%d'", chunk, chunk_size);*/ + chunk_size = strtol(chunk, (char **) NULL, 16); /* hex to dec */
+ /* + log_verbose3("chunk_size: '%s' as dec: '%d'", chunk, chunk_size);
+ */ return chunk_size; } if (i == 24) { - stream->err = STREAM_ERROR_NO_CHUNK_SIZE; + stream->err = herror_new(
+ "_http_input_stream_chunked_read_chunk_size", STREAM_ERROR_NO_CHUNK_SIZE,
+ "reached max line == %d", i); return -1; } else @@ -235,8 +248,10 @@ int _http_input_stream_chunked_read_chunk_size( } /* this should never happens */ - stream->err = STREAM_ERROR_NO_CHUNK_SIZE; - return -1; + stream->err = herror_new(
+ "_http_input_stream_chunked_read_chunk_size", STREAM_ERROR_NO_CHUNK_SIZE,
+ "reached max line == %d", i);
+ return -1;
} static @@ -245,7 +260,8 @@ int _http_input_stream_chunked_read( { int status, counter; int remain, read=0; - char ch; + char ch;
+ herror_t err; while (size > 0) { @@ -258,10 +274,10 @@ int _http_input_stream_chunked_read( counter = 100; /* maximum for stop infinity */ while (1) { - status = hsocket_read(stream->sock, &ch, 1, 1); + err = hsocket_read(stream->sock, &ch, 1, 1, &status ); - if (status == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; + if (err != H_OK) { + stream->err = err; return -1; } @@ -270,7 +286,8 @@ int _http_input_stream_chunked_read( break; } if (counter-- == 0) { - stream->err = STREAM_ERROR_WRONG_CHUNK_SIZE; + stream->err = herror_new("_http_input_stream_chunked_read",
+ STREAM_ERROR_WRONG_CHUNK_SIZE,"Wrong chunk-size"); return -1; } } @@ -298,20 +315,29 @@ int _http_input_stream_chunked_read( /* show remaining chunk size in socket */ if (remain < size) { - /* read from socket */ - status = hsocket_read(stream->sock, &(dest[read]), remain, 1); - if (status == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; - return -1; - } - + /* read from socket */
+ err = hsocket_read(stream->sock, &(dest[read]), remain, 1, &status); + if (err != H_OK) {
+ stream->err = err;
+ return -1;
+ }
+ if (status != remain) {
+ stream->err = herror_new("_http_input_stream_chunked_read",
+ GENERAL_INVALID_PARAM, "This should never happens(remain=%d)(status=%d)!", remain, status);
+ return -1;
+ }
} else { /* read from socket */ - status = hsocket_read(stream->sock, &(dest[read]), size, 1); - if (status == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; + err = hsocket_read(stream->sock, &(dest[read]), size, 1, &status); + if (status != size) {
+ stream->err = herror_new("_http_input_stream_chunked_read",
+ GENERAL_INVALID_PARAM, "This should never happens(size=%d)(status=%d)!", size, status);
+ return -1;
+ }
+ if (err != H_OK) { + stream->err = err; return -1; } } @@ -330,11 +356,12 @@ int _http_input_stream_connection_closed_read( http_input_stream_t *stream, byte_t *dest, int size) { int status; + herror_t err;
/* read from socket */ - status = hsocket_read(stream->sock, dest, size, 0); - if (status == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; + err = hsocket_read(stream->sock, dest, size, 0, &status ); + if (err != H_OK) { + stream->err = err; return -1; } @@ -354,7 +381,8 @@ int _http_input_stream_file_read( readed = fread(dest, 1, size, stream->fd); if (readed == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; + stream->err = herror_new("_http_input_stream_file_read",
+ HSOCKET_ERROR_RECEIVE, "fread() returned -1"); return -1; } @@ -420,7 +448,8 @@ int http_input_stream_read(http_input_stream_t *stream, readed=_http_input_stream_file_read(stream, dest, size); break; default: - stream->err = STREAM_ERROR_INVALID_TYPE; + stream->err = herror_new("http_input_stream_read",
+ STREAM_ERROR_INVALID_TYPE, "%d is invalid stream type", stream->type); return -1; } @@ -493,10 +522,10 @@ void http_output_stream_free(http_output_stream_t *stream) Writes 'size' bytes of 'bytes' into stream. Returns socket error flags or H_OK. */ -hstatus_t http_output_stream_write(http_output_stream_t *stream, +herror_t http_output_stream_write(http_output_stream_t *stream, const byte_t *bytes, int size) { - int status; + herror_t status; char chunked[15]; if (stream->type == HTTP_TRANSFER_CHUNKED) @@ -529,16 +558,16 @@ hstatus_t http_output_stream_write(http_output_stream_t *stream, Writes 'strlen()' bytes of 'str' into stream. Returns socket error flags or H_OK. */ -int http_output_stream_write_string(http_output_stream_t *stream, +herror_t http_output_stream_write_string(http_output_stream_t *stream, const char *str) { return http_output_stream_write(stream, str, strlen(str)); } -int http_output_stream_flush(http_output_stream_t *stream) +herror_t http_output_stream_flush(http_output_stream_t *stream) { - int status; + herror_t status; if (stream->type == HTTP_TRANSFER_CHUNKED) { diff --git a/nanohttp/nanohttp-stream.h b/nanohttp/nanohttp-stream.h index fb44284..7b0f0f8 100755 --- a/nanohttp/nanohttp-stream.h +++ b/nanohttp/nanohttp-stream.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-stream.h,v 1.2 2004/10/15 14:26:15 snowdrop Exp $ + * $Id: nanohttp-stream.h,v 1.3 2004/10/28 10:30:47 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -88,7 +88,7 @@ typedef enum http_transfer_type typedef struct http_input_stream { hsocket_t sock; - hstatus_t err; + herror_t err; http_transfer_type_t type; int received; int content_length; @@ -236,7 +236,7 @@ void http_output_stream_free(http_output_stream_t *stream); <BR>HSOCKET_ERROR_NOT_INITIALIZED <BR>HSOCKET_ERROR_SEND */ -hstatus_t http_output_stream_write(http_output_stream_t *stream, +herror_t http_output_stream_write(http_output_stream_t *stream, const byte_t *bytes, int size); /** @@ -249,7 +249,7 @@ hstatus_t http_output_stream_write(http_output_stream_t *stream, <BR>HSOCKET_ERROR_NOT_INITIALIZED <BR>HSOCKET_ERROR_SEND */ -hstatus_t http_output_stream_write_string(http_output_stream_t *stream, +herror_t http_output_stream_write_string(http_output_stream_t *stream, const char *str); @@ -263,7 +263,7 @@ hstatus_t http_output_stream_write_string(http_output_stream_t *stream, <BR>HSOCKET_ERROR_NOT_INITIALIZED <BR>HSOCKET_ERROR_SEND */ -hstatus_t http_output_stream_flush(http_output_stream_t *stream); +herror_t http_output_stream_flush(http_output_stream_t *stream); #endif |