Statistics
| Branch: | Tag: | Revision:

mongoose / examples / big_upload / big_upload.c @ eaef5bd1

History | View | Annotate | Download (3.1 KB)

1
// Copyright (c) 2015 Cesanta Software Limited
2
// All rights reserved
3
//
4
// This example demonstrates how to handle very large requests without keeping
5
// them in memory.
6

    
7
#include <stdio.h>
8
#include <string.h>
9
#include <stdlib.h>
10
#include "mongoose.h"
11

    
12
static const char *s_http_port = "8000";
13

    
14
struct file_writer_data {
15
  FILE *fp;
16
  size_t bytes_written;
17
};
18

    
19
static void handle_request(struct mg_connection *nc) {
20
  // This handler gets for all endpoints but /upload
21
  mg_printf(nc, "%s",
22
            "HTTP/1.1 200 OK\r\n"
23
            "Content-Type: text/html\r\n"
24
            "Connection: close\r\n"
25
            "\r\n"
26
            "<html><body>Upload example."
27
            "<form method=\"POST\" action=\"/upload\" "
28
            "  enctype=\"multipart/form-data\">"
29
            "<input type=\"file\" name=\"file\" /> <br/>"
30
            "<input type=\"submit\" value=\"Upload\" />"
31
            "</form></body></html>");
32
  nc->flags |= MG_F_SEND_AND_CLOSE;
33
}
34

    
35
static void handle_upload(struct mg_connection *nc, int ev, void *p) {
36
  struct file_writer_data *data = (struct file_writer_data *) nc->user_data;
37
  struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p;
38

    
39
  switch (ev) {
40
    case MG_EV_HTTP_PART_BEGIN: {
41
      if (data == NULL) {
42
        data = calloc(1, sizeof(struct file_writer_data));
43
        data->fp = tmpfile();
44
        data->bytes_written = 0;
45

    
46
        if (data->fp == NULL) {
47
          mg_printf(nc, "%s",
48
                    "HTTP/1.1 500 Failed to open a file\r\n"
49
                    "Content-Length: 0\r\n\r\n");
50
          nc->flags |= MG_F_SEND_AND_CLOSE;
51
          return;
52
        }
53
        nc->user_data = (void *) data;
54
      }
55
      break;
56
    }
57
    case MG_EV_HTTP_PART_DATA: {
58
      if (fwrite(mp->data.p, 1, mp->data.len, data->fp) != mp->data.len) {
59
        mg_printf(nc, "%s",
60
                  "HTTP/1.1 500 Failed to write to a file\r\n"
61
                  "Content-Length: 0\r\n\r\n");
62
        nc->flags |= MG_F_SEND_AND_CLOSE;
63
        return;
64
      }
65
      data->bytes_written += mp->data.len;
66
      break;
67
    }
68
    case MG_EV_HTTP_PART_END: {
69
      mg_printf(nc,
70
                "HTTP/1.1 200 OK\r\n"
71
                "Content-Type: text/plain\r\n"
72
                "Connection: close\r\n\r\n"
73
                "Written %ld of POST data to a temp file\n\n",
74
                (long) ftell(data->fp));
75
      nc->flags |= MG_F_SEND_AND_CLOSE;
76
      fclose(data->fp);
77
      free(data);
78
      nc->user_data = NULL;
79
      break;
80
    }
81
  }
82
}
83

    
84
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
85
  (void) ev_data;
86
  switch (ev) {
87
    case MG_EV_HTTP_REQUEST:
88
      // Invoked when the full HTTP request is in the buffer (including body).
89
      handle_request(nc);
90
      break;
91
  }
92
}
93

    
94
int main(void) {
95
  struct mg_mgr mgr;
96
  struct mg_connection *nc;
97

    
98
  mg_mgr_init(&mgr, NULL);
99
  nc = mg_bind(&mgr, s_http_port, ev_handler);
100

    
101
  mg_register_http_endpoint(nc, "/upload", handle_upload);
102
  // Set up HTTP server parameters
103
  mg_set_protocol_http_websocket(nc);
104

    
105
  printf("Starting web server on port %s\n", s_http_port);
106
  for (;;) {
107
    mg_mgr_poll(&mgr, 1000);
108
  }
109
  mg_mgr_free(&mgr);
110

    
111
  return 0;
112
}