From ef5a04485ae256f9b803da9173d6194ced82e358 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 23 May 2008 20:40:05 +0200 Subject: Rename http to esp, in preparation of adding a python backend. --- source4/web_server/web_server.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source4/web_server/web_server.c') diff --git a/source4/web_server/web_server.c b/source4/web_server/web_server.c index ac83a3384d..bfbd254d9e 100644 --- a/source4/web_server/web_server.c +++ b/source4/web_server/web_server.c @@ -280,8 +280,8 @@ static void websrv_task_init(struct task_server *task) /* startup the esp processor - unfortunately we can't do this per connection as that wouldn't allow for session variables */ - status = http_setup_esp(task); - if (!NT_STATUS_IS_OK(status)) goto failed; + task->private = http_setup_esp(task, task->lp_ctx); + if (task->private == NULL) goto failed; return; -- cgit From fda85985e91179f8e03538581335326f54456f4f Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sat, 24 May 2008 18:13:30 +0200 Subject: Remove some dependencies of the web server on esp. --- source4/web_server/web_server.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'source4/web_server/web_server.c') diff --git a/source4/web_server/web_server.c b/source4/web_server/web_server.c index bfbd254d9e..6d8b2a2758 100644 --- a/source4/web_server/web_server.c +++ b/source4/web_server/web_server.c @@ -123,7 +123,7 @@ static void websrv_recv(struct stream_connection *conn, uint16_t flags) destroy the stack variables being used by that rendering process when we handle the timeout. */ if (!talloc_reference(web->task, web)) goto failed; - http_process_input(web); + web->http_process_input(web); talloc_unlink(web->task, web); } return; @@ -192,13 +192,14 @@ static void websrv_send(struct stream_connection *conn, uint16_t flags) static void websrv_accept(struct stream_connection *conn) { struct task_server *task = talloc_get_type(conn->private, struct task_server); - struct esp_data *edata = talloc_get_type(task->private, struct esp_data); + struct web_server_data *wdata = talloc_get_type(task->private, struct web_server_data); struct websrv_context *web; struct socket_context *tls_socket; web = talloc_zero(conn, struct websrv_context); if (web == NULL) goto failed; + web->http_process_input = esp_process_http_input; web->task = task; web->conn = conn; conn->private = web; @@ -210,7 +211,7 @@ static void websrv_accept(struct stream_connection *conn) websrv_timeout, web); /* Overwrite the socket with a (possibly) TLS socket */ - tls_socket = tls_init_server(edata->tls_params, conn->socket, + tls_socket = tls_init_server(wdata->tls_params, conn->socket, conn->event.fde, "GPHO"); /* We might not have TLS, or it might not have initilised */ if (tls_socket) { @@ -243,6 +244,7 @@ static void websrv_task_init(struct task_server *task) NTSTATUS status; uint16_t port = lp_web_port(task->lp_ctx); const struct model_ops *model_ops; + struct web_server_data *wdata; task_server_set_title(task, "task[websrv]"); @@ -280,8 +282,16 @@ static void websrv_task_init(struct task_server *task) /* startup the esp processor - unfortunately we can't do this per connection as that wouldn't allow for session variables */ - task->private = http_setup_esp(task, task->lp_ctx); - if (task->private == NULL) goto failed; + wdata = talloc_zero(task, struct web_server_data); + if (wdata == NULL)goto failed; + + task->private = wdata; + + wdata->tls_params = tls_initialise(wdata, task->lp_ctx); + if (wdata->tls_params == NULL) goto failed; + + wdata->private = http_setup_esp(task, task->lp_ctx); + if (wdata->private == NULL) goto failed; return; -- cgit From 1271066234fed0e5f0e28a1e75420482abd20887 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 21 Sep 2008 16:53:29 +0200 Subject: Remove support for ESP in the web server. --- source4/web_server/web_server.c | 63 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 4 deletions(-) (limited to 'source4/web_server/web_server.c') diff --git a/source4/web_server/web_server.c b/source4/web_server/web_server.c index 6d8b2a2758..20924ff0dc 100644 --- a/source4/web_server/web_server.c +++ b/source4/web_server/web_server.c @@ -4,6 +4,7 @@ web server startup Copyright (C) Andrew Tridgell 2005 + Copyright (C) Jelmer Vernooij 2008 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -29,6 +30,7 @@ #include "system/network.h" #include "lib/socket/netif.h" #include "lib/tls/tls.h" +#include "lib/util/dlinklist.h" #include "param/param.h" /* don't allow connections to hang around forever */ @@ -61,6 +63,62 @@ static void websrv_timeout(struct event_context *event_context, stream_terminate_connection(conn, "websrv_timeout: timed out"); } +/* + setup for a raw http level error +*/ +void http_error(struct websrv_context *web, int code, const char *info) +{ + char *s; + s = talloc_asprintf(web,"Error %u

Error %u

%s

\r\n\r\n", + code, code, info); + if (s == NULL) { + stream_terminate_connection(web->conn, "http_error: out of memory"); + return; + } + /* FIXME: + http_writeBlock(web, s, strlen(s)); + http_setResponseCode(web, code); + http_output_headers(web); */ + EVENT_FD_NOT_READABLE(web->conn->event.fde); + EVENT_FD_WRITEABLE(web->conn->event.fde); + web->output.output_pending = true; +} + + +/* + parse one line of header input +*/ +NTSTATUS http_parse_header(struct websrv_context *web, const char *line) +{ + if (line[0] == 0) { + web->input.end_of_headers = true; + } else if (strncasecmp(line,"GET ", 4)==0) { + web->input.url = talloc_strndup(web, &line[4], strcspn(&line[4], " \t")); + } else if (strncasecmp(line,"POST ", 5)==0) { + web->input.post_request = true; + web->input.url = talloc_strndup(web, &line[5], strcspn(&line[5], " \t")); + } else if (strchr(line, ':') == NULL) { + http_error(web, 400, "This server only accepts GET and POST requests"); + return NT_STATUS_INVALID_PARAMETER; + } else if (strncasecmp(line, "Content-Length: ", 16)==0) { + web->input.content_length = strtoul(&line[16], NULL, 10); + } else { + struct http_header *hdr = talloc_zero(web, struct http_header); + char *colon = strchr(line, ':'); + if (colon == NULL) { + http_error(web, 500, "invalidly formatted header"); + return NT_STATUS_INVALID_PARAMETER; + } + + hdr->name = talloc_strndup(hdr, line, colon-line); + hdr->value = talloc_strdup(hdr, colon+1); + DLIST_ADD(web->input.headers, hdr); + } + + /* ignore all other headers for now */ + return NT_STATUS_OK; +} + /* called when a web connection becomes readable */ @@ -199,7 +257,7 @@ static void websrv_accept(struct stream_connection *conn) web = talloc_zero(conn, struct websrv_context); if (web == NULL) goto failed; - web->http_process_input = esp_process_http_input; + web->http_process_input = wsgi_process_http_input; web->task = task; web->conn = conn; conn->private = web; @@ -290,9 +348,6 @@ static void websrv_task_init(struct task_server *task) wdata->tls_params = tls_initialise(wdata, task->lp_ctx); if (wdata->tls_params == NULL) goto failed; - wdata->private = http_setup_esp(task, task->lp_ctx); - if (wdata->private == NULL) goto failed; - return; failed: -- cgit From 4141e70da97d924969b48fcd198e5996d615e75d Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 21 Sep 2008 18:45:09 +0200 Subject: Properly call WSGI request handler when requests come in. --- source4/web_server/web_server.c | 42 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) (limited to 'source4/web_server/web_server.c') diff --git a/source4/web_server/web_server.c b/source4/web_server/web_server.c index 20924ff0dc..1a3c48a03d 100644 --- a/source4/web_server/web_server.c +++ b/source4/web_server/web_server.c @@ -41,9 +41,6 @@ */ static int websrv_destructor(struct websrv_context *web) { - if (web->output.fd != -1) { - close(web->output.fd); - } return 0; } @@ -84,6 +81,11 @@ void http_error(struct websrv_context *web, int code, const char *info) web->output.output_pending = true; } +void websrv_output_headers(struct websrv_context *web, const char *status, struct http_header *headers) +{ + /* FIXME */ +} + /* parse one line of header input @@ -124,6 +126,7 @@ NTSTATUS http_parse_header(struct websrv_context *web, const char *line) */ static void websrv_recv(struct stream_connection *conn, uint16_t flags) { + struct web_server_data *wdata; struct websrv_context *web = talloc_get_type(conn->private, struct websrv_context); NTSTATUS status; @@ -181,7 +184,9 @@ static void websrv_recv(struct stream_connection *conn, uint16_t flags) destroy the stack variables being used by that rendering process when we handle the timeout. */ if (!talloc_reference(web->task, web)) goto failed; - web->http_process_input(web); + wdata = talloc_get_type(web->task->private, struct web_server_data); + if (wdata == NULL) goto failed; + wdata->http_process_input(wdata, web); talloc_unlink(web->task, web); } return; @@ -191,6 +196,7 @@ failed: } + /* called when a web connection becomes writable */ @@ -217,29 +223,7 @@ static void websrv_send(struct stream_connection *conn, uint16_t flags) web->output.nsent += nsent; - /* possibly read some more raw data from a file */ - if (web->output.content.length == web->output.nsent && - web->output.fd != -1) { - uint8_t buf[2048]; - ssize_t nread; - - data_blob_free(&web->output.content); - web->output.nsent = 0; - - nread = read(web->output.fd, buf, sizeof(buf)); - if (nread == -1 && errno == EINTR) { - return; - } - if (nread <= 0) { - close(web->output.fd); - web->output.fd = -1; - nread = 0; - } - web->output.content = data_blob_talloc(web, buf, nread); - } - - if (web->output.content.length == web->output.nsent && - web->output.fd == -1) { + if (web->output.content.length == web->output.nsent) { stream_terminate_connection(web->conn, "websrv_send: finished sending"); } } @@ -257,11 +241,9 @@ static void websrv_accept(struct stream_connection *conn) web = talloc_zero(conn, struct websrv_context); if (web == NULL) goto failed; - web->http_process_input = wsgi_process_http_input; web->task = task; web->conn = conn; conn->private = web; - web->output.fd = -1; talloc_set_destructor(web, websrv_destructor); event_add_timed(conn->event.ctx, web, @@ -348,6 +330,8 @@ static void websrv_task_init(struct task_server *task) wdata->tls_params = tls_initialise(wdata, task->lp_ctx); if (wdata->tls_params == NULL) goto failed; + if (!wsgi_initialize(wdata)) goto failed; + return; failed: -- cgit From 1ed040d8e22a63079c72126bbc896fa3c32c7f0f Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 21 Sep 2008 19:03:12 +0200 Subject: First GET request works. SWAT now displays a Hello world message. --- source4/web_server/web_server.c | 44 +++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 13 deletions(-) (limited to 'source4/web_server/web_server.c') diff --git a/source4/web_server/web_server.c b/source4/web_server/web_server.c index 1a3c48a03d..1ed37f657b 100644 --- a/source4/web_server/web_server.c +++ b/source4/web_server/web_server.c @@ -63,27 +63,45 @@ static void websrv_timeout(struct event_context *event_context, /* setup for a raw http level error */ -void http_error(struct websrv_context *web, int code, const char *info) +void http_error(struct websrv_context *web, const char *status, const char *info) { char *s; - s = talloc_asprintf(web,"Error %u

Error %u

%s

\r\n\r\n", - code, code, info); + s = talloc_asprintf(web,"Error %s

Error %s

%s

\r\n\r\n", + status, status, info); if (s == NULL) { stream_terminate_connection(web->conn, "http_error: out of memory"); return; } - /* FIXME: - http_writeBlock(web, s, strlen(s)); - http_setResponseCode(web, code); - http_output_headers(web); */ - EVENT_FD_NOT_READABLE(web->conn->event.fde); - EVENT_FD_WRITEABLE(web->conn->event.fde); - web->output.output_pending = true; + websrv_output_headers(web, status, NULL); + websrv_output(web, s, strlen(s)); } void websrv_output_headers(struct websrv_context *web, const char *status, struct http_header *headers) { - /* FIXME */ + char *s; + DATA_BLOB b; + struct http_header *hdr; + + s = talloc_asprintf(web, "HTTP/1.0 %s\r\n", status); + if (s == NULL) return; + for (hdr = headers; hdr; hdr = hdr->next) { + s = talloc_asprintf_append_buffer(s, "%s: %s\r\n", hdr->name, hdr->value); + } + + s = talloc_asprintf_append_buffer(s, "\r\n"); + + b = web->output.content; + web->output.content = data_blob_string_const(s); + websrv_output(web, b.data, b.length); + data_blob_free(&b); +} + +void websrv_output(struct websrv_context *web, void *data, size_t length) +{ + data_blob_append(web, &web->output.content, data, length); + EVENT_FD_NOT_READABLE(web->conn->event.fde); + EVENT_FD_WRITEABLE(web->conn->event.fde); + web->output.output_pending = true; } @@ -100,7 +118,7 @@ NTSTATUS http_parse_header(struct websrv_context *web, const char *line) web->input.post_request = true; web->input.url = talloc_strndup(web, &line[5], strcspn(&line[5], " \t")); } else if (strchr(line, ':') == NULL) { - http_error(web, 400, "This server only accepts GET and POST requests"); + http_error(web, "400 Bad request", "This server only accepts GET and POST requests"); return NT_STATUS_INVALID_PARAMETER; } else if (strncasecmp(line, "Content-Length: ", 16)==0) { web->input.content_length = strtoul(&line[16], NULL, 10); @@ -108,7 +126,7 @@ NTSTATUS http_parse_header(struct websrv_context *web, const char *line) struct http_header *hdr = talloc_zero(web, struct http_header); char *colon = strchr(line, ':'); if (colon == NULL) { - http_error(web, 500, "invalidly formatted header"); + http_error(web, "500 Internal Server Error", "invalidly formatted header"); return NT_STATUS_INVALID_PARAMETER; } -- cgit From 1d92b2211cc507dd62526f564ec7f75a07110e00 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Mon, 22 Sep 2008 18:15:24 +0200 Subject: s4: allways initialize the process model before it's used metze --- source4/web_server/web_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source4/web_server/web_server.c') diff --git a/source4/web_server/web_server.c b/source4/web_server/web_server.c index 1ed37f657b..d741992770 100644 --- a/source4/web_server/web_server.c +++ b/source4/web_server/web_server.c @@ -307,7 +307,7 @@ static void websrv_task_init(struct task_server *task) task_server_set_title(task, "task[websrv]"); /* run the web server as a single process */ - model_ops = process_model_byname("single"); + model_ops = process_model_startup(task->event_ctx, "single"); if (!model_ops) goto failed; if (lp_interfaces(task->lp_ctx) && lp_bind_interfaces_only(task->lp_ctx)) { -- cgit