2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file http_send.c paraslash's http sender */
9 #include <netinet/in.h>
10 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
20 #include "server.cmdline.h"
28 #include "close_on_fork.h"
31 #include "chunk_queue.h"
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"
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. */
41 /** Successfully received the get request. */
43 /** Connection is ready for sending audio data. */
45 /** We didn't receive a valid get request. */
46 HTTP_INVALID_GET_REQUEST
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;
55 static struct sender_status http_sender_status, *hss = &http_sender_status;
57 static int http_send_msg(struct sender_client *sc, const char *msg)
59 int ret = write_buffer(sc->fd, msg);
62 shutdown_client(sc, hss);
66 static void http_send_ok_msg(struct sender_client *sc)
68 PARA_INFO_LOG("sending http ok message to fd %d\n", sc->fd);
69 http_send_msg(sc, HTTP_OK_MSG);
72 static int http_send_err_msg(struct sender_client *sc)
74 PARA_NOTICE_LOG("sending bad request message to fd %d\n", sc->fd);
75 return http_send_msg(sc, HTTP_ERR_MSG);
78 static void http_shutdown_clients(void)
80 shutdown_clients(hss);
83 static int queue_chunk_or_shutdown(struct sender_client *sc,
84 struct sender_status *ss, const char *buf, size_t num_bytes)
86 int ret = cq_enqueue(sc->cq, buf, num_bytes);
88 shutdown_client(sc, ss);
93 * Send one chunk of audio data to a connected client.
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.
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.
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)
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);
120 ret = send_queued_chunks(sc->fd, sc->cq);
122 shutdown_client(sc, ss);
127 if (!ret) { /* still data left in the queue */
128 ret = queue_chunk_or_shutdown(sc, ss, buf, len);
131 ret = xwrite(sc->fd, buf, len);
133 shutdown_client(sc, ss);
137 ret = queue_chunk_or_shutdown(sc, ss, buf + ret, len - ret);
140 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
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)
147 struct sender_client *sc, *tmp;
149 list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
150 struct private_http_sender_data *phsd = sc->private_data;
152 if (phsd->status == HTTP_STREAMING)
153 http_send_chunk(sc, hss, current_chunk, buf, len,
154 header_buf, header_len);
158 static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
160 struct sender_client *sc, *tmp;
161 struct private_http_sender_data *phsd;
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 */
169 case HTTP_CONNECTED: /* need to recv get request */
170 ret = read_pattern(sc->fd, HTTP_GET_MSG, MAXLINE, rfds);
172 phsd->status = HTTP_INVALID_GET_REQUEST;
174 phsd->status = HTTP_GOT_GET_REQUEST;
175 PARA_INFO_LOG("received get request\n");
178 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
179 phsd->status = HTTP_STREAMING;
180 http_send_ok_msg(sc);
182 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
183 if (http_send_err_msg(sc) >= 0)
184 shutdown_client(sc, hss);
188 sc = accept_sender_client(hss, rfds);
191 phsd = para_malloc(sizeof(*phsd));
192 sc->private_data = phsd;
193 phsd->status = HTTP_CONNECTED;
196 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
198 struct sender_client *sc, *tmp;
200 if (hss->listen_fd < 0)
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);
213 static int http_com_on(__a_unused struct sender_command_data *scd)
215 return generic_com_on(hss, IPPROTO_TCP);
218 static int http_com_off(__a_unused struct sender_command_data *scd)
220 generic_com_off(hss);
224 static int http_com_deny(struct sender_command_data *scd)
226 generic_com_deny(scd, hss);
230 static int http_com_allow(struct sender_command_data *scd)
232 generic_com_allow(scd, hss);
236 static char *http_status(void)
238 return generic_sender_status(hss, "http");
242 * The init function of the http sender.
244 * \param s Pointer to the http sender struct.
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.
249 void http_send_init(struct sender *s)
252 s->status = http_status;
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;
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)
271 ret = generic_com_on(hss, IPPROTO_TCP);
273 PARA_ERROR_LOG("%s\n", para_strerror(-ret));