From 5bbb6fbbd513a5deac9de8ccff690fc9f159f092 Mon Sep 17 00:00:00 2001 From: valenok Date: Sun, 19 Jun 2011 17:45:52 +0100 Subject: [PATCH] Added mg_send_file() --- mongoose.c | 10 ++++++++++ mongoose.h | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/mongoose.c b/mongoose.c index 9b7a284d..3d92e62e 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2567,6 +2567,16 @@ static void handle_file_request(struct mg_connection *conn, const char *path, (void) fclose(fp); } +void mg_send_file(struct mg_connection *conn, const char *path) { + struct mgstat st; + if (mg_stat(path, &st) == 0) { + handle_file_request(conn, path, &st); + } else { + send_http_error(conn, 404, "Not Found", "%s", "File not found"); + } +} + + // Parse HTTP headers from the given buffer, advance buffer to the point // where parsing stopped. static void parse_http_headers(char **buf, struct mg_request_info *ri) { diff --git a/mongoose.h b/mongoose.h index 213bd8b4..d9098be6 100644 --- a/mongoose.h +++ b/mongoose.h @@ -158,6 +158,11 @@ int mg_write(struct mg_connection *, const void *buf, size_t len); int mg_printf(struct mg_connection *, const char *fmt, ...); +// Send contents of the entire file together with HTTP headers. +// Return 1 on success, 0 on error (e.g. file does not exist). +void mg_send_file(struct mg_connection *conn, const char *path); + + // Read data from the remote end, return number of bytes read. int mg_read(struct mg_connection *, void *buf, size_t len);