mongoose/examples/upload.c

62 lines
1.7 KiB
C
Raw Normal View History

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 <fcntl.h>
#include <stdlib.h>
2012-09-25 04:25:06 +08:00
#ifdef _WIN32
#include <windows.h>
#include <io.h>
#define strtoll strtol
typedef __int64 int64_t;
#else
2012-09-14 17:23:51 +08:00
#include <inttypes.h>
#include <unistd.h>
2012-09-25 04:25:06 +08:00
#endif // !_WIN32
2012-09-14 17:23:51 +08:00
#include "mongoose.h"
2013-02-02 00:48:30 +08:00
static int begin_request_handler(struct mg_connection *conn) {
if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) {
mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n");
mg_upload(conn, "/tmp");
} else {
// Show HTML form. Make sure it has enctype="multipart/form-data" attr.
static const char *html_form =
"<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></body></html>";
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
"Content-Length: %d\r\n"
"Content-Type: text/html\r\n\r\n%s",
(int) strlen(html_form), html_form);
2012-09-14 17:23:51 +08:00
}
2012-12-07 09:50:47 +08:00
2013-02-02 00:48:30 +08:00
// Mark request as processed
return 1;
}
static void upload_handler(struct mg_connection *conn, const char *path) {
mg_printf(conn, "Saved [%s]", path);
2012-09-14 17:23:51 +08:00
}
int main(void) {
struct mg_context *ctx;
const char *options[] = {"listening_ports", "8080", NULL};
2013-02-02 00:48:30 +08:00
struct mg_callbacks callbacks;
2012-09-14 17:23:51 +08:00
2013-02-02 00:48:30 +08:00
memset(&callbacks, 0, sizeof(callbacks));
callbacks.begin_request = begin_request_handler;
callbacks.upload = upload_handler;
ctx = mg_start(&callbacks, NULL, options);
2012-09-14 17:23:51 +08:00
getchar(); // Wait until user hits "enter"
mg_stop(ctx);
return 0;
}