From df58dad240fe368c261263e248d3520d3e0be1a3 Mon Sep 17 00:00:00 2001 From: m0gg Date: Thu, 30 Nov 2006 14:23:58 +0000 Subject: Code cleanup --- examples/csoap/echoattachments-client.c | 9 +- examples/csoap/simpleclient.c | 8 +- examples/csoap/soapclient.c | 8 +- examples/nanohttp/client_get.c | 112 --------------- examples/nanohttp/http_client.c | 214 ++++++++++++++-------------- examples/nanohttp/http_server.c | 239 +++++++++++++++----------------- 6 files changed, 224 insertions(+), 366 deletions(-) delete mode 100644 examples/nanohttp/client_get.c (limited to 'examples') diff --git a/examples/csoap/echoattachments-client.c b/examples/csoap/echoattachments-client.c index 5c22805..fbee05e 100755 --- a/examples/csoap/echoattachments-client.c +++ b/examples/csoap/echoattachments-client.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: echoattachments-client.c,v 1.15 2006/11/25 15:06:57 m0gg Exp $ + * $Id: echoattachments-client.c,v 1.16 2006/11/30 14:23:59 m0gg Exp $ * * CSOAP Project: CSOAP examples project * Copyright (C) 2003-2004 Ferhat Ayaz @@ -24,13 +24,8 @@ #include #include +#include -#include -#include -#include - -#include -#include #include static const char *urn = "urn:examples"; diff --git a/examples/csoap/simpleclient.c b/examples/csoap/simpleclient.c index 757b2cf..eacc886 100644 --- a/examples/csoap/simpleclient.c +++ b/examples/csoap/simpleclient.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: simpleclient.c,v 1.20 2006/11/29 11:04:24 m0gg Exp $ + * $Id: simpleclient.c,v 1.21 2006/11/30 14:23:59 m0gg Exp $ * * CSOAP Project: CSOAP examples project * Copyright (C) 2003-2004 Ferhat Ayaz @@ -26,12 +26,6 @@ #include #include -#include -#include - -#include -#include -#include #include static char *url = "http://localhost:10000/csoapserver"; diff --git a/examples/csoap/soapclient.c b/examples/csoap/soapclient.c index 5b98e91..00ddbbf 100644 --- a/examples/csoap/soapclient.c +++ b/examples/csoap/soapclient.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soapclient.c,v 1.12 2006/11/26 20:13:05 m0gg Exp $ + * $Id: soapclient.c,v 1.13 2006/11/30 14:23:59 m0gg Exp $ * * CSOAP Project: CSOAP examples project * Copyright (C) 2003-2006 Adrianus Warmehoven @@ -25,12 +25,8 @@ #include #include +#include -#include - -#include -#include -#include #include #define MAX_LINE_LENGTH 65535 diff --git a/examples/nanohttp/client_get.c b/examples/nanohttp/client_get.c deleted file mode 100644 index 4015ed2..0000000 --- a/examples/nanohttp/client_get.c +++ /dev/null @@ -1,112 +0,0 @@ -/****************************************************************** -* $Id: client_get.c,v 1.1 2004/10/15 13:42:07 snowdrop Exp $ -* -* CSOAP Project: A http client/server library in C (example) -* Copyright (C) 2003-2004 Ferhat Ayaz -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU Library General Public -* License as published by the Free Software Foundation; either -* version 2 of the License, or (at your option) any later version. -* -* This library is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Library General Public License for more details. -* -* You should have received a copy of the GNU Library General Public -* License along with this library; if not, write to the -* Free Software Foundation, Inc., 59 Temple Place - Suite 330, -* Boston, MA 02111-1307, USA. -* -* Email: ferhatayaz@yahoo.com -******************************************************************/ -#define MEM_DEBUG -#include -#include - -#ifdef MEM_DEBUG -#include -#endif - -#define MAX_BUFFER_SIZE 1024 - -static -void show_response(hresponse_t *res) -{ - hpair_t *pair; - byte_t buffer[MAX_BUFFER_SIZE+1]; - int read; - - if (res == NULL) - { - log_error1("Response is NULL!"); - return; - } - - log_info2("Status: %d", res->errcode); - log_info2("Desc : '%s'", res->desc); - - pair = res->header; - while (pair != NULL) { - log_debug3("%s: %s", pair->key, pair->value); - pair = pair->next; - } - - if (res->in == NULL) - { - log_warn1("No input stream!"); - return; - } - - - while (http_input_stream_is_ready(res->in)) - { - read = http_input_stream_read(res->in, buffer, MAX_BUFFER_SIZE); - buffer[read] = '\0'; - puts(buffer); - } - -} - -int main(int argc, char *argv[]) -{ - httpc_conn_t *conn; /* Client connection object */ - hresponse_t *res; /* Response object **/ - - /* check usage */ - if (argc < 2) { - fprintf(stderr, "usage %s \n", argv[0]); - exit(1); - } - - /* Set log level to see more information written by the library */ - log_set_level(HLOG_VERBOSE); - - /* Initialize httpc module */ - if (httpc_init(argc, argv)) - { - log_error1("Can not init httpc"); - return 1; - } - - /* Create the client connection object */ - conn = httpc_new(); - - /* Send GET method and receive response */ - res = httpc_get(conn, argv[1]); - - /* Show response */ - show_response(res); - - /* Clean up */ - hresponse_free(res); - httpc_free(conn); - -#ifdef MEM_DEBUG - _mem_report(); -#endif - - return 0; -} - diff --git a/examples/nanohttp/http_client.c b/examples/nanohttp/http_client.c index e320cb9..7c1b2f0 100644 --- a/examples/nanohttp/http_client.c +++ b/examples/nanohttp/http_client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: http_client.c,v 1.6 2006/11/25 15:06:57 m0gg Exp $ +* $Id: http_client.c,v 1.7 2006/11/30 14:23:59 m0gg Exp $ * * CSOAP Project: A http client/server library in C (example) * Copyright (C) 2003-2004 Ferhat Ayaz @@ -25,13 +25,7 @@ #include #include -#include -#include -#include -#include -#include #include -#include #define MAX_BUFFER_SIZE 1024 @@ -42,109 +36,109 @@ static int show_content = 1; static char *username = NULL; static char *password = NULL; -static void show_response(hresponse_t *res) { - - unsigned char buffer[MAX_BUFFER_SIZE+1]; - int read; - - if (!res) { - - printf("hresponse_t is NULL!"); - return; - } - - if (res->errcode != 200 || show_http_status_code) - printf("HTTP Status: %d \"%s\"\n", res->errcode, res->desc); - - 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); - } - - if (!res->in) { - - log_warn1("No input stream!"); - return; - } - - while (http_input_stream_is_ready(res->in)) { - - read = http_input_stream_read(res->in, buffer, MAX_BUFFER_SIZE); - - if (show_content) - fwrite(buffer, read, 1, stdout); - } - return; +static void show_response(hresponse_t *res) +{ + unsigned char buffer[MAX_BUFFER_SIZE+1]; + int read; + + if (!res) + { + fprintf(stderr, "hresponse_t is NULL!"); + return; + } + + if (res->errcode != 200 || show_http_status_code) + printf("HTTP Status: %d \"%s\"\n", res->errcode, res->desc); + + 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); + } + + if (!res->in) + { + fprintf(stderr, "No input stream!"); + return; + } + + while (http_input_stream_is_ready(res->in)) + { + read = http_input_stream_read(res->in, buffer, MAX_BUFFER_SIZE); + + if (show_content) + fwrite(buffer, read, 1, stdout); + } + return; } -int main(int argc, char **argv) { - - httpc_conn_t *conn; /* Client connection object */ - hresponse_t *res; /* Response object **/ - herror_t status; - int i; - - /* check usage */ - if (argc < 2) { - - fprintf(stderr, "usage: %s [-headers] [-status] [-noout] [-username name] [-password secret] \n", argv[0]); - exit(1); - } - - /* XXX: this is not safe... */ - for (i=0; i\n", argv[0]); + exit(1); + } + + /* XXX: this is not safe... */ + for (i=0; i #include -#include -#include -#include -#include -#include #include -#include static int simple_authenticator(struct hrequest_t *req, const char *user, const char *password) { + fprintf(stdout, "logging in user=\"%s\" password=\"%s\"", user, password); - log_info3("logging in user=\"%s\" password=\"%s\"", user, password); + if (strcmp(user, "bob")) { - if (strcmp(user, "bob")) { + fprintf(stderr, "user \"%s\" unkown", user); + return 0; + } - log_warn2("user \"%s\" unkown", user); - return 0; - } + if (strcmp(password, "builder")) { - if (strcmp(password, "builder")) { + fprintf(stderr, "wrong password"); + return 0; + } - log_warn1("wrong password"); - return 0; - } - - return 1; + return 1; } static void secure_service(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, - "" - "" - "Secure ressource!" - "" - "" - "

