Skip to content
Snippets Groups Projects
Commit 464c7731 authored by Sergey Lyubka's avatar Sergey Lyubka
Browse files

Amended file sending example

parent f51ca8d5
No related branches found
No related tags found
No related merge requests found
......@@ -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
#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;
}
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;
}
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;
}
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;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment