1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file http_send.c paraslash's http sender */
5 #include <netinet/in.h>
6 #include <sys/socket.h>
14 #include "server.lsg.h"
27 #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"
32 /** The possible states of a client from the server's POV. */
33 enum http_client_status {
34 /** We accepted the connection on the tcp socket. */
36 /** Successfully received the get request. */
38 /** Connection is ready for sending audio data. */
40 /** We didn't receive a valid get request. */
41 HTTP_INVALID_GET_REQUEST
44 /** For each connected client, a structure of this type is maintained. */
45 struct private_http_sender_data {
46 /** The current state of this client. */
47 enum http_client_status status;
50 static struct sender_status http_sender_status, *hss = &http_sender_status;
52 static int http_send_msg(struct sender_client *sc, const char *msg)
54 int ret = write_buffer(sc->fd, msg);
57 shutdown_client(sc, hss);
61 static void http_send_ok_msg(struct sender_client *sc)
63 PARA_INFO_LOG("sending http ok message to fd %d\n", sc->fd);
64 http_send_msg(sc, HTTP_OK_MSG);
67 static int http_send_err_msg(struct sender_client *sc)
69 PARA_NOTICE_LOG("sending bad request message to fd %d\n", sc->fd);
70 return http_send_msg(sc, HTTP_ERR_MSG);
73 static void http_shutdown_clients(void)
75 shutdown_clients(hss);
78 static int queue_chunk_or_shutdown(struct sender_client *sc,
79 struct sender_status *ss, const char *buf, size_t num_bytes)
81 int ret = cq_enqueue(sc->cq, buf, num_bytes);
83 shutdown_client(sc, ss);
88 * Send one chunk of audio data to a connected client.
90 * \param sc The client.
91 * \param ss The sender.
92 * \param current_chunk The number of the chunk to write.
93 * \param buf The data to write.
94 * \param len The number of bytes of \a buf.
95 * \param header_buf The audio file header.
96 * \param header_len The number of bytes of \a header_buf.
98 * On errors, the client is shut down. If only a part of the buffer could be
99 * written, the remainder is put into the chunk queue for that client.
101 static void http_send_chunk(struct sender_client *sc, struct sender_status *ss,
102 long unsigned current_chunk, const char *buf, size_t len,
103 const char *header_buf, size_t header_len)
107 if (!sc->header_sent && current_chunk) {
108 if (header_buf && header_len > 0) {
109 ret = queue_chunk_or_shutdown(sc, ss, header_buf, header_len);
115 ret = send_queued_chunks(sc->fd, sc->cq);
117 shutdown_client(sc, ss);
122 if (!ret) { /* still data left in the queue */
123 ret = queue_chunk_or_shutdown(sc, ss, buf, len);
126 ret = xwrite(sc->fd, buf, len);
128 shutdown_client(sc, ss);
132 ret = queue_chunk_or_shutdown(sc, ss, buf + ret, len - ret);
135 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
138 static void http_send(long unsigned current_chunk,
139 __a_unused long unsigned chunks_sent, const char *buf, size_t len,
140 const char *header_buf, size_t header_len)
142 struct sender_client *sc, *tmp;
144 list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
145 struct private_http_sender_data *phsd = sc->private_data;
147 if (phsd->status == HTTP_STREAMING)
148 http_send_chunk(sc, hss, current_chunk, buf, len,
149 header_buf, header_len);
153 static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
155 struct sender_client *sc, *tmp;
156 struct private_http_sender_data *phsd;
159 list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
160 phsd = sc->private_data;
161 switch (phsd->status) {
162 case HTTP_STREAMING: /* nothing to do */
164 case HTTP_CONNECTED: /* need to recv get request */
165 ret = read_pattern(sc->fd, HTTP_GET_MSG, MAXLINE, rfds);
167 phsd->status = HTTP_INVALID_GET_REQUEST;
169 phsd->status = HTTP_GOT_GET_REQUEST;
170 PARA_INFO_LOG("received get request\n");
173 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
174 phsd->status = HTTP_STREAMING;
175 http_send_ok_msg(sc);
177 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
178 if (http_send_err_msg(sc) >= 0)
179 shutdown_client(sc, hss);
183 sc = accept_sender_client(hss, rfds);
186 phsd = para_malloc(sizeof(*phsd));
187 sc->private_data = phsd;
188 phsd->status = HTTP_CONNECTED;
191 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
193 struct sender_client *sc, *tmp;
195 if (hss->listen_fd < 0)
197 para_fd_set(hss->listen_fd, rfds, max_fileno);
198 list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
199 struct private_http_sender_data *phsd = sc->private_data;
200 if (phsd->status == HTTP_CONNECTED) /* need to recv get request */
201 para_fd_set(sc->fd, rfds, max_fileno);
202 if (phsd->status == HTTP_GOT_GET_REQUEST ||
203 phsd->status == HTTP_INVALID_GET_REQUEST)
204 para_fd_set(sc->fd, wfds, max_fileno);
208 static int http_com_on(__a_unused struct sender_command_data *scd)
210 return generic_com_on(hss, IPPROTO_TCP);
213 static int http_com_off(__a_unused struct sender_command_data *scd)
215 generic_com_off(hss);
219 static int http_com_deny(struct sender_command_data *scd)
221 generic_com_deny(scd, hss);
225 static int http_com_allow(struct sender_command_data *scd)
227 generic_com_allow(scd, hss);
231 static char *http_status(void)
233 return generic_sender_status(hss, "http");
237 * Initialize the client list and the access control list, and optionally
238 * listen on the tcp port.
240 static void http_send_init(void)
244 init_sender_status(hss, OPT_RESULT(HTTP_ACCESS),
245 OPT_UINT32_VAL(HTTP_PORT), OPT_UINT32_VAL(HTTP_MAX_CLIENTS),
246 OPT_GIVEN(HTTP_DEFAULT_DENY));
247 if (OPT_GIVEN(HTTP_NO_AUTOSTART))
249 ret = generic_com_on(hss, IPPROTO_TCP);
251 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
257 * This is the only sender which does not FEC-encode the stream. This is not
258 * necessary because HTTP sits on top of TCP, a reliable transport which
259 * retransmits lost packets automatically. The sender employs per-client queues
260 * which queue chunks of audio data if they can not be sent immediately because
261 * the write operation would block. Most methods of the sender are implemented
262 * as wrappers for the generic functions defined in \ref send_common.c.
264 const struct sender http_sender = {
266 .init = http_send_init,
267 .shutdown = http_shutdown_clients,
268 .pre_select = http_pre_select,
269 .post_select = http_post_select,
271 .shutdown_clients = http_shutdown_clients,
273 [SENDER_on] = http_com_on,
274 [SENDER_off] = http_com_off,
275 [SENDER_deny] = http_com_deny,
276 [SENDER_allow] = http_com_allow,
278 .help = generic_sender_help,
279 .status = http_status,