mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-19 11:09:42 +00:00
Amended file sending example
This commit is contained in:
1 parent
f51ca8d5d1
commit
464c77314d
2 files changed
+14
-36
No files matched your search
+4
-1
@@ -50,6 +50,9 @@ upload: upload.c ../mongoose.c
|
||||
auth: auth.c ../mongoose.c
|
||||
$(CC) auth.c ../mongoose.c $(OUT) $(CFLAGS)
|
||||
|
||||
file: file.c ../mongoose.c
|
||||
$(CC) file.c ../mongoose.c $(OUT) $(CFLAGS)
|
||||
|
||||
form: form.c ../mongoose.c
|
||||
$(CC) form.c ../mongoose.c $(OUT) $(CFLAGS)
|
||||
|
||||
@@ -68,4 +71,4 @@ u:
|
||||
./$@
|
||||
|
||||
clean:
|
||||
-@$(RM) hello mjpg upload post websocket auth server multi_threaded proxy websocket_html.c *.exe *.dSYM *.obj .*o u a.out
|
||||
-@$(RM) hello mjpg upload post websocket auth file server multi_threaded proxy websocket_html.c *.exe *.dSYM *.obj .*o u a.out
|
||||
+10
-35
@@ -1,47 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "mongoose.h"
|
||||
|
||||
|
||||
static int index_html(struct mg_connection *conn) {
|
||||
FILE *fp = (FILE *) conn->connection_param;
|
||||
char buf[200];
|
||||
size_t n = 0;
|
||||
|
||||
if (fp == NULL) {
|
||||
fp = fopen("../mongoose.c", "r");
|
||||
conn->connection_param = (void *) fp;
|
||||
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
|
||||
switch (ev) {
|
||||
case MG_REQUEST:
|
||||
mg_send_file(conn, "file.c");
|
||||
return MG_MORE; // It is important to return MG_MORE after mg_send_file!
|
||||
case MG_AUTH: return MG_TRUE;
|
||||
default: return MG_FALSE;
|
||||
}
|
||||
|
||||
if (fp != NULL) {
|
||||
n = fread(buf, 1, sizeof(buf), fp);
|
||||
mg_send_data(conn, buf, n);
|
||||
|
||||
if (n < sizeof(buf) || conn->wsbits != 0) {
|
||||
fclose(fp);
|
||||
conn->connection_param = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return n < sizeof(buf) ? MG_REQUEST_PROCESSED : MG_REQUEST_CALL_AGAIN;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_server *server;
|
||||
|
||||
// Create and configure the server
|
||||
server = mg_create_server(NULL);
|
||||
struct mg_server *server = mg_create_server(NULL, ev_handler);
|
||||
mg_set_option(server, "listening_port", "8080");
|
||||
mg_set_request_handler(server, index_html);
|
||||
|
||||
// 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
|
||||
for (;;) mg_poll_server(server, 1000);
|
||||
mg_destroy_server(&server);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user