From b73c5d785a71edade3ba473cbb13ec57aaeec7ed Mon Sep 17 00:00:00 2001 From: snowdrop Date: Mon, 27 Feb 2006 22:26:01 +0000 Subject: - removes a memleak in examples/csoap/simpleserver.c say_hello - adds various malloc error messages - does some libcsoap/*.c #include fixups - removes a memleak in libcsoap/soap-server.c soap_server_entry - removes the double free of SoapCtx->action (again!!!) - rewrites more or less cleanly hsocket_close - adds volatile keywords for thread shared data items - _httpd_parse_arguments cleanup - rwerites the _httpd_connection initialization - adds a call to pthread_attr_destroy in httpd_session_main - fixes a wrong loop initialization in _httpd_wait_for_emtpy_conn - fixes a memleak in httpd_session_main (req) - more sophisticated httpd_server example - HTTP authentication SEGfault without password fixed --- examples/csoap/simpleserver.c | 3 +- examples/nanohttp/http_server.c | 131 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 125 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/csoap/simpleserver.c b/examples/csoap/simpleserver.c index 8e181d9..cde4e79 100644 --- a/examples/csoap/simpleserver.c +++ b/examples/csoap/simpleserver.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: simpleserver.c,v 1.17 2006/01/11 09:18:17 snowdrop Exp $ + * $Id: simpleserver.c,v 1.18 2006/02/27 22:26:01 snowdrop Exp $ * * CSOAP Project: CSOAP examples project * Copyright (C) 2003-2004 Ferhat Ayaz @@ -52,6 +52,7 @@ say_hello(SoapCtx * req, SoapCtx * res) 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); + xmlFree(name); } return H_OK; diff --git a/examples/nanohttp/http_server.c b/examples/nanohttp/http_server.c index 2f939f9..f8a0536 100644 --- a/examples/nanohttp/http_server.c +++ b/examples/nanohttp/http_server.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: http_server.c,v 1.1 2006/02/19 08:45:13 snowdrop Exp $ +* $Id: http_server.c,v 1.2 2006/02/27 22:26:02 snowdrop Exp $ * * CSOAP Project: A http client/server library in C (example) * Copyright (C) 2003 Ferhat Ayaz @@ -22,42 +22,157 @@ * Email: hero@persua.de ******************************************************************/ #include +#include #include static int simple_authenticator(const char *user, const char *password) { - log_verbose3("loggin in user=\"%s\" password=\"%s\"", user, password); + + log_info3("logging in user=\"%s\" password=\"%s\"", user, password); + + if (strcmp(user, "bob")) { + + log_warn2("user \"%s\" unkown", user); + return 0; + } + + if (strcmp(password, "builder")) { + + log_warn1("wrong password"); + return 0; + } return 1; } -static void simple_service(httpd_conn_t *conn, hrequest_t *req) +static void secure_service(httpd_conn_t *conn, hrequest_t *req) +{ + + httpd_send_header(conn, 200, "OK"); + hsocket_send(conn->sock, + "" + "" + "Secure ressource!" + "" + "" + "

Authenticated access!!!

" + "" + ""); + + return; +} + +static void default_service(httpd_conn_t *conn, hrequest_t *req) { + httpd_send_header(conn, 404, "Not found"); + hsocket_send(conn->sock, + "" + "" + "Default error page" + "" + "" + "

Default error page

" + "
"); + + hsocket_send(conn->sock, req->path); + + hsocket_send(conn->sock, " can not be found" + "
" + "" + ""); + + return; +} + +static void headers_service(httpd_conn_t *conn, hrequest_t *req) +{ + hpair_t *walker; + + httpd_send_header(conn, 200, "OK"); + hsocket_send(conn->sock, + "" + "" + "Request headers" + "" + "" + "

Request headers

" + "
    "); + + for (walker=req->header; walker; walker=walker->next) + { + hsocket_send(conn->sock, "
  • "); + hsocket_send(conn->sock, walker->key); + hsocket_send(conn->sock, " = "); + hsocket_send(conn->sock, walker->value); + hsocket_send(conn->sock, "
  • "); + } + + hsocket_send(conn->sock, + "
" + "" + ""); + + return; +} + +static void root_service(httpd_conn_t *conn, hrequest_t *req) +{ httpd_send_header(conn, 200, "OK"); - hsocket_send(conn->sock, "Success!

Success!!!

"); + hsocket_send(conn->sock, + "" + "" + "nanoHTTP server examples" + "" + "" + "

nanoHTTP server examples

" + "" + "" + ""); return; } int main(int argc, char *argv[]) { - log_set_level(HLOG_VERBOSE); + log_set_level(HLOG_INFO); if (httpd_init(argc, argv)) { - fprintf(stderr, "can not init httpd"); + fprintf(stderr, "Can not init httpd"); return 1; } - if (!httpd_register_secure("/", simple_service, simple_authenticator)) - { + if (!httpd_register("/", root_service)) { fprintf(stderr, "Can not register service"); return 1; } + if (!httpd_register_secure("/secure", secure_service, simple_authenticator)) { + + fprintf(stderr, "Can not register secure service"); + return 1; + } + + if (!httpd_register("/headers", headers_service)) { + + fprintf(stderr, "Can not register headers service"); + return 1; + } + + if (!httpd_register_default("/error", default_service)) { + + fprintf(stderr, "Can not register default service"); + return 1; + } + if (httpd_run()) { fprintf(stderr, "can not run httpd"); -- cgit v1.1-32-gdbae