mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-28 20:58:31 +00:00
Amended file sending example
This commit is contained in:
1 parent
f51ca8d5d1
commit
464c77314d
2 files changed
+13
-35
No files matched your search
+4
-1
@@ -50,6 +50,9 @@ upload: upload.c ../mongoose.c
|
|||||||
auth: auth.c ../mongoose.c
|
auth: auth.c ../mongoose.c
|
||||||
$(CC) auth.c ../mongoose.c $(OUT) $(CFLAGS)
|
$(CC) auth.c ../mongoose.c $(OUT) $(CFLAGS)
|
||||||
|
|
||||||
|
file: file.c ../mongoose.c
|
||||||
|
$(CC) file.c ../mongoose.c $(OUT) $(CFLAGS)
|
||||||
|
|
||||||
form: form.c ../mongoose.c
|
form: form.c ../mongoose.c
|
||||||
$(CC) form.c ../mongoose.c $(OUT) $(CFLAGS)
|
$(CC) form.c ../mongoose.c $(OUT) $(CFLAGS)
|
||||||
|
|
||||||
@@ -68,4 +71,4 @@ u:
|
|||||||
./$@
|
./$@
|
||||||
|
|
||||||
clean:
|
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
|
||||||
+9
-34
@@ -1,46 +1,21 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "mongoose.h"
|
#include "mongoose.h"
|
||||||
|
|
||||||
|
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
|
||||||
static int index_html(struct mg_connection *conn) {
|
switch (ev) {
|
||||||
FILE *fp = (FILE *) conn->connection_param;
|
case MG_REQUEST:
|
||||||
char buf[200];
|
mg_send_file(conn, "file.c");
|
||||||
size_t n = 0;
|
return MG_MORE; // It is important to return MG_MORE after mg_send_file!
|
||||||
|
case MG_AUTH: return MG_TRUE;
|
||||||
if (fp == NULL) {
|
default: return MG_FALSE;
|
||||||
fp = fopen("../mongoose.c", "r");
|
|
||||||
conn->connection_param = (void *) fp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
int main(void) {
|
||||||
struct mg_server *server;
|
struct mg_server *server = mg_create_server(NULL, ev_handler);
|
||||||
|
|
||||||
// Create and configure the server
|
|
||||||
server = mg_create_server(NULL);
|
|
||||||
mg_set_option(server, "listening_port", "8080");
|
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"));
|
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
|
||||||
for (;;) {
|
for (;;) mg_poll_server(server, 1000);
|
||||||
mg_poll_server(server, 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cleanup, and free server instance
|
|
||||||
mg_destroy_server(&server);
|
mg_destroy_server(&server);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in new issue
Block a user