diff --git a/docs/Embed.md b/docs/Embed.md index ec79f9df..7a102b64 100644 --- a/docs/Embed.md +++ b/docs/Embed.md @@ -90,6 +90,7 @@ a couple of kilobytes to the executable size, and also has some runtime penalty. Mongoose source code contains a well-commented example code, listed below: * [hello.c](https://github.com/cesanta/mongoose/blob/master/examples/hello.c) - is a minimalisting hello world example + is a minimalistic hello world example * [post.c](https://github.com/cesanta/mongoose/blob/master/examples/post.c) - shows how to handle form input + * [upload.c](https://github.com/cesanta/mongoose/blob/master/examples/post.c) + shows how to upload files diff --git a/examples/Makefile b/examples/Makefile index 94695542..562d4793 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -23,8 +23,8 @@ all: $(CC) qcomm.c ../mongoose.c -o qcomm $(CFLAGS) $(CC) post.c ../mongoose.c -o post $(CFLAGS) $(CC) multi_threaded.c ../mongoose.c -o multi_threaded $(CFLAGS) + $(CC) upload.c ../mongoose.c -o upload $(CFLAGS) -# $(CC) upload.c ../mongoose.c -o upload $(CFLAGS) # $(CC) -DUSE_WEBSOCKET websocket.c ../mongoose.c -o $@ $(CFLAGS) # $(CC) chat.c ../mongoose.c -o chat $(CFLAGS) # $(CC) lua_dll.c ../build/lua_5.2.1.c -o $@.so $(CFLAGS) $(DLL_FLAGS) @@ -40,7 +40,8 @@ windows: $(CL) hello.c ../mongoose.c $(CLFLAGS) $(LFLAGS) $(CL) post.c ../mongoose.c $(CLFLAGS) $(LFLAGS) $(CL) multi_threaded.c ../mongoose.c $(CLFLAGS) $(LFLAGS) -# $(CL) upload.c ../mongoose.c $(CLFLAGS) $(LFLAGS) + $(CL) upload.c ../mongoose.c $(CLFLAGS) $(LFLAGS) + # $(CL) /DUSE_WEBSOCKET websocket.c ../mongoose.c $(CLFLAGS) $(LFLAGS) #$(CL) lua_dll.c $(CLFLAGS) $(DLL_FLAGS) /DLL $(LFLAGS) /SUBSYSTEM:WINDOWS /ENTRY:luaopen_lua_dll /EXPORT:luaopen_lua_dll /out:lua_dll.dll diff --git a/examples/upload.c b/examples/upload.c index 0ce0fac7..b8369395 100644 --- a/examples/upload.c +++ b/examples/upload.c @@ -5,48 +5,50 @@ #include #include "mongoose.h" -static int event_handler(struct mg_event *event) { +static int index_html(struct mg_connection *conn) { + const char *data; + int data_len; + char var_name[100], file_name[100]; - if (event->type == MG_REQUEST_BEGIN) { - if (!strcmp(event->request_info->uri, "/handle_post_request")) { - char path[200]; - FILE *fp = mg_upload(event->conn, "/tmp", path, sizeof(path)); - if (fp != NULL) { - fclose(fp); - mg_printf(event->conn, "HTTP/1.0 200 OK\r\n\r\nSaved: [%s]", path); - } else { - mg_printf(event->conn, "%s", "HTTP/1.0 200 OK\r\n\r\nNo files sent"); - } - } else { - // Show HTML form. Make sure it has enctype="multipart/form-data" attr. - static const char *html_form = - "Upload example." - "
" - "
" - "" - "
"; + mg_printf_data(conn, "%s", + "Upload example." + "
" + "
" + "" + "
"); - mg_printf(event->conn, "HTTP/1.0 200 OK\r\n" - "Content-Length: %d\r\n" - "Content-Type: text/html\r\n\r\n%s", - (int) strlen(html_form), html_form); - } + if (mg_parse_multipart(conn->content, conn->content_len, + var_name, sizeof(var_name), + file_name, sizeof(file_name), + &data, &data_len) > 0) { - // Mark request as processed - return 1; + mg_printf_data(conn, "%s", "Uploaded file:
");
+    mg_send_data(conn, data, data_len);
+    mg_printf_data(conn, "%s", "/pre>");
   }
 
-  // All other events left unprocessed
+  mg_printf_data(conn, "%s", "");
+
   return 1;
 }
 
 int main(void) {
-  struct mg_context *ctx;
-  const char *options[] = {"listening_ports", "8080", NULL};
-  ctx = mg_start(options, event_handler, NULL);
-  getchar();  // Wait until user hits "enter"
-  mg_stop(ctx);
+  struct mg_server *server;
+
+  // Create and configure the server
+  server = mg_create_server(NULL);
+  mg_set_option(server, "listening_port", "8080");
+  mg_add_uri_handler(server, "/", index_html);
+
+  // Serve request. Hit Ctrl-C to terminate the program
+  printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
+  for (;;) {
+    mg_poll_server(server, 1000);
+  }
+
+  // Cleanup, and free server instance
+  mg_destroy_server(&server);
 
   return 0;
 }