From f4c29af542bbded2fe36d9bdc80808c3a7f5d92b Mon Sep 17 00:00:00 2001 From: m0gg Date: Sun, 10 Dec 2006 19:21:05 +0000 Subject: nhttp client fix and documentation update --- nanohttp/nanohttp-admin.c | 87 ++++++++++++++++++++++------------ nanohttp/nanohttp-admin.h | 12 ++++- nanohttp/nanohttp-client.c | 20 +++++--- nanohttp/nanohttp-client.h | 36 +++++++------- nanohttp/nanohttp-common.c | 5 +- nanohttp/nanohttp-common.h | 3 +- nanohttp/nanohttp-error.h | 22 +++------ nanohttp/nanohttp-logging.h | 39 ++++++++++++++-- nanohttp/nanohttp-response.c | 34 +++++++------- nanohttp/nanohttp-server.c | 8 ++-- nanohttp/nanohttp-server.h | 27 +++++------ nanohttp/nanohttp-socket.h | 70 +++++++++++++++++---------- nanohttp/nanohttp-stream.h | 31 ++++++------ nanohttp/nanohttp-url.c | 109 +++++++++++++++++++++++++++---------------- nanohttp/nanohttp-url.h | 33 ++++++++----- 15 files changed, 330 insertions(+), 206 deletions(-) (limited to 'nanohttp') diff --git a/nanohttp/nanohttp-admin.c b/nanohttp/nanohttp-admin.c index 29462a0..2babec6 100644 --- a/nanohttp/nanohttp-admin.c +++ b/nanohttp/nanohttp-admin.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-admin.c,v 1.7 2006/12/02 21:50:47 m0gg Exp $ +* $Id: nanohttp-admin.c,v 1.8 2006/12/10 19:21:06 m0gg Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -66,6 +66,14 @@ _httpd_admin_send_title(httpd_conn_t *conn, const char *title) return; } +static void +_httpd_admin_send_footer(httpd_conn_t *conn) +{ + http_output_stream_write_string(conn->out, ""); + + return; +} + static void _httpd_admin_list_services(httpd_conn_t *conn) { @@ -91,7 +99,7 @@ _httpd_admin_list_services(httpd_conn_t *conn) } http_output_stream_write_string(conn->out, ""); - http_output_stream_write_string(conn->out, ""); + _httpd_admin_send_footer(conn); return; } @@ -108,8 +116,10 @@ _httpd_admin_list_statistics(httpd_conn_t *conn, const char *service_name) if (!(service = httpd_find_service(service_name))) { http_output_stream_write_string(conn->out, - "

Service not found!

" - ""); + "

" + "Service not found!" + "

"); + _httpd_admin_send_footer(conn); return; } @@ -129,7 +139,7 @@ _httpd_admin_list_statistics(httpd_conn_t *conn, const char *service_name) http_output_stream_write_string(conn->out, buffer); - http_output_stream_write_string(conn->out, ""); + _httpd_admin_send_footer(conn); return; } @@ -146,16 +156,22 @@ _httpd_admin_enable_service(httpd_conn_t *conn, const char *service_name) if (!(service = httpd_find_service(service_name))) { http_output_stream_write_string(conn->out, - "

Service not found!

" - ""); + "

" + "Service not found!" + "

"); + + _httpd_admin_send_footer(conn); return; } httpd_enable_service(service); http_output_stream_write_string(conn->out, - "

Service enabled

" - ""); + "

" + "Service enabled" + "

"); + + _httpd_admin_send_footer(conn); return; } @@ -172,16 +188,20 @@ _httpd_admin_disable_service(httpd_conn_t *conn, const char *service_name) if (!(service = httpd_find_service(service_name))) { http_output_stream_write_string(conn->out, - "

service not found!

" - ""); + "

" + "Service not found!" + "

"); + _httpd_admin_send_footer(conn); return; } httpd_disable_service(service); http_output_stream_write_string(conn->out, - "

Service disabled

" - ""); + "

" + "Service disabled" + "

"); + _httpd_admin_send_footer(conn); return; } @@ -211,12 +231,17 @@ _httpd_admin_handle_get(httpd_conn_t * conn, struct hrequest_t *req) { _httpd_admin_send_title(conn, "Welcome to the admin site"); - http_output_stream_write_string(conn->out, ""); - - http_output_stream_write_string(conn->out, ""); + ""); + + _httpd_admin_send_footer(conn); } return; @@ -233,15 +258,15 @@ _httpd_admin_entry(httpd_conn_t * conn, struct hrequest_t *req) { httpd_send_header(conn, 200, HTTP_STATUS_200_REASON_PHRASE); http_output_stream_write_string(conn->out, - "" - "" - "" - "" - "

Sorry!

" - "
" - "
POST Service is not implemented now. Use your browser.
" - "" - ""); + "" + "" + "" + "" + "

Sorry!

" + "
" + "
POST Service is not implemented now. Use your browser.
" + "" + ""); } return; } @@ -251,10 +276,10 @@ httpd_admin_init_args(int argc, char **argv) { int i; - for (i=0; isock)) != H_OK) { log_warn2("hsocket_init failed (%s)", herror_message(status)); + hurl_free(res->url); + free(res->sock); free(res); return NULL; } @@ -157,7 +159,8 @@ httpc_free(httpc_conn_t * conn) hsocket_free(conn->sock); hurl_free(conn->url); - free(conn->sock); + if (conn->sock) + free(conn->sock); free(conn); return; @@ -360,8 +363,7 @@ If success, this function will return 0. >0 otherwise. ----------------------------------------------------*/ static herror_t -_httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, - const char *urlstr) +_httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, const char *urlstr) { char buffer[4096]; herror_t status; @@ -369,15 +371,15 @@ _httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, if (conn == NULL) { - return herror_new("httpc_talk_to_server", - GENERAL_INVALID_PARAM, "httpc_conn_t param is NULL"); + return herror_new("httpc_talk_to_server", GENERAL_INVALID_PARAM, "httpc_conn_t param is NULL"); } + /* Build request header */ httpc_header_set_date(conn); if ((status = hurl_parse(conn->url, urlstr)) != H_OK) { - log_error2("Can not parse URL '%s'", SAVE_STR(urlstr)); + log_error2("Cannot parse URL \"%s\"", SAVE_STR(urlstr)); return status; } /* TODO (#1#): Check for HTTP protocol in URL */ @@ -386,10 +388,14 @@ _httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, httpc_set_header(conn, HEADER_HOST, conn->url->host); ssl = conn->url->protocol == PROTOCOL_HTTPS ? 1 : 0; + log_error4("ssl = %i (%i %i)", ssl, conn->url->protocol, PROTOCOL_HTTPS); /* Open connection */ if ((status = hsocket_open(conn->sock, conn->url->host, conn->url->port, ssl)) != H_OK) + { + log_error2("hsocket_open failed (%s)", herror_message(status)); return status; + } switch(method) { diff --git a/nanohttp/nanohttp-client.h b/nanohttp/nanohttp-client.h index eaab46d..b845ac3 100644 --- a/nanohttp/nanohttp-client.h +++ b/nanohttp/nanohttp-client.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-client.h,v 1.33 2006/12/10 12:23:45 m0gg Exp $ + * $Id: nanohttp-client.h,v 1.34 2006/12/10 19:21:06 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -34,9 +34,9 @@ #include #endif -/** @page nanohttp_client Howto write an HTTP client +/** @page nanohttp_client_page Howto write an HTTP client * - * \section client_sec Table of contents + * @section client_sec Table of contents * * - Client initialization * - Connection initialization @@ -45,14 +45,14 @@ * - Setting an HTTP header with uniq key * - Setting multiple headers * - Fetch the network resource - * - HTTP GET command - * - HTTP POST command + * - @ref get_sec + * - @ref post_sec * - MIME attachments * - Print out the result * - Connection cleanup * - Client cleanup * - * \subsection init_sec Client initialization + * @section init_sec Client initialization * * @code * int main(int argc, char **argv) @@ -75,7 +75,7 @@ * } * @endcode * - * \subsection conn_sec Connection initialization + * @section conn_sec Connection initialization * * @code * if (!(conn = httpc_new())) @@ -86,11 +86,11 @@ * } * @endcode * - * \subsection ssl_sec SSL related functions + * @section ssl_sec SSL related functions * * T.B.D. * - * \subsection header_sec Setting HTTP headers (optional) + * @section header_sec Setting HTTP headers (optional) * * @code * httpc_set_header(conn, "my-key", "my-value"); @@ -101,9 +101,12 @@ * httpc_add_header(conn, "Cookie", "name2:value2"); * @endcode * - * \subsection fetch_sec Fetch the network resource + * Please see @ref general_header_fields and @ref request_header_fields for more + * information. * - * \subsubsection get_sec HTTP GET command + * @section fetch_sec Fetch the network resource + * + * @subsection get_sec HTTP GET method * * @code * if ((status = httpc_get(conn, &result, argv[1])) != H_OK) @@ -115,7 +118,7 @@ * } * @endcode * - * \subsubsection post_sec HTTP POST command + * @subsection post_sec HTTP POST method * * @code * if ((status = httpc_post_begin(conn, argv[1])) != H_OK) @@ -143,11 +146,11 @@ * } * @endcode * - * \subsubsection mime_sec MIME attachments + * @section mime_sec MIME attachments * * T.B.D. * - * \subsection output_sec Print out the result + * @section output_sec Print out the result * * @code * while (http_input_stream_is_read(res->in)) @@ -157,17 +160,18 @@ * } * @endcode * - * \subsection conn_clean_sec Connection cleanup + * @section conn_clean_sec Connection cleanup * * @code * hresponse_free(res); * @endcode * - * \subsection client_clean Client cleanup + * @section client_clean Client cleanup * * @code * httpc_free(conn); * + * exit(0); * } * @endcode * diff --git a/nanohttp/nanohttp-common.c b/nanohttp/nanohttp-common.c index d552a75..a7f7f50 100644 --- a/nanohttp/nanohttp-common.c +++ b/nanohttp/nanohttp-common.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-common.c,v 1.35 2006/12/09 09:04:16 m0gg Exp $ +* $Id: nanohttp-common.c,v 1.36 2006/12/10 19:21:06 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -225,11 +225,12 @@ hpairnode_get_ignore_case(hpair_t * pair, const char *key) log_error1("key is NULL"); return NULL; } + while (pair != NULL) { if (pair->key != NULL) { - if (strcasecmp(pair->key, key)) + if (!strcasecmp(pair->key, key)) { return pair->value; } diff --git a/nanohttp/nanohttp-common.h b/nanohttp/nanohttp-common.h index 2cf6b90..052f814 100644 --- a/nanohttp/nanohttp-common.h +++ b/nanohttp/nanohttp-common.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-common.h,v 1.42 2006/12/10 13:20:38 m0gg Exp $ + * $Id: nanohttp-common.h,v 1.43 2006/12/10 19:21:06 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -1371,6 +1371,7 @@ extern void hpairnode_dump(const hpair_t *pair); * content_type_free(); * * @see content_type_free + * */ extern content_type_t *content_type_new(const char *content_type_str); diff --git a/nanohttp/nanohttp-error.h b/nanohttp/nanohttp-error.h index 0ceb162..77e5721 100644 --- a/nanohttp/nanohttp-error.h +++ b/nanohttp/nanohttp-error.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-error.h,v 1.3 2006/12/03 17:30:57 m0gg Exp $ + * $Id: nanohttp-error.h,v 1.4 2006/12/10 19:21:06 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -39,17 +39,19 @@ * * Example: * + * @code * herror_t err; * - * if ((err = soap_client_invoke(...)) != H_OK) + * if ((err = http_client_invoke("http://somewhere")) != H_OK) * { * printf("Message: %s\n", herror_message(err)); * printf("Error code: %d\n", herror_code(err)); * printf("In function: %s\n", herror_func(err)); * herror_release(err); * } + * @endcode * - * Note that you "must" call herror_release() to free the resources. + * Note that you MUST call herror_release() to free the resources. * */ @@ -60,18 +62,6 @@ */ #define H_OK 0 -/** - * - * XXX: Move this to nanohttp-url.h - * - * URL errors - * - */ -#define URL_ERROR 1100 -#define URL_ERROR_UNKNOWN_PROTOCOL (URL_ERROR + 1) -#define URL_ERROR_NO_PROTOCOL (URL_ERROR + 2) -#define URL_ERROR_NO_HOST (URL_ERROR + 3) - /** * * General errors @@ -100,7 +90,7 @@ /** * - * XXX: Remove me. + * @todo Remove me. * * Dummy deklaration to hide the implementation. * diff --git a/nanohttp/nanohttp-logging.h b/nanohttp/nanohttp-logging.h index 16a8ddb..cf1fe3f 100644 --- a/nanohttp/nanohttp-logging.h +++ b/nanohttp/nanohttp-logging.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-logging.h,v 1.4 2006/11/25 17:03:20 m0gg Exp $ + * $Id: nanohttp-logging.h,v 1.5 2006/12/10 19:21:06 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2006 Ferhat Ayaz @@ -44,10 +44,42 @@ typedef enum log_level extern "C" { #endif +/** + * + * Set the loglevel. + * + * @param level The new loglevel. + * + * @return The old loglevel. + * + */ extern log_level_t hlog_set_level(log_level_t level); + +/** + * + * Get the loglevel. + * + * @return The current loglevel. + * + */ extern log_level_t hlog_get_level(void); +/** + * + * Set the logfile. + * + * @param filename The filename of the logfile. + * + */ extern void hlog_set_file(const char *filename); + +/** + * + * Get the filename of the logfile. + * + * @return Pointer to the filename or null otherwise. + * + */ extern char *hlog_get_file(void); #ifdef WIN32 @@ -67,8 +99,9 @@ extern void hlog_error(const char *FUNC, const char *format, ...); } #endif -/* - * XXX: This isn't the "right" way +/** + * + * @todo This isn't the "right" way * * #define log_debug(fmt, ...) fprintf(stderr, "EMERGENCY: %s: " fmt "\n", \ * __FUNCTION__, ## __VA_ARGS__) diff --git a/nanohttp/nanohttp-response.c b/nanohttp/nanohttp-response.c index 707b07b..5741683 100755 --- a/nanohttp/nanohttp-response.c +++ b/nanohttp/nanohttp-response.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-response.c,v 1.16 2006/12/01 10:56:00 m0gg Exp $ +* $Id: nanohttp-response.c,v 1.17 2006/12/10 19:21:07 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -54,13 +54,12 @@ #include "nanohttp-response.h" static hresponse_t * -hresponse_new(void) +_hresponse_new(void) { hresponse_t *res; if (!(res = (hresponse_t *) malloc(sizeof(hresponse_t)))) { - log_error2("malloc failed (%s)", strerror(errno)); return NULL; } @@ -83,7 +82,7 @@ _hresponse_parse_header(const char *buffer) char *s1, *s2, *str; /* create response object */ - res = hresponse_new(); + res = _hresponse_new(); /* *** parse spec *** */ /* [HTTP/1.1 | 1.2] [CODE] [DESC] */ @@ -154,7 +153,6 @@ _hresponse_parse_header(const char *buffer) return res; } - herror_t hresponse_new_from_socket(struct hsocket_t *sock, hresponse_t ** out) { @@ -239,24 +237,24 @@ read_header: /* for errorcode: 100 (continue) */ return H_OK; } - - void hresponse_free(hresponse_t * res) { - if (res == NULL) - return; + if (res) + { + if (res->header) + hpairnode_free_deep(res->header); - if (res->header) - hpairnode_free_deep(res->header); + if (res->in) + http_input_stream_free(res->in); - if (res->in) - http_input_stream_free(res->in); + if (res->content_type) + content_type_free(res->content_type); - if (res->content_type) - content_type_free(res->content_type); + if (res->attachments) + attachments_free(res->attachments); - if (res->attachments) - attachments_free(res->attachments); - free(res); + free(res); + } + return; } diff --git a/nanohttp/nanohttp-server.c b/nanohttp/nanohttp-server.c index 1efb0e5..1808b32 100644 --- a/nanohttp/nanohttp-server.c +++ b/nanohttp/nanohttp-server.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-server.c,v 1.73 2006/12/02 21:50:47 m0gg Exp $ +* $Id: nanohttp-server.c,v 1.74 2006/12/10 19:21:07 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -630,11 +630,9 @@ _httpd_authenticate_request(struct hrequest_t * req, httpd_auth auth) if (!auth) return 1; - if (! - (authorization = - hpairnode_get_ignore_case(req->header, HEADER_AUTHORIZATION))) + if (!(authorization = hpairnode_get_ignore_case(req->header, HEADER_AUTHORIZATION))) { - log_debug2("%s header not set", HEADER_AUTHORIZATION); + log_debug2("\"%s\" header not set", HEADER_AUTHORIZATION); return 0; } diff --git a/nanohttp/nanohttp-server.h b/nanohttp/nanohttp-server.h index ebea33f..4205900 100644 --- a/nanohttp/nanohttp-server.h +++ b/nanohttp/nanohttp-server.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-server.h,v 1.29 2006/12/10 12:23:46 m0gg Exp $ + * $Id: nanohttp-server.h,v 1.30 2006/12/10 19:21:07 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -48,9 +48,9 @@ * * \section links_sec Howto to the nanoHTTP library * - * - \ref nanohttp_client - * - \ref nanohttp_server - * - \ref nanohttp_mime + * - \ref nanohttp_client_page + * - \ref nanohttp_server_page + * - \ref nanohttp_mime_page * * @author Ferhat Ayaz * @author Michael Rans @@ -64,7 +64,7 @@ * */ -/** @page nanohttp_server Howto write an HTTP server +/** @page nanohttp_server_page Howto write an HTTP server * * \section server_sec Table of contents * @@ -87,7 +87,7 @@ * fprintf(stderr, "Cannot init httpd\n"); * exit(1); * } - * @code + * @endcode * * \section service_sec Service registration * @@ -99,7 +99,7 @@ * httpd_destroy(); * exit(1); * } - * @code + * @endcode * * @code * if ((status = httpd_register_secure("/secure", secure_service, simple_authenticator)) != H_OK) @@ -109,7 +109,7 @@ * httpd_destroy(); * exit(1); * } - * @code + * @endcode * * @code * if ((status = httpd_register("/headers", headers_service)) != H_OK) @@ -119,7 +119,7 @@ * httpd_destroy(); * exit(1); * } - * @code + * @endcode * * @code * if ((status = httpd_register_default("/error", default_service)) != H_OK) @@ -129,7 +129,7 @@ * httpd_destroy(); * exit(1); * } - * @code + * @endcode * * \section running_sec Running the server * @@ -141,7 +141,7 @@ * httpd_destroy(); * exit(1); * } - * @code + * @endcode * * \section cleanup_sec Server cleanup * @@ -150,7 +150,7 @@ * * exit(0); * } - * @code + * @endcode * * \section function_seq Sample service function * @@ -185,7 +185,7 @@ * * return; * } - * @code + * @endcode * */ @@ -323,6 +323,7 @@ extern void httpd_add_headers(httpd_conn_t * conn, const hpair_t * values); * XXX: move to nanohttp-mime.c * * MIME support httpd_mime_* function set + * */ /** diff --git a/nanohttp/nanohttp-socket.h b/nanohttp/nanohttp-socket.h index dcb19eb..36f2c09 100644 --- a/nanohttp/nanohttp-socket.h +++ b/nanohttp/nanohttp-socket.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-socket.h,v 1.36 2006/12/03 17:30:58 m0gg Exp $ + * $Id: nanohttp-socket.h,v 1.37 2006/12/10 19:21:07 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -24,9 +24,14 @@ #ifndef __nanohttp_socket_h #define __nanohttp_socket_h +/** @defgroup socket_errors Socket errors + * + */ +/*{*/ + /** * - * Socket error + * Generic socket error * */ #define HSOCKET_ERROR 1000 @@ -40,9 +45,17 @@ #define HSOCKET_ERROR_ACCEPT (HSOCKET_ERROR + 8) #define HSOCKET_ERROR_NOT_INITIALIZED (HSOCKET_ERROR + 9) #define HSOCKET_ERROR_IOCTL (HSOCKET_ERROR + 10) + +/** + * + * @todo move the next two items to nanohttp-ssl.h + * + */ #define HSOCKET_ERROR_SSLCLOSE (HSOCKET_ERROR + 11) #define HSOCKET_ERROR_SSLCTX (HSOCKET_ERROR + 11) +/*}*/ + /** * * Socket definition @@ -71,7 +84,7 @@ extern "C" * Initializes the socket modul. This should be called only once for an * application. * - * @returns This function should always return H_OK. + * @return This function should always return H_OK. * */ extern herror_t hsocket_module_init(int argc, char **argv); @@ -94,7 +107,7 @@ extern void hsocket_module_destroy(void); * * @param sock the destination socket to initialize. * - * @returns This function should always return H_OK. + * @return This function should always return H_OK. * * @see hssl_module_init * @@ -120,10 +133,10 @@ extern void hsocket_free(struct hsocket_t * sock); * @param port port number to connect to * @param ssl whether to open a SSL connection * - * @returns H_OK if success. One of the followings if fails:

- * - HSOCKET_ERROR_CREATE - * - HSOCKET_ERROR_GET_HOSTNAME - * - HSOCKET_ERROR_CONNECT + * @return H_OK if success. One of the followings if fails: + * - HSOCKET_ERROR_CREATE + * - HSOCKET_ERROR_GET_HOSTNAME + * - HSOCKET_ERROR_CONNECT * */ extern herror_t hsocket_open(struct hsocket_t *sock, const char *host, int port, int ssl); @@ -145,9 +158,9 @@ extern void hsocket_close(struct hsocket_t *sock); * @param sock socket to use. * @param port port number to bind to * - * @returns H_OK if success. One of the followings if fails:

- * - HSOCKET_ERROR_CREATE - * - HSOCKET_ERROR_BIND + * @return H_OK on success. One of the followings if fails: + * - HSOCKET_ERROR_CREATE + * - HSOCKET_ERROR_BIND * * @see hsocket_listen * @@ -161,9 +174,10 @@ extern herror_t hsocket_bind(struct hsocket_t *sock, unsigned short port); * * @param sock the socket to use * - * @returns H_OK if success. One of the followings if fails:

- * - HSOCKET_ERROR_NOT_INITIALIZED - * - HSOCKET_ERROR_LISTEN + * @return H_OK if success. One of the followings if fails:< + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_LISTEN + * */ extern herror_t hsocket_listen(struct hsocket_t *sock); @@ -175,9 +189,9 @@ extern herror_t hsocket_listen(struct hsocket_t *sock); * @param sock the socket which listents to a port * @param dest the destination socket which will be created * - * @returns H_OK if success. One of the followings if fails:

- * - HSOCKET_ERROR_NOT_INITIALIZED - * - HSOCKET_ERROR_ACCEPT + * @return H_OK if success. One of the followings if fails:

+ * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_ACCEPT */ extern herror_t hsocket_accept(struct hsocket_t *sock, struct hsocket_t *dest); @@ -189,9 +203,9 @@ extern herror_t hsocket_accept(struct hsocket_t *sock, struct hsocket_t *dest); * @param bytes bytes to send * @param size size of memory to sent pointed by bytes. * - * @returns H_OK if success. One of the followings if fails:

- * - HSOCKET_ERROR_NOT_INITIALIZED - * - HSOCKET_ERROR_SEND + * @return H_OK if success. One of the followings if fails: + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_SEND */ extern herror_t hsocket_send(struct hsocket_t *sock, const unsigned char *bytes, int size); @@ -202,9 +216,9 @@ extern herror_t hsocket_send(struct hsocket_t *sock, const unsigned char *bytes, * @param sock the socket to use to send the data * @param str the null terminated string to sent * - * @returns H_OK if success. One of the followings if fails:

- * - HSOCKET_ERROR_NOT_INITIALIZED - * - HSOCKET_ERROR_SEND + * @return H_OK on success. One of the followings if fails: + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_SEND * */ extern herror_t hsocket_send_string(struct hsocket_t *sock, const char *str); @@ -222,8 +236,8 @@ extern int hsocket_select_recv(int sock, char *buf, size_t len); * of bytes (size parameter) was readed. Otherwise this function will not * wait and will return with the bytes quequed on the socket. * - * @returns This function will return -1 if an read error was occured. Otherwise - * the return value is the size of bytes readed from the socket. + * @return This function will return -1 if an read error was occured. Otherwise + * the return value is the size of bytes readed from the socket. * */ extern herror_t hsocket_recv(struct hsocket_t * sock, unsigned char *buffer, int size, int force, int *len); @@ -232,6 +246,10 @@ extern herror_t hsocket_recv(struct hsocket_t * sock, unsigned char *buffer, int * * Get the socket read/write timeout. * + * @return The socket timeout in seconds. + * + * @see hsocket_set_timeout + * */ extern int hsocket_get_timeout(void); @@ -241,6 +259,8 @@ extern int hsocket_get_timeout(void); * * @param secs Timeout in seconds. * + * @see hsocket_get_timeout + * */ extern void hsocket_set_timeout(int secs); diff --git a/nanohttp/nanohttp-stream.h b/nanohttp/nanohttp-stream.h index 1c27164..5434953 100755 --- a/nanohttp/nanohttp-stream.h +++ b/nanohttp/nanohttp-stream.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-stream.h,v 1.13 2006/12/03 17:30:58 m0gg Exp $ + * $Id: nanohttp-stream.h,v 1.14 2006/12/10 19:21:07 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -24,14 +24,14 @@ #ifndef __nanohttp_stream_h #define __nanohttp_stream_h -/** +/** @file * - * HTTP Stream modul: + * \section HTTP Stream modul * * nanohttp supports 2 different streams: * - * 1. http_input_stream_t - * 2. http_output_stream_t + * -# http_input_stream_t + * -# http_output_stream_t * * These are not regular streams. They will care about transfer styles while * sending/receiving data. @@ -235,10 +235,9 @@ extern void http_output_stream_free(struct http_output_stream_t * stream); * @param bytes bytes to send * @param size size of bytes to send * - * @returns H_OK if success. One of the followings otherwise - * - * - HSOCKET_ERROR_NOT_INITIALIZED - * - HSOCKET_ERROR_SEND + * @returns H_OK on success. One of the followings otherwise + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_SEND * */ extern herror_t http_output_stream_write(struct http_output_stream_t *stream, const unsigned char* bytes, int size); @@ -250,10 +249,9 @@ extern herror_t http_output_stream_write(struct http_output_stream_t *stream, co * @param stream the stream to use to send data * @param str a null terminated string to send * - * @returns H_OK if success. One of the followings otherwise - * - * - HSOCKET_ERROR_NOT_INITIALIZED - * - HSOCKET_ERROR_SEND + * @returns H_OK on success. One of the followings otherwise + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_SEND * */ extern herror_t http_output_stream_write_string(struct http_output_stream_t *stream, const char *str); @@ -265,10 +263,9 @@ extern herror_t http_output_stream_write_string(struct http_output_stream_t *str * * @param stream the stream to send post data. * - * @returns H_OK if success. One of the followings otherwise - * - * - HSOCKET_ERROR_NOT_INITIALIZED - * - HSOCKET_ERROR_SEND + * @returns H_OK on success. One of the followings otherwise + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_SEND * */ extern herror_t http_output_stream_flush(struct http_output_stream_t *stream); diff --git a/nanohttp/nanohttp-url.c b/nanohttp/nanohttp-url.c index 1588590..194eba9 100644 --- a/nanohttp/nanohttp-url.c +++ b/nanohttp/nanohttp-url.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-url.c,v 1.1 2006/12/08 21:21:41 m0gg Exp $ +* $Id: nanohttp-url.c,v 1.2 2006/12/10 19:21:07 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -29,32 +29,40 @@ #include #endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#ifdef HAVE_NETDB_H +#include +#endif + #include "nanohttp-logging.h" #include "nanohttp-error.h" #include "nanohttp-url.h" -/* TODO (#1#): find proper ports */ -#define URL_DEFAULT_PORT_HTTP 80 -#define URL_DEFAULT_PORT_HTTPS 81 -#define URL_DEFAULT_PORT_FTP 120 +#define HTTP_DEFAULT_PORT 80 +#define HTTPS_DEFAULT_PORT 443 static void -_hurl_dump(const struct hurl_t * url) +_hurl_dump(const struct hurl_t *url) { - - if (url == NULL) + if (!url) { - log_error1("url is NULL!"); + log_error1("parameter url is NULL"); return; } - log_verbose2("PROTOCOL : %d", url->protocol); - log_verbose2(" HOST : %s", url->host); - log_verbose2(" PORT : %d", url->port); - log_verbose2(" CONTEXT : %s", url->context); + + log_verbose2("PROTOCOL: %d", url->protocol); + log_verbose2(" HOST: \"%s\"", url->host); + log_verbose2(" PORT: %d", url->port); + log_verbose2(" CONTEXT: \"%s\"", url->context); + + return; } herror_t -hurl_parse(struct hurl_t * url, const char *urlstr) +hurl_parse(struct hurl_t *url, const char *urlstr) { int iprotocol; int ihost; @@ -63,6 +71,7 @@ hurl_parse(struct hurl_t * url, const char *urlstr) int size; char tmp[8]; char protocol[1024]; + struct servent *entry; iprotocol = 0; len = strlen(urlstr); @@ -89,6 +98,7 @@ hurl_parse(struct hurl_t * url, const char *urlstr) log_error1("no protocol"); return herror_new("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol"); } + /* find host */ ihost = iprotocol + 3; while (urlstr[ihost] != ':' @@ -102,6 +112,7 @@ hurl_parse(struct hurl_t * url, const char *urlstr) log_error1("no host"); return herror_new("hurl_parse", URL_ERROR_NO_HOST, "No host"); } + /* find port */ iport = ihost; if (ihost + 1 < len) @@ -118,17 +129,39 @@ hurl_parse(struct hurl_t * url, const char *urlstr) /* find protocol */ strncpy(protocol, urlstr, iprotocol); protocol[iprotocol] = '\0'; - if (strcasecmp(protocol, "http")) + if (!strncasecmp(protocol, "http", 5)) url->protocol = PROTOCOL_HTTP; - else if (strcasecmp(protocol, "https")) + else if (!strncasecmp(protocol, "https", 6)) url->protocol = PROTOCOL_HTTPS; - else if (strcasecmp(protocol, "ftp")) - url->protocol = PROTOCOL_FTP; else - return herror_new("hurl_parse", URL_ERROR_UNKNOWN_PROTOCOL, "Unknown protocol '%s'", protocol); + return herror_new("hurl_parse", URL_ERROR_UNKNOWN_PROTOCOL, "Unknown protocol \"%s\"", protocol); + + /* find right port */ + if (!(entry = getservbyname(protocol, "tcp"))) + { + log_warn2("getservbyname(\"%s\", \"tcp\") returned NULL, please edit services database", protocol); + + switch (url->protocol) + { + case PROTOCOL_HTTP: + url->port = HTTP_DEFAULT_PORT; + break; + case PROTOCOL_HTTPS: + url->port = HTTPS_DEFAULT_PORT; + break; + } + } + else + { + url->port = ntohs(entry->s_port); + } - /* TODO (#1#): add max of size and URL_MAX_HOST_SIZE */ size = ihost - iprotocol - 3; + if (!(url->host = (char *)malloc(size + 1))) + { + log_error2("malloc failed (%s)", strerror(errno)); + return herror_new("hurl_parse", URL_ERROR, "malloc failed (%s)", strerror(errno)); + } strncpy(url->host, &urlstr[iprotocol + 3], size); url->host[size] = '\0'; @@ -138,33 +171,23 @@ hurl_parse(struct hurl_t * url, const char *urlstr) strncpy(tmp, &urlstr[ihost + 1], size); url->port = atoi(tmp); } - else - { - switch (url->protocol) - { - case PROTOCOL_HTTP: - url->port = URL_DEFAULT_PORT_HTTP; - break; - case PROTOCOL_HTTPS: - url->port = URL_DEFAULT_PORT_HTTPS; - break; - case PROTOCOL_FTP: - url->port = URL_DEFAULT_PORT_FTP; - break; - } - } + /* find path */ len = strlen(urlstr); if (len > iport) { - /* TODO (#1#): find max of size and URL_MAX_CONTEXT_SIZE */ size = len - iport; + if (!(url->context = (char *)malloc(size + 1))) + { + log_error2("malloc failed (%s)", strerror(errno)); + return herror_new("hurl_parse", URL_ERROR, "malloc failed (%s)", strerror(errno)); + } strncpy(url->context, &urlstr[iport], size); url->context[size] = '\0'; } else { - url->context[0] = '\0'; + url->context = strdup(""); } _hurl_dump(url); @@ -175,10 +198,16 @@ hurl_parse(struct hurl_t * url, const char *urlstr) void hurl_free(struct hurl_t *url) { - if (!url) - return; + if (url) + { + if (url->host) + free(url->host); - free(url); + if (url->context) + free(url->context); + + free(url); + } return; } diff --git a/nanohttp/nanohttp-url.h b/nanohttp/nanohttp-url.h index ad35c90..61342f5 100644 --- a/nanohttp/nanohttp-url.h +++ b/nanohttp/nanohttp-url.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-url.h,v 1.1 2006/12/08 21:21:41 m0gg Exp $ + * $Id: nanohttp-url.h,v 1.2 2006/12/10 19:21:07 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -24,6 +24,16 @@ #ifndef __nanohttp_url_h #define __nanohttp_url_h +/** + * + * URL errors + * + */ +#define URL_ERROR 1100 +#define URL_ERROR_UNKNOWN_PROTOCOL (URL_ERROR + 1) +#define URL_ERROR_NO_PROTOCOL (URL_ERROR + 2) +#define URL_ERROR_NO_HOST (URL_ERROR + 3) + /** * * The protocol types in enumeration format. Used in some other nanohttp objects @@ -35,8 +45,7 @@ typedef enum _hprotocol { PROTOCOL_HTTP, - PROTOCOL_HTTPS, - PROTOCOL_FTP + PROTOCOL_HTTPS } hprotocol_t; #define URL_MAX_HOST_SIZE 120 @@ -46,7 +55,7 @@ typedef enum _hprotocol * * The URL object. A representation of an URL like: * - * [protocol]://[host]:[port]/[context] + * [protocol]://[user]@[host]:[port]/[context]['#' fragment]['?' query] * * @see http://www.ietf.org/rfc/rfc2396.txt * @@ -64,27 +73,26 @@ struct hurl_t /** * * The port number. If no port number was given in the URL, one of the default - * port numbers will be selected. - * - URL_HTTP_DEFAULT_PORT - * - URL_HTTPS_DEFAULT_PORT - * - URL_FTP_DEFAULT_PORT + * port numbers will be selected: + * - HTTP_DEFAULT_PORT + * - HTTPS_DEFAULT_PORT * */ - short port; + unsigned short port; /** * * The hostname * */ - char host[URL_MAX_HOST_SIZE]; + char *host; /** * * The string after the hostname. * */ - char context[URL_MAX_CONTEXT_SIZE]; + char *context; }; #ifdef __cplusplus @@ -94,6 +102,7 @@ extern "C" { /** * * Parses the given 'urlstr' and fills the given hurl_t object. + * Parse an URI URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ] * * @param obj the destination URL object to fill * @param url the URL in string format @@ -110,6 +119,8 @@ extern herror_t hurl_parse(struct hurl_t * obj, const char *url); * * Frees the resources within a url and the url itself. * + * @param url pointer to an hurl_t + * */ extern void hurl_free(struct hurl_t *url); -- cgit v1.1-32-gdbae