mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-18 11:28:13 +08:00
Using slash, "/" as the universal directory separator throughout the code
This commit is contained in:
parent
d82f6b7607
commit
167338f216
51
mongoose.c
51
mongoose.c
@ -106,8 +106,6 @@ typedef long off_t;
|
|||||||
#define NO_SOCKLEN_T
|
#define NO_SOCKLEN_T
|
||||||
#define SSL_LIB "ssleay32.dll"
|
#define SSL_LIB "ssleay32.dll"
|
||||||
#define CRYPTO_LIB "libeay32.dll"
|
#define CRYPTO_LIB "libeay32.dll"
|
||||||
#define DIRSEP '\\'
|
|
||||||
#define IS_DIRSEP_CHAR(c) ((c) == '/' || (c) == '\\')
|
|
||||||
#define O_NONBLOCK 0
|
#define O_NONBLOCK 0
|
||||||
#if !defined(EWOULDBLOCK)
|
#if !defined(EWOULDBLOCK)
|
||||||
#define EWOULDBLOCK WSAEWOULDBLOCK
|
#define EWOULDBLOCK WSAEWOULDBLOCK
|
||||||
@ -202,8 +200,6 @@ typedef struct DIR {
|
|||||||
#define CRYPTO_LIB "libcrypto.so"
|
#define CRYPTO_LIB "libcrypto.so"
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
#define DIRSEP '/'
|
|
||||||
#define IS_DIRSEP_CHAR(c) ((c) == '/')
|
|
||||||
#ifndef O_BINARY
|
#ifndef O_BINARY
|
||||||
#define O_BINARY 0
|
#define O_BINARY 0
|
||||||
#endif // O_BINARY
|
#endif // O_BINARY
|
||||||
@ -1243,7 +1239,7 @@ static pid_t spawn_process(struct mg_connection *conn, const char *prog,
|
|||||||
interp = conn->ctx->config[CGI_INTERPRETER];
|
interp = conn->ctx->config[CGI_INTERPRETER];
|
||||||
if (interp == NULL) {
|
if (interp == NULL) {
|
||||||
buf[2] = '\0';
|
buf[2] = '\0';
|
||||||
mg_snprintf(conn, cmdline, sizeof(cmdline), "%s%c%s", dir, DIRSEP, prog);
|
mg_snprintf(conn, cmdline, sizeof(cmdline), "%s%c%s", dir, '/', prog);
|
||||||
if ((fp = fopen(cmdline, "r")) != NULL) {
|
if ((fp = fopen(cmdline, "r")) != NULL) {
|
||||||
(void) fgets(buf, sizeof(buf), fp);
|
(void) fgets(buf, sizeof(buf), fp);
|
||||||
if (buf[0] != '#' || buf[1] != '!') {
|
if (buf[0] != '#' || buf[1] != '!') {
|
||||||
@ -1261,7 +1257,7 @@ static pid_t spawn_process(struct mg_connection *conn, const char *prog,
|
|||||||
}
|
}
|
||||||
|
|
||||||
(void) mg_snprintf(conn, cmdline, sizeof(cmdline), "%s%s%s%c%s",
|
(void) mg_snprintf(conn, cmdline, sizeof(cmdline), "%s%s%s%c%s",
|
||||||
interp, interp[0] == '\0' ? "" : " ", dir, DIRSEP, prog);
|
interp, interp[0] == '\0' ? "" : " ", dir, '\\', prog);
|
||||||
|
|
||||||
DEBUG_TRACE(("Running [%s]", cmdline));
|
DEBUG_TRACE(("Running [%s]", cmdline));
|
||||||
if (CreateProcessA(NULL, cmdline, NULL, NULL, TRUE,
|
if (CreateProcessA(NULL, cmdline, NULL, NULL, TRUE,
|
||||||
@ -1674,10 +1670,6 @@ static int convert_uri_to_file_name(struct mg_connection *conn, char *buf,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(_WIN32) && !defined(__SYMBIAN32__)
|
|
||||||
//change_slashes_to_backslashes(buf);
|
|
||||||
#endif // _WIN32
|
|
||||||
|
|
||||||
if ((stat_result = mg_stat(buf, st)) != 0) {
|
if ((stat_result = mg_stat(buf, st)) != 0) {
|
||||||
// Support PATH_INFO for CGI scripts.
|
// Support PATH_INFO for CGI scripts.
|
||||||
for (p = buf + strlen(buf); p > buf + 1; p--) {
|
for (p = buf + strlen(buf); p > buf + 1; p--) {
|
||||||
@ -1782,15 +1774,16 @@ static void remove_double_dots_and_double_slashes(char *s) {
|
|||||||
|
|
||||||
while (*s != '\0') {
|
while (*s != '\0') {
|
||||||
*p++ = *s++;
|
*p++ = *s++;
|
||||||
if (IS_DIRSEP_CHAR(s[-1])) {
|
if (s[-1] == '/' || s[-1] == '\\') {
|
||||||
// Skip all following slashes and backslashes
|
// Skip all following slashes, backslashes and double-dots
|
||||||
while (IS_DIRSEP_CHAR(s[0])) {
|
while (s[0] != '\0') {
|
||||||
s++;
|
if (s[0] == '/' || s[0] == '\\') {
|
||||||
}
|
s++;
|
||||||
|
} else if (s[0] == '.' && s[1] == '.') {
|
||||||
// Skip all double-dots
|
s += 2;
|
||||||
while (*s == '.' && s[1] == '.') {
|
} else {
|
||||||
s += 2;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2155,15 +2148,15 @@ static FILE *open_auth_file(struct mg_connection *conn, const char *path) {
|
|||||||
ctx->config[GLOBAL_PASSWORDS_FILE], strerror(ERRNO));
|
ctx->config[GLOBAL_PASSWORDS_FILE], strerror(ERRNO));
|
||||||
} else if (!mg_stat(path, &st) && st.is_directory) {
|
} else if (!mg_stat(path, &st) && st.is_directory) {
|
||||||
(void) mg_snprintf(conn, name, sizeof(name), "%s%c%s",
|
(void) mg_snprintf(conn, name, sizeof(name), "%s%c%s",
|
||||||
path, DIRSEP, PASSWORDS_FILE_NAME);
|
path, '/', PASSWORDS_FILE_NAME);
|
||||||
fp = mg_fopen(name, "r");
|
fp = mg_fopen(name, "r");
|
||||||
} else {
|
} else {
|
||||||
// Try to find .htpasswd in requested directory.
|
// Try to find .htpasswd in requested directory.
|
||||||
for (p = path, e = p + strlen(p) - 1; e > p; e--)
|
for (p = path, e = p + strlen(p) - 1; e > p; e--)
|
||||||
if (IS_DIRSEP_CHAR(*e))
|
if (e[0] == '/')
|
||||||
break;
|
break;
|
||||||
(void) mg_snprintf(conn, name, sizeof(name), "%.*s%c%s",
|
(void) mg_snprintf(conn, name, sizeof(name), "%.*s%c%s",
|
||||||
(int) (e - p), p, DIRSEP, PASSWORDS_FILE_NAME);
|
(int) (e - p), p, '/', PASSWORDS_FILE_NAME);
|
||||||
fp = mg_fopen(name, "r");
|
fp = mg_fopen(name, "r");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2503,7 +2496,7 @@ static int scan_directory(struct mg_connection *conn, const char *dir,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
mg_snprintf(conn, path, sizeof(path), "%s%c%s", dir, DIRSEP, dp->d_name);
|
mg_snprintf(conn, path, sizeof(path), "%s%c%s", dir, '/', dp->d_name);
|
||||||
|
|
||||||
// If we don't memset stat structure to zero, mtime will have
|
// If we don't memset stat structure to zero, mtime will have
|
||||||
// garbage and strftime() will segfault later on in
|
// garbage and strftime() will segfault later on in
|
||||||
@ -2812,10 +2805,10 @@ static int substitute_index_file(struct mg_connection *conn, char *path,
|
|||||||
// The 'path' given to us points to the directory. Remove all trailing
|
// The 'path' given to us points to the directory. Remove all trailing
|
||||||
// directory separator characters from the end of the path, and
|
// directory separator characters from the end of the path, and
|
||||||
// then append single directory separator character.
|
// then append single directory separator character.
|
||||||
while (n > 0 && IS_DIRSEP_CHAR(path[n - 1])) {
|
while (n > 0 && path[n - 1] == '/') {
|
||||||
n--;
|
n--;
|
||||||
}
|
}
|
||||||
path[n] = DIRSEP;
|
path[n] = '/';
|
||||||
|
|
||||||
// Traverse index files list. For each entry, append it to the given
|
// Traverse index files list. For each entry, append it to the given
|
||||||
// path and see if the file exists. If it exists, break the loop
|
// path and see if the file exists. If it exists, break the loop
|
||||||
@ -3090,7 +3083,7 @@ static void handle_cgi_request(struct mg_connection *conn, const char *prog) {
|
|||||||
// directory containing executable program, 'p' must point to the
|
// directory containing executable program, 'p' must point to the
|
||||||
// executable program name relative to 'dir'.
|
// executable program name relative to 'dir'.
|
||||||
(void) mg_snprintf(conn, dir, sizeof(dir), "%s", prog);
|
(void) mg_snprintf(conn, dir, sizeof(dir), "%s", prog);
|
||||||
if ((p = strrchr(dir, DIRSEP)) != NULL) {
|
if ((p = strrchr(dir, '/')) != NULL) {
|
||||||
*p++ = '\0';
|
*p++ = '\0';
|
||||||
} else {
|
} else {
|
||||||
dir[0] = '.', dir[1] = '\0';
|
dir[0] = '.', dir[1] = '\0';
|
||||||
@ -3224,7 +3217,7 @@ static int put_dir(const char *path) {
|
|||||||
struct mgstat st;
|
struct mgstat st;
|
||||||
int len, res = 1;
|
int len, res = 1;
|
||||||
|
|
||||||
for (s = p = path + 2; (p = strchr(s, DIRSEP)) != NULL; s = ++p) {
|
for (s = p = path + 2; (p = strchr(s, '/')) != NULL; s = ++p) {
|
||||||
len = p - path;
|
len = p - path;
|
||||||
if (len >= (int) sizeof(buf)) {
|
if (len >= (int) sizeof(buf)) {
|
||||||
res = -1;
|
res = -1;
|
||||||
@ -3294,7 +3287,7 @@ static void do_ssi_include(struct mg_connection *conn, const char *ssi,
|
|||||||
if (sscanf(tag, " virtual=\"%[^\"]\"", file_name) == 1) {
|
if (sscanf(tag, " virtual=\"%[^\"]\"", file_name) == 1) {
|
||||||
// File name is relative to the webserver root
|
// File name is relative to the webserver root
|
||||||
(void) mg_snprintf(conn, path, sizeof(path), "%s%c%s",
|
(void) mg_snprintf(conn, path, sizeof(path), "%s%c%s",
|
||||||
conn->ctx->config[DOCUMENT_ROOT], DIRSEP, file_name);
|
conn->ctx->config[DOCUMENT_ROOT], '/', file_name);
|
||||||
} else if (sscanf(tag, " file=\"%[^\"]\"", file_name) == 1) {
|
} else if (sscanf(tag, " file=\"%[^\"]\"", file_name) == 1) {
|
||||||
// File name is relative to the webserver working directory
|
// File name is relative to the webserver working directory
|
||||||
// or it is absolute system path
|
// or it is absolute system path
|
||||||
@ -3302,7 +3295,7 @@ static void do_ssi_include(struct mg_connection *conn, const char *ssi,
|
|||||||
} else if (sscanf(tag, " \"%[^\"]\"", file_name) == 1) {
|
} else if (sscanf(tag, " \"%[^\"]\"", file_name) == 1) {
|
||||||
// File name is relative to the currect document
|
// File name is relative to the currect document
|
||||||
(void) mg_snprintf(conn, path, sizeof(path), "%s", ssi);
|
(void) mg_snprintf(conn, path, sizeof(path), "%s", ssi);
|
||||||
if ((p = strrchr(path, DIRSEP)) != NULL) {
|
if ((p = strrchr(path, '/')) != NULL) {
|
||||||
p[1] = '\0';
|
p[1] = '\0';
|
||||||
}
|
}
|
||||||
(void) mg_snprintf(conn, path + strlen(path),
|
(void) mg_snprintf(conn, path + strlen(path),
|
||||||
|
@ -112,17 +112,14 @@ static void test_remove_double_dots() {
|
|||||||
{"/...///", "/./"},
|
{"/...///", "/./"},
|
||||||
{"/a...///", "/a.../"},
|
{"/a...///", "/a.../"},
|
||||||
{"/.x", "/.x"},
|
{"/.x", "/.x"},
|
||||||
#if defined(_WIN32)
|
|
||||||
{"/\\", "/"},
|
{"/\\", "/"},
|
||||||
#else
|
|
||||||
{"/\\", "/\\"},
|
|
||||||
#endif
|
|
||||||
{"/a\\", "/a\\"},
|
{"/a\\", "/a\\"},
|
||||||
|
{"/a\\\\...", "/a\\."},
|
||||||
};
|
};
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
for (i = 0; i < ARRAY_SIZE(data); i++) {
|
for (i = 0; i < ARRAY_SIZE(data); i++) {
|
||||||
//printf("[%s] -> [%s]\n", data[i].before, data[i].after);
|
printf("[%s] -> [%s]\n", data[i].before, data[i].after);
|
||||||
remove_double_dots_and_double_slashes(data[i].before);
|
remove_double_dots_and_double_slashes(data[i].before);
|
||||||
ASSERT(strcmp(data[i].before, data[i].after) == 0);
|
ASSERT(strcmp(data[i].before, data[i].after) == 0);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user