mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-15 02:08:08 +08:00
Use mg_fopen only on win32
This commit is contained in:
parent
a771d6a19d
commit
55e2077a46
27
mongoose.c
27
mongoose.c
@ -803,7 +803,7 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm,
|
|||||||
snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name);
|
snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name);
|
||||||
LOG(LL_DEBUG,
|
LOG(LL_DEBUG,
|
||||||
("%p %d bytes @ %d [%s]", c->fd, (int) hm->body.len, (int) oft, name));
|
("%p %d bytes @ %d [%s]", c->fd, (int) hm->body.len, (int) oft, name));
|
||||||
if ((fp = mg_fopen(path, oft == 0 ? "wb" : "ab")) == NULL) {
|
if ((fp = fopen(path, oft == 0 ? "wb" : "ab")) == NULL) {
|
||||||
mg_http_reply(c, 400, "", "fopen(%s): %d", name, errno);
|
mg_http_reply(c, 400, "", "fopen(%s): %d", name, errno);
|
||||||
return -2;
|
return -2;
|
||||||
} else {
|
} else {
|
||||||
@ -926,7 +926,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
|
|||||||
struct mg_str *inm = mg_http_get_header(hm, "If-None-Match");
|
struct mg_str *inm = mg_http_get_header(hm, "If-None-Match");
|
||||||
mg_stat_t st;
|
mg_stat_t st;
|
||||||
char etag[64];
|
char etag[64];
|
||||||
FILE *fp = mg_fopen(path, "rb");
|
FILE *fp = fopen(path, "rb");
|
||||||
if (fp == NULL || mg_stat(path, &st) != 0 ||
|
if (fp == NULL || mg_stat(path, &st) != 0 ||
|
||||||
mg_http_etag(etag, sizeof(etag), &st) != etag) {
|
mg_http_etag(etag, sizeof(etag), &st) != etag) {
|
||||||
LOG(LL_DEBUG, ("404 [%.*s] [%s] %p", (int) hm->uri.len, hm->uri.ptr, path,
|
LOG(LL_DEBUG, ("404 [%.*s] [%s] %p", (int) hm->uri.len, hm->uri.ptr, path,
|
||||||
@ -1267,14 +1267,14 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
|
|||||||
mg_http_reply(c, 404, "", "Invalid URI [%.*s]\n", (int) hm->uri.len,
|
mg_http_reply(c, 404, "", "Invalid URI [%.*s]\n", (int) hm->uri.len,
|
||||||
hm->uri.ptr);
|
hm->uri.ptr);
|
||||||
} else {
|
} else {
|
||||||
FILE *fp = mg_fopen(t2, "r");
|
FILE *fp = fopen(t2, "r");
|
||||||
#if MG_ENABLE_SSI
|
#if MG_ENABLE_SSI
|
||||||
if (is_index && fp == NULL) {
|
if (is_index && fp == NULL) {
|
||||||
char *p = t2 + strlen(t2);
|
char *p = t2 + strlen(t2);
|
||||||
while (p > t2 && p[-1] != '/') p--;
|
while (p > t2 && p[-1] != '/') p--;
|
||||||
strncpy(p, "index.shtml", (size_t)(&t2[sizeof(t2)] - p - 2));
|
strncpy(p, "index.shtml", (size_t)(&t2[sizeof(t2)] - p - 2));
|
||||||
t2[sizeof(t2) - 1] = '\0';
|
t2[sizeof(t2) - 1] = '\0';
|
||||||
fp = mg_fopen(t2, "r");
|
fp = fopen(t2, "r");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (is_index && fp == NULL) {
|
if (is_index && fp == NULL) {
|
||||||
@ -3209,7 +3209,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
|
|||||||
#if MG_ENABLE_SSI
|
#if MG_ENABLE_SSI
|
||||||
static char *mg_ssi(const char *path, const char *root, int depth) {
|
static char *mg_ssi(const char *path, const char *root, int depth) {
|
||||||
struct mg_iobuf b = {NULL, 0, 0};
|
struct mg_iobuf b = {NULL, 0, 0};
|
||||||
FILE *fp = mg_fopen(path, "rb");
|
FILE *fp = fopen(path, "rb");
|
||||||
if (fp != NULL) {
|
if (fp != NULL) {
|
||||||
char buf[BUFSIZ], arg[sizeof(buf)];
|
char buf[BUFSIZ], arg[sizeof(buf)];
|
||||||
int ch, intag = 0;
|
int ch, intag = 0;
|
||||||
@ -3921,17 +3921,6 @@ int mg_stat(const char *path, mg_stat_t *st) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *mg_fopen(const char *path, const char *mode) {
|
|
||||||
#ifdef _WIN32
|
|
||||||
wchar_t b1[MG_PATH_MAX], b2[10];
|
|
||||||
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
|
|
||||||
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));
|
|
||||||
return _wfopen(b1, b2);
|
|
||||||
#else
|
|
||||||
return fopen(path, mode);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t mg_file_size(const char *path) {
|
int64_t mg_file_size(const char *path) {
|
||||||
#if MG_ARCH == MG_ARCH_FREERTOS_TCP && defined(MG_ENABLE_FF)
|
#if MG_ARCH == MG_ARCH_FREERTOS_TCP && defined(MG_ENABLE_FF)
|
||||||
struct FF_STAT st;
|
struct FF_STAT st;
|
||||||
@ -3946,7 +3935,7 @@ char *mg_file_read(const char *path, size_t *sizep) {
|
|||||||
FILE *fp;
|
FILE *fp;
|
||||||
char *data = NULL;
|
char *data = NULL;
|
||||||
size_t size = (size_t) mg_file_size(path);
|
size_t size = (size_t) mg_file_size(path);
|
||||||
if ((fp = mg_fopen(path, "rb")) != NULL) {
|
if ((fp = fopen(path, "rb")) != NULL) {
|
||||||
data = (char *) calloc(1, size + 1);
|
data = (char *) calloc(1, size + 1);
|
||||||
if (data != NULL) {
|
if (data != NULL) {
|
||||||
if (fread(data, 1, size, fp) != size) {
|
if (fread(data, 1, size, fp) != size) {
|
||||||
@ -3967,7 +3956,7 @@ bool mg_file_write(const char *path, const void *buf, size_t len) {
|
|||||||
FILE *fp;
|
FILE *fp;
|
||||||
char tmp[MG_PATH_MAX];
|
char tmp[MG_PATH_MAX];
|
||||||
snprintf(tmp, sizeof(tmp), "%s.%d", path, rand());
|
snprintf(tmp, sizeof(tmp), "%s.%d", path, rand());
|
||||||
fp = mg_fopen(tmp, "wb");
|
fp = fopen(tmp, "wb");
|
||||||
if (fp != NULL) {
|
if (fp != NULL) {
|
||||||
result = fwrite(buf, 1, len, fp) == len;
|
result = fwrite(buf, 1, len, fp) == len;
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
@ -4002,7 +3991,7 @@ void mg_random(void *buf, size_t len) {
|
|||||||
while (len--) *p++ = (unsigned char) (esp_random() & 255);
|
while (len--) *p++ = (unsigned char) (esp_random() & 255);
|
||||||
#elif MG_ARCH == MG_ARCH_WIN32
|
#elif MG_ARCH == MG_ARCH_WIN32
|
||||||
#elif MG_ARCH_UNIX && MG_ENABLE_FS
|
#elif MG_ARCH_UNIX && MG_ENABLE_FS
|
||||||
FILE *fp = mg_fopen("/dev/urandom", "rb");
|
FILE *fp = fopen("/dev/urandom", "rb");
|
||||||
if (fp != NULL) {
|
if (fp != NULL) {
|
||||||
if (fread(buf, 1, len, fp) == len) done = true;
|
if (fread(buf, 1, len, fp) == len) done = true;
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
@ -360,6 +360,7 @@ typedef int socklen_t;
|
|||||||
#define EWOULDBLOCK WSAEWOULDBLOCK
|
#define EWOULDBLOCK WSAEWOULDBLOCK
|
||||||
#endif
|
#endif
|
||||||
#define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
|
#define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
|
||||||
|
#define fopen(a, b) mg_fopen((a), (b))
|
||||||
#ifndef va_copy
|
#ifndef va_copy
|
||||||
#ifdef __va_copy
|
#ifdef __va_copy
|
||||||
#define va_copy __va_copy
|
#define va_copy __va_copy
|
||||||
@ -373,6 +374,13 @@ typedef int socklen_t;
|
|||||||
|
|
||||||
#define MG_INT64_FMT "%I64d"
|
#define MG_INT64_FMT "%I64d"
|
||||||
|
|
||||||
|
static __inline FILE *mg_fopen(const char *path, const char *mode) {
|
||||||
|
wchar_t b1[PATH_MAX], b2[10];
|
||||||
|
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
|
||||||
|
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));
|
||||||
|
return _wfopen(b1, b2);
|
||||||
|
}
|
||||||
|
|
||||||
// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
|
// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
|
||||||
static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) {
|
static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) {
|
||||||
(void) tm;
|
(void) tm;
|
||||||
@ -560,7 +568,6 @@ typedef struct _stati64 mg_stat_t;
|
|||||||
typedef struct stat mg_stat_t;
|
typedef struct stat mg_stat_t;
|
||||||
#endif
|
#endif
|
||||||
int mg_stat(const char *path, mg_stat_t *);
|
int mg_stat(const char *path, mg_stat_t *);
|
||||||
FILE *mg_fopen(const char *fp, const char *mode);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define mg_htons(x) mg_ntohs(x)
|
#define mg_htons(x) mg_ntohs(x)
|
||||||
|
@ -67,6 +67,7 @@ typedef int socklen_t;
|
|||||||
#define EWOULDBLOCK WSAEWOULDBLOCK
|
#define EWOULDBLOCK WSAEWOULDBLOCK
|
||||||
#endif
|
#endif
|
||||||
#define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
|
#define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
|
||||||
|
#define fopen(a, b) mg_fopen((a), (b))
|
||||||
#ifndef va_copy
|
#ifndef va_copy
|
||||||
#ifdef __va_copy
|
#ifdef __va_copy
|
||||||
#define va_copy __va_copy
|
#define va_copy __va_copy
|
||||||
@ -80,6 +81,13 @@ typedef int socklen_t;
|
|||||||
|
|
||||||
#define MG_INT64_FMT "%I64d"
|
#define MG_INT64_FMT "%I64d"
|
||||||
|
|
||||||
|
static __inline FILE *mg_fopen(const char *path, const char *mode) {
|
||||||
|
wchar_t b1[PATH_MAX], b2[10];
|
||||||
|
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
|
||||||
|
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));
|
||||||
|
return _wfopen(b1, b2);
|
||||||
|
}
|
||||||
|
|
||||||
// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
|
// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
|
||||||
static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) {
|
static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) {
|
||||||
(void) tm;
|
(void) tm;
|
||||||
|
@ -392,7 +392,7 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm,
|
|||||||
snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name);
|
snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name);
|
||||||
LOG(LL_DEBUG,
|
LOG(LL_DEBUG,
|
||||||
("%p %d bytes @ %d [%s]", c->fd, (int) hm->body.len, (int) oft, name));
|
("%p %d bytes @ %d [%s]", c->fd, (int) hm->body.len, (int) oft, name));
|
||||||
if ((fp = mg_fopen(path, oft == 0 ? "wb" : "ab")) == NULL) {
|
if ((fp = fopen(path, oft == 0 ? "wb" : "ab")) == NULL) {
|
||||||
mg_http_reply(c, 400, "", "fopen(%s): %d", name, errno);
|
mg_http_reply(c, 400, "", "fopen(%s): %d", name, errno);
|
||||||
return -2;
|
return -2;
|
||||||
} else {
|
} else {
|
||||||
@ -515,7 +515,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
|
|||||||
struct mg_str *inm = mg_http_get_header(hm, "If-None-Match");
|
struct mg_str *inm = mg_http_get_header(hm, "If-None-Match");
|
||||||
mg_stat_t st;
|
mg_stat_t st;
|
||||||
char etag[64];
|
char etag[64];
|
||||||
FILE *fp = mg_fopen(path, "rb");
|
FILE *fp = fopen(path, "rb");
|
||||||
if (fp == NULL || mg_stat(path, &st) != 0 ||
|
if (fp == NULL || mg_stat(path, &st) != 0 ||
|
||||||
mg_http_etag(etag, sizeof(etag), &st) != etag) {
|
mg_http_etag(etag, sizeof(etag), &st) != etag) {
|
||||||
LOG(LL_DEBUG, ("404 [%.*s] [%s] %p", (int) hm->uri.len, hm->uri.ptr, path,
|
LOG(LL_DEBUG, ("404 [%.*s] [%s] %p", (int) hm->uri.len, hm->uri.ptr, path,
|
||||||
@ -856,14 +856,14 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
|
|||||||
mg_http_reply(c, 404, "", "Invalid URI [%.*s]\n", (int) hm->uri.len,
|
mg_http_reply(c, 404, "", "Invalid URI [%.*s]\n", (int) hm->uri.len,
|
||||||
hm->uri.ptr);
|
hm->uri.ptr);
|
||||||
} else {
|
} else {
|
||||||
FILE *fp = mg_fopen(t2, "r");
|
FILE *fp = fopen(t2, "r");
|
||||||
#if MG_ENABLE_SSI
|
#if MG_ENABLE_SSI
|
||||||
if (is_index && fp == NULL) {
|
if (is_index && fp == NULL) {
|
||||||
char *p = t2 + strlen(t2);
|
char *p = t2 + strlen(t2);
|
||||||
while (p > t2 && p[-1] != '/') p--;
|
while (p > t2 && p[-1] != '/') p--;
|
||||||
strncpy(p, "index.shtml", (size_t)(&t2[sizeof(t2)] - p - 2));
|
strncpy(p, "index.shtml", (size_t)(&t2[sizeof(t2)] - p - 2));
|
||||||
t2[sizeof(t2) - 1] = '\0';
|
t2[sizeof(t2) - 1] = '\0';
|
||||||
fp = mg_fopen(t2, "r");
|
fp = fopen(t2, "r");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (is_index && fp == NULL) {
|
if (is_index && fp == NULL) {
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
#if MG_ENABLE_SSI
|
#if MG_ENABLE_SSI
|
||||||
static char *mg_ssi(const char *path, const char *root, int depth) {
|
static char *mg_ssi(const char *path, const char *root, int depth) {
|
||||||
struct mg_iobuf b = {NULL, 0, 0};
|
struct mg_iobuf b = {NULL, 0, 0};
|
||||||
FILE *fp = mg_fopen(path, "rb");
|
FILE *fp = fopen(path, "rb");
|
||||||
if (fp != NULL) {
|
if (fp != NULL) {
|
||||||
char buf[BUFSIZ], arg[sizeof(buf)];
|
char buf[BUFSIZ], arg[sizeof(buf)];
|
||||||
int ch, intag = 0;
|
int ch, intag = 0;
|
||||||
|
17
src/util.c
17
src/util.c
@ -13,17 +13,6 @@ int mg_stat(const char *path, mg_stat_t *st) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *mg_fopen(const char *path, const char *mode) {
|
|
||||||
#ifdef _WIN32
|
|
||||||
wchar_t b1[MG_PATH_MAX], b2[10];
|
|
||||||
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
|
|
||||||
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));
|
|
||||||
return _wfopen(b1, b2);
|
|
||||||
#else
|
|
||||||
return fopen(path, mode);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t mg_file_size(const char *path) {
|
int64_t mg_file_size(const char *path) {
|
||||||
#if MG_ARCH == MG_ARCH_FREERTOS_TCP && defined(MG_ENABLE_FF)
|
#if MG_ARCH == MG_ARCH_FREERTOS_TCP && defined(MG_ENABLE_FF)
|
||||||
struct FF_STAT st;
|
struct FF_STAT st;
|
||||||
@ -38,7 +27,7 @@ char *mg_file_read(const char *path, size_t *sizep) {
|
|||||||
FILE *fp;
|
FILE *fp;
|
||||||
char *data = NULL;
|
char *data = NULL;
|
||||||
size_t size = (size_t) mg_file_size(path);
|
size_t size = (size_t) mg_file_size(path);
|
||||||
if ((fp = mg_fopen(path, "rb")) != NULL) {
|
if ((fp = fopen(path, "rb")) != NULL) {
|
||||||
data = (char *) calloc(1, size + 1);
|
data = (char *) calloc(1, size + 1);
|
||||||
if (data != NULL) {
|
if (data != NULL) {
|
||||||
if (fread(data, 1, size, fp) != size) {
|
if (fread(data, 1, size, fp) != size) {
|
||||||
@ -59,7 +48,7 @@ bool mg_file_write(const char *path, const void *buf, size_t len) {
|
|||||||
FILE *fp;
|
FILE *fp;
|
||||||
char tmp[MG_PATH_MAX];
|
char tmp[MG_PATH_MAX];
|
||||||
snprintf(tmp, sizeof(tmp), "%s.%d", path, rand());
|
snprintf(tmp, sizeof(tmp), "%s.%d", path, rand());
|
||||||
fp = mg_fopen(tmp, "wb");
|
fp = fopen(tmp, "wb");
|
||||||
if (fp != NULL) {
|
if (fp != NULL) {
|
||||||
result = fwrite(buf, 1, len, fp) == len;
|
result = fwrite(buf, 1, len, fp) == len;
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
@ -94,7 +83,7 @@ void mg_random(void *buf, size_t len) {
|
|||||||
while (len--) *p++ = (unsigned char) (esp_random() & 255);
|
while (len--) *p++ = (unsigned char) (esp_random() & 255);
|
||||||
#elif MG_ARCH == MG_ARCH_WIN32
|
#elif MG_ARCH == MG_ARCH_WIN32
|
||||||
#elif MG_ARCH_UNIX && MG_ENABLE_FS
|
#elif MG_ARCH_UNIX && MG_ENABLE_FS
|
||||||
FILE *fp = mg_fopen("/dev/urandom", "rb");
|
FILE *fp = fopen("/dev/urandom", "rb");
|
||||||
if (fp != NULL) {
|
if (fp != NULL) {
|
||||||
if (fread(buf, 1, len, fp) == len) done = true;
|
if (fread(buf, 1, len, fp) == len) done = true;
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
@ -44,7 +44,6 @@ typedef struct _stati64 mg_stat_t;
|
|||||||
typedef struct stat mg_stat_t;
|
typedef struct stat mg_stat_t;
|
||||||
#endif
|
#endif
|
||||||
int mg_stat(const char *path, mg_stat_t *);
|
int mg_stat(const char *path, mg_stat_t *);
|
||||||
FILE *mg_fopen(const char *fp, const char *mode);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define mg_htons(x) mg_ntohs(x)
|
#define mg_htons(x) mg_ntohs(x)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user