summaryrefslogtreecommitdiffstats
path: root/examples/nanohttp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/nanohttp')
-rw-r--r--examples/nanohttp/http_client.c10
-rw-r--r--examples/nanohttp/http_server.c71
2 files changed, 71 insertions, 10 deletions
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<argc; i++)
{
if (!strcmp("-headers", argv[i]))
diff --git a/examples/nanohttp/http_server.c b/examples/nanohttp/http_server.c
index 5daad9a..6baa52e 100644
--- a/examples/nanohttp/http_server.c
+++ b/examples/nanohttp/http_server.c
@@ -1,5 +1,5 @@
/******************************************************************
-* $Id: http_server.c,v 1.15 2007/11/03 22:40:09 m0gg Exp $
+* $Id: http_server.c,v 1.16 2007/11/04 07:37:38 m0gg Exp $
*
* CSOAP Project: A http client/server library in C (example)
* Copyright (C) 2003 Ferhat Ayaz
@@ -104,8 +104,6 @@ headers_service(httpd_conn_t *conn, struct hrequest_t *req)
"</ul>"
"</body>"
"</html>");
-
- return;
}
static void
@@ -115,6 +113,53 @@ mime_service(httpd_conn_t *conn, struct hrequest_t *req)
}
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,
+ "<html>"
+ "<head>"
+ "<title>POST service</title>"
+ "</head>"
+ "<body>"
+ "<h1>You posted</h1>"
+ "<pre>");
+
+ if (req->content_type && req->content_type->type)
+ {
+ len = sprintf(buffer, "<p>Content-Type: %s</p>", 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,
+ "</pre>");
+
+ len = sprintf(buffer, "<p>Received %li bytes</p>", total);
+ http_output_stream_write(conn->out, buffer, len);
+
+ http_output_stream_write_string(conn->out,
+ "</body>"
+ "</html>");
+ }
+ else
+ {
+ httpd_send_not_implemented(conn, "post_service");
+ }
+}
+
+static void
root_service(httpd_conn_t *conn, struct hrequest_t *req)
{
httpd_send_header(conn, 200, HTTP_STATUS_200_REASON_PHRASE);
@@ -130,13 +175,21 @@ root_service(httpd_conn_t *conn, struct hrequest_t *req)
"<li><a href=\"/secure\">Secure service</a> (try: bob/builder)</li>"
"<li><a href=\"/headers\">Request headers</a></li>"
"<li><a href=\"/mime\">MIME service</a></li>"
+ "<li>"
+ "<form action=\"/post\" method=\"post\" enctype=\"multipart/form-data\">"
+ "<fieldset>"
+ "<legend>Upload file</legend>"
+ "<input name=\"Text\" type=\"text\" value=\"test-field\"? "
+ "<input name=\"File\" type=\"file\" accept=\"text/*\"> "
+ "<input value=\"Submit\" type=\"submit\">"
+ "</fieldset>"
+ "</form>"
+ "</li>"
"<li><a href=\"/not_existent\">The default service</a></li>"
"<li><a href=\"/nhttp\">Admin page</a> (try -NHTTPDadmin on the command line)</li>"
"</ul>"
"</body>"
"</html>");
-
- 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));