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 /** Connection is ready for sending audio data. */
41 /** We didn't receive a valid get request. */
42 HTTP_INVALID_GET_REQUEST
45 /** Clients will be kicked if there are more than that many bytes pending. */
46 #define MAX_BACKLOG 400000
47 /** The list of connected clients. */
48 static struct list_head clients;
49 /** The whitelist/blacklist. */
50 static struct list_head http_acl;
52 static int listen_fd = -1, numclients;
54 struct private_http_sender_data {
55 enum http_status status;
58 static int http_send_msg(struct sender_client *sc, const char *msg)
60 int ret = send_buffer(sc->fd, msg);
67 static void http_send_ok_msg(struct sender_client *sc)
69 PARA_INFO_LOG("sending http ok message to fd %d\n", sc->fd);
70 http_send_msg(sc, HTTP_OK_MSG);
73 static int http_send_err_msg(struct sender_client *sc)
75 PARA_NOTICE_LOG("sending bad request message to fd %d\n", sc->fd);
76 return http_send_msg(sc, HTTP_ERR_MSG);
79 static void http_shutdown_clients(void)
81 struct sender_client *sc, *tmp;
82 list_for_each_entry_safe(sc, tmp, &clients, node)
86 static void http_send(long unsigned current_chunk,
87 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
89 struct sender_client *sc, *tmp;
91 list_for_each_entry_safe(sc, tmp, &clients, node) {
92 struct private_http_sender_data *phsd = sc->private_data;
93 if (phsd->status != HTTP_STREAMING)
95 send_chunk(sc, 0, current_chunk, buf, len);
99 static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
101 int ret, i = -1, match;
102 struct sender_client *sc, *tmp;
103 struct private_http_sender_data *phsd;
107 list_for_each_entry_safe(sc, tmp, &clients, node) {
109 phsd = sc->private_data;
110 // PARA_DEBUG_LOG("handling client %d: %s\n", i, remote_name(sc->fd));
111 switch (phsd->status) {
112 case HTTP_STREAMING: /* nothing to do */
114 case HTTP_CONNECTED: /* need to recv get request */
115 if (FD_ISSET(sc->fd, rfds)) {
116 if (recv_pattern(sc->fd, HTTP_GET_MSG, MAXLINE)
118 phsd->status = HTTP_INVALID_GET_REQUEST;
120 phsd->status = HTTP_GOT_GET_REQUEST;
122 "received get request\n");
126 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
127 phsd->status = HTTP_STREAMING;
128 http_send_ok_msg(sc);
130 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
131 if (http_send_err_msg(sc) >= 0)
136 if (!FD_ISSET(listen_fd, rfds))
138 ret = para_accept(listen_fd, NULL, 0);
140 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
143 sc = para_calloc(sizeof(*sc));
145 sc->name = make_message("%s", remote_name(sc->fd));
146 PARA_NOTICE_LOG("connection from %s (fd %d)\n", sc->name, sc->fd);
147 ret = -E_MAX_CLIENTS;
148 if (conf.http_max_clients_arg > 0 && numclients >=
149 conf.http_max_clients_arg) {
152 match = acl_lookup(sc->fd, &http_acl);
153 PARA_DEBUG_LOG("acl lookup returned %d\n", match);
155 if ((match && !conf.http_default_deny_given) ||
156 (!match && conf.http_default_deny_given))
158 ret = mark_fd_nonblocking(sc->fd);
161 phsd = para_malloc(sizeof(*phsd));
162 sc->private_data = phsd;
163 phsd->status = HTTP_CONNECTED;
164 sc->cq = cq_new(MAX_BACKLOG);
166 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
168 para_list_add(&sc->node, &clients);
169 add_close_on_fork_list(sc->fd);
172 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
177 static void http_pre_select(int *max_fileno, fd_set *rfds, __a_unused fd_set *wfds)
179 struct sender_client *sc, *tmp;
183 para_fd_set(listen_fd, rfds, max_fileno);
184 list_for_each_entry_safe(sc, tmp, &clients, node) {
185 struct private_http_sender_data *phsd = sc->private_data;
186 if (phsd->status == HTTP_CONNECTED) /* need to recv get request */
187 para_fd_set(sc->fd, rfds, max_fileno);
191 static int http_com_on(__a_unused struct sender_command_data *scd)
195 return open_sender(IPPROTO_TCP, conf.http_port_arg);
198 static int http_com_off(__a_unused struct sender_command_data *scd)
202 PARA_NOTICE_LOG("closing http port %d\n", conf.http_port_arg);
204 del_close_on_fork_list(listen_fd);
205 http_shutdown_clients();
210 static int http_com_deny(struct sender_command_data *scd)
212 if (conf.http_default_deny_given)
213 acl_del_entry(&http_acl, scd->addr, scd->netmask);
215 acl_add_entry(&http_acl, scd->addr, scd->netmask);
219 static int http_com_allow(struct sender_command_data *scd)
221 if (conf.http_default_deny_given)
222 acl_add_entry(&http_acl, scd->addr, scd->netmask);
224 acl_del_entry(&http_acl, scd->addr, scd->netmask);
228 static char *http_info(void)
230 char *clnts = NULL, *ret;
231 struct sender_client *sc, *tmp_sc;
233 char *acl_contents = acl_get_contents(&http_acl);
234 list_for_each_entry_safe(sc, tmp_sc, &clients, node) {
235 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
241 "http tcp port: %d\n"
243 "http maximal number of clients: %d%s\n"
244 "http connected clients: %s\n"
245 "http access %s list: %s\n",
246 (listen_fd >= 0)? "on" : "off",
249 conf.http_max_clients_arg,
250 conf.http_max_clients_arg > 0? "" : " (unlimited)",
251 clnts? clnts : "(none)",
252 conf.http_default_deny_given? "allow" : "deny",
253 acl_contents? acl_contents : "(none)"
260 static char *http_help(void)
264 "usage: {allow|deny} IP mask\n"
265 "example: allow 127.0.0.1 32\n"
270 * The init function of the http sender.
272 * \param s Pointer to the http sender struct.
274 * It initializes all function pointers of \a s, the client list and the access
275 * control list. If the autostart option was given, the tcp port is opened.
277 void http_send_init(struct sender *s)
280 INIT_LIST_HEAD(&clients);
283 s->pre_select = http_pre_select;
284 s->post_select = http_post_select;
285 s->shutdown_clients = http_shutdown_clients;
287 s->client_cmds[SENDER_ON] = http_com_on;
288 s->client_cmds[SENDER_OFF] = http_com_off;
289 s->client_cmds[SENDER_DENY] = http_com_deny;
290 s->client_cmds[SENDER_ALLOW] = http_com_allow;
291 s->client_cmds[SENDER_ADD] = NULL;
292 s->client_cmds[SENDER_DELETE] = NULL;
293 acl_init(&http_acl, conf.http_access_arg, conf.http_access_given);
294 if (conf.http_no_autostart_given)
296 ret = open_sender(IPPROTO_TCP, conf.http_port_arg);
298 PARA_ERROR_LOG("%s\n", para_strerror(-ret));