Commit 920e8d91 authored by Alexander Alashkin's avatar Alexander Alashkin Committed by Cesanta Bot

Add shims for mongoose file operations

PUBLISHED_FROM=b5b12cdf4273f21dbcdac9bfb6a06063152914c9
parent aed6d05d
...@@ -8,6 +8,8 @@ items: ...@@ -8,6 +8,8 @@ items:
- { name: mg_basic_auth_header.md } - { name: mg_basic_auth_header.md }
- { name: mg_conn_addr_to_str.md } - { name: mg_conn_addr_to_str.md }
- { name: mg_fopen.md } - { name: mg_fopen.md }
- { name: mg_fread.md }
- { name: mg_fwrite.md }
- { name: mg_hexdump.md } - { name: mg_hexdump.md }
- { name: mg_hexdump_connection.md } - { name: mg_hexdump_connection.md }
- { name: mg_hexdumpf.md } - { name: mg_hexdumpf.md }
......
---
title: "mg_fread()"
decl_name: "mg_fread"
symbol_kind: "func"
signature: |
size_t mg_fread(void *ptr, size_t size, size_t count, FILE *f);
---
Reads data from the given file stream.
Return value is a number of bytes readen.
---
title: "mg_fwrite()"
decl_name: "mg_fwrite"
symbol_kind: "func"
signature: |
size_t mg_fwrite(const void *ptr, size_t size, size_t count, FILE *f);
---
Writes data to the given file stream.
Return value is a number of bytes wtitten.
...@@ -9,3 +9,4 @@ title: Tunables ...@@ -9,3 +9,4 @@ title: Tunables
- `MG_SSL_CRYPTO_MODERN`, `MG_SSL_CRYPTO_OLD` - choose either "Modern" or "Old" ciphers - `MG_SSL_CRYPTO_MODERN`, `MG_SSL_CRYPTO_OLD` - choose either "Modern" or "Old" ciphers
instead of the default "Intermediate" setting. instead of the default "Intermediate" setting.
See [this article](https://wiki.mozilla.org/Security/Server_Side_TLS#Recommended_configurations) for details. See [this article](https://wiki.mozilla.org/Security/Server_Side_TLS#Recommended_configurations) for details.
- `MG_USER_FILE_FUNCTIONS` allow you to use custom file operation, by defining you own `mg_stat`, `mg_fopen`, `mg_open`, `mg_fread` and `mg_fwrite` functions
\ No newline at end of file
...@@ -5374,7 +5374,7 @@ static void mg_http_transfer_file_data(struct mg_connection *nc) { ...@@ -5374,7 +5374,7 @@ static void mg_http_transfer_file_data(struct mg_connection *nc) {
if (to_read == 0) { if (to_read == 0) {
/* Rate limiting. send_mbuf is too full, wait until it's drained. */ /* Rate limiting. send_mbuf is too full, wait until it's drained. */
} else if (pd->file.sent < pd->file.cl && } else if (pd->file.sent < pd->file.cl &&
(n = fread(buf, 1, to_read, pd->file.fp)) > 0) { (n = mg_fread(buf, 1, to_read, pd->file.fp)) > 0) {
mg_send(nc, buf, n); mg_send(nc, buf, n);
pd->file.sent += n; pd->file.sent += n;
} else { } else {
...@@ -5384,7 +5384,7 @@ static void mg_http_transfer_file_data(struct mg_connection *nc) { ...@@ -5384,7 +5384,7 @@ static void mg_http_transfer_file_data(struct mg_connection *nc) {
} else if (pd->file.type == DATA_PUT) { } else if (pd->file.type == DATA_PUT) {
struct mbuf *io = &nc->recv_mbuf; struct mbuf *io = &nc->recv_mbuf;
size_t to_write = left <= 0 ? 0 : left < io->len ? (size_t) left : io->len; size_t to_write = left <= 0 ? 0 : left < io->len ? (size_t) left : io->len;
size_t n = fwrite(io->buf, 1, to_write, pd->file.fp); size_t n = mg_fwrite(io->buf, 1, to_write, pd->file.fp);
if (n > 0) { if (n > 0) {
mbuf_remove(io, n); mbuf_remove(io, n);
pd->file.sent += n; pd->file.sent += n;
...@@ -7515,7 +7515,7 @@ void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data, ...@@ -7515,7 +7515,7 @@ void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
struct file_upload_state *fus = struct file_upload_state *fus =
(struct file_upload_state *) mp->user_data; (struct file_upload_state *) mp->user_data;
if (fus == NULL || fus->fp == NULL) break; if (fus == NULL || fus->fp == NULL) break;
if (fwrite(mp->data.p, 1, mp->data.len, fus->fp) != mp->data.len) { if (mg_fwrite(mp->data.p, 1, mp->data.len, fus->fp) != mp->data.len) {
LOG(LL_ERROR, ("Failed to write to %s: %d, wrote %d", fus->lfn, LOG(LL_ERROR, ("Failed to write to %s: %d, wrote %d", fus->lfn,
mg_get_errno(), (int) fus->num_recd)); mg_get_errno(), (int) fus->num_recd));
if (mg_get_errno() == ENOSPC if (mg_get_errno() == ENOSPC
...@@ -8328,7 +8328,7 @@ static void mg_send_ssi_file(struct mg_connection *nc, struct http_message *hm, ...@@ -8328,7 +8328,7 @@ static void mg_send_ssi_file(struct mg_connection *nc, struct http_message *hm,
static void mg_send_file_data(struct mg_connection *nc, FILE *fp) { static void mg_send_file_data(struct mg_connection *nc, FILE *fp) {
char buf[BUFSIZ]; char buf[BUFSIZ];
size_t n; size_t n;
while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { while ((n = mg_fread(buf, 1, sizeof(buf), fp)) > 0) {
mg_send(nc, buf, n); mg_send(nc, buf, n);
} }
} }
...@@ -9209,7 +9209,7 @@ static int lowercase(const char *s) { ...@@ -9209,7 +9209,7 @@ static int lowercase(const char *s) {
return tolower(*(const unsigned char *) s); return tolower(*(const unsigned char *) s);
} }
#if MG_ENABLE_FILESYSTEM #if MG_ENABLE_FILESYSTEM && !defined(MG_USER_FILE_FUNCTIONS)
int mg_stat(const char *path, cs_stat_t *st) { int mg_stat(const char *path, cs_stat_t *st) {
#ifdef _WIN32 #ifdef _WIN32
wchar_t wpath[MAX_PATH_SIZE]; wchar_t wpath[MAX_PATH_SIZE];
...@@ -9241,6 +9241,14 @@ int mg_open(const char *path, int flag, int mode) { /* LCOV_EXCL_LINE */ ...@@ -9241,6 +9241,14 @@ int mg_open(const char *path, int flag, int mode) { /* LCOV_EXCL_LINE */
return open(path, flag, mode); /* LCOV_EXCL_LINE */ return open(path, flag, mode); /* LCOV_EXCL_LINE */
#endif #endif
} }
size_t mg_fread(void *ptr, size_t size, size_t count, FILE *f) {
return fread(ptr, size, count, f);
}
size_t mg_fwrite(const void *ptr, size_t size, size_t count, FILE *f) {
return fwrite(ptr, size, count, f);
}
#endif #endif
void mg_base64_encode(const unsigned char *src, int src_len, char *dst) { void mg_base64_encode(const unsigned char *src, int src_len, char *dst) {
......
...@@ -3898,6 +3898,21 @@ FILE *mg_fopen(const char *path, const char *mode); ...@@ -3898,6 +3898,21 @@ FILE *mg_fopen(const char *path, const char *mode);
* Return value is the same as for the `open()` syscall. * Return value is the same as for the `open()` syscall.
*/ */
int mg_open(const char *path, int flag, int mode); int mg_open(const char *path, int flag, int mode);
/*
* Reads data from the given file stream.
*
* Return value is a number of bytes readen.
*/
size_t mg_fread(void *ptr, size_t size, size_t count, FILE *f);
/*
* Writes data to the given file stream.
*
* Return value is a number of bytes wtitten.
*/
size_t mg_fwrite(const void *ptr, size_t size, size_t count, FILE *f);
#endif /* MG_ENABLE_FILESYSTEM */ #endif /* MG_ENABLE_FILESYSTEM */
#if MG_ENABLE_THREADS #if MG_ENABLE_THREADS
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment