http_send.c: Introduce http_write.
[paraslash.git] / http_send.c
1 /*
2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file http_send.c paraslash's http sender */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "string.h"
15 #include "server.cmdline.h"
16 #include "afh.h"
17 #include "afs.h"
18 #include "server.h"
19 #include "http.h"
20 #include "vss.h"
21 #include "send.h"
22 #include "list.h"
23 #include "close_on_fork.h"
24 #include "net.h"
25 #include "fd.h"
26 #include "chunk_queue.h"
27 #include "acl.h"
28
29 /** Message sent to clients that do not send a valid get request. */
30 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
31
32
33 /** The possible states of a client from the server's POV. */
34 enum http_status {
35 /** We accepted the connection on the tcp socket. */
36 HTTP_CONNECTED,
37 /** Successfully received the get request. */
38 HTTP_GOT_GET_REQUEST,
39 /** We sent the OK message back to the client. */
40 HTTP_SENT_OK_MSG,
41 /** Connection established, we might need to send the audio file header. */
42 HTTP_READY_TO_STREAM,
43 /** Connection is ready for sending audio data. */
44 HTTP_STREAMING,
45 /** We didn't receive a valid get request. */
46 HTTP_INVALID_GET_REQUEST
47 };
48
49 /** Clients will be kicked if there are more than that many bytes pending. */
50 #define MAX_BACKLOG 400000
51 /** The list of connected clients. */
52 static struct list_head clients;
53 /** The whitelist/blacklist. */
54 static struct list_head http_acl;
55
56 /** Describes one client that connected the tcp port of the http sender. */
57 struct http_client {
58 /** The file descriptor of the client. */
59 int fd;
60 /** The socket `name' of the client. */
61 char *name;
62 /** The client's current status. */
63 enum http_status status;
64 /** Non-zero if we included \a fd in the read set.*/
65 int check_r;
66 /** Non-zero if we included \a fd in the write set. */
67 int check_w;
68 /** The position of this client in the client list. */
69 struct list_head node;
70 /** The list of pending chunks for this client. */
71 struct chunk_queue *cq;
72 };
73
74 static int listen_fd = -1, numclients;
75 static struct sender *self;
76
77
78 static void http_shutdown_client(struct http_client *hc, const char *msg)
79 {
80 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", hc->name, hc->fd,
81 msg);
82 numclients--;
83 free(hc->name);
84 close(hc->fd);
85 del_close_on_fork_list(hc->fd);
86 cq_destroy(hc->cq);
87 list_del(&hc->node);
88 free(hc);
89 }
90
91 static void http_shutdown_clients(void)
92 {
93 struct http_client *hc, *tmp;
94 list_for_each_entry_safe(hc, tmp, &clients, node)
95 http_shutdown_client(hc, "vss request");
96 }
97
98 static int http_send_msg(struct http_client *hc, const char *msg)
99 {
100 int ret = send_buffer(hc->fd, msg);
101
102 if (ret < 0)
103 http_shutdown_client(hc, "send msg failed");
104 return ret;
105 }
106
107 static void http_send_ok_msg(struct http_client *hc)
108 {
109 PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
110 http_send_msg(hc, HTTP_OK_MSG);
111 }
112
113 static int http_send_err_msg(struct http_client *hc)
114 {
115 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
116 return http_send_msg(hc, HTTP_ERR_MSG);
117 }
118
119 /*
120 * ret: Negative on errors, zero if nothing was written and write returned
121 * EAGAIN, number of bytes written else.
122 */
123 static int http_write(int fd, const char *buf, size_t len)
124 {
125 size_t written = 0;
126
127 while (written < len) {
128 int ret = write(fd, buf + written, len - written);
129 if (ret < 0 && errno == EAGAIN)
130 return written;
131 if (ret < 0)
132 return -ERRNO_TO_PARA_ERROR(errno);
133 written += ret;
134 }
135 return written;
136 }
137
138
139 static int send_queued_chunks(struct http_client *hc)
140 {
141 struct queued_chunk *qc;
142 while ((qc = cq_peek(hc->cq))) {
143 char *buf;
144 size_t len;
145 int ret;
146 cq_get(qc, &buf, &len);
147 ret = http_write(hc->fd, buf, len);
148 if (ret < 0)
149 return ret;
150 cq_update(hc->cq, ret);
151 if (ret != len)
152 return 1;
153 cq_dequeue(hc->cq);
154 }
155 return 1;
156 }
157
158 static int queue_chunk_or_shutdown(struct http_client *hc, long unsigned chunk_num,
159 size_t sent)
160 {
161 int ret = cq_enqueue(hc->cq, chunk_num, sent);
162 if (ret < 0)
163 http_shutdown_client(hc, "queue error");
164 return ret;
165 }
166
167 static void http_send( long unsigned current_chunk,
168 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
169 {
170 struct http_client *hc, *tmp;
171 int ret;
172
173 list_for_each_entry_safe(hc, tmp, &clients, node) {
174 if (hc->status != HTTP_STREAMING &&
175 hc->status != HTTP_READY_TO_STREAM)
176 continue;
177 if (hc->status == HTTP_READY_TO_STREAM) {
178 size_t hlen;
179 char *hbuf = vss_get_header(&hlen);
180 if (hbuf && hlen > 0 && current_chunk) {
181 /* need to send header */
182 PARA_INFO_LOG("queueing header: %zu\n", hlen);
183 if (queue_chunk_or_shutdown(hc, -1U, 0) < 0)
184 continue;
185 } else
186 PARA_INFO_LOG("no need to queue header\n");
187 hc->status = HTTP_STREAMING;
188 }
189 ret = send_queued_chunks(hc);
190 if (ret < 0) {
191 http_shutdown_client(hc, "queue send error");
192 continue;
193 }
194 if (!len)
195 continue;
196 if (!ret || write_ok(hc->fd) <= 0) {
197 queue_chunk_or_shutdown(hc, current_chunk, 0);
198 continue;
199 }
200 // PARA_DEBUG_LOG("sending %d -> %s\n", len, remote_name(hc->fd));
201 ret = write(hc->fd, buf, len);
202 // PARA_DEBUG_LOG("ret: %d\n", ret);
203 if (ret < 0) {
204 http_shutdown_client(hc, "send error");
205 continue;
206 }
207 if (ret != len)
208 queue_chunk_or_shutdown(hc, current_chunk, ret);
209 }
210 }
211
212 static void http_post_select(fd_set *rfds, fd_set *wfds)
213 {
214 int i = -1, match;
215 struct http_client *hc, *tmp;
216 const char *err_msg;
217
218 if (listen_fd < 0)
219 return;
220 list_for_each_entry_safe(hc, tmp, &clients, node) {
221 i++;
222 // PARA_DEBUG_LOG("handling client %d: %s\n", i, remote_name(hc->fd));
223 switch (hc->status) {
224 case HTTP_STREAMING: /* nothing to do */
225 case HTTP_READY_TO_STREAM:
226 break;
227 case HTTP_CONNECTED: /* need to recv get request */
228 if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
229 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
230 < 0) {
231 hc->status = HTTP_INVALID_GET_REQUEST;
232 } else {
233 hc->status = HTTP_GOT_GET_REQUEST;
234 PARA_INFO_LOG("%s",
235 "received get request\n");
236 }
237 }
238 break;
239 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
240 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
241 hc->status = HTTP_SENT_OK_MSG;
242 http_send_ok_msg(hc);
243 }
244 break;
245 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
246 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
247 if (http_send_err_msg(hc) >= 0)
248 http_shutdown_client(hc,
249 "invalid get request");
250 }
251 break;
252 case HTTP_SENT_OK_MSG: /* need to send header? */
253 if (hc->check_w && FD_ISSET(hc->fd, wfds))
254 hc->status = HTTP_READY_TO_STREAM;
255 break;
256 }
257 }
258 if (!FD_ISSET(listen_fd, rfds))
259 return;
260 hc = para_calloc(sizeof(struct http_client));
261 err_msg = "accept error";
262 hc->fd = para_accept(listen_fd, NULL, 0);
263 if (hc->fd <= 0)
264 goto err_out;
265 hc->name = make_message("%s", remote_name(hc->fd));
266 PARA_NOTICE_LOG("connection from %s (fd %d)\n", hc->name, hc->fd);
267 if (conf.http_max_clients_arg > 0 && numclients >=
268 conf.http_max_clients_arg) {
269 err_msg = "server full";
270 goto err_out;
271 }
272 match = acl_lookup(hc->fd, &http_acl);
273 PARA_DEBUG_LOG("acl lookup returned %d\n", match);
274 if ((match && !conf.http_default_deny_given) ||
275 (!match && conf.http_default_deny_given)) {
276 err_msg = "permission denied";
277 goto err_out;
278 }
279 err_msg = "failed to mark fd non-blocking";
280 if (mark_fd_nonblocking(hc->fd) < 0)
281 goto err_out;
282 hc->status = HTTP_CONNECTED;
283 hc->cq = cq_new(MAX_BACKLOG);
284 numclients++;
285 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
286 hc->name, hc->fd);
287 para_list_add(&hc->node, &clients);
288 add_close_on_fork_list(hc->fd);
289 return;
290 err_out:
291 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
292 hc->name, err_msg);
293 if (hc->fd > 0)
294 close(hc->fd);
295 free(hc);
296 }
297
298 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
299 {
300 struct http_client *hc, *tmp;
301
302 if (listen_fd < 0)
303 return;
304 para_fd_set(listen_fd, rfds, max_fileno);
305 list_for_each_entry_safe(hc, tmp, &clients, node) {
306 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
307 hc->check_r = 0;
308 hc->check_w = 0;
309 switch (hc->status) {
310 case HTTP_STREAMING:
311 case HTTP_READY_TO_STREAM:
312 break;
313 case HTTP_CONNECTED: /* need to recv get request */
314 para_fd_set(hc->fd, rfds, max_fileno);
315 hc->check_r = 1;
316 break;
317 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
318 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
319 para_fd_set(hc->fd, wfds, max_fileno);
320 hc->check_w = 1;
321 break;
322 case HTTP_SENT_OK_MSG:
323 if (!vss_playing())
324 break; /* wait until server starts playing */
325 para_fd_set(hc->fd, wfds, max_fileno);
326 hc->check_w = 1;
327 break;
328 }
329 }
330 }
331
332 static int http_open(void)
333 {
334 int ret;
335
336 listen_fd = para_listen(AF_UNSPEC, IPPROTO_TCP, conf.http_port_arg);
337 if (listen_fd < 0)
338 return listen_fd;
339 ret = mark_fd_nonblocking(listen_fd);
340 if (ret < 0) {
341 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
342 exit(EXIT_FAILURE);
343 }
344 add_close_on_fork_list(listen_fd);
345 return 1;
346 }
347
348 static int http_com_on(__a_unused struct sender_command_data *scd)
349 {
350 if (listen_fd >= 0)
351 return 1;
352 return http_open();
353 }
354
355 static int http_com_off(__a_unused struct sender_command_data *scd)
356 {
357 if (listen_fd < 0)
358 return 1;
359 PARA_NOTICE_LOG("closing http port %d\n", conf.http_port_arg);
360 close(listen_fd);
361 del_close_on_fork_list(listen_fd);
362 http_shutdown_clients();
363 listen_fd = -1;
364 return 1;
365 }
366
367 static int http_com_deny(struct sender_command_data *scd)
368 {
369 if (conf.http_default_deny_given)
370 acl_del_entry(&http_acl, scd->addr, scd->netmask);
371 else
372 acl_add_entry(&http_acl, scd->addr, scd->netmask);
373 return 1;
374 }
375
376 static int http_com_allow(struct sender_command_data *scd)
377 {
378 if (conf.http_default_deny_given)
379 acl_add_entry(&http_acl, scd->addr, scd->netmask);
380 else
381 acl_del_entry(&http_acl, scd->addr, scd->netmask);
382 return 1;
383 }
384
385 static char *http_info(void)
386 {
387 char *clnts = NULL, *ret;
388 struct http_client *hc, *tmp_hc;
389
390 char *acl_contents = acl_get_contents(&http_acl);
391 list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
392 char *tmp = make_message("%s%s ", clnts? clnts : "", hc->name);
393 free(clnts);
394 clnts = tmp;
395 }
396 ret = make_message(
397 "http status: %s\n"
398 "http tcp port: %d\n"
399 "http clients: %d\n"
400 "http maximal number of clients: %d%s\n"
401 "http connected clients: %s\n"
402 "http access %s list: %s\n",
403 (listen_fd >= 0)? "on" : "off",
404 conf.http_port_arg,
405 numclients,
406 conf.http_max_clients_arg,
407 conf.http_max_clients_arg > 0? "" : " (unlimited)",
408 clnts? clnts : "(none)",
409 conf.http_default_deny_given? "allow" : "deny",
410 acl_contents? acl_contents : "(none)"
411 );
412 free(acl_contents);
413 free(clnts);
414 return ret;
415 }
416
417 static char *http_help(void)
418 {
419 return make_message(
420 "usage: {on|off}\n"
421 "usage: {allow|deny} IP mask\n"
422 "example: allow 127.0.0.1 32\n"
423 );
424 }
425
426 /**
427 * The init function of the http sender.
428 *
429 * \param s Pointer to the http sender struct.
430 *
431 * It initializes all function pointers of \a s, the client list and the access
432 * control list. If the autostart option was given, the tcp port is opened.
433 */
434 void http_send_init(struct sender *s)
435 {
436 INIT_LIST_HEAD(&clients);
437 s->info = http_info;
438 s->send = http_send;
439 s->pre_select = http_pre_select;
440 s->post_select = http_post_select;
441 s->shutdown_clients = http_shutdown_clients;
442 s->help = http_help;
443 s->client_cmds[SENDER_ON] = http_com_on;
444 s->client_cmds[SENDER_OFF] = http_com_off;
445 s->client_cmds[SENDER_DENY] = http_com_deny;
446 s->client_cmds[SENDER_ALLOW] = http_com_allow;
447 s->client_cmds[SENDER_ADD] = NULL;
448 s->client_cmds[SENDER_DELETE] = NULL;
449 self = s;
450 acl_init(&http_acl, conf.http_access_arg, conf.http_access_given);
451 if (!conf.http_no_autostart_given)
452 http_open(); /* ignore errors */
453 PARA_DEBUG_LOG("%s", "http sender init complete\n");
454 }