big_upload.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "mongoose.h"
5 
6 static int handle_request(struct mg_connection *conn) {
7  if (strcmp(conn->uri, "/upload") == 0) {
8  FILE *fp = (FILE *) conn->connection_param;
9  if (fp != NULL) {
10  fwrite(conn->content, 1, conn->content_len, fp); // Write last bits
11  mg_printf(conn, "HTTP/1.1 200 OK\r\n"
12  "Content-Type: text/plain\r\n"
13  "Connection: close\r\n\r\n"
14  "Written %ld of POST data to a temp file:\n\n",
15  (long) ftell(fp));
16 
17  // Temp file will be destroyed after fclose(), do something with the
18  // data here -- for example, parse it and extract uploaded files.
19  // As an example, we just echo the whole POST buffer back to the client.
20  rewind(fp);
21  mg_send_file_data(conn, fileno(fp));
22  return MG_MORE; // Tell Mongoose reply is not completed yet
23  } else {
24  mg_printf_data(conn, "%s", "Had no data to write...");
25  return MG_TRUE; // Tell Mongoose we're done with this request
26  }
27  } else {
28  mg_printf_data(conn, "%s",
29  "<html><body>Upload example."
30  "<form method=\"POST\" action=\"/upload\" "
31  " enctype=\"multipart/form-data\">"
32  "<input type=\"file\" name=\"file\" /> <br/>"
33  "<input type=\"submit\" value=\"Upload\" />"
34  "</form></body></html>");
35  return MG_TRUE; // Tell mongoose to close this connection
36  }
37 }
38 
39 // Mongoose sends MG_RECV for every received POST chunk.
40 // When last POST chunk is received, Mongoose sends MG_REQUEST, then MG_CLOSE.
41 static int handle_recv(struct mg_connection *conn) {
42  FILE *fp = (FILE *) conn->connection_param;
43 
44  // Open temporary file where we going to write data
45  if (fp == NULL && ((conn->connection_param = fp = tmpfile())) == NULL) {
46  return -1; // Close connection on error
47  }
48 
49  // Return number of bytes written to a temporary file: that is how many
50  // bytes we want to discard from the receive buffer
51  return fwrite(conn->content, 1, conn->content_len, fp);
52 }
53 
54 // Make sure we free all allocated resources
55 static int handle_close(struct mg_connection *conn) {
56  if (conn->connection_param != NULL) {
57  fclose((FILE *) conn->connection_param);
58  conn->connection_param = NULL;
59  }
60  return MG_TRUE;
61 }
62 
63 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
64  switch (ev) {
65  case MG_AUTH: return MG_TRUE;
66  case MG_REQUEST: return handle_request(conn);
67  case MG_RECV: return handle_recv(conn);
68  case MG_CLOSE: return handle_close(conn);
69  default: return MG_FALSE;
70  }
71 }
72 
73 int main(void) {
75  mg_set_option(server, "listening_port", "8080");
76  printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
77 
78  for (;;) {
79  mg_poll_server(server, 1000);
80  }
81 
82  mg_destroy_server(&server);
83  return 0;
84 }
void * connection_param
Definition: mongoose.h:56
const char * uri
Definition: mongoose.h:34
static int handle_request(struct mg_connection *conn)
Definition: big_upload.c:6
struct mg_server * mg_create_server(void *server_data, mg_handler_t handler)
Definition: mongoose.c:5431
const char * mg_get_option(const struct mg_server *server, const char *name)
Definition: mongoose.c:5425
if(strcmp(arg,"1305")!=0)
Definition: unit1305.c:127
time_t mg_poll_server(struct mg_server *server, int milliseconds)
Definition: mongoose.c:4965
size_t mg_printf_data(struct mg_connection *c, const char *fmt,...)
Definition: mongoose.c:2785
#define printf
Definition: curl_printf.h:40
mg_event
Definition: mongoose.h:62
static struct mg_server * server
Definition: web_server.c:72
static int ev_handler(struct mg_connection *conn, enum mg_event ev)
Definition: big_upload.c:63
static int handle_close(struct mg_connection *conn)
Definition: big_upload.c:55
int main(void)
Definition: big_upload.c:73
const char * mg_set_option(struct mg_server *server, const char *name, const char *value)
Definition: mongoose.c:5143
void mg_send_file_data(struct mg_connection *c, int fd)
Definition: mongoose.c:3393
size_t mg_printf(struct mg_connection *conn, const char *fmt,...)
Definition: mongoose.c:1949
int fileno(FILE *stream)
void mg_destroy_server(struct mg_server **server)
Definition: mongoose.c:4969
size_t fwrite(const void *, size_t, size_t, FILE *)
static int handle_recv(struct mg_connection *conn)
Definition: big_upload.c:41
size_t content_len
Definition: mongoose.h:50
char * content
Definition: mongoose.h:49


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:08