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 established, we might need to send the audio file header. */
43 /** Connection is ready for sending audio data. */
45 /** We didn't receive a valid get request. */
46 HTTP_INVALID_GET_REQUEST
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;
56 /** Describes one client that connected the tcp port of the http sender. */
58 /** The file descriptor of the client. */
60 /** The socket `name' of the client. */
62 /** The client's current status. */
63 enum http_status status;
64 /** Non-zero if we included \a fd in the read set.*/
66 /** Non-zero if we included \a fd in the write set. */
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;
74 static int server_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);
119 static int send_queued_chunks(struct http_client *hc)
121 struct queued_chunk *qc;
122 while ((qc = cq_peek(hc->cq))) {
125 int ret = write_ok(hc->fd);
127 return ret? -E_WRITE_OK : 0;
128 cq_get(qc, &buf, &len);
129 ret = write(hc->fd, buf, len);
131 return -E_SEND_QUEUED_CHUNK;
132 cq_update(hc->cq, ret);
140 static int queue_chunk_or_shutdown(struct http_client *hc, long unsigned chunk_num,
143 int ret = cq_enqueue(hc->cq, chunk_num, sent);
145 http_shutdown_client(hc, "queue error");
149 static void http_send( long unsigned current_chunk,
150 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
152 struct http_client *hc, *tmp;
155 list_for_each_entry_safe(hc, tmp, &clients, node) {
156 if (hc->status != HTTP_STREAMING &&
157 hc->status != HTTP_READY_TO_STREAM)
159 if (hc->status == HTTP_READY_TO_STREAM) {
161 char *hbuf = vss_get_header(&hlen);
162 if (hbuf && hlen > 0 && current_chunk) {
163 /* need to send header */
164 PARA_INFO_LOG("queueing header: %zu\n", hlen);
165 if (queue_chunk_or_shutdown(hc, -1U, 0) < 0)
168 PARA_INFO_LOG("no need to queue header\n");
169 hc->status = HTTP_STREAMING;
171 ret = send_queued_chunks(hc);
173 http_shutdown_client(hc, "queue send error");
178 if (!ret || write_ok(hc->fd) <= 0) {
179 queue_chunk_or_shutdown(hc, current_chunk, 0);
182 // PARA_DEBUG_LOG("sending %d -> %s\n", len, remote_name(hc->fd));
183 ret = write(hc->fd, buf, len);
184 // PARA_DEBUG_LOG("ret: %d\n", ret);
186 http_shutdown_client(hc, "send error");
190 queue_chunk_or_shutdown(hc, current_chunk, ret);
194 static void http_post_select(fd_set *rfds, fd_set *wfds)
197 struct http_client *hc, *tmp;
200 list_for_each_entry_safe(hc, tmp, &clients, node) {
202 // PARA_DEBUG_LOG("handling client %d: %s\n", i, remote_name(hc->fd));
203 switch (hc->status) {
204 case HTTP_STREAMING: /* nothing to do */
205 case HTTP_READY_TO_STREAM:
207 case HTTP_CONNECTED: /* need to recv get request */
208 if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
209 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
211 hc->status = HTTP_INVALID_GET_REQUEST;
213 hc->status = HTTP_GOT_GET_REQUEST;
215 "received get request\n");
219 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
220 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
221 hc->status = HTTP_SENT_OK_MSG;
222 http_send_ok_msg(hc);
225 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
226 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
227 if (http_send_err_msg(hc) >= 0)
228 http_shutdown_client(hc,
229 "invalid get request");
232 case HTTP_SENT_OK_MSG: /* need to send header? */
233 if (hc->check_w && FD_ISSET(hc->fd, wfds))
234 hc->status = HTTP_READY_TO_STREAM;
238 if (!FD_ISSET(server_fd, rfds))
240 hc = para_calloc(sizeof(struct http_client));
241 err_msg = "accept error";
242 hc->fd = para_accept(server_fd, NULL, 0);
245 hc->name = make_message("%s", remote_name(hc->fd));
246 PARA_NOTICE_LOG("connection from %s (fd %d)\n", hc->name, hc->fd);
247 if (conf.http_max_clients_arg > 0 && numclients >=
248 conf.http_max_clients_arg) {
249 err_msg = "server full";
252 match = acl_lookup(hc->fd, &http_acl);
253 PARA_DEBUG_LOG("acl lookup returned %d\n", match);
254 if ((match && !conf.http_default_deny_given) ||
255 (!match && conf.http_default_deny_given)) {
256 err_msg = "permission denied";
259 hc->status = HTTP_CONNECTED;
260 hc->cq = cq_new(MAX_BACKLOG);
262 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
264 para_list_add(&hc->node, &clients);
265 add_close_on_fork_list(hc->fd);
266 mark_fd_nonblocking(hc->fd);
269 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
276 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
278 struct http_client *hc, *tmp;
282 para_fd_set(server_fd, rfds, max_fileno);
283 list_for_each_entry_safe(hc, tmp, &clients, node) {
284 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
287 switch (hc->status) {
289 case HTTP_READY_TO_STREAM:
291 case HTTP_CONNECTED: /* need to recv get request */
292 para_fd_set(hc->fd, rfds, max_fileno);
295 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
296 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
297 para_fd_set(hc->fd, wfds, max_fileno);
300 case HTTP_SENT_OK_MSG:
302 break; /* wait until server starts playing */
303 para_fd_set(hc->fd, wfds, max_fileno);
310 static int http_open(int port)
314 server_fd = para_listen(AF_UNSPEC, IPPROTO_TCP, port);
316 http_shutdown_clients();
317 self->status = SENDER_OFF;
320 ret = mark_fd_nonblocking(server_fd);
322 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
325 self->status = SENDER_ON;
326 add_close_on_fork_list(server_fd);
330 static int http_com_on(__a_unused struct sender_command_data *scd)
332 if (self->status == SENDER_ON)
334 return http_open(conf.http_port_arg);
337 static int http_com_off(__a_unused struct sender_command_data *scd)
339 self->status = SENDER_OFF;
342 del_close_on_fork_list(server_fd);
345 http_shutdown_clients();
349 static int http_com_deny(struct sender_command_data *scd)
351 if (conf.http_default_deny_given)
352 acl_del_entry(&http_acl, scd->addr, scd->netmask);
354 acl_add_entry(&http_acl, scd->addr, scd->netmask);
358 static int http_com_allow(struct sender_command_data *scd)
360 if (conf.http_default_deny_given)
361 acl_add_entry(&http_acl, scd->addr, scd->netmask);
363 acl_del_entry(&http_acl, scd->addr, scd->netmask);
367 static char *http_info(void)
369 char *clnts = NULL, *ret;
370 struct http_client *hc, *tmp_hc;
372 char *acl_contents = acl_get_contents(&http_acl);
373 list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
374 char *tmp = make_message("%s%s ", clnts? clnts : "", hc->name);
380 "http tcp port: %d\n"
382 "http maximal number of clients: %d%s\n"
383 "http connected clients: %s\n"
384 "http access %s list: %s\n",
385 (self->status == SENDER_ON)? "on" : "off",
388 conf.http_max_clients_arg,
389 conf.http_max_clients_arg > 0? "" : " (unlimited)",
390 clnts? clnts : "(none)",
391 conf.http_default_deny_given? "allow" : "deny",
392 acl_contents? acl_contents : "(none)"
399 static char *http_help(void)
403 "usage: {allow|deny} IP mask\n"
404 "example: allow 127.0.0.1 32\n"
409 * The init function of the http sender.
411 * \param s Pointer to the http sender struct.
413 * It initializes all function pointers of \a s, the client list and the access
414 * control list. If the autostart option was given, the tcp port is opened.
416 void http_send_init(struct sender *s)
418 INIT_LIST_HEAD(&clients);
421 s->pre_select = http_pre_select;
422 s->post_select = http_post_select;
423 s->shutdown_clients = http_shutdown_clients;
425 s->client_cmds[SENDER_ON] = http_com_on;
426 s->client_cmds[SENDER_OFF] = http_com_off;
427 s->client_cmds[SENDER_DENY] = http_com_deny;
428 s->client_cmds[SENDER_ALLOW] = http_com_allow;
429 s->client_cmds[SENDER_ADD] = NULL;
430 s->client_cmds[SENDER_DELETE] = NULL;
432 acl_init(&http_acl, conf.http_access_arg, conf.http_access_given);
433 if (!conf.http_no_autostart_given)
434 http_open(conf.http_port_arg); /* ignore errors */
435 PARA_DEBUG_LOG("%s", "http sender init complete\n");