2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file http_send.c paraslash's http sender */
15 #include "server.cmdline.h"
23 #include "close_on_fork.h"
26 #include "chunk_queue.h"
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"
33 /** The possible states of a client from the server's POV. */
35 /** We accepted the connection on the tcp socket. */
37 /** Successfully received the get request. */
39 /** We sent the OK message back to the client. */
41 /** Connection is ready for sending audio data. */
43 /** We didn't receive a valid get request. */
44 HTTP_INVALID_GET_REQUEST
47 /** Clients will be kicked if there are more than that many bytes pending. */
48 #define MAX_BACKLOG 400000
49 /** The list of connected clients. */
50 static struct list_head clients;
51 /** The whitelist/blacklist. */
52 static struct list_head http_acl;
54 /** Describes one client that connected the tcp port of the http sender. */
56 /** The file descriptor of the client. */
58 /** The socket `name' of the client. */
60 /** The client's current status. */
61 enum http_status status;
62 /** Non-zero if we included \a fd in the read set.*/
64 /** Non-zero if we included \a fd in the write set. */
66 /** The position of this client in the client list. */
67 struct list_head node;
68 /** non-zero if audio file header has been sent */
70 /** The list of pending chunks for this client. */
71 struct chunk_queue *cq;
74 static int listen_fd = -1, numclients;
75 static struct sender *self;
78 static void http_shutdown_client(struct http_client *hc, const char *msg)
80 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", hc->name, hc->fd,
85 del_close_on_fork_list(hc->fd);
91 static void http_shutdown_clients(void)
93 struct http_client *hc, *tmp;
94 list_for_each_entry_safe(hc, tmp, &clients, node)
95 http_shutdown_client(hc, "vss request");
98 static int http_send_msg(struct http_client *hc, const char *msg)
100 int ret = send_buffer(hc->fd, msg);
103 http_shutdown_client(hc, "send msg failed");
107 static void http_send_ok_msg(struct http_client *hc)
109 PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
110 http_send_msg(hc, HTTP_OK_MSG);
113 static int http_send_err_msg(struct http_client *hc)
115 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
116 return http_send_msg(hc, HTTP_ERR_MSG);
120 * ret: Negative on errors, zero if nothing was written and write returned
121 * EAGAIN, number of bytes written else.
123 static int http_write(int fd, const char *buf, size_t len)
127 while (written < len) {
128 int ret = write(fd, buf + written, len - written);
129 if (ret < 0 && errno == EAGAIN)
132 return -ERRNO_TO_PARA_ERROR(errno);
139 static int send_queued_chunks(struct http_client *hc)
141 struct queued_chunk *qc;
142 while ((qc = cq_peek(hc->cq))) {
146 cq_get(qc, &buf, &len);
147 ret = http_write(hc->fd, buf, len);
150 cq_update(hc->cq, ret);
158 static int queue_chunk_or_shutdown(struct http_client *hc, long unsigned chunk_num,
161 int ret = cq_enqueue(hc->cq, chunk_num, sent);
163 http_shutdown_client(hc, "queue error");
167 static void http_send( long unsigned current_chunk,
168 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
170 struct http_client *hc, *tmp;
173 list_for_each_entry_safe(hc, tmp, &clients, node) {
174 if (hc->status != HTTP_STREAMING)
176 if (!hc->header_sent && current_chunk) {
178 char *hbuf = vss_get_header(&hlen);
179 if (hbuf && hlen > 0) { /* need to send header */
180 PARA_INFO_LOG("queueing header: %zu\n", hlen);
181 if (queue_chunk_or_shutdown(hc, -1U, 0) < 0)
184 PARA_INFO_LOG("no need to queue header\n");
187 ret = send_queued_chunks(hc);
189 http_shutdown_client(hc, "queue send error");
194 if (!ret || write_ok(hc->fd) <= 0) {
195 queue_chunk_or_shutdown(hc, current_chunk, 0);
198 // PARA_DEBUG_LOG("sending %d -> %s\n", len, remote_name(hc->fd));
199 ret = write(hc->fd, buf, len);
200 // PARA_DEBUG_LOG("ret: %d\n", ret);
202 http_shutdown_client(hc, "send error");
206 queue_chunk_or_shutdown(hc, current_chunk, ret);
210 static void http_post_select(fd_set *rfds, fd_set *wfds)
213 struct http_client *hc, *tmp;
218 list_for_each_entry_safe(hc, tmp, &clients, node) {
220 // PARA_DEBUG_LOG("handling client %d: %s\n", i, remote_name(hc->fd));
221 switch (hc->status) {
222 case HTTP_STREAMING: /* nothing to do */
224 case HTTP_CONNECTED: /* need to recv get request */
225 if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
226 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
228 hc->status = HTTP_INVALID_GET_REQUEST;
230 hc->status = HTTP_GOT_GET_REQUEST;
232 "received get request\n");
236 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
237 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
238 hc->status = HTTP_SENT_OK_MSG;
239 http_send_ok_msg(hc);
242 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
243 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
244 if (http_send_err_msg(hc) >= 0)
245 http_shutdown_client(hc,
246 "invalid get request");
249 case HTTP_SENT_OK_MSG: /* need to send header? */
250 if (hc->check_w && FD_ISSET(hc->fd, wfds))
251 hc->status = HTTP_STREAMING;
255 if (!FD_ISSET(listen_fd, rfds))
257 hc = para_calloc(sizeof(struct http_client));
258 err_msg = "accept error";
259 hc->fd = para_accept(listen_fd, NULL, 0);
262 hc->name = make_message("%s", remote_name(hc->fd));
263 PARA_NOTICE_LOG("connection from %s (fd %d)\n", hc->name, hc->fd);
264 if (conf.http_max_clients_arg > 0 && numclients >=
265 conf.http_max_clients_arg) {
266 err_msg = "server full";
269 match = acl_lookup(hc->fd, &http_acl);
270 PARA_DEBUG_LOG("acl lookup returned %d\n", match);
271 if ((match && !conf.http_default_deny_given) ||
272 (!match && conf.http_default_deny_given)) {
273 err_msg = "permission denied";
276 err_msg = "failed to mark fd non-blocking";
277 if (mark_fd_nonblocking(hc->fd) < 0)
279 hc->status = HTTP_CONNECTED;
280 hc->cq = cq_new(MAX_BACKLOG);
282 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
284 para_list_add(&hc->node, &clients);
285 add_close_on_fork_list(hc->fd);
288 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
295 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
297 struct http_client *hc, *tmp;
301 para_fd_set(listen_fd, rfds, max_fileno);
302 list_for_each_entry_safe(hc, tmp, &clients, node) {
303 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
306 switch (hc->status) {
309 case HTTP_CONNECTED: /* need to recv get request */
310 para_fd_set(hc->fd, rfds, max_fileno);
313 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
314 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
315 para_fd_set(hc->fd, wfds, max_fileno);
318 case HTTP_SENT_OK_MSG:
320 break; /* wait until server starts playing */
321 para_fd_set(hc->fd, wfds, max_fileno);
328 static int http_open(void)
332 listen_fd = para_listen(AF_UNSPEC, IPPROTO_TCP, conf.http_port_arg);
335 ret = mark_fd_nonblocking(listen_fd);
337 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
340 add_close_on_fork_list(listen_fd);
344 static int http_com_on(__a_unused struct sender_command_data *scd)
351 static int http_com_off(__a_unused struct sender_command_data *scd)
355 PARA_NOTICE_LOG("closing http port %d\n", conf.http_port_arg);
357 del_close_on_fork_list(listen_fd);
358 http_shutdown_clients();
363 static int http_com_deny(struct sender_command_data *scd)
365 if (conf.http_default_deny_given)
366 acl_del_entry(&http_acl, scd->addr, scd->netmask);
368 acl_add_entry(&http_acl, scd->addr, scd->netmask);
372 static int http_com_allow(struct sender_command_data *scd)
374 if (conf.http_default_deny_given)
375 acl_add_entry(&http_acl, scd->addr, scd->netmask);
377 acl_del_entry(&http_acl, scd->addr, scd->netmask);
381 static char *http_info(void)
383 char *clnts = NULL, *ret;
384 struct http_client *hc, *tmp_hc;
386 char *acl_contents = acl_get_contents(&http_acl);
387 list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
388 char *tmp = make_message("%s%s ", clnts? clnts : "", hc->name);
394 "http tcp port: %d\n"
396 "http maximal number of clients: %d%s\n"
397 "http connected clients: %s\n"
398 "http access %s list: %s\n",
399 (listen_fd >= 0)? "on" : "off",
402 conf.http_max_clients_arg,
403 conf.http_max_clients_arg > 0? "" : " (unlimited)",
404 clnts? clnts : "(none)",
405 conf.http_default_deny_given? "allow" : "deny",
406 acl_contents? acl_contents : "(none)"
413 static char *http_help(void)
417 "usage: {allow|deny} IP mask\n"
418 "example: allow 127.0.0.1 32\n"
423 * The init function of the http sender.
425 * \param s Pointer to the http sender struct.
427 * It initializes all function pointers of \a s, the client list and the access
428 * control list. If the autostart option was given, the tcp port is opened.
430 void http_send_init(struct sender *s)
432 INIT_LIST_HEAD(&clients);
435 s->pre_select = http_pre_select;
436 s->post_select = http_post_select;
437 s->shutdown_clients = http_shutdown_clients;
439 s->client_cmds[SENDER_ON] = http_com_on;
440 s->client_cmds[SENDER_OFF] = http_com_off;
441 s->client_cmds[SENDER_DENY] = http_com_deny;
442 s->client_cmds[SENDER_ALLOW] = http_com_allow;
443 s->client_cmds[SENDER_ADD] = NULL;
444 s->client_cmds[SENDER_DELETE] = NULL;
446 acl_init(&http_acl, conf.http_access_arg, conf.http_access_given);
447 if (!conf.http_no_autostart_given)
448 http_open(); /* ignore errors */
449 PARA_DEBUG_LOG("%s", "http sender init complete\n");