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 "smbd/service_stream.h"
26 #include "web_server/web_server.h"
27 #include "lib/events/events.h"
28 #include "system/filesys.h"
30 /* don't allow connections to hang around forever */
31 #define HTTP_TIMEOUT 30
34 destroy a web connection
36 static int websrv_destructor(void *ptr)
38 struct websrv_context *web = talloc_get_type(ptr, struct websrv_context);
39 if (web->output.fd != -1) {
40 close(web->output.fd);
46 called when a connection times out. This prevents a stuck connection
47 from hanging around forever
49 static void websrv_timeout(struct event_context *event_context,
50 struct timed_event *te,
51 struct timeval t, void *private)
53 struct websrv_context *web = talloc_get_type(private, struct websrv_context);
54 stream_terminate_connection(web->conn, "websrv_context: timeout");
58 called when a web connection becomes readable
60 static void websrv_recv(struct stream_connection *conn, uint16_t flags)
62 struct websrv_context *web = talloc_get_type(conn->private,
63 struct websrv_context);
70 /* not the most efficient http parser ever, but good enough for us */
71 status = socket_recv(conn->socket, buf, sizeof(buf), &nread, 0);
72 if (NT_STATUS_IS_ERR(status)) goto failed;
73 if (!NT_STATUS_IS_OK(status)) return;
75 status = data_blob_append(web, &web->input.partial, buf, nread);
76 if (!NT_STATUS_IS_OK(status)) goto failed;
78 /* parse any lines that are available */
79 b = web->input.partial;
80 while (!web->input.end_of_headers &&
81 (p=memchr(b.data, '\n', b.length))) {
82 const char *line = b.data;
84 if (p != b.data && p[-1] == '\r') {
87 status = http_parse_header(web, line);
88 if (!NT_STATUS_IS_OK(status)) return;
89 b.length -= (p - b.data) + 1;
93 /* keep any remaining bytes in web->input.partial */
97 b = data_blob_talloc(web, b.data, b.length);
98 data_blob_free(&web->input.partial);
99 web->input.partial = b;
101 /* we finish when we have both the full headers (terminated by
102 a blank line) and any post data, as indicated by the
104 if (web->input.end_of_headers &&
105 web->input.partial.length == web->input.content_length) {
106 EVENT_FD_NOT_READABLE(web->conn->event.fde);
107 http_process_input(web);
112 stream_terminate_connection(conn, "websrv_recv: failed\n");
117 called when a web connection becomes writable
119 static void websrv_send(struct stream_connection *conn, uint16_t flags)
121 struct websrv_context *web = talloc_get_type(conn->private,
122 struct websrv_context);
127 b = web->output.content;
128 b.data += web->output.nsent;
129 b.length -= web->output.nsent;
131 status = socket_send(conn->socket, &b, &nsent, 0);
132 if (NT_STATUS_IS_ERR(status)) {
133 stream_terminate_connection(web->conn, "socket_send: failed");
136 if (!NT_STATUS_IS_OK(status)) {
140 web->output.nsent += nsent;
142 /* possibly read some more raw data from a file */
143 if (web->output.content.length == web->output.nsent &&
144 web->output.fd != -1) {
148 data_blob_free(&web->output.content);
149 web->output.nsent = 0;
151 nread = read(web->output.fd, buf, sizeof(buf));
153 close(web->output.fd);
156 if (nread == -1 && errno == EINTR) {
159 web->output.content = data_blob_talloc(web, buf, nread);
162 if (web->output.content.length == web->output.nsent) {
163 stream_terminate_connection(web->conn, NULL);
168 establish a new connection to the web server
170 static void websrv_accept(struct stream_connection *conn)
172 struct websrv_context *web;
174 web = talloc_zero(conn, struct websrv_context);
175 if (web == NULL) goto failed;
180 talloc_set_destructor(web, websrv_destructor);
182 event_add_timed(conn->event.ctx, web,
183 timeval_current_ofs(HTTP_TIMEOUT, 0),
184 websrv_timeout, web);
192 static const struct stream_server_ops web_stream_ops = {
194 .accept_connection = websrv_accept,
195 .recv_handler = websrv_recv,
196 .send_handler = websrv_send,
200 startup the web server task
202 static void websrv_task_init(struct task_server *task)
205 uint16_t port = lp_web_port();
206 const struct model_ops *model_ops;
208 /* run the web server as a single process */
209 model_ops = process_model_byname("single");
210 if (!model_ops) goto failed;
212 if (lp_interfaces() && lp_bind_interfaces_only()) {
213 int num_interfaces = iface_count();
215 for(i = 0; i < num_interfaces; i++) {
216 const char *address = iface_n_ip(i);
217 status = stream_setup_socket(task->event_ctx, model_ops,
221 if (!NT_STATUS_IS_OK(status)) goto failed;
224 status = stream_setup_socket(task->event_ctx, model_ops,
226 "ipv4", lp_socket_address(),
228 if (!NT_STATUS_IS_OK(status)) goto failed;
234 task_terminate(task, "Failed to startup web server task");
239 called on startup of the web server service It's job is to start
240 listening on all configured sockets
242 static NTSTATUS websrv_init(struct event_context *event_context,
243 const struct model_ops *model_ops)
245 return task_server_startup(event_context, model_ops, websrv_task_init);
248 /* called at smbd startup - register ourselves as a server service */
249 NTSTATUS server_service_web_init(void)
251 return register_server_service("web", websrv_init);