Merge branch 'maint'
[paraslash.git] / http_send.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file http_send.c paraslash's http sender */
4
5 #include <netinet/in.h>
6 #include <sys/socket.h>
7 #include <regex.h>
8 #include <sys/types.h>
9 #include <arpa/inet.h>
10 #include <sys/un.h>
11 #include <netdb.h>
12 #include <lopsub.h>
13
14 #include "server.lsg.h"
15 #include "para.h"
16 #include "error.h"
17 #include "string.h"
18 #include "afh.h"
19 #include "net.h"
20 #include "server.h"
21 #include "http.h"
22 #include "list.h"
23 #include "send.h"
24 #include "sched.h"
25 #include "vss.h"
26 #include "close_on_fork.h"
27 #include "fd.h"
28 #include "chunk_queue.h"
29
30 /** Message sent to clients that do not send a valid get request. */
31 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
32
33 /** The possible states of a client from the server's POV. */
34 enum http_client_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         /** Connection is ready for sending audio data. */
40         HTTP_STREAMING,
41         /** We didn't receive a valid get request. */
42         HTTP_INVALID_GET_REQUEST
43 };
44
45 /** For each connected client, a structure of this type is maintained. */
46 struct private_http_sender_data {
47         /** The current state of this client. */
48         enum http_client_status status;
49 };
50
51 static struct sender_status http_sender_status, *hss = &http_sender_status;
52
53 static int http_send_msg(struct sender_client *sc, const char *msg)
54 {
55         int ret = write_buffer(sc->fd, msg);
56
57         if (ret < 0)
58                 shutdown_client(sc, hss);
59         return ret;
60 }
61
62 static void http_send_ok_msg(struct sender_client *sc)
63 {
64         PARA_INFO_LOG("sending http ok message to fd %d\n", sc->fd);
65         http_send_msg(sc, HTTP_OK_MSG);
66 }
67
68 static int http_send_err_msg(struct sender_client *sc)
69 {
70         PARA_NOTICE_LOG("sending bad request message to fd %d\n", sc->fd);
71         return http_send_msg(sc, HTTP_ERR_MSG);
72 }
73
74 static void http_shutdown_clients(void)
75 {
76         shutdown_clients(hss);
77 }
78
79 static void http_shutdown(void)
80 {
81         http_shutdown_clients();
82         generic_acl_deplete(&hss->acl);
83         free_sender_status(hss);
84 }
85
86 static int queue_chunk_or_shutdown(struct sender_client *sc,
87                 struct sender_status *ss, const char *buf, size_t num_bytes)
88 {
89         int ret = cq_enqueue(sc->cq, buf, num_bytes);
90         if (ret < 0)
91                 shutdown_client(sc, ss);
92         return ret;
93 }
94
95 /**
96  * Send one chunk of audio data to a connected client.
97  *
98  * \param sc The client.
99  * \param ss The sender.
100  * \param current_chunk The number of the chunk to write.
101  * \param buf The data to write.
102  * \param len The number of bytes of \a buf.
103  * \param header_buf The audio file header.
104  * \param header_len The number of bytes of \a header_buf.
105  *
106  * On errors, the client is shut down. If only a part of the buffer could be
107  * written, the remainder is put into the chunk queue for that client.
108  */
109 static void http_send_chunk(struct sender_client *sc, struct sender_status *ss,
110                 long unsigned current_chunk, const char *buf, size_t len,
111                 const char *header_buf, size_t header_len)
112 {
113         int ret;
114
115         if (!sc->header_sent && current_chunk) {
116                 if (header_buf && header_len > 0) {
117                         ret = queue_chunk_or_shutdown(sc, ss, header_buf, header_len);
118                         if (ret < 0)
119                                 goto out;
120                 }
121         }
122         sc->header_sent = 1;
123         ret = send_queued_chunks(sc->fd, sc->cq);
124         if (ret < 0) {
125                 shutdown_client(sc, ss);
126                 goto out;
127         }
128         if (!len)
129                 goto out;
130         if (!ret) { /* still data left in the queue */
131                 ret = queue_chunk_or_shutdown(sc, ss, buf, len);
132                 goto out;
133         }
134         ret = xwrite(sc->fd, buf, len);
135         if (ret < 0) {
136                 shutdown_client(sc, ss);
137                 goto out;
138         }
139         if (ret != len)
140                 ret = queue_chunk_or_shutdown(sc, ss, buf + ret, len - ret);
141 out:
142         if (ret < 0)
143                 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
144 }
145
146 static void http_send(long unsigned current_chunk,
147         __a_unused long unsigned chunks_sent, const char *buf, size_t len,
148         const char *header_buf, size_t header_len)
149 {
150         struct sender_client *sc, *tmp;
151
152         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
153                 struct private_http_sender_data *phsd = sc->private_data;
154
155                 if (phsd->status == HTTP_STREAMING)
156                         http_send_chunk(sc, hss, current_chunk, buf, len,
157                                    header_buf, header_len);
158         }
159 }
160
161 static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
162 {
163         struct sender_client *sc, *tmp;
164         struct private_http_sender_data *phsd;
165         int ret;
166
167         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
168                 phsd = sc->private_data;
169                 switch (phsd->status) {
170                 case HTTP_STREAMING: /* nothing to do */
171                         break;
172                 case HTTP_CONNECTED: /* need to recv get request */
173                         ret = read_pattern(sc->fd, HTTP_GET_MSG, MAXLINE, rfds);
174                         if (ret < 0)
175                                 phsd->status = HTTP_INVALID_GET_REQUEST;
176                         else if (ret > 0) {
177                                 phsd->status = HTTP_GOT_GET_REQUEST;
178                                 PARA_INFO_LOG("received get request\n");
179                         }
180                         break;
181                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
182                         phsd->status = HTTP_STREAMING;
183                         http_send_ok_msg(sc);
184                         break;
185                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
186                         if (http_send_err_msg(sc) >= 0)
187                                 shutdown_client(sc, hss);
188                         break;
189                 }
190         }
191         sc = accept_sender_client(hss, rfds);
192         if (!sc)
193                 return;
194         phsd = para_malloc(sizeof(*phsd));
195         sc->private_data = phsd;
196         phsd->status = HTTP_CONNECTED;
197 }
198
199 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
200 {
201         struct sender_client *sc, *tmp;
202         unsigned n;
203
204         FOR_EACH_LISTEN_FD(n, hss) {
205                 if (hss->listen_fds[n] < 0)
206                         continue;
207                 para_fd_set(hss->listen_fds[n], rfds, max_fileno);
208         }
209         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
210                 struct private_http_sender_data *phsd = sc->private_data;
211                 if (phsd->status == HTTP_CONNECTED) /* need to recv get request */
212                         para_fd_set(sc->fd, rfds, max_fileno);
213                 if (phsd->status == HTTP_GOT_GET_REQUEST ||
214                                 phsd->status == HTTP_INVALID_GET_REQUEST)
215                         para_fd_set(sc->fd, wfds, max_fileno);
216         }
217 }
218
219 static int http_com_on(__a_unused struct sender_command_data *scd)
220 {
221         generic_com_on(hss, IPPROTO_TCP);
222         return 1;
223 }
224
225 static int http_com_off(__a_unused struct sender_command_data *scd)
226 {
227         generic_com_off(hss);
228         return 1;
229 }
230
231 static int http_com_deny(struct sender_command_data *scd)
232 {
233         generic_com_deny(scd, hss);
234         return 1;
235 }
236
237 static int http_com_allow(struct sender_command_data *scd)
238 {
239         generic_com_allow(scd, hss);
240         return 1;
241 }
242
243 static char *http_status(void)
244 {
245         return generic_sender_status(hss, "http");
246 }
247
248 /*
249  * Initialize the client list and the access control list, and optionally
250  * listen on the tcp port.
251  */
252 static void http_send_init(void)
253 {
254         init_sender_status(hss, OPT_RESULT(HTTP_ACCESS),
255                 OPT_RESULT(HTTP_LISTEN_ADDRESS),
256                 OPT_UINT32_VAL(HTTP_PORT), OPT_UINT32_VAL(HTTP_MAX_CLIENTS),
257                 OPT_GIVEN(HTTP_DEFAULT_DENY));
258         if (OPT_GIVEN(HTTP_NO_AUTOSTART))
259                 return;
260         generic_com_on(hss, IPPROTO_TCP);
261 }
262
263 /**
264  * The HTTP sender.
265  *
266  * This is the only sender which does not FEC-encode the stream. This is not
267  * necessary because HTTP sits on top of TCP, a reliable transport which
268  * retransmits lost packets automatically. The sender employs per-client queues
269  * which queue chunks of audio data if they can not be sent immediately because
270  * the write operation would block. Most methods of the sender are implemented
271  * as wrappers for the generic functions defined in \ref send_common.c.
272  */
273 const struct sender http_sender = {
274         .name = "http",
275         .init = http_send_init,
276         .shutdown = http_shutdown,
277         .pre_select = http_pre_select,
278         .post_select = http_post_select,
279         .send = http_send,
280         .shutdown_clients = http_shutdown_clients,
281         .client_cmds = {
282                 [SENDER_on] = http_com_on,
283                 [SENDER_off] = http_com_off,
284                 [SENDER_deny] = http_com_deny,
285                 [SENDER_allow] = http_com_allow,
286         },
287         .help = generic_sender_help,
288         .status = http_status,
289 };