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