Merge branch 'refs/heads/t/taggers'
[paraslash.git] / http_send.c
1 /*
2  * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
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 <netinet/in.h>
10 #include <sys/socket.h>
11 #include <regex.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
14 #include <sys/un.h>
15 #include <netdb.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "string.h"
20 #include "server.cmdline.h"
21 #include "afh.h"
22 #include "server.h"
23 #include "http.h"
24 #include "list.h"
25 #include "send.h"
26 #include "sched.h"
27 #include "vss.h"
28 #include "close_on_fork.h"
29 #include "net.h"
30 #include "fd.h"
31 #include "chunk_queue.h"
32 #include "acl.h"
33
34 /** Message sent to clients that do not send a valid get request. */
35 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
36
37 /** The possible states of a client from the server's POV. */
38 enum http_client_status {
39         /** We accepted the connection on the tcp socket. */
40         HTTP_CONNECTED,
41         /** Successfully received the get request. */
42         HTTP_GOT_GET_REQUEST,
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 /** For each connected client, a structure of this type is maintained. */
50 struct private_http_sender_data {
51         /** The current state of this client. */
52         enum http_client_status status;
53 };
54
55 static struct sender_status http_sender_status, *hss = &http_sender_status;
56
57 static int http_send_msg(struct sender_client *sc, const char *msg)
58 {
59         int ret = write_buffer(sc->fd, msg);
60
61         if (ret < 0)
62                 shutdown_client(sc, hss);
63         return ret;
64 }
65
66 static void http_send_ok_msg(struct sender_client *sc)
67 {
68         PARA_INFO_LOG("sending http ok message to fd %d\n", sc->fd);
69         http_send_msg(sc, HTTP_OK_MSG);
70 }
71
72 static int http_send_err_msg(struct sender_client *sc)
73 {
74         PARA_NOTICE_LOG("sending bad request message to fd %d\n", sc->fd);
75         return http_send_msg(sc, HTTP_ERR_MSG);
76 }
77
78 static void http_shutdown_clients(void)
79 {
80         shutdown_clients(hss);
81 }
82
83 static int queue_chunk_or_shutdown(struct sender_client *sc,
84                 struct sender_status *ss, const char *buf, size_t num_bytes)
85 {
86         int ret = cq_enqueue(sc->cq, buf, num_bytes);
87         if (ret < 0)
88                 shutdown_client(sc, ss);
89         return ret;
90 }
91
92 /**
93  * Send one chunk of audio data to a connected client.
94  *
95  * \param sc The client.
96  * \param ss The sender.
97  * \param current_chunk The number of the chunk to write.
98  * \param buf The data to write.
99  * \param len The number of bytes of \a buf.
100  * \param header_buf The audio file header.
101  * \param header_len The number of bytes of \a header_buf.
102  *
103  * On errors, the client is shut down. If only a part of the buffer could be
104  * written, the remainder is put into the chunk queue for that client.
105  */
106 static void http_send_chunk(struct sender_client *sc, struct sender_status *ss,
107                 long unsigned current_chunk, const char *buf, size_t len,
108                 const char *header_buf, size_t header_len)
109 {
110         int ret;
111
112         if (!sc->header_sent && current_chunk) {
113                 if (header_buf && header_len > 0) {
114                         ret = queue_chunk_or_shutdown(sc, ss, header_buf, header_len);
115                         if (ret < 0)
116                                 goto out;
117                 }
118         }
119         sc->header_sent = 1;
120         ret = send_queued_chunks(sc->fd, sc->cq);
121         if (ret < 0) {
122                 shutdown_client(sc, ss);
123                 goto out;
124         }
125         if (!len)
126                 goto out;
127         if (!ret) { /* still data left in the queue */
128                 ret = queue_chunk_or_shutdown(sc, ss, buf, len);
129                 goto out;
130         }
131         ret = xwrite(sc->fd, buf, len);
132         if (ret < 0) {
133                 shutdown_client(sc, ss);
134                 goto out;
135         }
136         if (ret != len)
137                 ret = queue_chunk_or_shutdown(sc, ss, buf + ret, len - ret);
138 out:
139         if (ret < 0)
140                 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
141 }
142
143 static void http_send(long unsigned current_chunk,
144         __a_unused long unsigned chunks_sent, const char *buf, size_t len,
145         const char *header_buf, size_t header_len)
146 {
147         struct sender_client *sc, *tmp;
148
149         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
150                 struct private_http_sender_data *phsd = sc->private_data;
151
152                 if (phsd->status == HTTP_STREAMING)
153                         http_send_chunk(sc, hss, current_chunk, buf, len,
154                                    header_buf, header_len);
155         }
156 }
157
158 static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
159 {
160         struct sender_client *sc, *tmp;
161         struct private_http_sender_data *phsd;
162         int ret;
163
164         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
165                 phsd = sc->private_data;
166                 switch (phsd->status) {
167                 case HTTP_STREAMING: /* nothing to do */
168                         break;
169                 case HTTP_CONNECTED: /* need to recv get request */
170                         ret = read_pattern(sc->fd, HTTP_GET_MSG, MAXLINE, rfds);
171                         if (ret < 0)
172                                 phsd->status = HTTP_INVALID_GET_REQUEST;
173                         else if (ret > 0) {
174                                 phsd->status = HTTP_GOT_GET_REQUEST;
175                                 PARA_INFO_LOG("received get request\n");
176                         }
177                         break;
178                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
179                         phsd->status = HTTP_STREAMING;
180                         http_send_ok_msg(sc);
181                         break;
182                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
183                         if (http_send_err_msg(sc) >= 0)
184                                 shutdown_client(sc, hss);
185                         break;
186                 }
187         }
188         sc = accept_sender_client(hss, rfds);
189         if (!sc)
190                 return;
191         phsd = para_malloc(sizeof(*phsd));
192         sc->private_data = phsd;
193         phsd->status = HTTP_CONNECTED;
194 }
195
196 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
197 {
198         struct sender_client *sc, *tmp;
199
200         if (hss->listen_fd < 0)
201                 return;
202         para_fd_set(hss->listen_fd, rfds, max_fileno);
203         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
204                 struct private_http_sender_data *phsd = sc->private_data;
205                 if (phsd->status == HTTP_CONNECTED) /* need to recv get request */
206                         para_fd_set(sc->fd, rfds, max_fileno);
207                 if (phsd->status == HTTP_GOT_GET_REQUEST ||
208                                 phsd->status == HTTP_INVALID_GET_REQUEST)
209                         para_fd_set(sc->fd, wfds, max_fileno);
210         }
211 }
212
213 static int http_com_on(__a_unused struct sender_command_data *scd)
214 {
215         return generic_com_on(hss, IPPROTO_TCP);
216 }
217
218 static int http_com_off(__a_unused struct sender_command_data *scd)
219 {
220         generic_com_off(hss);
221         return 1;
222 }
223
224 static int http_com_deny(struct sender_command_data *scd)
225 {
226         generic_com_deny(scd, hss);
227         return 1;
228 }
229
230 static int http_com_allow(struct sender_command_data *scd)
231 {
232         generic_com_allow(scd, hss);
233         return 1;
234 }
235
236 static char *http_status(void)
237 {
238         return generic_sender_status(hss, "http");
239 }
240
241 /**
242  * The init function of the http sender.
243  *
244  * \param s Pointer to the http sender struct.
245  *
246  * It initializes all function pointers of \a s, the client list and the access
247  * control list. If the autostart option was given, the tcp port is opened.
248  */
249 void http_send_init(struct sender *s)
250 {
251         int ret;
252         s->status = http_status;
253         s->send = http_send;
254         s->pre_select = http_pre_select;
255         s->post_select = http_post_select;
256         s->shutdown_clients = http_shutdown_clients;
257         s->resolve_target = NULL;
258         s->help = generic_sender_help;
259         s->client_cmds[SENDER_ON] = http_com_on;
260         s->client_cmds[SENDER_OFF] = http_com_off;
261         s->client_cmds[SENDER_DENY] = http_com_deny;
262         s->client_cmds[SENDER_ALLOW] = http_com_allow;
263         s->client_cmds[SENDER_ADD] = NULL;
264         s->client_cmds[SENDER_DELETE] = NULL;
265
266         init_sender_status(hss, conf.http_access_arg, conf.http_access_given,
267                 conf.http_port_arg, conf.http_max_clients_arg,
268                 conf.http_default_deny_given);
269         if (conf.http_no_autostart_given)
270                 return;
271         ret = generic_com_on(hss, IPPROTO_TCP);
272         if (ret < 0)
273                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
274 }