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