2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "smbd/service_task.h"
25 #include "web_server/web_server.h"
26 #include "smbd/service_stream.h"
27 #include "lib/events/events.h"
28 #include "system/time.h"
29 #include "lib/appweb/esp/esp.h"
30 #include "lib/appweb/ejs/ejsInternal.h"
31 #include "dlinklist.h"
32 #include "lib/tls/tls.h"
34 #define SWAT_SESSION_KEY "SwatSessionId"
35 #define HTTP_PREAUTH_URI "/scripting/preauth.esp"
37 /* state of the esp subsystem for a specific request */
39 struct websrv_context *web;
40 struct EspRequest *req;
41 struct MprVar variables[ESP_OBJ_MAX];
42 struct session_data *session;
46 output the http headers
48 static void http_output_headers(struct websrv_context *web)
53 uint32_t content_length = 0;
54 const char *response_string = "Unknown Code";
57 const char *response_string;
63 { 304, "Not Modified" },
64 { 400, "Bad request" },
65 { 401, "Unauthorized" },
68 { 500, "Internal Server Error" },
69 { 501, "Not implemented" }
71 for (i=0;i<ARRAY_SIZE(codes);i++) {
72 if (codes[i].code == web->output.response_code) {
73 response_string = codes[i].response_string;
77 if (web->output.headers == NULL) return;
78 s = talloc_asprintf(web, "HTTP/1.0 %u %s\r\n",
79 web->output.response_code, response_string);
80 if (s == NULL) return;
81 for (i=0;web->output.headers[i];i++) {
82 s = talloc_asprintf_append(s, "%s\r\n", web->output.headers[i]);
85 /* work out the content length */
86 content_length = web->output.content.length;
87 if (web->output.fd != -1) {
89 fstat(web->output.fd, &st);
90 content_length += st.st_size;
92 s = talloc_asprintf_append(s, "Content-Length: %u\r\n\r\n", content_length);
93 if (s == NULL) return;
95 b = web->output.content;
96 web->output.content = data_blob_string_const(s);
97 data_blob_append(web, &web->output.content, b.data, b.length);
102 return the local path for a URL
104 static const char *http_local_path(struct websrv_context *web, const char *url)
109 /* check that the url is OK */
110 if (url[0] != '/') return NULL;
112 for (i=0;url[i];i++) {
113 if ((!isalnum((unsigned char)url[i]) && !strchr("./_", url[i])) ||
114 (url[i] == '.' && strchr("/.", url[i+1]))) {
119 path = talloc_asprintf(web, "%s/%s", lp_swat_directory(), url+1);
120 if (path == NULL) return NULL;
122 if (directory_exist(path)) {
123 path = talloc_asprintf_append(path, "/index.esp");
129 called when esp wants to read a file to support include() calls
131 static int http_readFile(EspHandle handle, char **buf, int *len, const char *path)
133 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
138 path = http_local_path(web, path);
139 if (path == NULL) goto failed;
141 fd = open(path, O_RDONLY);
142 if (fd == -1 || fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) goto failed;
144 *buf = talloc_size(handle, st.st_size+1);
145 if (*buf == NULL) goto failed;
147 if (read(fd, *buf, st.st_size) != st.st_size) goto failed;
149 (*buf)[st.st_size] = 0;
156 DEBUG(0,("Failed to read file %s - %s\n", path, strerror(errno)));
157 if (fd != -1) close(fd);
164 called when esp wants to find the real path of a file
166 static int http_mapToStorage(EspHandle handle, char *path, int len, const char *uri, int flags)
168 if (uri == NULL || strlen(uri) >= len) return -1;
169 strncpy(path, uri, len);
174 called when esp wants to output something
176 static int http_writeBlock(EspHandle handle, const char *buf, int size)
178 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
180 status = data_blob_append(web, &web->output.content, buf, size);
181 if (!NT_STATUS_IS_OK(status)) return -1;
189 static void http_setHeader(EspHandle handle, const char *value, bool allowMultiple)
191 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
192 char *p = strchr(value, ':');
194 if (p && !allowMultiple && web->output.headers) {
196 for (i=0;web->output.headers[i];i++) {
197 if (strncmp(web->output.headers[i], value, (p+1)-value) == 0) {
198 web->output.headers[i] = talloc_strdup(web, value);
204 web->output.headers = str_list_add(web->output.headers, value);
205 talloc_steal(web, web->output.headers);
209 set a http response code
211 static void http_setResponseCode(EspHandle handle, int code)
213 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
214 web->output.response_code = code;
218 redirect to another web page
220 static void http_redirect(EspHandle handle, int code, char *url)
222 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
223 const char *host = web->input.host;
225 /* form the full url, unless it already looks like a url */
226 if (strchr(url, ':') == NULL) {
228 struct socket_address *socket_address = socket_get_my_addr(web->conn->socket, web);
229 if (socket_address == NULL) goto internal_error;
230 host = talloc_asprintf(web, "%s:%u",
231 socket_address->addr, socket_address->port);
233 if (host == NULL) goto internal_error;
235 char *p = strrchr(web->input.url, '/');
236 if (p == web->input.url) {
237 url = talloc_asprintf(web, "http%s://%s/%s",
238 tls_enabled(web->tls)?"s":"",
241 int dirlen = p - web->input.url;
242 url = talloc_asprintf(web, "http%s://%s%*.*s/%s",
243 tls_enabled(web->tls)?"s":"",
245 dirlen, dirlen, web->input.url,
248 if (url == NULL) goto internal_error;
252 http_setHeader(handle, talloc_asprintf(web, "Location: %s", url), 0);
254 /* make sure we give a valid redirect code */
255 if (code >= 300 && code < 400) {
256 http_setResponseCode(handle, code);
258 http_setResponseCode(handle, 302);
263 http_error(web, 500, "Internal server error");
270 static void http_setCookie(EspHandle handle, const char *name, const char *value,
271 int lifetime, const char *path, bool secure)
273 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
277 buf = talloc_asprintf(web, "Set-Cookie: %s=%s; path=%s; Expires=%s; %s",
278 name, value, path?path:"/",
279 http_timestring(web, time(NULL)+lifetime),
282 buf = talloc_asprintf(web, "Set-Cookie: %s=%s; path=%s; %s",
283 name, value, path?path:"/",
286 http_setHeader(handle, "Cache-control: no-cache=\"set-cookie\"", 0);
287 http_setHeader(handle, buf, 0);
292 return the session id
294 static const char *http_getSessionId(EspHandle handle)
296 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
297 return web->session->id;
303 static void http_createSession(EspHandle handle, int timeout)
305 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
307 web->session->lifetime = timeout;
308 http_setCookie(web, SWAT_SESSION_KEY, web->session->id,
309 web->session->lifetime, "/", 0);
316 static void http_destroySession(EspHandle handle)
318 struct websrv_context *web = talloc_get_type(handle, struct websrv_context);
319 talloc_free(web->session);
325 setup for a raw http level error
327 void http_error(struct websrv_context *web, int code, const char *info)
330 s = talloc_asprintf(web,"<HTML><HEAD><TITLE>Error %u</TITLE></HEAD><BODY><H1>Error %u</H1><pre>%s</pre><p></BODY></HTML>\r\n\r\n",
333 stream_terminate_connection(web->conn, "http_error: out of memory");
336 http_writeBlock(web, s, strlen(s));
337 http_setResponseCode(web, code);
338 http_output_headers(web);
339 EVENT_FD_NOT_READABLE(web->conn->event.fde);
340 EVENT_FD_WRITEABLE(web->conn->event.fde);
341 web->output.output_pending = True;
345 map a unix error code to a http error
347 void http_error_unix(struct websrv_context *web, const char *info)
359 info = talloc_asprintf(web, "%s<p>%s<p>\n", info, strerror(errno));
360 http_error(web, code, info);
365 a simple file request
367 static void http_simple_request(struct websrv_context *web)
369 const char *url = web->input.url;
373 path = http_local_path(web, url);
374 if (path == NULL) goto invalid;
377 web->output.fd = open(path, O_RDONLY);
378 if (web->output.fd == -1) {
379 DEBUG(0,("Failed to read file %s - %s\n", path, strerror(errno)));
380 http_error_unix(web, path);
384 if (fstat(web->output.fd, &st) != 0 || !S_ISREG(st.st_mode)) {
385 close(web->output.fd);
392 http_error(web, 400, "Malformed URL");
396 setup the standard ESP arrays
398 static void http_setup_arrays(struct esp_state *esp)
400 struct websrv_context *web = esp->web;
401 struct esp_data *edata = talloc_get_type(web->task->private, struct esp_data);
402 struct EspRequest *req = esp->req;
403 struct socket_address *socket_address = socket_get_my_addr(web->conn->socket, esp);
404 struct socket_address *peer_address = socket_get_my_addr(web->conn->socket, esp);
407 #define SETVAR(type, name, value) do { \
408 const char *v = value; \
409 if (v) espSetStringVar(req, type, name, v); \
412 SETVAR(ESP_REQUEST_OBJ, "CONTENT_LENGTH",
413 talloc_asprintf(esp, "%u", web->input.content_length));
414 SETVAR(ESP_REQUEST_OBJ, "QUERY_STRING", web->input.query_string);
415 SETVAR(ESP_REQUEST_OBJ, "REQUEST_METHOD", web->input.post_request?"POST":"GET");
416 SETVAR(ESP_REQUEST_OBJ, "REQUEST_URI", web->input.url);
417 p = strrchr(web->input.url, '/');
418 SETVAR(ESP_REQUEST_OBJ, "SCRIPT_NAME", p+1);
419 SETVAR(ESP_REQUEST_OBJ, "SCRIPT_FILENAME", web->input.url);
421 SETVAR(ESP_REQUEST_OBJ, "REMOTE_ADDR", peer_address->addr);
423 p = socket_get_peer_name(web->conn->socket, esp);
424 SETVAR(ESP_REQUEST_OBJ, "REMOTE_HOST", p);
425 SETVAR(ESP_REQUEST_OBJ, "REMOTE_USER", "");
426 SETVAR(ESP_REQUEST_OBJ, "CONTENT_TYPE", web->input.content_type);
428 SETVAR(ESP_REQUEST_OBJ, "SESSION_ID", web->session->id);
430 SETVAR(ESP_REQUEST_OBJ, "COOKIE_SUPPORT", web->input.cookie?"True":"False");
432 SETVAR(ESP_HEADERS_OBJ, "HTT_REFERER", web->input.referer);
433 SETVAR(ESP_HEADERS_OBJ, "HOST", web->input.host);
434 SETVAR(ESP_HEADERS_OBJ, "ACCEPT_ENCODING", web->input.accept_encoding);
435 SETVAR(ESP_HEADERS_OBJ, "ACCEPT_LANGUAGE", web->input.accept_language);
436 SETVAR(ESP_HEADERS_OBJ, "ACCEPT_CHARSET", web->input.accept_charset);
437 SETVAR(ESP_HEADERS_OBJ, "COOKIE", web->input.cookie);
438 SETVAR(ESP_HEADERS_OBJ, "USER_AGENT", web->input.user_agent);
440 if (socket_address) {
441 SETVAR(ESP_SERVER_OBJ, "SERVER_ADDR", socket_address->addr);
442 SETVAR(ESP_SERVER_OBJ, "SERVER_NAME", socket_address->addr);
443 SETVAR(ESP_SERVER_OBJ, "SERVER_HOST", socket_address->addr);
444 SETVAR(ESP_SERVER_OBJ, "SERVER_PORT",
445 talloc_asprintf(esp, "%u", socket_address->port));
448 SETVAR(ESP_SERVER_OBJ, "DOCUMENT_ROOT", lp_swat_directory());
449 SETVAR(ESP_SERVER_OBJ, "SERVER_PROTOCOL", tls_enabled(web->tls)?"https":"http");
450 SETVAR(ESP_SERVER_OBJ, "SERVER_SOFTWARE", "SWAT");
451 SETVAR(ESP_SERVER_OBJ, "GATEWAY_INTERFACE", "CGI/1.1");
452 SETVAR(ESP_SERVER_OBJ, "TLS_SUPPORT", tls_support(edata->tls_params)?"True":"False");
456 /* the esp scripting lirary generates exceptions when
457 it hits a major error. We need to catch these and
458 report a internal server error via http
461 static jmp_buf ejs_exception_buf;
462 static const char *exception_reason;
464 void ejs_exception(const char *reason)
468 ejsSetErrorMsg(0, "%s", reason);
469 exception_reason = ep->error;
471 exception_reason = reason;
473 DEBUG(0,("%s", exception_reason));
474 longjmp(ejs_exception_buf, -1);
477 void ejs_exception(const char *reason)
479 DEBUG(0,("%s", reason));
485 process a esp request
487 static void esp_request(struct esp_state *esp, const char *url)
489 struct websrv_context *web = esp->web;
492 char *emsg = NULL, *buf;
494 if (http_readFile(web, &buf, &size, url) != 0) {
495 http_error_unix(web, url);
500 if (setjmp(ejs_exception_buf) != 0) {
501 http_error(web, 500, exception_reason);
506 res = espProcessRequest(esp->req, url, buf, &emsg);
507 if (res != 0 && emsg) {
508 http_writeBlock(web, "<pre>", 5);
509 http_writeBlock(web, emsg, strlen(emsg));
510 http_writeBlock(web, "</pre>", 6);
517 perform pre-authentication on every page is /scripting/preauth.esp
518 exists. If this script generates any non-whitepace output at all,
519 then we don't run the requested URL.
521 note that the preauth is run even for static pages such as images.
523 static BOOL http_preauth(struct esp_state *esp)
525 const char *path = http_local_path(esp->web, HTTP_PREAUTH_URI);
528 http_error(esp->web, 500, "Internal server error");
531 if (!file_exist(path)) {
532 /* if the preath script is not installed then allow access */
535 esp_request(esp, HTTP_PREAUTH_URI);
536 for (i=0;i<esp->web->output.content.length;i++) {
537 if (!isspace(esp->web->output.content.data[i])) {
538 /* if the preauth has generated content, then force it to be
539 html, so that we can show the login page for failed
541 http_setHeader(esp->web, "Content-Type: text/html", 0);
545 data_blob_free(&esp->web->output.content);
551 handling of + and % escapes in http variables
553 static const char *http_unescape(TALLOC_CTX *mem_ctx, const char *p)
555 char *s0 = talloc_strdup(mem_ctx, p);
557 if (s == NULL) return NULL;
561 if (*s == '+') *s = ' ';
562 if (*s == '%' && sscanf(s+1, "%02x", &v) == 1) {
564 memmove(s+1, s+3, strlen(s+3)+1);
573 set a form or GET variable
575 static void esp_putvar(struct esp_state *esp, const char *var, const char *value)
577 if (strcasecmp(var, SWAT_SESSION_KEY) == 0) {
578 /* special case support for browsers without cookie
580 esp->web->input.session_key = talloc_strdup(esp, value);
582 mprSetPropertyValue(&esp->variables[ESP_FORM_OBJ],
583 http_unescape(esp, var),
584 mprCreateStringVar(http_unescape(esp, value), 0));
590 parse the variables in a POST style request
592 static NTSTATUS http_parse_post(struct esp_state *esp)
594 DATA_BLOB b = esp->web->input.partial;
600 p = memchr(b.data, '&', b.length);
604 len = p - (char *)b.data;
606 line = talloc_strndup(esp, (char *)b.data, len);
607 NT_STATUS_HAVE_NO_MEMORY(line);
609 p = strchr(line,'=');
612 esp_putvar(esp, line, p+1);
627 parse the variables in a GET style request
629 static NTSTATUS http_parse_get(struct esp_state *esp)
631 struct websrv_context *web = esp->web;
635 p = strchr(web->input.url, '?');
636 web->input.query_string = p+1;
639 s = talloc_strdup(esp, esp->web->input.query_string);
640 NT_STATUS_HAVE_NO_MEMORY(s);
642 for (tok=strtok_r(s,"&;", &pp);tok;tok=strtok_r(NULL,"&;", &pp)) {
646 esp_putvar(esp, tok, p+1);
653 called when a session times out
655 static void session_timeout(struct event_context *ev, struct timed_event *te,
656 struct timeval t, void *private)
658 struct session_data *s = talloc_get_type(private, struct session_data);
665 static int session_destructor(void *ptr)
667 struct session_data *s = talloc_get_type(ptr, struct session_data);
668 DLIST_REMOVE(s->edata->sessions, s);
673 setup the session for this request
675 static void http_setup_session(struct esp_state *esp)
677 const char *session_key = SWAT_SESSION_KEY;
679 const char *cookie = esp->web->input.cookie;
680 const char *key = NULL;
681 struct esp_data *edata = talloc_get_type(esp->web->task->private, struct esp_data);
682 struct session_data *s;
683 BOOL generated_key = False;
685 /* look for our session key */
686 if (cookie && (p = strstr(cookie, session_key)) &&
687 p[strlen(session_key)] == '=') {
688 p += strlen(session_key)+1;
689 key = talloc_strndup(esp, p, strcspn(p, ";"));
692 if (key == NULL && esp->web->input.session_key) {
693 key = esp->web->input.session_key;
694 } else if (key == NULL) {
695 key = generate_random_str_list(esp, 16, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
696 generated_key = True;
699 /* try to find this session in the existing session list */
700 for (s=edata->sessions;s;s=s->next) {
701 if (strcmp(key, s->id) == 0) {
707 /* create a new session */
708 s = talloc_zero(edata, struct session_data);
709 s->id = talloc_steal(s, key);
713 s->lifetime = lp_parm_int(-1, "web", "sessiontimeout", 900);
714 DLIST_ADD(edata->sessions, s);
715 talloc_set_destructor(s, session_destructor);
716 if (!generated_key) {
717 mprSetPropertyValue(&esp->variables[ESP_REQUEST_OBJ],
718 "SESSION_EXPIRED", mprCreateStringVar("True", 0));
722 http_setCookie(esp->web, session_key, key, s->lifetime, "/", 0);
725 mprCopyVar(&esp->variables[ESP_SESSION_OBJ], s->data, MPR_DEEP_COPY);
728 esp->web->session = s;
732 /* callbacks for esp processing */
733 static const struct Esp esp_control = {
734 .maxScriptSize = 60000,
735 .writeBlock = http_writeBlock,
736 .setHeader = http_setHeader,
737 .redirect = http_redirect,
738 .setResponseCode = http_setResponseCode,
739 .readFile = http_readFile,
740 .mapToStorage = http_mapToStorage,
741 .setCookie = http_setCookie,
742 .createSession = http_createSession,
743 .destroySession = http_destroySession,
744 .getSessionId = http_getSessionId
748 process a complete http request
750 void http_process_input(struct websrv_context *web)
753 struct esp_state *esp;
754 struct esp_data *edata = talloc_get_type(web->task->private, struct esp_data);
756 void *save_mpr_ctx = mprMemCtx();
757 void *ejs_save = ejs_save_state();
759 const char *file_type = NULL;
760 BOOL esp_enable = False;
762 const char *extension;
763 const char *mime_type;
766 {"gif", "image/gif"},
767 {"png", "image/png"},
768 {"jpg", "image/jpeg"},
769 {"txt", "text/plain"},
770 {"ico", "image/x-icon"},
772 {"esp", "text/html", True}
775 esp = talloc_zero(web, struct esp_state);
776 if (esp == NULL) goto internal_error;
782 if (espOpen(&esp_control) != 0) goto internal_error;
784 for (i=0;i<ARRAY_SIZE(esp->variables);i++) {
785 esp->variables[i] = mprCreateUndefinedVar();
787 esp->variables[ESP_HEADERS_OBJ] = mprCreateObjVar("headers", ESP_HASH_SIZE);
788 esp->variables[ESP_FORM_OBJ] = mprCreateObjVar("form", ESP_HASH_SIZE);
789 esp->variables[ESP_APPLICATION_OBJ] = mprCreateObjVar("application", ESP_HASH_SIZE);
790 esp->variables[ESP_COOKIES_OBJ] = mprCreateObjVar("cookies", ESP_HASH_SIZE);
791 esp->variables[ESP_FILES_OBJ] = mprCreateObjVar("files", ESP_HASH_SIZE);
792 esp->variables[ESP_REQUEST_OBJ] = mprCreateObjVar("request", ESP_HASH_SIZE);
793 esp->variables[ESP_SERVER_OBJ] = mprCreateObjVar("server", ESP_HASH_SIZE);
794 esp->variables[ESP_SESSION_OBJ] = mprCreateObjVar("session", ESP_HASH_SIZE);
796 if (edata->application_data) {
797 mprCopyVar(&esp->variables[ESP_APPLICATION_OBJ],
798 edata->application_data, MPR_DEEP_COPY);
801 smb_setup_ejs_functions();
803 if (web->input.url == NULL) {
804 http_error(web, 400, "You must specify a GET or POST request");
805 mprSetCtx(save_mpr_ctx);
806 ejs_restore_state(ejs_save);
810 /* parse any form or get variables */
811 if (web->input.post_request) {
812 status = http_parse_post(esp);
813 if (!NT_STATUS_IS_OK(status)) {
814 http_error(web, 400, "Malformed POST data");
815 mprSetCtx(save_mpr_ctx);
816 ejs_restore_state(ejs_save);
820 if (strchr(web->input.url, '?')) {
821 status = http_parse_get(esp);
822 if (!NT_STATUS_IS_OK(status)) {
823 http_error(web, 400, "Malformed GET data");
824 mprSetCtx(save_mpr_ctx);
825 ejs_restore_state(ejs_save);
830 http_setup_session(esp);
832 esp->req = espCreateRequest(web, web->input.url, esp->variables);
833 if (esp->req == NULL) goto internal_error;
835 /* work out the mime type */
836 p = strrchr(web->input.url, '.');
840 for (i=0;p && i<ARRAY_SIZE(mime_types);i++) {
841 if (strcmp(mime_types[i].extension, p+1) == 0) {
842 file_type = mime_types[i].mime_type;
843 esp_enable = mime_types[i].esp_enable;
846 if (file_type == NULL) {
847 file_type = "text/html";
850 /* setup basic headers */
851 http_setResponseCode(web, 200);
852 http_setHeader(web, talloc_asprintf(esp, "Date: %s",
853 http_timestring(esp, time(NULL))), 0);
854 http_setHeader(web, "Server: Samba", 0);
855 http_setHeader(web, "Connection: close", 0);
856 http_setHeader(web, talloc_asprintf(esp, "Content-Type: %s", file_type), 0);
858 http_setup_arrays(esp);
860 /* possibly do pre-authentication */
861 if (http_preauth(esp)) {
863 esp_request(esp, web->input.url);
865 http_simple_request(web);
869 if (web->conn == NULL) {
870 /* the connection has been terminated above us, probably
875 if (!web->output.output_pending) {
876 http_output_headers(web);
877 EVENT_FD_WRITEABLE(web->conn->event.fde);
878 web->output.output_pending = True;
881 /* copy any application data to long term storage in edata */
882 talloc_free(edata->application_data);
883 edata->application_data = talloc_zero(edata, struct MprVar);
884 mprSetCtx(edata->application_data);
885 mprCopyVar(edata->application_data, &esp->variables[ESP_APPLICATION_OBJ],
889 /* copy any session data */
891 talloc_free(web->session->data);
892 web->session->data = talloc_zero(web->session, struct MprVar);
893 if (esp->variables[ESP_SESSION_OBJ].properties == NULL ||
894 esp->variables[ESP_SESSION_OBJ].properties[0].numItems == 0) {
895 talloc_free(web->session);
898 mprSetCtx(web->session->data);
899 mprCopyVar(web->session->data, &esp->variables[ESP_SESSION_OBJ],
901 /* setup the timeout for the session data */
903 talloc_free(web->session->te);
904 web->session->te = event_add_timed(web->conn->event.ctx, web->session,
905 timeval_current_ofs(web->session->lifetime, 0),
906 session_timeout, web->session);
911 mprSetCtx(save_mpr_ctx);
912 ejs_restore_state(ejs_save);
918 if (web->conn != NULL) {
919 http_error(web, 500, "Internal server error");
921 mprSetCtx(save_mpr_ctx);
922 ejs_restore_state(ejs_save);
927 parse one line of header input
929 NTSTATUS http_parse_header(struct websrv_context *web, const char *line)
932 web->input.end_of_headers = True;
933 } else if (strncasecmp(line,"GET ", 4)==0) {
934 web->input.url = talloc_strndup(web, &line[4], strcspn(&line[4], " \t"));
935 } else if (strncasecmp(line,"POST ", 5)==0) {
936 web->input.post_request = True;
937 web->input.url = talloc_strndup(web, &line[5], strcspn(&line[5], " \t"));
938 } else if (strchr(line, ':') == NULL) {
939 http_error(web, 400, "This server only accepts GET and POST requests");
940 return NT_STATUS_INVALID_PARAMETER;
941 } else if (strncasecmp(line,"Content-Length: ", 16)==0) {
942 web->input.content_length = strtoul(&line[16], NULL, 10);
944 #define PULL_HEADER(v, s) do { \
945 if (strncmp(line, s, strlen(s)) == 0) { \
946 web->input.v = talloc_strdup(web, &line[strlen(s)]); \
947 return NT_STATUS_OK; \
950 PULL_HEADER(content_type, "Content-Type: ");
951 PULL_HEADER(user_agent, "User-Agent: ");
952 PULL_HEADER(referer, "Referer: ");
953 PULL_HEADER(host, "Host: ");
954 PULL_HEADER(accept_encoding, "Accept-Encoding: ");
955 PULL_HEADER(accept_language, "Accept-Language: ");
956 PULL_HEADER(accept_charset, "Accept-Charset: ");
957 PULL_HEADER(cookie, "Cookie: ");
960 /* ignore all other headers for now */
966 setup the esp processor - called at task initialisation
968 NTSTATUS http_setup_esp(struct task_server *task)
970 struct esp_data *edata;
972 edata = talloc_zero(task, struct esp_data);
973 NT_STATUS_HAVE_NO_MEMORY(edata);
975 task->private = edata;
977 edata->tls_params = tls_initialise(edata);
978 NT_STATUS_HAVE_NO_MEMORY(edata->tls_params);