Commit 75059d1b authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by Cesanta Bot

Rename MG_DISABLE_CGI -> MG_ENABLE_CGI

Also factor out CGI-related code from http.c, it's getting too big.

Build tests with -Wundef

PUBLISHED_FROM=a1e82fafba73812c249db0b49c59d1b48417b60d
parent 15e9a927
...@@ -47,7 +47,6 @@ ...@@ -47,7 +47,6 @@
#define NO_LIBC #define NO_LIBC
#define MG_DISABLE_FILESYSTEM #define MG_DISABLE_FILESYSTEM
#define MG_DISABLE_POPEN #define MG_DISABLE_POPEN
#define MG_DISABLE_CGI
#define MG_DISABLE_DIRECTORY_LISTING #define MG_DISABLE_DIRECTORY_LISTING
#define MG_DISABLE_SOCKETPAIR #define MG_DISABLE_SOCKETPAIR
#define MG_DISABLE_PFS #define MG_DISABLE_PFS
...@@ -123,6 +122,15 @@ extern void *(*test_calloc)(size_t count, size_t size); ...@@ -123,6 +122,15 @@ extern void *(*test_calloc)(size_t count, size_t size);
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif #endif
#if !MG_DISABLE_HTTP && MG_ENABLE_CGI
MG_INTERNAL void mg_handle_cgi(struct mg_connection *nc, const char *prog,
const struct mg_str *path_info,
const struct http_message *hm,
const struct mg_serve_http_opts *opts);
struct mg_http_proto_data_cgi;
MG_INTERNAL void mg_http_free_proto_data_cgi(struct mg_http_proto_data_cgi *d);
#endif
#endif /* CS_MONGOOSE_SRC_INTERNAL_H_ */ #endif /* CS_MONGOOSE_SRC_INTERNAL_H_ */
#ifdef MG_MODULE_LINES #ifdef MG_MODULE_LINES
#line 1 "common/cs_dbg.h" #line 1 "common/cs_dbg.h"
...@@ -3806,11 +3814,6 @@ int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out) { ...@@ -3806,11 +3814,6 @@ int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out) {
#define MG_WS_NO_HOST_HEADER_MAGIC ((char *) 0x1) #define MG_WS_NO_HOST_HEADER_MAGIC ((char *) 0x1)
#endif #endif
/* CGI requires socketpair. */
#if MG_DISABLE_SOCKETPAIR && !MG_DISABLE_CGI
#define MG_DISABLE_CGI 1
#endif
static const char *mg_version_header = "Mongoose/" MG_VERSION; static const char *mg_version_header = "Mongoose/" MG_VERSION;
enum mg_http_proto_data_type { DATA_NONE, DATA_FILE, DATA_PUT }; enum mg_http_proto_data_type { DATA_NONE, DATA_FILE, DATA_PUT };
...@@ -3823,9 +3826,11 @@ struct mg_http_proto_data_file { ...@@ -3823,9 +3826,11 @@ struct mg_http_proto_data_file {
enum mg_http_proto_data_type type; enum mg_http_proto_data_type type;
}; };
#if MG_ENABLE_CGI
struct mg_http_proto_data_cgi { struct mg_http_proto_data_cgi {
struct mg_connection *cgi_nc; struct mg_connection *cgi_nc;
}; };
#endif
struct mg_http_proto_data_chuncked { struct mg_http_proto_data_chuncked {
int64_t body_len; /* How many bytes of chunked body was reassembled. */ int64_t body_len; /* How many bytes of chunked body was reassembled. */
...@@ -3863,7 +3868,7 @@ struct mg_http_proto_data { ...@@ -3863,7 +3868,7 @@ struct mg_http_proto_data {
#if !MG_DISABLE_FILESYSTEM #if !MG_DISABLE_FILESYSTEM
struct mg_http_proto_data_file file; struct mg_http_proto_data_file file;
#endif #endif
#if !MG_DISABLE_CGI #if MG_ENABLE_CGI
struct mg_http_proto_data_cgi cgi; struct mg_http_proto_data_cgi cgi;
#endif #endif
#if MG_ENABLE_HTTP_STREAMING_MULTIPART #if MG_ENABLE_HTTP_STREAMING_MULTIPART
...@@ -3907,15 +3912,6 @@ static void mg_http_free_proto_data_file(struct mg_http_proto_data_file *d) { ...@@ -3907,15 +3912,6 @@ static void mg_http_free_proto_data_file(struct mg_http_proto_data_file *d) {
} }
#endif #endif
#if !MG_DISABLE_CGI
static void mg_http_free_proto_data_cgi(struct mg_http_proto_data_cgi *d) {
if (d != NULL) {
if (d->cgi_nc != NULL) d->cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
memset(d, 0, sizeof(struct mg_http_proto_data_cgi));
}
}
#endif
static void mg_http_free_proto_data_endpoints(struct mg_http_endpoint **ep) { static void mg_http_free_proto_data_endpoints(struct mg_http_endpoint **ep) {
struct mg_http_endpoint *current = *ep; struct mg_http_endpoint *current = *ep;
...@@ -3934,7 +3930,7 @@ static void mg_http_conn_destructor(void *proto_data) { ...@@ -3934,7 +3930,7 @@ static void mg_http_conn_destructor(void *proto_data) {
#if !MG_DISABLE_FILESYSTEM #if !MG_DISABLE_FILESYSTEM
mg_http_free_proto_data_file(&pd->file); mg_http_free_proto_data_file(&pd->file);
#endif #endif
#if !MG_DISABLE_CGI #if MG_ENABLE_CGI
mg_http_free_proto_data_cgi(&pd->cgi); mg_http_free_proto_data_cgi(&pd->cgi);
#endif #endif
#if MG_ENABLE_HTTP_STREAMING_MULTIPART #if MG_ENABLE_HTTP_STREAMING_MULTIPART
...@@ -3944,24 +3940,6 @@ static void mg_http_conn_destructor(void *proto_data) { ...@@ -3944,24 +3940,6 @@ static void mg_http_conn_destructor(void *proto_data) {
free(proto_data); free(proto_data);
} }
/*
* This structure helps to create an environment for the spawned CGI program.
* Environment is an array of "VARIABLE=VALUE\0" ASCIIZ strings,
* last element must be NULL.
* However, on Windows there is a requirement that all these VARIABLE=VALUE\0
* strings must reside in a contiguous buffer. The end of the buffer is
* marked by two '\0' characters.
* We satisfy both worlds: we create an envp array (which is vars), all
* entries are actually pointers inside buf.
*/
struct mg_cgi_env_block {
struct mg_connection *nc;
char buf[MG_CGI_ENVIRONMENT_SIZE]; /* Environment buffer */
const char *vars[MG_MAX_CGI_ENVIR_VARS]; /* char *envp[] */
int len; /* Space taken */
int nvars; /* Number of variables in envp[] */
};
#if !MG_DISABLE_FILESYSTEM #if !MG_DISABLE_FILESYSTEM
#define MIME_ENTRY(_ext, _type) \ #define MIME_ENTRY(_ext, _type) \
...@@ -4539,7 +4517,7 @@ static void mg_http_transfer_file_data(struct mg_connection *nc) { ...@@ -4539,7 +4517,7 @@ static void mg_http_transfer_file_data(struct mg_connection *nc) {
mg_http_free_proto_data_file(&pd->file); mg_http_free_proto_data_file(&pd->file);
} }
} }
#if !MG_DISABLE_CGI #if MG_ENABLE_CGI
else if (pd->cgi.cgi_nc != NULL) { else if (pd->cgi.cgi_nc != NULL) {
/* This is POST data that needs to be forwarded to the CGI process */ /* This is POST data that needs to be forwarded to the CGI process */
if (pd->cgi.cgi_nc != NULL) { if (pd->cgi.cgi_nc != NULL) {
...@@ -6684,912 +6662,946 @@ out: ...@@ -6684,912 +6662,946 @@ out:
return ok; return ok;
} }
#if !MG_DISABLE_CGI static int mg_get_month_index(const char *s) {
#ifdef _WIN32 static const char *month_names[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
struct mg_threadparam { "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
sock_t s; size_t i;
HANDLE hPipe;
};
static int mg_wait_until_ready(sock_t sock, int for_read) { for (i = 0; i < ARRAY_SIZE(month_names); i++)
fd_set set; if (!strcmp(s, month_names[i])) return (int) i;
FD_ZERO(&set);
FD_SET(sock, &set);
return select(sock + 1, for_read ? &set : 0, for_read ? 0 : &set, 0, 0) == 1;
}
static void *mg_push_to_stdin(void *arg) { return -1;
struct mg_threadparam *tp = (struct mg_threadparam *) arg; }
int n, sent, stop = 0;
DWORD k;
char buf[BUFSIZ];
while (!stop && mg_wait_until_ready(tp->s, 1) && static int mg_num_leap_years(int year) {
(n = recv(tp->s, buf, sizeof(buf), 0)) > 0) { return year / 4 - year / 100 + year / 400;
if (n == -1 && GetLastError() == WSAEWOULDBLOCK) continue;
for (sent = 0; !stop && sent < n; sent += k) {
if (!WriteFile(tp->hPipe, buf + sent, n - sent, &k, 0)) stop = 1;
}
}
DBG(("%s", "FORWARED EVERYTHING TO CGI"));
CloseHandle(tp->hPipe);
MG_FREE(tp);
_endthread();
return NULL;
} }
static void *mg_pull_from_stdout(void *arg) { /* Parse UTC date-time string, and return the corresponding time_t value. */
struct mg_threadparam *tp = (struct mg_threadparam *) arg; MG_INTERNAL time_t mg_parse_date_string(const char *datetime) {
int k = 0, stop = 0; static const unsigned short days_before_month[] = {
DWORD n, sent; 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
char buf[BUFSIZ]; char month_str[32];
int second, minute, hour, day, month, year, leap_days, days;
time_t result = (time_t) 0;
while (!stop && ReadFile(tp->hPipe, buf, sizeof(buf), &n, NULL)) { if (((sscanf(datetime, "%d/%3s/%d %d:%d:%d", &day, month_str, &year, &hour,
for (sent = 0; !stop && sent < n; sent += k) { &minute, &second) == 6) ||
if (mg_wait_until_ready(tp->s, 0) && (sscanf(datetime, "%d %3s %d %d:%d:%d", &day, month_str, &year, &hour,
(k = send(tp->s, buf + sent, n - sent, 0)) <= 0) &minute, &second) == 6) ||
stop = 1; (sscanf(datetime, "%*3s, %d %3s %d %d:%d:%d", &day, month_str, &year,
} &hour, &minute, &second) == 6) ||
(sscanf(datetime, "%d-%3s-%d %d:%d:%d", &day, month_str, &year, &hour,
&minute, &second) == 6)) &&
year > 1970 && (month = mg_get_month_index(month_str)) != -1) {
leap_days = mg_num_leap_years(year) - mg_num_leap_years(1970);
year -= 1970;
days = year * 365 + days_before_month[month] + (day - 1) + leap_days;
result = days * 24 * 3600 + hour * 3600 + minute * 60 + second;
} }
DBG(("%s", "EOF FROM CGI"));
CloseHandle(tp->hPipe); return result;
shutdown(tp->s, 2); // Without this, IO thread may get truncated data
closesocket(tp->s);
MG_FREE(tp);
_endthread();
return NULL;
} }
static void mg_spawn_stdio_thread(sock_t sock, HANDLE hPipe, MG_INTERNAL int mg_is_not_modified(struct http_message *hm, cs_stat_t *st) {
void *(*func)(void *)) { struct mg_str *hdr;
struct mg_threadparam *tp = (struct mg_threadparam *) MG_MALLOC(sizeof(*tp)); if ((hdr = mg_get_http_header(hm, "If-None-Match")) != NULL) {
if (tp != NULL) { char etag[64];
tp->s = sock; mg_http_construct_etag(etag, sizeof(etag), st);
tp->hPipe = hPipe; return mg_vcasecmp(hdr, etag) == 0;
mg_start_thread(func, tp); } else if ((hdr = mg_get_http_header(hm, "If-Modified-Since")) != NULL) {
return st->st_mtime <= mg_parse_date_string(hdr->p);
} else {
return 0;
} }
} }
static void mg_abs_path(const char *utf8_path, char *abs_path, size_t len) { static void mg_http_send_digest_auth_request(struct mg_connection *c,
wchar_t buf[MAX_PATH_SIZE], buf2[MAX_PATH_SIZE]; const char *domain) {
to_wchar(utf8_path, buf, ARRAY_SIZE(buf)); mg_printf(c,
GetFullPathNameW(buf, ARRAY_SIZE(buf2), buf2, NULL); "HTTP/1.1 401 Unauthorized\r\n"
WideCharToMultiByte(CP_UTF8, 0, buf2, wcslen(buf2) + 1, abs_path, len, 0, 0); "WWW-Authenticate: Digest qop=\"auth\", "
"realm=\"%s\", nonce=\"%lu\"\r\n"
"Content-Length: 0\r\n\r\n",
domain, (unsigned long) time(NULL));
} }
static int mg_start_process(const char *interp, const char *cmd, static void mg_http_send_options(struct mg_connection *nc) {
const char *env, const char *envp[], mg_printf(nc, "%s",
const char *dir, sock_t sock) { "HTTP/1.1 200 OK\r\nAllow: GET, POST, HEAD, CONNECT, OPTIONS"
STARTUPINFOW si; #if !MG_DISABLE_DAV
PROCESS_INFORMATION pi; ", MKCOL, PUT, DELETE, PROPFIND, MOVE\r\nDAV: 1,2"
HANDLE a[2], b[2], me = GetCurrentProcess(); #endif
wchar_t wcmd[MAX_PATH_SIZE], full_dir[MAX_PATH_SIZE]; "\r\n\r\n");
char buf[MAX_PATH_SIZE], buf2[MAX_PATH_SIZE], buf5[MAX_PATH_SIZE], nc->flags |= MG_F_SEND_AND_CLOSE;
buf4[MAX_PATH_SIZE], cmdline[MAX_PATH_SIZE]; }
DWORD flags = DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS;
FILE *fp;
memset(&si, 0, sizeof(si)); static int mg_is_creation_request(const struct http_message *hm) {
memset(&pi, 0, sizeof(pi)); return mg_vcmp(&hm->method, "MKCOL") == 0 || mg_vcmp(&hm->method, "PUT") == 0;
}
si.cb = sizeof(si); MG_INTERNAL void mg_send_http_file(struct mg_connection *nc, char *path,
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; const struct mg_str *path_info,
si.wShowWindow = SW_HIDE; struct http_message *hm,
si.hStdError = GetStdHandle(STD_ERROR_HANDLE); struct mg_serve_http_opts *opts) {
int exists, is_directory, is_dav = mg_is_dav_request(&hm->method);
int is_cgi;
char *index_file = NULL;
cs_stat_t st;
CreatePipe(&a[0], &a[1], NULL, 0); exists = (mg_stat(path, &st) == 0);
CreatePipe(&b[0], &b[1], NULL, 0); is_directory = exists && S_ISDIR(st.st_mode);
DuplicateHandle(me, a[0], me, &si.hStdInput, 0, TRUE, flags);
DuplicateHandle(me, b[1], me, &si.hStdOutput, 0, TRUE, flags);
if (interp == NULL && (fp = fopen(cmd, "r")) != NULL) { if (is_directory)
buf[0] = buf[1] = '\0'; mg_find_index_file(path, opts->index_files, &index_file, &st);
fgets(buf, sizeof(buf), fp);
buf[sizeof(buf) - 1] = '\0';
if (buf[0] == '#' && buf[1] == '!') {
interp = buf + 2;
/* Trim leading spaces: https://github.com/cesanta/mongoose/issues/489 */
while (*interp != '\0' && isspace(*(unsigned char *) interp)) {
interp++;
}
}
fclose(fp);
}
snprintf(buf, sizeof(buf), "%s/%s", dir, cmd); is_cgi =
mg_abs_path(buf, buf2, ARRAY_SIZE(buf2)); (mg_match_prefix(opts->cgi_file_pattern, strlen(opts->cgi_file_pattern),
index_file ? index_file : path) > 0);
mg_abs_path(dir, buf5, ARRAY_SIZE(buf5)); DBG(("%p %.*s [%s] exists=%d is_dir=%d is_dav=%d is_cgi=%d index=%s", nc,
to_wchar(dir, full_dir, ARRAY_SIZE(full_dir)); (int) hm->method.len, hm->method.p, path, exists, is_directory, is_dav,
is_cgi, index_file ? index_file : ""));
if (interp != NULL) { if (is_directory && hm->uri.p[hm->uri.len - 1] != '/' && !is_dav) {
mg_abs_path(interp, buf4, ARRAY_SIZE(buf4)); mg_printf(nc,
snprintf(cmdline, sizeof(cmdline), "%s \"%s\"", buf4, buf2); "HTTP/1.1 301 Moved\r\nLocation: %.*s/\r\n"
} else { "Content-Length: 0\r\n\r\n",
snprintf(cmdline, sizeof(cmdline), "\"%s\"", buf2); (int) hm->uri.len, hm->uri.p);
MG_FREE(index_file);
return;
} }
to_wchar(cmdline, wcmd, ARRAY_SIZE(wcmd));
if (CreateProcessW(NULL, wcmd, NULL, NULL, TRUE, CREATE_NEW_PROCESS_GROUP,
(void *) env, full_dir, &si, &pi) != 0) {
mg_spawn_stdio_thread(sock, a[1], mg_push_to_stdin);
mg_spawn_stdio_thread(sock, b[0], mg_pull_from_stdout);
CloseHandle(si.hStdOutput);
CloseHandle(si.hStdInput);
CloseHandle(pi.hThread); /* If we have path_info, the only way to handle it is CGI. */
CloseHandle(pi.hProcess); if (path_info->len > 0 && !is_cgi) {
} else { mg_http_send_error(nc, 501, NULL);
CloseHandle(a[1]); MG_FREE(index_file);
CloseHandle(b[0]); return;
closesocket(sock);
} }
DBG(("CGI command: [%ls] -> %p", wcmd, pi.hProcess));
/* Not closing a[0] and b[1] because we've used DUPLICATE_CLOSE_SOURCE */ if (is_dav && opts->dav_document_root == NULL) {
(void) envp; mg_http_send_error(nc, 501, NULL);
return (pi.hProcess != NULL); } else if (!mg_is_authorized(hm, path, is_directory, opts->auth_domain,
} opts->global_auth_file, 1) ||
!mg_is_authorized(hm, path, is_directory, opts->auth_domain,
opts->per_directory_auth_file, 0)) {
mg_http_send_digest_auth_request(nc, opts->auth_domain);
} else if (is_cgi) {
#if MG_ENABLE_CGI
mg_handle_cgi(nc, index_file ? index_file : path, path_info, hm, opts);
#else #else
static int mg_start_process(const char *interp, const char *cmd, mg_http_send_error(nc, 501, NULL);
const char *env, const char *envp[], #endif /* MG_DISABLE_CGI */
const char *dir, sock_t sock) { } else if ((!exists ||
char buf[500]; mg_is_file_hidden(path, opts, 0 /* specials are ok */)) &&
pid_t pid = fork(); !mg_is_creation_request(hm)) {
(void) env; mg_http_send_error(nc, 404, NULL);
#if !MG_DISABLE_DAV
if (pid == 0) { } else if (!mg_vcmp(&hm->method, "PROPFIND")) {
/* mg_handle_propfind(nc, path, &st, hm, opts);
* In Linux `chdir` declared with `warn_unused_result` attribute #if !MG_DISABLE_DAV_AUTH
* To shutup compiler we have yo use result in some way } else if (is_dav &&
*/ (opts->dav_auth_file == NULL ||
int tmp = chdir(dir); (strcmp(opts->dav_auth_file, "-") != 0 &&
(void) tmp; !mg_is_authorized(hm, path, is_directory, opts->auth_domain,
(void) dup2(sock, 0); opts->dav_auth_file, 1)))) {
(void) dup2(sock, 1); mg_http_send_digest_auth_request(nc, opts->auth_domain);
closesocket(sock); #endif
} else if (!mg_vcmp(&hm->method, "MKCOL")) {
/* mg_handle_mkcol(nc, path, hm);
* After exec, all signal handlers are restored to their default values, } else if (!mg_vcmp(&hm->method, "DELETE")) {
* with one exception of SIGCHLD. According to POSIX.1-2001 and Linux's mg_handle_delete(nc, opts, path);
* implementation, SIGCHLD's handler will leave unchanged after exec } else if (!mg_vcmp(&hm->method, "PUT")) {
* if it was set to be ignored. Restore it to default action. mg_handle_put(nc, path, hm);
*/ } else if (!mg_vcmp(&hm->method, "MOVE")) {
signal(SIGCHLD, SIG_DFL); mg_handle_move(nc, opts, path, hm);
#if MG_ENABLE_FAKE_DAVLOCK
if (interp == NULL) { } else if (!mg_vcmp(&hm->method, "LOCK")) {
execle(cmd, cmd, (char *) 0, envp); /* (char *) 0 to squash warning */ mg_handle_lock(nc, path);
#endif
#endif
} else if (!mg_vcmp(&hm->method, "OPTIONS")) {
mg_http_send_options(nc);
} else if (is_directory && index_file == NULL) {
#if !MG_DISABLE_DIRECTORY_LISTING
if (strcmp(opts->enable_directory_listing, "yes") == 0) {
mg_send_directory_listing(nc, path, hm, opts);
} else { } else {
execle(interp, interp, cmd, (char *) 0, envp); mg_http_send_error(nc, 403, NULL);
} }
snprintf(buf, sizeof(buf), #else
"Status: 500\r\n\r\n" mg_http_send_error(nc, 501, NULL);
"500 Server Error: %s%s%s: %s", #endif
interp == NULL ? "" : interp, interp == NULL ? "" : " ", cmd, } else if (mg_is_not_modified(hm, &st)) {
strerror(errno)); mg_http_send_error(nc, 304, "Not Modified");
send(1, buf, strlen(buf), 0); } else {
exit(EXIT_FAILURE); /* exec call failed */ mg_http_serve_file2(nc, index_file ? index_file : path, hm, opts);
} }
MG_FREE(index_file);
return (pid != 0);
} }
#endif /* _WIN32 */
/* void mg_serve_http(struct mg_connection *nc, struct http_message *hm,
* Append VARIABLE=VALUE\0 string to the buffer, and add a respective struct mg_serve_http_opts opts) {
* pointer into the vars array. char *path = NULL;
*/ struct mg_str *hdr, path_info;
static char *mg_addenv(struct mg_cgi_env_block *block, const char *fmt, ...) { uint32_t remote_ip = ntohl(*(uint32_t *) &nc->sa.sin.sin_addr);
int n, space;
char *added = block->buf + block->len;
va_list ap;
/* Calculate how much space is left in the buffer */ if (mg_check_ip_acl(opts.ip_acl, remote_ip) != 1) {
space = sizeof(block->buf) - (block->len + 2); /* Not allowed to connect */
if (space > 0) { mg_http_send_error(nc, 403, NULL);
/* Copy VARIABLE=VALUE\0 string into the free space */ nc->flags |= MG_F_SEND_AND_CLOSE;
va_start(ap, fmt); return;
n = vsnprintf(added, (size_t) space, fmt, ap); }
va_end(ap);
/* Make sure we do not overflow buffer and the envp array */ if (mg_http_send_port_based_redirect(nc, hm, &opts)) {
if (n > 0 && n + 1 < space && return;
block->nvars < (int) ARRAY_SIZE(block->vars) - 2) { }
/* Append a pointer to the added string into the envp array */
block->vars[block->nvars++] = added; if (opts.document_root == NULL) {
/* Bump up used length counter. Include \0 terminator */ opts.document_root = ".";
block->len += n + 1; }
if (opts.per_directory_auth_file == NULL) {
opts.per_directory_auth_file = ".htpasswd";
}
if (opts.enable_directory_listing == NULL) {
opts.enable_directory_listing = "yes";
} }
if (opts.cgi_file_pattern == NULL) {
opts.cgi_file_pattern = "**.cgi$|**.php$";
}
if (opts.ssi_pattern == NULL) {
opts.ssi_pattern = "**.shtml$|**.shtm$";
}
if (opts.index_files == NULL) {
opts.index_files = "index.html,index.htm,index.shtml,index.cgi,index.php";
}
/* Normalize path - resolve "." and ".." (in-place). */
if (!mg_normalize_uri_path(&hm->uri, &hm->uri)) {
mg_http_send_error(nc, 400, NULL);
return;
}
if (mg_uri_to_local_path(hm, &opts, &path, &path_info) == 0) {
mg_http_send_error(nc, 404, NULL);
return;
} }
mg_send_http_file(nc, path, &path_info, hm, &opts);
return added; MG_FREE(path);
} path = NULL;
static void mg_addenv2(struct mg_cgi_env_block *blk, const char *name) { /* Close connection for non-keep-alive requests */
const char *s; if (mg_vcmp(&hm->proto, "HTTP/1.1") != 0 ||
if ((s = getenv(name)) != NULL) mg_addenv(blk, "%s=%s", name, s); ((hdr = mg_get_http_header(hm, "Connection")) != NULL &&
mg_vcmp(hdr, "keep-alive") != 0)) {
#if 0
nc->flags |= MG_F_SEND_AND_CLOSE;
#endif
}
} }
static void mg_prepare_cgi_environment(struct mg_connection *nc, #endif /* MG_DISABLE_FILESYSTEM */
const char *prog,
const struct mg_str *path_info,
const struct http_message *hm,
const struct mg_serve_http_opts *opts,
struct mg_cgi_env_block *blk) {
const char *s;
struct mg_str *h;
char *p;
size_t i;
char buf[100];
blk->len = blk->nvars = 0; /* returns 0 on success, -1 on error */
blk->nc = nc; static int mg_http_common_url_parse(const char *url, const char *schema,
const char *schema_tls, int *use_ssl,
char **addr, int *port_i,
const char **path) {
int addr_len = 0;
if ((s = getenv("SERVER_NAME")) != NULL) { if (memcmp(url, schema, strlen(schema)) == 0) {
mg_addenv(blk, "SERVER_NAME=%s", s); url += strlen(schema);
} else { } else if (memcmp(url, schema_tls, strlen(schema_tls)) == 0) {
mg_sock_to_str(nc->sock, buf, sizeof(buf), 3); url += strlen(schema_tls);
mg_addenv(blk, "SERVER_NAME=%s", buf); *use_ssl = 1;
#if !MG_ENABLE_SSL
return -1; /* SSL is not enabled, cannot do HTTPS URLs */
#endif
} }
mg_addenv(blk, "SERVER_ROOT=%s", opts->document_root);
mg_addenv(blk, "DOCUMENT_ROOT=%s", opts->document_root);
mg_addenv(blk, "SERVER_SOFTWARE=%s/%s", "Mongoose", MG_VERSION);
/* Prepare the environment block */ while (*url != '\0') {
mg_addenv(blk, "%s", "GATEWAY_INTERFACE=CGI/1.1"); *addr = (char *) MG_REALLOC(*addr, addr_len + 6 /* space for port too. */);
mg_addenv(blk, "%s", "SERVER_PROTOCOL=HTTP/1.1"); if (*addr == NULL) {
mg_addenv(blk, "%s", "REDIRECT_STATUS=200"); /* For PHP */ DBG(("OOM"));
return -1;
}
if (*url == '/') {
break;
}
if (*url == ':') *port_i = addr_len;
(*addr)[addr_len++] = *url;
(*addr)[addr_len] = '\0';
url++;
}
if (addr_len == 0) goto cleanup;
if (*port_i < 0) {
*port_i = addr_len;
strcpy(*addr + *port_i, *use_ssl ? ":443" : ":80");
} else {
*port_i = -1;
}
mg_addenv(blk, "REQUEST_METHOD=%.*s", (int) hm->method.len, hm->method.p); if (*path == NULL) *path = url;
mg_addenv(blk, "REQUEST_URI=%.*s%s%.*s", (int) hm->uri.len, hm->uri.p, if (**path == '\0') *path = "/";
hm->query_string.len == 0 ? "" : "?", (int) hm->query_string.len,
hm->query_string.p);
mg_conn_addr_to_str(nc, buf, sizeof(buf), DBG(("%s %s", *addr, *path));
MG_SOCK_STRINGIFY_REMOTE | MG_SOCK_STRINGIFY_IP);
mg_addenv(blk, "REMOTE_ADDR=%s", buf);
mg_conn_addr_to_str(nc, buf, sizeof(buf), MG_SOCK_STRINGIFY_PORT);
mg_addenv(blk, "SERVER_PORT=%s", buf);
s = hm->uri.p + hm->uri.len - path_info->len - 1; return 0;
if (*s == '/') {
const char *base_name = strrchr(prog, DIRSEP);
mg_addenv(blk, "SCRIPT_NAME=%.*s/%s", (int) (s - hm->uri.p), hm->uri.p,
(base_name != NULL ? base_name + 1 : prog));
} else {
mg_addenv(blk, "SCRIPT_NAME=%.*s", (int) (s - hm->uri.p + 1), hm->uri.p);
}
mg_addenv(blk, "SCRIPT_FILENAME=%s", prog);
if (path_info != NULL && path_info->len > 0) { cleanup:
mg_addenv(blk, "PATH_INFO=%.*s", (int) path_info->len, path_info->p); MG_FREE(*addr);
/* Not really translated... */ return -1;
mg_addenv(blk, "PATH_TRANSLATED=%.*s", (int) path_info->len, path_info->p); }
}
#if MG_ENABLE_SSL struct mg_connection *mg_connect_http_base(
mg_addenv(blk, "HTTPS=%s", nc->ssl != NULL ? "on" : "off"); struct mg_mgr *mgr, mg_event_handler_t ev_handler,
#else struct mg_connect_opts opts, const char *schema, const char *schema_ssl,
mg_addenv(blk, "HTTPS=off"); const char *url, const char **path, char **addr) {
#endif struct mg_connection *nc = NULL;
int port_i = -1;
int use_ssl = 0;
if ((h = mg_get_http_header((struct http_message *) hm, "Content-Type")) != if (mg_http_common_url_parse(url, schema, schema_ssl, &use_ssl, addr, &port_i,
NULL) { path) < 0) {
mg_addenv(blk, "CONTENT_TYPE=%.*s", (int) h->len, h->p); return NULL;
} }
if (hm->query_string.len > 0) { LOG(LL_DEBUG, ("%s use_ssl? %d", url, use_ssl));
mg_addenv(blk, "QUERY_STRING=%.*s", (int) hm->query_string.len, if (use_ssl) {
hm->query_string.p); #if MG_ENABLE_SSL
/*
* Schema requires SSL, but no SSL parameters were provided in opts.
* In order to maintain backward compatibility, use a faux-SSL with no
* verification.
*/
if (opts.ssl_ca_cert == NULL) {
opts.ssl_ca_cert = "*";
} }
#else
if ((h = mg_get_http_header((struct http_message *) hm, "Content-Length")) != MG_SET_PTRPTR(opts.error_string, "ssl is disabled");
NULL) { MG_FREE(addr);
mg_addenv(blk, "CONTENT_LENGTH=%.*s", (int) h->len, h->p); return NULL;
#endif
} }
mg_addenv2(blk, "PATH"); if ((nc = mg_connect_opt(mgr, *addr, ev_handler, opts)) != NULL) {
mg_addenv2(blk, "TMP"); mg_set_protocol_http_websocket(nc);
mg_addenv2(blk, "TEMP"); /* If the port was addred by us, restore the original host. */
mg_addenv2(blk, "TMPDIR"); if (port_i >= 0) (*addr)[port_i] = '\0';
mg_addenv2(blk, "PERLLIB"); }
mg_addenv2(blk, MG_ENV_EXPORT_TO_CGI);
#if defined(_WIN32) return nc;
mg_addenv2(blk, "COMSPEC"); }
mg_addenv2(blk, "SYSTEMROOT");
mg_addenv2(blk, "SystemDrive");
mg_addenv2(blk, "ProgramFiles");
mg_addenv2(blk, "ProgramFiles(x86)");
mg_addenv2(blk, "CommonProgramFiles(x86)");
#else
mg_addenv2(blk, "LD_LIBRARY_PATH");
#endif /* _WIN32 */
/* Add all headers as HTTP_* variables */ struct mg_connection *mg_connect_http_opt(struct mg_mgr *mgr,
for (i = 0; hm->header_names[i].len > 0; i++) { mg_event_handler_t ev_handler,
p = mg_addenv(blk, "HTTP_%.*s=%.*s", (int) hm->header_names[i].len, struct mg_connect_opts opts,
hm->header_names[i].p, (int) hm->header_values[i].len, const char *url,
hm->header_values[i].p); const char *extra_headers,
const char *post_data) {
char *addr = NULL;
const char *path = NULL;
struct mg_connection *nc = mg_connect_http_base(
mgr, ev_handler, opts, "http://", "https://", url, &path, &addr);
/* Convert variable name into uppercase, and change - to _ */ if (nc == NULL) {
for (; *p != '=' && *p != '\0'; p++) { return NULL;
if (*p == '-') *p = '_';
*p = (char) toupper(*(unsigned char *) p);
} }
mg_printf(nc, "%s %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %" SIZE_T_FMT
"\r\n%s\r\n%s",
post_data == NULL ? "GET" : "POST", path, addr,
post_data == NULL ? 0 : strlen(post_data),
extra_headers == NULL ? "" : extra_headers,
post_data == NULL ? "" : post_data);
MG_FREE(addr);
return nc;
}
struct mg_connection *mg_connect_http(struct mg_mgr *mgr,
mg_event_handler_t ev_handler,
const char *url,
const char *extra_headers,
const char *post_data) {
struct mg_connect_opts opts;
memset(&opts, 0, sizeof(opts));
return mg_connect_http_opt(mgr, ev_handler, opts, url, extra_headers,
post_data);
}
#if !MG_DISABLE_HTTP_WEBSOCKET
struct mg_connection *mg_connect_ws_opt(struct mg_mgr *mgr,
mg_event_handler_t ev_handler,
struct mg_connect_opts opts,
const char *url, const char *protocol,
const char *extra_headers) {
char *addr = NULL;
const char *path = NULL;
struct mg_connection *nc = mg_connect_http_base(
mgr, ev_handler, opts, "ws://", "wss://", url, &path, &addr);
if (nc != NULL) {
mg_send_websocket_handshake2(nc, path, addr, protocol, extra_headers);
} }
blk->vars[blk->nvars++] = NULL; MG_FREE(addr);
blk->buf[blk->len++] = '\0'; return nc;
} }
static void mg_cgi_ev_handler(struct mg_connection *cgi_nc, int ev, struct mg_connection *mg_connect_ws(struct mg_mgr *mgr,
void *ev_data) { mg_event_handler_t ev_handler,
struct mg_connection *nc = (struct mg_connection *) cgi_nc->user_data; const char *url, const char *protocol,
(void) ev_data; const char *extra_headers) {
struct mg_connect_opts opts;
memset(&opts, 0, sizeof(opts));
return mg_connect_ws_opt(mgr, ev_handler, opts, url, protocol, extra_headers);
}
#endif /* MG_DISABLE_HTTP_WEBSOCKET */
if (nc == NULL) return; size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name,
size_t var_name_len, char *file_name,
size_t file_name_len, const char **data,
size_t *data_len) {
static const char cd[] = "Content-Disposition: ";
size_t hl, bl, n, ll, pos, cdl = sizeof(cd) - 1;
switch (ev) { if (buf == NULL || buf_len <= 0) return 0;
case MG_EV_RECV: if ((hl = mg_http_get_request_len(buf, buf_len)) <= 0) return 0;
/* if (buf[0] != '-' || buf[1] != '-' || buf[2] == '\n') return 0;
* CGI script does not output reply line, like "HTTP/1.1 CODE XXXXX\n"
* It outputs headers, then body. Headers might include "Status"
* header, which changes CODE, and it might include "Location" header
* which changes CODE to 302.
*
* Therefore we do not send the output from the CGI script to the user
* until all CGI headers are received.
*
* Here we parse the output from the CGI script, and if all headers has
* been received, send appropriate reply line, and forward all
* received headers to the client.
*/
if (nc->flags & MG_F_USER_1) {
struct mbuf *io = &cgi_nc->recv_mbuf;
int len = mg_http_get_request_len(io->buf, io->len);
if (len == 0) break; /* Get boundary length */
if (len < 0 || io->len > MG_MAX_HTTP_REQUEST_SIZE) { bl = mg_get_line_len(buf, buf_len);
cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
mg_http_send_error(nc, 500, "Bad headers"); /* Loop through headers, fetch variable name and file name */
} else { var_name[0] = file_name[0] = '\0';
struct http_message hm; for (n = bl; (ll = mg_get_line_len(buf + n, hl - n)) > 0; n += ll) {
struct mg_str *h; if (mg_ncasecmp(cd, buf + n, cdl) == 0) {
mg_http_parse_headers(io->buf, io->buf + io->len, io->len, &hm); struct mg_str header;
if (mg_get_http_header(&hm, "Location") != NULL) { header.p = buf + n + cdl;
mg_printf(nc, "%s", "HTTP/1.1 302 Moved\r\n"); header.len = ll - (cdl + 2);
} else if ((h = mg_get_http_header(&hm, "Status")) != NULL) { mg_http_parse_header(&header, "name", var_name, var_name_len);
mg_printf(nc, "HTTP/1.1 %.*s\r\n", (int) h->len, h->p); mg_http_parse_header(&header, "filename", file_name, file_name_len);
} else {
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\n");
}
} }
nc->flags &= ~MG_F_USER_1;
} }
if (!(nc->flags & MG_F_USER_1)) {
mg_forward(cgi_nc, nc); /* Scan through the body, search for terminating boundary */
for (pos = hl; pos + (bl - 2) < buf_len; pos++) {
if (buf[pos] == '-' && !memcmp(buf, &buf[pos], bl - 2)) {
if (data_len != NULL) *data_len = (pos - 2) - hl;
if (data != NULL) *data = buf + hl;
return pos;
} }
break;
case MG_EV_CLOSE:
mg_http_free_proto_data_cgi(&mg_http_get_proto_data(cgi_nc)->cgi);
nc->flags |= MG_F_SEND_AND_CLOSE;
break;
} }
return 0;
} }
static void mg_handle_cgi(struct mg_connection *nc, const char *prog, void mg_register_http_endpoint(struct mg_connection *nc, const char *uri_path,
const struct mg_str *path_info, mg_event_handler_t handler) {
const struct http_message *hm, struct mg_http_proto_data *pd = NULL;
const struct mg_serve_http_opts *opts) { struct mg_http_endpoint *new_ep = NULL;
struct mg_cgi_env_block blk;
char dir[MAX_PATH_SIZE];
const char *p;
sock_t fds[2];
DBG(("%p [%s]", nc, prog)); if (nc == NULL) return;
mg_prepare_cgi_environment(nc, prog, path_info, hm, opts, &blk); new_ep = (struct mg_http_endpoint *) calloc(1, sizeof(*new_ep));
/* if (new_ep == NULL) return;
* CGI must be executed in its own directory. 'dir' must point to the
* directory containing executable program, 'p' must point to the
* executable program name relative to 'dir'.
*/
if ((p = strrchr(prog, DIRSEP)) == NULL) {
snprintf(dir, sizeof(dir), "%s", ".");
} else {
snprintf(dir, sizeof(dir), "%.*s", (int) (p - prog), prog);
prog = p + 1;
}
/* pd = mg_http_get_proto_data(nc);
* Try to create socketpair in a loop until success. mg_socketpair() new_ep->name = strdup(uri_path);
* can be interrupted by a signal and fail. new_ep->name_len = strlen(new_ep->name);
* TODO(lsm): use sigaction to restart interrupted syscall new_ep->handler = handler;
new_ep->next = pd->endpoints;
pd->endpoints = new_ep;
}
#endif /* MG_DISABLE_HTTP */
#ifdef MG_MODULE_LINES
#line 1 "mongoose/src/http_cgi.c"
#endif
/*
* Copyright (c) 2014-2016 Cesanta Software Limited
* All rights reserved
*/ */
do {
mg_socketpair(fds, SOCK_STREAM);
} while (fds[0] == INVALID_SOCKET);
if (mg_start_process(opts->cgi_interpreter, prog, blk.buf, blk.vars, dir, #if !MG_DISABLE_HTTP && MG_ENABLE_CGI
fds[1]) != 0) {
size_t n = nc->recv_mbuf.len - (hm->message.len - hm->body.len);
struct mg_connection *cgi_nc =
mg_add_sock(nc->mgr, fds[0], mg_cgi_ev_handler);
struct mg_http_proto_data *cgi_pd = mg_http_get_proto_data(cgi_nc);
cgi_pd->cgi.cgi_nc = cgi_nc;
cgi_pd->cgi.cgi_nc->user_data = nc;
nc->flags |= MG_F_USER_1;
/* Push POST data to the CGI */
if (n > 0 && n < nc->recv_mbuf.len) {
mg_send(cgi_pd->cgi.cgi_nc, hm->body.p, n);
}
mbuf_remove(&nc->recv_mbuf, nc->recv_mbuf.len);
} else {
closesocket(fds[0]);
mg_http_send_error(nc, 500, "CGI failure");
}
#ifndef _WIN32
closesocket(fds[1]); /* On Windows, CGI stdio thread closes that socket */
#endif
}
#endif
static int mg_get_month_index(const char *s) {
static const char *month_names[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
size_t i;
for (i = 0; i < ARRAY_SIZE(month_names); i++) /*
if (!strcmp(s, month_names[i])) return (int) i; * This structure helps to create an environment for the spawned CGI program.
* Environment is an array of "VARIABLE=VALUE\0" ASCIIZ strings,
* last element must be NULL.
* However, on Windows there is a requirement that all these VARIABLE=VALUE\0
* strings must reside in a contiguous buffer. The end of the buffer is
* marked by two '\0' characters.
* We satisfy both worlds: we create an envp array (which is vars), all
* entries are actually pointers inside buf.
*/
struct mg_cgi_env_block {
struct mg_connection *nc;
char buf[MG_CGI_ENVIRONMENT_SIZE]; /* Environment buffer */
const char *vars[MG_MAX_CGI_ENVIR_VARS]; /* char *envp[] */
int len; /* Space taken */
int nvars; /* Number of variables in envp[] */
};
return -1; #ifdef _WIN32
} struct mg_threadparam {
sock_t s;
HANDLE hPipe;
};
static int mg_num_leap_years(int year) { static int mg_wait_until_ready(sock_t sock, int for_read) {
return year / 4 - year / 100 + year / 400; fd_set set;
FD_ZERO(&set);
FD_SET(sock, &set);
return select(sock + 1, for_read ? &set : 0, for_read ? 0 : &set, 0, 0) == 1;
} }
/* Parse UTC date-time string, and return the corresponding time_t value. */ static void *mg_push_to_stdin(void *arg) {
MG_INTERNAL time_t mg_parse_date_string(const char *datetime) { struct mg_threadparam *tp = (struct mg_threadparam *) arg;
static const unsigned short days_before_month[] = { int n, sent, stop = 0;
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; DWORD k;
char month_str[32]; char buf[BUFSIZ];
int second, minute, hour, day, month, year, leap_days, days;
time_t result = (time_t) 0;
if (((sscanf(datetime, "%d/%3s/%d %d:%d:%d", &day, month_str, &year, &hour, while (!stop && mg_wait_until_ready(tp->s, 1) &&
&minute, &second) == 6) || (n = recv(tp->s, buf, sizeof(buf), 0)) > 0) {
(sscanf(datetime, "%d %3s %d %d:%d:%d", &day, month_str, &year, &hour, if (n == -1 && GetLastError() == WSAEWOULDBLOCK) continue;
&minute, &second) == 6) || for (sent = 0; !stop && sent < n; sent += k) {
(sscanf(datetime, "%*3s, %d %3s %d %d:%d:%d", &day, month_str, &year, if (!WriteFile(tp->hPipe, buf + sent, n - sent, &k, 0)) stop = 1;
&hour, &minute, &second) == 6) ||
(sscanf(datetime, "%d-%3s-%d %d:%d:%d", &day, month_str, &year, &hour,
&minute, &second) == 6)) &&
year > 1970 && (month = mg_get_month_index(month_str)) != -1) {
leap_days = mg_num_leap_years(year) - mg_num_leap_years(1970);
year -= 1970;
days = year * 365 + days_before_month[month] + (day - 1) + leap_days;
result = days * 24 * 3600 + hour * 3600 + minute * 60 + second;
} }
return result;
}
MG_INTERNAL int mg_is_not_modified(struct http_message *hm, cs_stat_t *st) {
struct mg_str *hdr;
if ((hdr = mg_get_http_header(hm, "If-None-Match")) != NULL) {
char etag[64];
mg_http_construct_etag(etag, sizeof(etag), st);
return mg_vcasecmp(hdr, etag) == 0;
} else if ((hdr = mg_get_http_header(hm, "If-Modified-Since")) != NULL) {
return st->st_mtime <= mg_parse_date_string(hdr->p);
} else {
return 0;
} }
DBG(("%s", "FORWARED EVERYTHING TO CGI"));
CloseHandle(tp->hPipe);
MG_FREE(tp);
_endthread();
return NULL;
} }
static void mg_http_send_digest_auth_request(struct mg_connection *c, static void *mg_pull_from_stdout(void *arg) {
const char *domain) { struct mg_threadparam *tp = (struct mg_threadparam *) arg;
mg_printf(c, int k = 0, stop = 0;
"HTTP/1.1 401 Unauthorized\r\n" DWORD n, sent;
"WWW-Authenticate: Digest qop=\"auth\", " char buf[BUFSIZ];
"realm=\"%s\", nonce=\"%lu\"\r\n"
"Content-Length: 0\r\n\r\n",
domain, (unsigned long) time(NULL));
}
static void mg_http_send_options(struct mg_connection *nc) { while (!stop && ReadFile(tp->hPipe, buf, sizeof(buf), &n, NULL)) {
mg_printf(nc, "%s", for (sent = 0; !stop && sent < n; sent += k) {
"HTTP/1.1 200 OK\r\nAllow: GET, POST, HEAD, CONNECT, OPTIONS" if (mg_wait_until_ready(tp->s, 0) &&
#if !MG_DISABLE_DAV (k = send(tp->s, buf + sent, n - sent, 0)) <= 0)
", MKCOL, PUT, DELETE, PROPFIND, MOVE\r\nDAV: 1,2" stop = 1;
#endif }
"\r\n\r\n"); }
nc->flags |= MG_F_SEND_AND_CLOSE; DBG(("%s", "EOF FROM CGI"));
CloseHandle(tp->hPipe);
shutdown(tp->s, 2); // Without this, IO thread may get truncated data
closesocket(tp->s);
MG_FREE(tp);
_endthread();
return NULL;
} }
static int mg_is_creation_request(const struct http_message *hm) { static void mg_spawn_stdio_thread(sock_t sock, HANDLE hPipe,
return mg_vcmp(&hm->method, "MKCOL") == 0 || mg_vcmp(&hm->method, "PUT") == 0; void *(*func)(void *)) {
struct mg_threadparam *tp = (struct mg_threadparam *) MG_MALLOC(sizeof(*tp));
if (tp != NULL) {
tp->s = sock;
tp->hPipe = hPipe;
mg_start_thread(func, tp);
}
} }
MG_INTERNAL void mg_send_http_file(struct mg_connection *nc, char *path, static void mg_abs_path(const char *utf8_path, char *abs_path, size_t len) {
const struct mg_str *path_info, wchar_t buf[MAX_PATH_SIZE], buf2[MAX_PATH_SIZE];
struct http_message *hm, to_wchar(utf8_path, buf, ARRAY_SIZE(buf));
struct mg_serve_http_opts *opts) { GetFullPathNameW(buf, ARRAY_SIZE(buf2), buf2, NULL);
int exists, is_directory, is_dav = mg_is_dav_request(&hm->method); WideCharToMultiByte(CP_UTF8, 0, buf2, wcslen(buf2) + 1, abs_path, len, 0, 0);
int is_cgi; }
char *index_file = NULL;
cs_stat_t st;
exists = (mg_stat(path, &st) == 0); static int mg_start_process(const char *interp, const char *cmd,
is_directory = exists && S_ISDIR(st.st_mode); const char *env, const char *envp[],
const char *dir, sock_t sock) {
STARTUPINFOW si;
PROCESS_INFORMATION pi;
HANDLE a[2], b[2], me = GetCurrentProcess();
wchar_t wcmd[MAX_PATH_SIZE], full_dir[MAX_PATH_SIZE];
char buf[MAX_PATH_SIZE], buf2[MAX_PATH_SIZE], buf5[MAX_PATH_SIZE],
buf4[MAX_PATH_SIZE], cmdline[MAX_PATH_SIZE];
DWORD flags = DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS;
FILE *fp;
if (is_directory) memset(&si, 0, sizeof(si));
mg_find_index_file(path, opts->index_files, &index_file, &st); memset(&pi, 0, sizeof(pi));
is_cgi = si.cb = sizeof(si);
(mg_match_prefix(opts->cgi_file_pattern, strlen(opts->cgi_file_pattern), si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
index_file ? index_file : path) > 0); si.wShowWindow = SW_HIDE;
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
DBG(("%p %.*s [%s] exists=%d is_dir=%d is_dav=%d is_cgi=%d index=%s", nc, CreatePipe(&a[0], &a[1], NULL, 0);
(int) hm->method.len, hm->method.p, path, exists, is_directory, is_dav, CreatePipe(&b[0], &b[1], NULL, 0);
is_cgi, index_file ? index_file : "")); DuplicateHandle(me, a[0], me, &si.hStdInput, 0, TRUE, flags);
DuplicateHandle(me, b[1], me, &si.hStdOutput, 0, TRUE, flags);
if (is_directory && hm->uri.p[hm->uri.len - 1] != '/' && !is_dav) { if (interp == NULL && (fp = fopen(cmd, "r")) != NULL) {
mg_printf(nc, buf[0] = buf[1] = '\0';
"HTTP/1.1 301 Moved\r\nLocation: %.*s/\r\n" fgets(buf, sizeof(buf), fp);
"Content-Length: 0\r\n\r\n", buf[sizeof(buf) - 1] = '\0';
(int) hm->uri.len, hm->uri.p); if (buf[0] == '#' && buf[1] == '!') {
MG_FREE(index_file); interp = buf + 2;
return; /* Trim leading spaces: https://github.com/cesanta/mongoose/issues/489 */
while (*interp != '\0' && isspace(*(unsigned char *) interp)) {
interp++;
} }
/* If we have path_info, the only way to handle it is CGI. */
if (path_info->len > 0 && !is_cgi) {
mg_http_send_error(nc, 501, NULL);
MG_FREE(index_file);
return;
} }
fclose(fp);
if (is_dav && opts->dav_document_root == NULL) {
mg_http_send_error(nc, 501, NULL);
} else if (!mg_is_authorized(hm, path, is_directory, opts->auth_domain,
opts->global_auth_file, 1) ||
!mg_is_authorized(hm, path, is_directory, opts->auth_domain,
opts->per_directory_auth_file, 0)) {
mg_http_send_digest_auth_request(nc, opts->auth_domain);
} else if (is_cgi) {
#if !MG_DISABLE_CGI
mg_handle_cgi(nc, index_file ? index_file : path, path_info, hm, opts);
#else
mg_http_send_error(nc, 501, NULL);
#endif /* MG_DISABLE_CGI */
} else if ((!exists ||
mg_is_file_hidden(path, opts, 0 /* specials are ok */)) &&
!mg_is_creation_request(hm)) {
mg_http_send_error(nc, 404, NULL);
#if !MG_DISABLE_DAV
} else if (!mg_vcmp(&hm->method, "PROPFIND")) {
mg_handle_propfind(nc, path, &st, hm, opts);
#if !MG_DISABLE_DAV_AUTH
} else if (is_dav &&
(opts->dav_auth_file == NULL ||
(strcmp(opts->dav_auth_file, "-") != 0 &&
!mg_is_authorized(hm, path, is_directory, opts->auth_domain,
opts->dav_auth_file, 1)))) {
mg_http_send_digest_auth_request(nc, opts->auth_domain);
#endif
} else if (!mg_vcmp(&hm->method, "MKCOL")) {
mg_handle_mkcol(nc, path, hm);
} else if (!mg_vcmp(&hm->method, "DELETE")) {
mg_handle_delete(nc, opts, path);
} else if (!mg_vcmp(&hm->method, "PUT")) {
mg_handle_put(nc, path, hm);
} else if (!mg_vcmp(&hm->method, "MOVE")) {
mg_handle_move(nc, opts, path, hm);
#if MG_ENABLE_FAKE_DAVLOCK
} else if (!mg_vcmp(&hm->method, "LOCK")) {
mg_handle_lock(nc, path);
#endif
#endif
} else if (!mg_vcmp(&hm->method, "OPTIONS")) {
mg_http_send_options(nc);
} else if (is_directory && index_file == NULL) {
#if !MG_DISABLE_DIRECTORY_LISTING
if (strcmp(opts->enable_directory_listing, "yes") == 0) {
mg_send_directory_listing(nc, path, hm, opts);
} else {
mg_http_send_error(nc, 403, NULL);
} }
#else
mg_http_send_error(nc, 501, NULL); snprintf(buf, sizeof(buf), "%s/%s", dir, cmd);
#endif mg_abs_path(buf, buf2, ARRAY_SIZE(buf2));
} else if (mg_is_not_modified(hm, &st)) {
mg_http_send_error(nc, 304, "Not Modified"); mg_abs_path(dir, buf5, ARRAY_SIZE(buf5));
to_wchar(dir, full_dir, ARRAY_SIZE(full_dir));
if (interp != NULL) {
mg_abs_path(interp, buf4, ARRAY_SIZE(buf4));
snprintf(cmdline, sizeof(cmdline), "%s \"%s\"", buf4, buf2);
} else { } else {
mg_http_serve_file2(nc, index_file ? index_file : path, hm, opts); snprintf(cmdline, sizeof(cmdline), "\"%s\"", buf2);
} }
MG_FREE(index_file); to_wchar(cmdline, wcmd, ARRAY_SIZE(wcmd));
}
void mg_serve_http(struct mg_connection *nc, struct http_message *hm, if (CreateProcessW(NULL, wcmd, NULL, NULL, TRUE, CREATE_NEW_PROCESS_GROUP,
struct mg_serve_http_opts opts) { (void *) env, full_dir, &si, &pi) != 0) {
char *path = NULL; mg_spawn_stdio_thread(sock, a[1], mg_push_to_stdin);
struct mg_str *hdr, path_info; mg_spawn_stdio_thread(sock, b[0], mg_pull_from_stdout);
uint32_t remote_ip = ntohl(*(uint32_t *) &nc->sa.sin.sin_addr);
if (mg_check_ip_acl(opts.ip_acl, remote_ip) != 1) { CloseHandle(si.hStdOutput);
/* Not allowed to connect */ CloseHandle(si.hStdInput);
mg_http_send_error(nc, 403, NULL);
nc->flags |= MG_F_SEND_AND_CLOSE;
return;
}
if (mg_http_send_port_based_redirect(nc, hm, &opts)) { CloseHandle(pi.hThread);
return; CloseHandle(pi.hProcess);
} else {
CloseHandle(a[1]);
CloseHandle(b[0]);
closesocket(sock);
} }
DBG(("CGI command: [%ls] -> %p", wcmd, pi.hProcess));
if (opts.document_root == NULL) { /* Not closing a[0] and b[1] because we've used DUPLICATE_CLOSE_SOURCE */
opts.document_root = "."; (void) envp;
} return (pi.hProcess != NULL);
if (opts.per_directory_auth_file == NULL) { }
opts.per_directory_auth_file = ".htpasswd"; #else
} static int mg_start_process(const char *interp, const char *cmd,
if (opts.enable_directory_listing == NULL) { const char *env, const char *envp[],
opts.enable_directory_listing = "yes"; const char *dir, sock_t sock) {
} char buf[500];
if (opts.cgi_file_pattern == NULL) { pid_t pid = fork();
opts.cgi_file_pattern = "**.cgi$|**.php$"; (void) env;
}
if (opts.ssi_pattern == NULL) {
opts.ssi_pattern = "**.shtml$|**.shtm$";
}
if (opts.index_files == NULL) {
opts.index_files = "index.html,index.htm,index.shtml,index.cgi,index.php";
}
/* Normalize path - resolve "." and ".." (in-place). */
if (!mg_normalize_uri_path(&hm->uri, &hm->uri)) {
mg_http_send_error(nc, 400, NULL);
return;
}
if (mg_uri_to_local_path(hm, &opts, &path, &path_info) == 0) {
mg_http_send_error(nc, 404, NULL);
return;
}
mg_send_http_file(nc, path, &path_info, hm, &opts);
MG_FREE(path); if (pid == 0) {
path = NULL; /*
* In Linux `chdir` declared with `warn_unused_result` attribute
* To shutup compiler we have yo use result in some way
*/
int tmp = chdir(dir);
(void) tmp;
(void) dup2(sock, 0);
(void) dup2(sock, 1);
closesocket(sock);
/* Close connection for non-keep-alive requests */ /*
if (mg_vcmp(&hm->proto, "HTTP/1.1") != 0 || * After exec, all signal handlers are restored to their default values,
((hdr = mg_get_http_header(hm, "Connection")) != NULL && * with one exception of SIGCHLD. According to POSIX.1-2001 and Linux's
mg_vcmp(hdr, "keep-alive") != 0)) { * implementation, SIGCHLD's handler will leave unchanged after exec
#if 0 * if it was set to be ignored. Restore it to default action.
nc->flags |= MG_F_SEND_AND_CLOSE; */
#endif signal(SIGCHLD, SIG_DFL);
if (interp == NULL) {
execle(cmd, cmd, (char *) 0, envp); /* (char *) 0 to squash warning */
} else {
execle(interp, interp, cmd, (char *) 0, envp);
}
snprintf(buf, sizeof(buf),
"Status: 500\r\n\r\n"
"500 Server Error: %s%s%s: %s",
interp == NULL ? "" : interp, interp == NULL ? "" : " ", cmd,
strerror(errno));
send(1, buf, strlen(buf), 0);
exit(EXIT_FAILURE); /* exec call failed */
} }
}
#endif /* MG_DISABLE_FILESYSTEM */ return (pid != 0);
}
#endif /* _WIN32 */
/* returns 0 on success, -1 on error */ /*
static int mg_http_common_url_parse(const char *url, const char *schema, * Append VARIABLE=VALUE\0 string to the buffer, and add a respective
const char *schema_tls, int *use_ssl, * pointer into the vars array.
char **addr, int *port_i, */
const char **path) { static char *mg_addenv(struct mg_cgi_env_block *block, const char *fmt, ...) {
int addr_len = 0; int n, space;
char *added = block->buf + block->len;
va_list ap;
if (memcmp(url, schema, strlen(schema)) == 0) { /* Calculate how much space is left in the buffer */
url += strlen(schema); space = sizeof(block->buf) - (block->len + 2);
} else if (memcmp(url, schema_tls, strlen(schema_tls)) == 0) { if (space > 0) {
url += strlen(schema_tls); /* Copy VARIABLE=VALUE\0 string into the free space */
*use_ssl = 1; va_start(ap, fmt);
#if !MG_ENABLE_SSL n = vsnprintf(added, (size_t) space, fmt, ap);
return -1; /* SSL is not enabled, cannot do HTTPS URLs */ va_end(ap);
#endif
}
while (*url != '\0') { /* Make sure we do not overflow buffer and the envp array */
*addr = (char *) MG_REALLOC(*addr, addr_len + 6 /* space for port too. */); if (n > 0 && n + 1 < space &&
if (*addr == NULL) { block->nvars < (int) ARRAY_SIZE(block->vars) - 2) {
DBG(("OOM")); /* Append a pointer to the added string into the envp array */
return -1; block->vars[block->nvars++] = added;
} /* Bump up used length counter. Include \0 terminator */
if (*url == '/') { block->len += n + 1;
break;
}
if (*url == ':') *port_i = addr_len;
(*addr)[addr_len++] = *url;
(*addr)[addr_len] = '\0';
url++;
} }
if (addr_len == 0) goto cleanup;
if (*port_i < 0) {
*port_i = addr_len;
strcpy(*addr + *port_i, *use_ssl ? ":443" : ":80");
} else {
*port_i = -1;
} }
if (*path == NULL) *path = url; return added;
}
if (**path == '\0') *path = "/"; static void mg_addenv2(struct mg_cgi_env_block *blk, const char *name) {
const char *s;
if ((s = getenv(name)) != NULL) mg_addenv(blk, "%s=%s", name, s);
}
DBG(("%s %s", *addr, *path)); static void mg_prepare_cgi_environment(struct mg_connection *nc,
const char *prog,
const struct mg_str *path_info,
const struct http_message *hm,
const struct mg_serve_http_opts *opts,
struct mg_cgi_env_block *blk) {
const char *s;
struct mg_str *h;
char *p;
size_t i;
char buf[100];
return 0; blk->len = blk->nvars = 0;
blk->nc = nc;
cleanup: if ((s = getenv("SERVER_NAME")) != NULL) {
MG_FREE(*addr); mg_addenv(blk, "SERVER_NAME=%s", s);
return -1; } else {
} mg_sock_to_str(nc->sock, buf, sizeof(buf), 3);
mg_addenv(blk, "SERVER_NAME=%s", buf);
}
mg_addenv(blk, "SERVER_ROOT=%s", opts->document_root);
mg_addenv(blk, "DOCUMENT_ROOT=%s", opts->document_root);
mg_addenv(blk, "SERVER_SOFTWARE=%s/%s", "Mongoose", MG_VERSION);
struct mg_connection *mg_connect_http_base( /* Prepare the environment block */
struct mg_mgr *mgr, mg_event_handler_t ev_handler, mg_addenv(blk, "%s", "GATEWAY_INTERFACE=CGI/1.1");
struct mg_connect_opts opts, const char *schema, const char *schema_ssl, mg_addenv(blk, "%s", "SERVER_PROTOCOL=HTTP/1.1");
const char *url, const char **path, char **addr) { mg_addenv(blk, "%s", "REDIRECT_STATUS=200"); /* For PHP */
struct mg_connection *nc = NULL;
int port_i = -1;
int use_ssl = 0;
if (mg_http_common_url_parse(url, schema, schema_ssl, &use_ssl, addr, &port_i, mg_addenv(blk, "REQUEST_METHOD=%.*s", (int) hm->method.len, hm->method.p);
path) < 0) {
return NULL;
}
LOG(LL_DEBUG, ("%s use_ssl? %d", url, use_ssl)); mg_addenv(blk, "REQUEST_URI=%.*s%s%.*s", (int) hm->uri.len, hm->uri.p,
if (use_ssl) { hm->query_string.len == 0 ? "" : "?", (int) hm->query_string.len,
#if MG_ENABLE_SSL hm->query_string.p);
/*
* Schema requires SSL, but no SSL parameters were provided in opts. mg_conn_addr_to_str(nc, buf, sizeof(buf),
* In order to maintain backward compatibility, use a faux-SSL with no MG_SOCK_STRINGIFY_REMOTE | MG_SOCK_STRINGIFY_IP);
* verification. mg_addenv(blk, "REMOTE_ADDR=%s", buf);
*/ mg_conn_addr_to_str(nc, buf, sizeof(buf), MG_SOCK_STRINGIFY_PORT);
if (opts.ssl_ca_cert == NULL) { mg_addenv(blk, "SERVER_PORT=%s", buf);
opts.ssl_ca_cert = "*";
} s = hm->uri.p + hm->uri.len - path_info->len - 1;
#else if (*s == '/') {
MG_SET_PTRPTR(opts.error_string, "ssl is disabled"); const char *base_name = strrchr(prog, DIRSEP);
MG_FREE(addr); mg_addenv(blk, "SCRIPT_NAME=%.*s/%s", (int) (s - hm->uri.p), hm->uri.p,
return NULL; (base_name != NULL ? base_name + 1 : prog));
#endif } else {
mg_addenv(blk, "SCRIPT_NAME=%.*s", (int) (s - hm->uri.p + 1), hm->uri.p);
} }
mg_addenv(blk, "SCRIPT_FILENAME=%s", prog);
if ((nc = mg_connect_opt(mgr, *addr, ev_handler, opts)) != NULL) { if (path_info != NULL && path_info->len > 0) {
mg_set_protocol_http_websocket(nc); mg_addenv(blk, "PATH_INFO=%.*s", (int) path_info->len, path_info->p);
/* If the port was addred by us, restore the original host. */ /* Not really translated... */
if (port_i >= 0) (*addr)[port_i] = '\0'; mg_addenv(blk, "PATH_TRANSLATED=%.*s", (int) path_info->len, path_info->p);
} }
return nc; #if MG_ENABLE_SSL
} mg_addenv(blk, "HTTPS=%s", nc->ssl != NULL ? "on" : "off");
#else
mg_addenv(blk, "HTTPS=off");
#endif
struct mg_connection *mg_connect_http_opt(struct mg_mgr *mgr, if ((h = mg_get_http_header((struct http_message *) hm, "Content-Type")) !=
mg_event_handler_t ev_handler, NULL) {
struct mg_connect_opts opts, mg_addenv(blk, "CONTENT_TYPE=%.*s", (int) h->len, h->p);
const char *url, }
const char *extra_headers,
const char *post_data) {
char *addr = NULL;
const char *path = NULL;
struct mg_connection *nc = mg_connect_http_base(
mgr, ev_handler, opts, "http://", "https://", url, &path, &addr);
if (nc == NULL) { if (hm->query_string.len > 0) {
return NULL; mg_addenv(blk, "QUERY_STRING=%.*s", (int) hm->query_string.len,
hm->query_string.p);
} }
mg_printf(nc, "%s %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %" SIZE_T_FMT if ((h = mg_get_http_header((struct http_message *) hm, "Content-Length")) !=
"\r\n%s\r\n%s", NULL) {
post_data == NULL ? "GET" : "POST", path, addr, mg_addenv(blk, "CONTENT_LENGTH=%.*s", (int) h->len, h->p);
post_data == NULL ? 0 : strlen(post_data), }
extra_headers == NULL ? "" : extra_headers,
post_data == NULL ? "" : post_data);
MG_FREE(addr); mg_addenv2(blk, "PATH");
return nc; mg_addenv2(blk, "TMP");
} mg_addenv2(blk, "TEMP");
mg_addenv2(blk, "TMPDIR");
mg_addenv2(blk, "PERLLIB");
mg_addenv2(blk, MG_ENV_EXPORT_TO_CGI);
struct mg_connection *mg_connect_http(struct mg_mgr *mgr, #ifdef _WIN32
mg_event_handler_t ev_handler, mg_addenv2(blk, "COMSPEC");
const char *url, mg_addenv2(blk, "SYSTEMROOT");
const char *extra_headers, mg_addenv2(blk, "SystemDrive");
const char *post_data) { mg_addenv2(blk, "ProgramFiles");
struct mg_connect_opts opts; mg_addenv2(blk, "ProgramFiles(x86)");
memset(&opts, 0, sizeof(opts)); mg_addenv2(blk, "CommonProgramFiles(x86)");
return mg_connect_http_opt(mgr, ev_handler, opts, url, extra_headers, #else
post_data); mg_addenv2(blk, "LD_LIBRARY_PATH");
} #endif /* _WIN32 */
#if !MG_DISABLE_HTTP_WEBSOCKET /* Add all headers as HTTP_* variables */
struct mg_connection *mg_connect_ws_opt(struct mg_mgr *mgr, for (i = 0; hm->header_names[i].len > 0; i++) {
mg_event_handler_t ev_handler, p = mg_addenv(blk, "HTTP_%.*s=%.*s", (int) hm->header_names[i].len,
struct mg_connect_opts opts, hm->header_names[i].p, (int) hm->header_values[i].len,
const char *url, const char *protocol, hm->header_values[i].p);
const char *extra_headers) {
char *addr = NULL;
const char *path = NULL;
struct mg_connection *nc = mg_connect_http_base(
mgr, ev_handler, opts, "ws://", "wss://", url, &path, &addr);
if (nc != NULL) { /* Convert variable name into uppercase, and change - to _ */
mg_send_websocket_handshake2(nc, path, addr, protocol, extra_headers); for (; *p != '=' && *p != '\0'; p++) {
if (*p == '-') *p = '_';
*p = (char) toupper(*(unsigned char *) p);
}
} }
MG_FREE(addr); blk->vars[blk->nvars++] = NULL;
return nc; blk->buf[blk->len++] = '\0';
}
struct mg_connection *mg_connect_ws(struct mg_mgr *mgr,
mg_event_handler_t ev_handler,
const char *url, const char *protocol,
const char *extra_headers) {
struct mg_connect_opts opts;
memset(&opts, 0, sizeof(opts));
return mg_connect_ws_opt(mgr, ev_handler, opts, url, protocol, extra_headers);
} }
#endif /* MG_DISABLE_HTTP_WEBSOCKET */
size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name, static void mg_cgi_ev_handler(struct mg_connection *cgi_nc, int ev,
size_t var_name_len, char *file_name, void *ev_data) {
size_t file_name_len, const char **data, struct mg_connection *nc = (struct mg_connection *) cgi_nc->user_data;
size_t *data_len) { (void) ev_data;
static const char cd[] = "Content-Disposition: ";
size_t hl, bl, n, ll, pos, cdl = sizeof(cd) - 1;
if (buf == NULL || buf_len <= 0) return 0; if (nc == NULL) return;
if ((hl = mg_http_get_request_len(buf, buf_len)) <= 0) return 0;
if (buf[0] != '-' || buf[1] != '-' || buf[2] == '\n') return 0;
/* Get boundary length */ switch (ev) {
bl = mg_get_line_len(buf, buf_len); case MG_EV_RECV:
/*
* CGI script does not output reply line, like "HTTP/1.1 CODE XXXXX\n"
* It outputs headers, then body. Headers might include "Status"
* header, which changes CODE, and it might include "Location" header
* which changes CODE to 302.
*
* Therefore we do not send the output from the CGI script to the user
* until all CGI headers are received.
*
* Here we parse the output from the CGI script, and if all headers has
* been received, send appropriate reply line, and forward all
* received headers to the client.
*/
if (nc->flags & MG_F_USER_1) {
struct mbuf *io = &cgi_nc->recv_mbuf;
int len = mg_http_get_request_len(io->buf, io->len);
/* Loop through headers, fetch variable name and file name */ if (len == 0) break;
var_name[0] = file_name[0] = '\0'; if (len < 0 || io->len > MG_MAX_HTTP_REQUEST_SIZE) {
for (n = bl; (ll = mg_get_line_len(buf + n, hl - n)) > 0; n += ll) { cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
if (mg_ncasecmp(cd, buf + n, cdl) == 0) { mg_http_send_error(nc, 500, "Bad headers");
struct mg_str header; } else {
header.p = buf + n + cdl; struct http_message hm;
header.len = ll - (cdl + 2); struct mg_str *h;
mg_http_parse_header(&header, "name", var_name, var_name_len); mg_http_parse_headers(io->buf, io->buf + io->len, io->len, &hm);
mg_http_parse_header(&header, "filename", file_name, file_name_len); if (mg_get_http_header(&hm, "Location") != NULL) {
mg_printf(nc, "%s", "HTTP/1.1 302 Moved\r\n");
} else if ((h = mg_get_http_header(&hm, "Status")) != NULL) {
mg_printf(nc, "HTTP/1.1 %.*s\r\n", (int) h->len, h->p);
} else {
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\n");
} }
} }
nc->flags &= ~MG_F_USER_1;
/* Scan through the body, search for terminating boundary */ }
for (pos = hl; pos + (bl - 2) < buf_len; pos++) { if (!(nc->flags & MG_F_USER_1)) {
if (buf[pos] == '-' && !memcmp(buf, &buf[pos], bl - 2)) { mg_forward(cgi_nc, nc);
if (data_len != NULL) *data_len = (pos - 2) - hl;
if (data != NULL) *data = buf + hl;
return pos;
} }
break;
case MG_EV_CLOSE:
mg_http_free_proto_data_cgi(&mg_http_get_proto_data(cgi_nc)->cgi);
nc->flags |= MG_F_SEND_AND_CLOSE;
break;
} }
return 0;
} }
void mg_register_http_endpoint(struct mg_connection *nc, const char *uri_path, MG_INTERNAL void mg_handle_cgi(struct mg_connection *nc, const char *prog,
mg_event_handler_t handler) { const struct mg_str *path_info,
struct mg_http_proto_data *pd = NULL; const struct http_message *hm,
struct mg_http_endpoint *new_ep = NULL; const struct mg_serve_http_opts *opts) {
struct mg_cgi_env_block blk;
char dir[MAX_PATH_SIZE];
const char *p;
sock_t fds[2];
if (nc == NULL) return; DBG(("%p [%s]", nc, prog));
new_ep = (struct mg_http_endpoint *) calloc(1, sizeof(*new_ep)); mg_prepare_cgi_environment(nc, prog, path_info, hm, opts, &blk);
if (new_ep == NULL) return; /*
* CGI must be executed in its own directory. 'dir' must point to the
* directory containing executable program, 'p' must point to the
* executable program name relative to 'dir'.
*/
if ((p = strrchr(prog, DIRSEP)) == NULL) {
snprintf(dir, sizeof(dir), "%s", ".");
} else {
snprintf(dir, sizeof(dir), "%.*s", (int) (p - prog), prog);
prog = p + 1;
}
pd = mg_http_get_proto_data(nc); /*
new_ep->name = strdup(uri_path); * Try to create socketpair in a loop until success. mg_socketpair()
new_ep->name_len = strlen(new_ep->name); * can be interrupted by a signal and fail.
new_ep->handler = handler; * TODO(lsm): use sigaction to restart interrupted syscall
new_ep->next = pd->endpoints; */
pd->endpoints = new_ep; do {
mg_socketpair(fds, SOCK_STREAM);
} while (fds[0] == INVALID_SOCKET);
if (mg_start_process(opts->cgi_interpreter, prog, blk.buf, blk.vars, dir,
fds[1]) != 0) {
size_t n = nc->recv_mbuf.len - (hm->message.len - hm->body.len);
struct mg_connection *cgi_nc =
mg_add_sock(nc->mgr, fds[0], mg_cgi_ev_handler);
struct mg_http_proto_data *cgi_pd = mg_http_get_proto_data(cgi_nc);
cgi_pd->cgi.cgi_nc = cgi_nc;
cgi_pd->cgi.cgi_nc->user_data = nc;
nc->flags |= MG_F_USER_1;
/* Push POST data to the CGI */
if (n > 0 && n < nc->recv_mbuf.len) {
mg_send(cgi_pd->cgi.cgi_nc, hm->body.p, n);
}
mbuf_remove(&nc->recv_mbuf, nc->recv_mbuf.len);
} else {
closesocket(fds[0]);
mg_http_send_error(nc, 500, "CGI failure");
}
#ifndef _WIN32
closesocket(fds[1]); /* On Windows, CGI stdio thread closes that socket */
#endif
} }
#endif /* MG_DISABLE_HTTP */ MG_INTERNAL void mg_http_free_proto_data_cgi(struct mg_http_proto_data_cgi *d) {
if (d != NULL) {
if (d->cgi_nc != NULL) d->cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
memset(d, 0, sizeof(struct mg_http_proto_data_cgi));
}
}
#endif /* MG_ENABLE_HTTP && MG_ENABLE_CGI */
#ifdef MG_MODULE_LINES #ifdef MG_MODULE_LINES
#line 1 "mongoose/src/util.c" #line 1 "mongoose/src/util.c"
#endif #endif
......
...@@ -454,7 +454,6 @@ void mg_lwip_set_keepalive_params(struct mg_connection *nc, int idle, ...@@ -454,7 +454,6 @@ void mg_lwip_set_keepalive_params(struct mg_connection *nc, int idle,
#define MG_DISABLE_SOCKETPAIR 1 #define MG_DISABLE_SOCKETPAIR 1
#define MG_DISABLE_SYNC_RESOLVER 1 #define MG_DISABLE_SYNC_RESOLVER 1
#define MG_DISABLE_POPEN 1 #define MG_DISABLE_POPEN 1
#define MG_DISABLE_CGI 1
#define MG_DISABLE_DAV 1 #define MG_DISABLE_DAV 1
#define MG_DISABLE_DIRECTORY_LISTING 1 #define MG_DISABLE_DIRECTORY_LISTING 1
#define MG_DISABLE_FILESYSTEM 1 #define MG_DISABLE_FILESYSTEM 1
...@@ -514,7 +513,6 @@ int inet_pton(int af, const char *src, void *dst); ...@@ -514,7 +513,6 @@ int inet_pton(int af, const char *src, void *dst);
#define MG_DISABLE_SOCKETPAIR 1 #define MG_DISABLE_SOCKETPAIR 1
#define MG_DISABLE_SYNC_RESOLVER 1 #define MG_DISABLE_SYNC_RESOLVER 1
#define MG_DISABLE_POPEN 1 #define MG_DISABLE_POPEN 1
#define MG_DISABLE_CGI 1
/* Only SPIFFS supports directories, SLFS does not. */ /* Only SPIFFS supports directories, SLFS does not. */
#ifndef CC3200_FS_SPIFFS #ifndef CC3200_FS_SPIFFS
#define MG_DISABLE_DAV 1 #define MG_DISABLE_DAV 1
...@@ -644,7 +642,6 @@ struct dirent *readdir(DIR *dir); ...@@ -644,7 +642,6 @@ struct dirent *readdir(DIR *dir);
#define MG_DISABLE_SOCKETPAIR 1 #define MG_DISABLE_SOCKETPAIR 1
#define MG_DISABLE_SYNC_RESOLVER 1 #define MG_DISABLE_SYNC_RESOLVER 1
#define MG_DISABLE_POPEN 1 #define MG_DISABLE_POPEN 1
#define MG_DISABLE_CGI 1
#define MG_DISABLE_DAV 1 #define MG_DISABLE_DAV 1
#define MG_DISABLE_DIRECTORY_LISTING 1 #define MG_DISABLE_DIRECTORY_LISTING 1
...@@ -1186,10 +1183,6 @@ const char *c_strnstr(const char *s, const char *find, size_t slen); ...@@ -1186,10 +1183,6 @@ const char *c_strnstr(const char *s, const char *find, size_t slen);
#ifndef CS_MONGOOSE_SRC_FEATURES_H_ #ifndef CS_MONGOOSE_SRC_FEATURES_H_
#define CS_MONGOOSE_SRC_FEATURES_H_ #define CS_MONGOOSE_SRC_FEATURES_H_
#ifndef MG_DISABLE_CGI
#define MG_DISABLE_CGI 0
#endif
#ifndef MG_DISABLE_DIRECTORY_LISTING #ifndef MG_DISABLE_DIRECTORY_LISTING
#define MG_DISABLE_DIRECTORY_LISTING 0 #define MG_DISABLE_DIRECTORY_LISTING 0
#endif #endif
...@@ -1262,6 +1255,10 @@ const char *c_strnstr(const char *s, const char *find, size_t slen); ...@@ -1262,6 +1255,10 @@ const char *c_strnstr(const char *s, const char *find, size_t slen);
#define MG_DISABLE_WS_RANDOM_MASK 0 #define MG_DISABLE_WS_RANDOM_MASK 0
#endif #endif
#ifndef MG_ENABLE_CGI
#define MG_ENABLE_CGI (CS_PLATFORM == CS_P_UNIX || CS_PLATFORM == CS_P_WINDOWS)
#endif
#ifndef MG_ENABLE_COAP #ifndef MG_ENABLE_COAP
#define MG_ENABLE_COAP 0 #define MG_ENABLE_COAP 0
#endif #endif
......
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