big_upload.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004 #include "mongoose.h"
00005 
00006 static int handle_request(struct mg_connection *conn) {
00007   if (strcmp(conn->uri, "/upload") == 0) {
00008     FILE *fp = (FILE *) conn->connection_param;
00009     if (fp != NULL) {
00010       fwrite(conn->content, 1, conn->content_len, fp); // Write last bits
00011       mg_printf(conn, "HTTP/1.1 200 OK\r\n"
00012                 "Content-Type: text/plain\r\n"
00013                 "Connection: close\r\n\r\n"
00014                 "Written %ld of POST data to a temp file:\n\n",
00015                 (long) ftell(fp));
00016 
00017       // Temp file will be destroyed after fclose(), do something with the
00018       // data here -- for example, parse it and extract uploaded files.
00019       // As an example, we just echo the whole POST buffer back to the client.
00020       rewind(fp);
00021       mg_send_file_data(conn, fileno(fp));
00022       return MG_MORE;  // Tell Mongoose reply is not completed yet
00023     } else {
00024       mg_printf_data(conn, "%s", "Had no data to write...");
00025       return MG_TRUE; // Tell Mongoose we're done with this request
00026     }
00027   } else {
00028     mg_printf_data(conn, "%s",
00029                    "<html><body>Upload example."
00030                    "<form method=\"POST\" action=\"/upload\" "
00031                    "  enctype=\"multipart/form-data\">"
00032                    "<input type=\"file\" name=\"file\" /> <br/>"
00033                    "<input type=\"submit\" value=\"Upload\" />"
00034                    "</form></body></html>");
00035     return MG_TRUE;   // Tell mongoose to close this connection
00036   }
00037 }
00038 
00039 // Mongoose sends MG_RECV for every received POST chunk.
00040 // When last POST chunk is received, Mongoose sends MG_REQUEST, then MG_CLOSE.
00041 static int handle_recv(struct mg_connection *conn) {
00042   FILE *fp = (FILE *) conn->connection_param;
00043 
00044   // Open temporary file where we going to write data
00045   if (fp == NULL && ((conn->connection_param = fp = tmpfile())) == NULL) {
00046     return -1;  // Close connection on error
00047   }
00048 
00049   // Return number of bytes written to a temporary file: that is how many
00050   // bytes we want to discard from the receive buffer
00051   return fwrite(conn->content, 1, conn->content_len, fp);
00052 }
00053 
00054 // Make sure we free all allocated resources
00055 static int handle_close(struct mg_connection *conn) {
00056   if (conn->connection_param != NULL) {
00057     fclose((FILE *) conn->connection_param);
00058     conn->connection_param = NULL;
00059   }
00060   return MG_TRUE;
00061 }
00062 
00063 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
00064   switch (ev) {
00065     case MG_AUTH:     return MG_TRUE;
00066     case MG_REQUEST:  return handle_request(conn);
00067     case MG_RECV:     return handle_recv(conn);
00068     case MG_CLOSE:    return handle_close(conn);
00069     default:          return MG_FALSE;
00070   }
00071 }
00072 
00073 int main(void) {
00074   struct mg_server *server = mg_create_server(NULL, ev_handler);
00075   mg_set_option(server, "listening_port", "8080");
00076   printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
00077 
00078   for (;;) {
00079     mg_poll_server(server, 1000);
00080   }
00081 
00082   mg_destroy_server(&server);
00083   return 0;
00084 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:01