Authenticated access!!!

" - "" - ""); - - return; + httpd_send_header(conn, 200, HTTP_STATUS_200_REASON_PHRASE); + http_output_stream_write_string(conn->out, + "" + "" + "Secure ressource!" + "" + "" + "

Authenticated access!!!

" + "" + ""); + + return; } static void default_service(httpd_conn_t *conn, struct hrequest_t *req) { - - httpd_send_header(conn, 404, HTTP_STATUS_404_REASON_PHRASE); - http_output_stream_write_string(conn->out, - "" - "" - "Default error page" - "" - "" - "

Default error page

" - "
"); - - http_output_stream_write_string(conn->out, req->path); - - http_output_stream_write_string(conn->out, " can not be found" - "
" - "" - ""); - - return; -} + httpd_send_header(conn, 404, HTTP_STATUS_404_REASON_PHRASE); + http_output_stream_write_string(conn->out, + "" + "" + "Default error page" + "" + "" + "

Default error page

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

Request headers

" - "
    "); - - for (walker=req->header; walker; walker=walker->next) - { - http_output_stream_write_string(conn->out, "
  • "); - http_output_stream_write_string(conn->out, walker->key); - http_output_stream_write_string(conn->out, " = "); - http_output_stream_write_string(conn->out, walker->value); - http_output_stream_write_string(conn->out, "
  • "); - } - - http_output_stream_write_string(conn->out, - "
" - "" - ""); - - return; + hpair_t *walker; + + httpd_send_header(conn, 200, HTTP_STATUS_200_REASON_PHRASE); + http_output_stream_write_string(conn->out, + "" + "" + "Request headers" + "" + "" + "

Request headers

" + "
    "); + + for (walker=req->header; walker; walker=walker->next) + { + http_output_stream_write_string(conn->out, "
  • "); + http_output_stream_write_string(conn->out, walker->key); + http_output_stream_write_string(conn->out, " = "); + http_output_stream_write_string(conn->out, walker->value); + http_output_stream_write_string(conn->out, "
  • "); + } + + http_output_stream_write_string(conn->out, + "
" + "" + ""); + + return; } static void root_service(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, - "" - "" - "nanoHTTP server examples" - "" - "" - "

nanoHTTP server examples

" - "" - "" - ""); - - return; + httpd_send_header(conn, 200, HTTP_STATUS_200_REASON_PHRASE); + http_output_stream_write_string(conn->out, + "" + "" + "nanoHTTP server examples" + "" + "" + "

nanoHTTP server examples

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