2012-09-14 17:23:51 +08:00
|
|
|
// Copyright (c) 2004-2012 Sergey Lyubka
|
|
|
|
// This file is a part of mongoose project, http://github.com/valenok/mongoose
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
2014-06-24 23:35:59 +08:00
|
|
|
static int send_index_page(struct mg_connection *conn) {
|
2014-01-14 01:01:11 +08:00
|
|
|
const char *data;
|
2014-06-24 23:35:59 +08:00
|
|
|
int data_len, ofs = 0;
|
2014-01-14 01:01:11 +08:00
|
|
|
char var_name[100], file_name[100];
|
|
|
|
|
|
|
|
mg_printf_data(conn, "%s",
|
|
|
|
"<html><body>Upload example."
|
|
|
|
"<form method=\"POST\" action=\"/handle_post_request\" "
|
|
|
|
" enctype=\"multipart/form-data\">"
|
|
|
|
"<input type=\"file\" name=\"file\" /> <br/>"
|
|
|
|
"<input type=\"submit\" value=\"Upload\" />"
|
|
|
|
"</form>");
|
|
|
|
|
2014-06-24 23:35:59 +08:00
|
|
|
while ((ofs = mg_parse_multipart(conn->content + ofs, conn->content_len - ofs,
|
|
|
|
var_name, sizeof(var_name),
|
|
|
|
file_name, sizeof(file_name),
|
|
|
|
&data, &data_len)) > 0) {
|
|
|
|
mg_printf_data(conn, "var: %s, file_name: %s, size: %d bytes<br>",
|
|
|
|
var_name, file_name, data_len);
|
2012-09-14 17:23:51 +08:00
|
|
|
}
|
2012-12-07 09:50:47 +08:00
|
|
|
|
2014-01-14 01:01:11 +08:00
|
|
|
mg_printf_data(conn, "%s", "</body></html>");
|
2014-06-24 23:35:59 +08:00
|
|
|
|
|
|
|
return MG_TRUE;
|
2014-03-02 20:16:09 +08:00
|
|
|
}
|
2014-01-14 01:01:11 +08:00
|
|
|
|
2014-03-02 20:16:09 +08:00
|
|
|
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
|
2014-06-24 23:35:59 +08:00
|
|
|
switch (ev) {
|
|
|
|
case MG_AUTH: return MG_TRUE;
|
|
|
|
case MG_REQUEST: return send_index_page(conn);
|
|
|
|
default: return MG_FALSE;
|
2014-03-02 20:16:09 +08:00
|
|
|
}
|
2013-02-02 00:48:30 +08:00
|
|
|
}
|
|
|
|
|
2012-09-14 17:23:51 +08:00
|
|
|
int main(void) {
|
2014-01-14 01:01:11 +08:00
|
|
|
struct mg_server *server;
|
|
|
|
|
|
|
|
// Create and configure the server
|
2014-03-02 20:16:09 +08:00
|
|
|
server = mg_create_server(NULL, ev_handler);
|
2014-01-14 01:01:11 +08:00
|
|
|
mg_set_option(server, "listening_port", "8080");
|
|
|
|
|
|
|
|
// 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);
|
2012-09-14 17:23:51 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|