diff options
-rw-r--r-- | examples/csoap/simpleserver.c | 8 | ||||
-rw-r--r-- | examples/nanohttp/http_client.c | 9 | ||||
-rw-r--r-- | libcsoap/soap-client.h | 87 | ||||
-rw-r--r-- | libcsoap/soap-nudp.c | 4 | ||||
-rw-r--r-- | libcsoap/soap-server.h | 128 | ||||
-rw-r--r-- | nanohttp/nanohttp-admin.c | 87 | ||||
-rw-r--r-- | nanohttp/nanohttp-admin.h | 12 | ||||
-rw-r--r-- | nanohttp/nanohttp-client.c | 20 | ||||
-rw-r--r-- | nanohttp/nanohttp-client.h | 36 | ||||
-rw-r--r-- | nanohttp/nanohttp-common.c | 5 | ||||
-rw-r--r-- | nanohttp/nanohttp-common.h | 3 | ||||
-rw-r--r-- | nanohttp/nanohttp-error.h | 22 | ||||
-rw-r--r-- | nanohttp/nanohttp-logging.h | 39 | ||||
-rwxr-xr-x | nanohttp/nanohttp-response.c | 34 | ||||
-rw-r--r-- | nanohttp/nanohttp-server.c | 8 | ||||
-rw-r--r-- | nanohttp/nanohttp-server.h | 27 | ||||
-rw-r--r-- | nanohttp/nanohttp-socket.h | 70 | ||||
-rwxr-xr-x | nanohttp/nanohttp-stream.h | 31 | ||||
-rw-r--r-- | nanohttp/nanohttp-url.c | 109 | ||||
-rw-r--r-- | nanohttp/nanohttp-url.h | 33 |
20 files changed, 553 insertions, 219 deletions
diff --git a/examples/csoap/simpleserver.c b/examples/csoap/simpleserver.c index 676be5f..6439a65 100644 --- a/examples/csoap/simpleserver.c +++ b/examples/csoap/simpleserver.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: simpleserver.c,v 1.27 2006/11/28 23:45:57 m0gg Exp $ + * $Id: simpleserver.c,v 1.28 2006/12/10 19:21:05 m0gg Exp $ * * CSOAP Project: CSOAP examples project * Copyright (C) 2003-2004 Ferhat Ayaz @@ -102,6 +102,7 @@ main(int argc, char **argv) { printf("soap_router_new failed (%p)\n", router); herror_release(err); + soap_server_destroy(); exit(1); } @@ -109,6 +110,7 @@ main(int argc, char **argv) { printf("%s(): %s [%d]\n", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); + soap_server_destroy(); exit(1); } @@ -116,6 +118,7 @@ main(int argc, char **argv) { printf("%s(): %s [%d]\n", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); + soap_server_destroy(); exit(1); } printf("router (%p) registered for \"%s\"\n", router, url); @@ -125,11 +128,12 @@ main(int argc, char **argv) { printf("%s(): %s [%d]\n", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); + soap_server_destroy(); exit(1); } printf("shutting down\n"); soap_server_destroy(); - return 0; + exit(0); } diff --git a/examples/nanohttp/http_client.c b/examples/nanohttp/http_client.c index 7deeb03..1b9c912 100644 --- a/examples/nanohttp/http_client.c +++ b/examples/nanohttp/http_client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: http_client.c,v 1.8 2006/12/01 10:56:00 m0gg Exp $ +* $Id: http_client.c,v 1.9 2006/12/10 19:21:06 m0gg Exp $ * * CSOAP Project: A http client/server library in C (example) * Copyright (C) 2003-2004 Ferhat Ayaz @@ -39,7 +39,7 @@ static char *password = NULL; static void show_response(hresponse_t *res) { unsigned char buffer[MAX_BUFFER_SIZE+1]; - int read; + int len; if (!res) { @@ -53,6 +53,7 @@ static void show_response(hresponse_t *res) if (show_headers) { hpair_t *pair; + printf("HTTP Headers:\n"); for (pair = res->header; pair; pair=pair->next) printf(" %s: %s\n", pair->key, pair->value); @@ -66,10 +67,10 @@ static void show_response(hresponse_t *res) while (http_input_stream_is_ready(res->in)) { - read = http_input_stream_read(res->in, buffer, MAX_BUFFER_SIZE); + len = http_input_stream_read(res->in, buffer, MAX_BUFFER_SIZE); if (show_content) - fwrite(buffer, read, 1, stdout); + fwrite(buffer, len, 1, stdout); } return; } diff --git a/libcsoap/soap-client.h b/libcsoap/soap-client.h index e821b9b..b4a0cce 100644 --- a/libcsoap/soap-client.h +++ b/libcsoap/soap-client.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-client.h,v 1.16 2006/11/30 14:23:59 m0gg Exp $ + * $Id: soap-client.h,v 1.17 2006/12/10 19:21:06 m0gg Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -32,6 +32,91 @@ #include <libcsoap/soap-addressing.h> #endif +/** @page nanohttp_client Howto write a SOAP client + * + * @section toc_sec Table of contents + * + * - @ref soap_client_init + * - @ref envelope_sec + * - @ref invoke_sec + * - @ref result_sec + * - @ref soap_request_cleanup_sec + * - @ref soap_client_cleanup_sec + * + * @section soap_client_init Client initialization + * + * @code + * static char *url = "http://localhost:10000/csoapserver"; + * static char *urn = "urn:examples"; + * static char *method = "sayHello"; + * + * int * main(int argc, char **argv) + * { + * struct SoapCtx *request; + * struct SoapCtx *response; + * herror_t err; + * + * if ((err = soap_client_init_args(argc, argv)) != H_OK) + * { + * printf("%s():%s [%d]\n", herror_func(err), herror_message(err), herror_code(err)); + * herror_release(err); + * exit(1); + * } + * @endcode + * + * @section envelope_sec Envelope creation + * + * @code + * if ((err = soap_ctx_new_with_method(urn, method, &request)) != H_OK) + * { + * printf("%s():%s [%d]\n", herror_func(err), herror_message(err), herror_code(err)); + * herror_release(err); + * soap_client_destroy(); + * exit(1); + * } + * + * soap_env_add_item(request->env, "xsd:string", "name", "Jonny B. Good"); + * @endcode + * + * @section invoke_sec Invocation + * + * @code + * if ((err = soap_client_invoke(request, &response, url, "")) != H_OK) + * { + * printf("[%d] %s(): %s\n", herror_code(err), herror_func(err), herror_message(err)); + * herror_release(err); + * soap_ctx_free(request); + * soap_client_destroy(); + * exit(1); + * } + * @endcode + * + * @section result_sec Printout result + * + * @code + * printf("**** received from \"%s\" ****\n", soap_addressing_get_from_address_string(response->env)); + * xmlDocFormatDump(stdout, response->env->root->doc, 1); + * @endcode + * + * @section soap_request_cleanup_sec Request cleanup + * + * @code + * soap_ctx_free(response); + * soap_ctx_free(request); + * @endcode + * + * @section soap_client_cleanup_sec Client cleanup + * + * @code + * soap_client_destroy(); + * + * exit(0); + * } + * @endcode + * + */ + + #define SOAP_ERROR_CLIENT 5000 #define SOAP_ERROR_CLIENT_GENERIC (SOAP_ERROR_CLIENT + 0) #define SOAP_ERROR_CLIENT_INIT (SOAP_ERROR_CLIENT + 1) diff --git a/libcsoap/soap-nudp.c b/libcsoap/soap-nudp.c index 60a7d25..cde774a 100644 --- a/libcsoap/soap-nudp.c +++ b/libcsoap/soap-nudp.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: soap-nudp.c,v 1.7 2006/12/06 11:27:21 m0gg Exp $ +* $Id: soap-nudp.c,v 1.8 2006/12/10 19:21:06 m0gg Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2006 Heiko Ronsdorf @@ -103,7 +103,7 @@ _soap_nudp_server_set_port(void) } else { - _soap_nudp_port = entry->s_port; + _soap_nudp_port = ntohs(entry->s_port); } return _soap_nudp_port; } diff --git a/libcsoap/soap-server.h b/libcsoap/soap-server.h index e858db9..8cad6ff 100644 --- a/libcsoap/soap-server.h +++ b/libcsoap/soap-server.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-server.h,v 1.19 2006/12/10 12:23:45 m0gg Exp $ + * $Id: soap-server.h,v 1.20 2006/12/10 19:21:06 m0gg Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -56,9 +56,129 @@ * @see http://www.aleksey.com/xmlsec/ */ -/** @file - * - * @version $Revision: 1.19 $ +/** @page soap_server_page Howto write an SOAP server + * + * @section soap_server_toc_sec Table of contents + * + * - @ref soap_server_init_sec + * - @ref soap_server_router_sec + * - @ref soap_server_registration_sec + * - @ref soap_server_running_sec + * - @ref soap_server_cleanup_sec + * - @ref soap_server_function_sec + * + * @section soap_server_init_sec Server initialization + * + * @code + * int main(int argc, char **argv) + * { + * herror_t err; + * struct SoapRouter *router; + * + * if ((err = soap_server_init_args(argc, argv)) != H_OK) + * { + * printf("%s(): %s [%d]\n", herror_func(err), herror_message(err), herror_code(err)); + * herror_release(err); + * exit(1); + * } + * @endcode + * + * @section soap_server_router_sec Router creation + * + * @code + * if (!(router = soap_router_new())) + * { + * printf("soap_router_new failed (router == %p)\n", router); + * herror_release(err); + * exit(1); + * } + * @endcode + * + * @section soap_server_registration_sec Service registration + * + * @code + * if ((err = soap_router_register_service(router, say_hello, method, urn)) != H_OK) + * { + * printf("%s(): %s [%d]\n", herror_func(err), herror_message(err), herror_code(err)); + * herror_release(err); + * exit(1); + * } + * @endcode + * + * @code + * if ((err = soap_server_register_router(router, url))) + * { + * printf("%s(): %s [%d]\n", herror_func(err), herror_message(err), herror_code(err)); + * herror_release(err); + * exit(1); + * } + * printf("router (%p) registered for \"%s\"\n", router, url); + * @endcode + * + * @section soap_server_running_sec Serving requests + * + * @code + * printf("press ctrl-c to shutdown\n"); + * if ((err = soap_server_run()) != H_OK) + * { + * printf("%s(): %s [%d]\n", herror_func(err), herror_message(err), herror_code(err)); + * herror_release(err); + * exit(1); + * } + * @endcode + * + * @section soap_server_cleanup_sec Server cleanup + * + * @code + * soap_server_destroy(); + * + * exit(0); + * } + * @endcode + * + * @section soap_server_function_sec Service function + * + * @code + * herror_t say_hello(struct SoapCtx *req, struct SoapCtx *res) + * { + * herror_t err; + * char *name; + * xmlNodePtr method, node; + * + * printf("processing service request\n"); + * + * xmlDocFormatDump(stdout, req->env->root->doc, 1); + * + * err = soap_env_new_with_response(req->env, &res->env); + * if (err != H_OK) + * { + * printf("soap_env_new_with_response failed (%s)\n", herror_message(err)); + * return err; + * } + * printf("empty response created\n"); + * + * if (!(method = soap_env_get_method(req->env))) + * { + * printf("soap_env_get_method failed\n"); + * return herror_new("say_hello", 0, "There may be a bug in the library..."); + * } + * printf("method found\n"); + * + * printf("adding response content...\n"); + * node = soap_xml_get_children(method); + * + * while (node) + * { + * name = (char *) xmlNodeListGetString(node->doc, node->xmlChildrenNode, 1); + * soap_env_add_itemf(res->env, "xsd:string", "echo", "Hello '%s'", name); + * node = soap_xml_get_next(node); + * if (name) + * xmlFree(name); + * } + * printf("service request done\n"); + * return H_OK; + * } + * @endcode * */ 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 @@ -67,6 +67,14 @@ _httpd_admin_send_title(httpd_conn_t *conn, const char *title) } static void +_httpd_admin_send_footer(httpd_conn_t *conn) +{ + http_output_stream_write_string(conn->out, "</body></html>"); + + return; +} + +static void _httpd_admin_list_services(httpd_conn_t *conn) { char buffer[1024]; @@ -91,7 +99,7 @@ _httpd_admin_list_services(httpd_conn_t *conn) } http_output_stream_write_string(conn->out, "</ul>"); - http_output_stream_write_string(conn->out, "</body></html>"); + _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, - "<p>Service not found!</p>" - "</body></html>"); + "<p>" + "Service not found!" + "</p>"); + _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, "</body></html>"); + _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, - "<p>Service not found!</p>" - "</body></html>"); + "<p>" + "Service not found!" + "</p>"); + + _httpd_admin_send_footer(conn); return; } httpd_enable_service(service); http_output_stream_write_string(conn->out, - "<p>Service enabled</p>" - "</body></html>"); + "<p>" + "Service enabled" + "</p>"); + + _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, - "<p>service not found!</p>" - "</body></html>"); + "<p>" + "Service not found!" + "</p>"); + _httpd_admin_send_footer(conn); return; } httpd_disable_service(service); http_output_stream_write_string(conn->out, - "<p>Service disabled</p>" - "</body></html>"); + "<p>" + "Service disabled" + "</p>"); + _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, "<ul>"); http_output_stream_write_string(conn->out, - "<li><a href=\"?" NHTTPD_ADMIN_QUERY_SERVICES "\">Services</a></li>"); - http_output_stream_write_string(conn->out, "</ul>"); - - http_output_stream_write_string(conn->out, "</body></html>"); + "<ul>" + "<li>" + "<a href=\"?" NHTTPD_ADMIN_QUERY_SERVICES "\">Services</a>" + "</li>" + "<li>" + "<a href=\"?" NHTTPD_ADMIN_QUERY_STATISTICS "\">Statistics</a>" + "</li>" + "</ul>"); + + _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, - "<html>" - "<head>" - "</head>" - "<body>" - "<h1>Sorry!</h1>" - "<hr />" - "<div>POST Service is not implemented now. Use your browser.</div>" - "</body>" - "</html>"); + "<html>" + "<head>" + "</head>" + "<body>" + "<h1>Sorry!</h1>" + "<hr />" + "<div>POST Service is not implemented now. Use your browser.</div>" + "</body>" + "</html>"); } return; } @@ -251,10 +276,10 @@ httpd_admin_init_args(int argc, char **argv) { int i; - for (i=0; i<argc; i++) { - - if (!strcmp(argv[i], NHTTPD_ARG_ENABLE_ADMIN)) { - + for (i=0; i<argc; i++) + { + if (!strcmp(argv[i], NHTTPD_ARG_ENABLE_ADMIN)) + { httpd_register("/nhttp", _httpd_admin_entry); break; } diff --git a/nanohttp/nanohttp-admin.h b/nanohttp/nanohttp-admin.h index 02cc18e..05ce5cf 100644 --- a/nanohttp/nanohttp-admin.h +++ b/nanohttp/nanohttp-admin.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-admin.h,v 1.4 2006/12/02 21:50:47 m0gg Exp $ + * $Id: nanohttp-admin.h,v 1.5 2006/12/10 19:21:06 m0gg Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -40,7 +40,9 @@ * * Example query: * + * @code * http://localhost:10000/nhttp + * @endcode * * @see httpd_register * @@ -53,7 +55,9 @@ * * Example query: * + * @code * http://localhost:10000/nhttp?services=list + * @endcode * */ #define NHTTPD_ADMIN_QUERY_SERVICES "services" @@ -64,7 +68,9 @@ * * Example query: * + * @code * http://localhost:10000/nhttp?statistics=SERVICE_CONTEXT + * @endcode * */ #define NHTTPD_ADMIN_QUERY_STATISTICS "statistics" @@ -76,7 +82,9 @@ * * Example query: * + * @code * http://localhost:10000/nhttp?enable=SERVICE_CONTEXT + * @endcode * */ #define NHTTPD_ADMIN_QUERY_ENABLE_SERVICE "enable" @@ -87,7 +95,9 @@ * * Example query: * + * @code * http://localhost:10000/nhttp?disable=SERVICE_CONTEXT + * @endcode * */ #define NHTTPD_ADMIN_QUERY_DISABLE_SERVICE "disable" diff --git a/nanohttp/nanohttp-client.c b/nanohttp/nanohttp-client.c index e4a0337..6f740e9 100644 --- a/nanohttp/nanohttp-client.c +++ b/nanohttp/nanohttp-client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-client.c,v 1.50 2006/12/08 21:21:41 m0gg Exp $ +* $Id: nanohttp-client.c,v 1.51 2006/12/10 19:21:06 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -121,6 +121,8 @@ httpc_new(void) if ((status = hsocket_init(res->sock)) != 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 <nanohttp/nanohttp-logging.h> #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. * */ @@ -62,18 +64,6 @@ /** * - * 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:<P> - * - 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:<P> - * - 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:<P> - * - 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:<P> - * - HSOCKET_ERROR_NOT_INITIALIZED - * - HSOCKET_ERROR_ACCEPT + * @return H_OK if success. One of the followings if fails:<P> + * - 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:<P> - * - 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:<P> - * - 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 <stdio.h> #endif +#ifdef HAVE_ERRNO_H +#include <errno.h> +#endif + +#ifdef HAVE_NETDB_H +#include <netdb.h> +#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 @@ -26,6 +26,16 @@ /** * + * 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 * like hurl_t. * @@ -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); |