From ce0e0469d093be89de52b3ebde2673e5a42ee74f Mon Sep 17 00:00:00 2001 From: m0gg Date: Sun, 4 Nov 2007 07:37:38 +0000 Subject: Added POST example --- examples/nanohttp/http_client.c | 10 +++--- examples/nanohttp/http_server.c | 71 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/nanohttp/http_client.c b/examples/nanohttp/http_client.c index adcd199..e2b2654 100644 --- a/examples/nanohttp/http_client.c +++ b/examples/nanohttp/http_client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: http_client.c,v 1.10 2007/11/03 22:40:09 m0gg Exp $ +* $Id: http_client.c,v 1.11 2007/11/04 07:37:38 m0gg Exp $ * * CSOAP Project: A http client/server library in C (example) * Copyright (C) 2003-2004 Ferhat Ayaz @@ -78,9 +78,9 @@ static void show_response(hresponse_t *res) int main(int argc, char **argv) { - httpc_conn_t *conn; /* Client connection object */ - hresponse_t *res; /* Response object **/ - herror_t status; + httpc_conn_t *conn; /* Client connection */ + hresponse_t *res; /* HTTP response */ + herror_t status; /* Function return value */ int i; /* check usage */ @@ -90,7 +90,7 @@ int main(int argc, char **argv) exit(1); } - /* XXX: this is not safe... */ + /* XXX: argv[i+1] is not safe (userame/password) */ for (i=0; i" "" ""); - - return; } static void @@ -114,6 +112,53 @@ mime_service(httpd_conn_t *conn, struct hrequest_t *req) httpd_send_not_implemented(conn, "mime_service"); } +static void +post_service(httpd_conn_t *conn, struct hrequest_t *req) +{ + if (req->method == HTTP_REQUEST_POST) + { + unsigned char buffer[1024]; + long len, total; + + httpd_send_header(conn, 200, HTTP_STATUS_200_REASON_PHRASE); + http_output_stream_write_string(conn->out, + "" + "" + "POST service" + "" + "" + "

You posted

" + "
");
+
+    if (req->content_type && req->content_type->type)
+    {
+      len = sprintf(buffer, "

Content-Type: %s

", req->content_type->type); + http_output_stream_write(conn->out, buffer, len); + } + + while (http_input_stream_is_ready(req->in)) + { + len = http_input_stream_read(req->in, buffer, 1024); + http_output_stream_write(conn->out, buffer, len); + total += len; + } + + http_output_stream_write_string(conn->out, + "
"); + + len = sprintf(buffer, "

Received %li bytes

", total); + http_output_stream_write(conn->out, buffer, len); + + http_output_stream_write_string(conn->out, + "" + ""); + } + else + { + httpd_send_not_implemented(conn, "post_service"); + } +} + static void root_service(httpd_conn_t *conn, struct hrequest_t *req) { @@ -130,13 +175,21 @@ root_service(httpd_conn_t *conn, struct hrequest_t *req) "
  • Secure service (try: bob/builder)
  • " "
  • Request headers
  • " "
  • MIME service
  • " + "
  • " + "
    " + "
    " + "Upload file" + " " + "" + "
    " + "
    " + "
  • " "
  • The default service
  • " "
  • Admin page (try -NHTTPDadmin on the command line)
  • " "" "" ""); - - return; } int @@ -184,6 +237,14 @@ main(int argc, char **argv) exit(1); } + if ((status = httpd_register("/post", post_service)) != H_OK) + { + fprintf(stderr, "Cannot register POST service (%s)\n", herror_message(status)); + herror_release(status); + httpd_destroy(); + exit(1); + } + if ((status = httpd_register_default("/error", default_service)) != H_OK) { fprintf(stderr, "Cannot register default service (%s)\n", herror_message(status)); -- cgit v1.1-32-gdbae