r6981: first version of the builtin web server for Samba4
[sfrench/samba-autobuild/.git] / source4 / web_server / web_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    web server startup
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
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"
29
30 /* don't allow connections to hang around forever */
31 #define HTTP_TIMEOUT 30
32
33 /*
34   destroy a web connection
35 */
36 static int websrv_destructor(void *ptr)
37 {
38         struct websrv_context *web = talloc_get_type(ptr, struct websrv_context);
39         if (web->output.fd != -1) {
40                 close(web->output.fd);
41         }
42         return 0;
43 }
44
45 /*
46   called when a connection times out. This prevents a stuck connection
47   from hanging around forever
48 */
49 static void websrv_timeout(struct event_context *event_context, 
50                            struct timed_event *te, 
51                            struct timeval t, void *private)
52 {
53         struct websrv_context *web = talloc_get_type(private, struct websrv_context);
54         stream_terminate_connection(web->conn, "websrv_context: timeout");
55 }
56
57 /*
58   called when a web connection becomes readable
59 */
60 static void websrv_recv(struct stream_connection *conn, uint16_t flags)
61 {
62         struct websrv_context *web = talloc_get_type(conn->private, 
63                                                      struct websrv_context);
64         NTSTATUS status;
65         uint8_t buf[1024];
66         size_t nread;
67         uint8_t *p;
68         DATA_BLOB b;
69
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;
74
75         status = data_blob_append(web, &web->input.partial, buf, nread);
76         if (!NT_STATUS_IS_OK(status)) goto failed;
77
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;
83                 *p = 0;
84                 if (p != b.data && p[-1] == '\r') {
85                         p[-1] = 0;
86                 }
87                 status = http_parse_header(web, line);
88                 if (!NT_STATUS_IS_OK(status)) return;
89                 b.length -= (p - b.data) + 1;
90                 b.data = p+1;
91         }
92
93         /* keep any remaining bytes in web->input.partial */
94         if (b.length == 0) {
95                 b.data = NULL;
96         }
97         b = data_blob_talloc(web, b.data, b.length);
98         data_blob_free(&web->input.partial);
99         web->input.partial = b;
100
101         /* we finish when we have both the full headers (terminated by
102            a blank line) and any post data, as indicated by the
103            content_length */
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);
108         }
109         return;
110
111 failed:
112         stream_terminate_connection(conn, "websrv_recv: failed\n");
113 }
114
115
116 /*
117   called when a web connection becomes writable
118 */
119 static void websrv_send(struct stream_connection *conn, uint16_t flags)
120 {
121         struct websrv_context *web = talloc_get_type(conn->private, 
122                                                      struct websrv_context);
123         NTSTATUS status;
124         size_t nsent;
125         DATA_BLOB b;
126
127         b = web->output.content;
128         b.data += web->output.nsent;
129         b.length -= web->output.nsent;
130
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");
134                 return;
135         }
136         if (!NT_STATUS_IS_OK(status)) {
137                 return;
138         }
139
140         web->output.nsent += nsent;
141
142         /* possibly read some more raw data from a file */
143         if (web->output.content.length == web->output.nsent && 
144             web->output.fd != -1) {
145                 uint8_t buf[2048];
146                 ssize_t nread;
147
148                 data_blob_free(&web->output.content);
149                 web->output.nsent = 0;
150
151                 nread = read(web->output.fd, buf, sizeof(buf));
152                 if (nread == 0) {
153                         close(web->output.fd);
154                         web->output.fd = -1;
155                 }
156                 if (nread == -1 && errno == EINTR) {
157                         return;
158                 }
159                 web->output.content = data_blob_talloc(web, buf, nread);
160         }
161
162         if (web->output.content.length == web->output.nsent) {
163                 stream_terminate_connection(web->conn, NULL);
164         }
165 }
166
167 /*
168   establish a new connection to the web server
169 */
170 static void websrv_accept(struct stream_connection *conn)
171 {
172         struct websrv_context *web;
173
174         web = talloc_zero(conn, struct websrv_context);
175         if (web == NULL) goto failed;
176
177         web->conn = conn;
178         conn->private = web;
179         web->output.fd = -1;
180         talloc_set_destructor(web, websrv_destructor);
181
182         event_add_timed(conn->event.ctx, web, 
183                         timeval_current_ofs(HTTP_TIMEOUT, 0),
184                         websrv_timeout, web);
185         return;
186
187 failed:
188         talloc_free(conn);
189 }
190
191
192 static const struct stream_server_ops web_stream_ops = {
193         .name                   = "web",
194         .accept_connection      = websrv_accept,
195         .recv_handler           = websrv_recv,
196         .send_handler           = websrv_send,
197 };
198
199 /*
200   startup the web server task
201 */
202 static void websrv_task_init(struct task_server *task)
203 {
204         NTSTATUS status;
205         uint16_t port = lp_swat_port();
206         const struct model_ops *model_ops;
207
208         /* run the web server as a single process */
209         model_ops = process_model_byname("single");
210         if (!model_ops) goto failed;
211
212         if (lp_interfaces() && lp_bind_interfaces_only()) {
213                 int num_interfaces = iface_count();
214                 int i;
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, 
218                                                      &web_stream_ops, 
219                                                      "ipv4", address, 
220                                                      &port, task);
221                         if (!NT_STATUS_IS_OK(status)) goto failed;
222                 }
223         } else {
224                 status = stream_setup_socket(task->event_ctx, model_ops, 
225                                              &web_stream_ops, 
226                                              "ipv4", lp_socket_address(), 
227                                              &port, task);
228                 if (!NT_STATUS_IS_OK(status)) goto failed;
229         }
230
231         return;
232
233 failed:
234         task_terminate(task, "Failed to startup web server task");
235 }
236
237
238 /*
239   called on startup of the web server service It's job is to start
240   listening on all configured sockets
241 */
242 static NTSTATUS websrv_init(struct event_context *event_context, 
243                             const struct model_ops *model_ops)
244 {       
245         return task_server_startup(event_context, model_ops, websrv_task_init);
246 }
247
248 /* called at smbd startup - register ourselves as a server service */
249 NTSTATUS server_service_web_init(void)
250 {
251         return register_server_service("web", websrv_init);
252 }