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);
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
00018
00019
00020 rewind(fp);
00021 mg_send_file_data(conn, fileno(fp));
00022 return MG_MORE;
00023 } else {
00024 mg_printf_data(conn, "%s", "Had no data to write...");
00025 return MG_TRUE;
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;
00036 }
00037 }
00038
00039
00040
00041 static int handle_recv(struct mg_connection *conn) {
00042 FILE *fp = (FILE *) conn->connection_param;
00043
00044
00045 if (fp == NULL && ((conn->connection_param = fp = tmpfile())) == NULL) {
00046 return -1;
00047 }
00048
00049
00050
00051 return fwrite(conn->content, 1, conn->content_len, fp);
00052 }
00053
00054
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 }