From 72bff5390ac5312bcca09c95eaf12a777b25c057 Mon Sep 17 00:00:00 2001 From: bel Date: Mon, 8 Dec 2014 22:31:58 +0100 Subject: [PATCH] Improve documentation of example --- examples/upload/upload.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/examples/upload/upload.c b/examples/upload/upload.c index e4cb3538..8cd9fd64 100644 --- a/examples/upload/upload.c +++ b/examples/upload/upload.c @@ -1,5 +1,7 @@ -// Copyright (c) 2004-2012 Sergey Lyubka -// This file is a part of civetweb project, http://github.com/bel2125/civetweb +/* Copyright (c) 2014 the Civetweb developers + * Copyright (c) 2004-2012 Sergey Lyubka + * This file is a part of civetweb project, http://github.com/bel2125/civetweb + */ #include #include @@ -17,20 +19,27 @@ typedef __int64 int64_t; #include "civetweb.h" + +/* callback: used to generate all content */ static int begin_request_handler(struct mg_connection *conn) { if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) { mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n"); mg_upload(conn, "/tmp"); } else { - // Show HTML form. Make sure it has enctype="multipart/form-data" attr. + /* Show HTML form. */ + /* See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1 */ static const char *html_form = "Upload example." + "" + /* enctype="multipart/form-data" */ "
" "
" "" - "
"; + "" + "" + ""; mg_printf(conn, "HTTP/1.0 200 OK\r\n" "Content-Length: %d\r\n" @@ -42,22 +51,40 @@ static int begin_request_handler(struct mg_connection *conn) return 1; } + +/* callback: called after uploading a file is completed */ static void upload_handler(struct mg_connection *conn, const char *path) { mg_printf(conn, "Saved [%s]", path); } + +/* Main program: Set callbacks and start the server. */ int main(void) { + /* Test server will use this port */ + const char * PORT = "8080"; + + /* Startup options for the server */ struct mg_context *ctx; - const char *options[] = {"listening_ports", "8080", NULL}; + const char *options[] = { + "listening_ports", PORT, + NULL}; struct mg_callbacks callbacks; memset(&callbacks, 0, sizeof(callbacks)); callbacks.begin_request = begin_request_handler; callbacks.upload = upload_handler; + + /* Display a welcome message */ + printf("File upload demo.\n"); + printf("Open http://localhost:%s/ im your browser.\n\n", PORT); + + /* Start the server */ ctx = mg_start(&callbacks, NULL, options); - getchar(); // Wait until user hits "enter" + + /* Wait until thr user hits "enter", then stop the server */ + getchar(); mg_stop(ctx); return 0;