Optimise file serving path

This commit is contained in:
Sergey Lyubka 2021-07-24 21:23:52 +01:00
parent cddff2b1b8
commit fb4fae9174
2 changed files with 10 additions and 10 deletions

View File

@ -1275,17 +1275,18 @@ 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 = fopen(t2, "r"); struct stat st;
int res = stat(t2, &st);
#if MG_ENABLE_SSI #if MG_ENABLE_SSI
if (is_index && fp == NULL) { if (is_index && res != 0) {
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 = fopen(t2, "r"); res = stat(t2, &st);
} }
#endif #endif
if (is_index && fp == NULL) { if (is_index && res != 0) {
#if MG_ENABLE_DIRECTORY_LISTING #if MG_ENABLE_DIRECTORY_LISTING
listdir(c, hm, opts, t2); listdir(c, hm, opts, t2);
#else #else
@ -1302,7 +1303,6 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
mg_http_serve_file(c, hm, t2, guess_content_type(t2), mg_http_serve_file(c, hm, t2, guess_content_type(t2),
opts->extra_headers); opts->extra_headers);
} }
if (fp != NULL) fclose(fp);
} }
} }
} }

View File

@ -856,17 +856,18 @@ 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 = fopen(t2, "r"); struct stat st;
int res = stat(t2, &st);
#if MG_ENABLE_SSI #if MG_ENABLE_SSI
if (is_index && fp == NULL) { if (is_index && res != 0) {
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 = fopen(t2, "r"); res = stat(t2, &st);
} }
#endif #endif
if (is_index && fp == NULL) { if (is_index && res != 0) {
#if MG_ENABLE_DIRECTORY_LISTING #if MG_ENABLE_DIRECTORY_LISTING
listdir(c, hm, opts, t2); listdir(c, hm, opts, t2);
#else #else
@ -883,7 +884,6 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
mg_http_serve_file(c, hm, t2, guess_content_type(t2), mg_http_serve_file(c, hm, t2, guess_content_type(t2),
opts->extra_headers); opts->extra_headers);
} }
if (fp != NULL) fclose(fp);
} }
} }
} }