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