udp_recv: Replace recv_bin_buffer() by Use para_readv().
[paraslash.git] / http_send.c
1 /*
2  * Copyright (C) 2005-2009 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
14 #include "para.h"
15 #include "error.h"
16 #include "string.h"
17 #include "server.cmdline.h"
18 #include "afh.h"
19 #include "afs.h"
20 #include "server.h"
21 #include "http.h"
22 #include "vss.h"
23 #include "list.h"
24 #include "send.h"
25 #include "close_on_fork.h"
26 #include "net.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 = send_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 void http_send(long unsigned current_chunk,
81         __a_unused long unsigned chunks_sent, const char *buf, size_t len,
82         const char *header_buf, size_t header_len)
83 {
84         struct sender_client *sc, *tmp;
85
86         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
87                 struct private_http_sender_data *phsd = sc->private_data;
88                 if (phsd->status != HTTP_STREAMING)
89                         continue;
90                 send_chunk(sc, hss, 0, current_chunk, buf, len, header_buf,
91                         header_len);
92         }
93 }
94
95 static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
96 {
97         struct sender_client *sc, *tmp;
98         struct private_http_sender_data *phsd;
99
100         if (hss->listen_fd < 0)
101                 return;
102         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
103                 phsd = sc->private_data;
104                 switch (phsd->status) {
105                 case HTTP_STREAMING: /* nothing to do */
106                         break;
107                 case HTTP_CONNECTED: /* need to recv get request */
108                         if (FD_ISSET(sc->fd, rfds)) {
109                                 if (recv_pattern(sc->fd, HTTP_GET_MSG, MAXLINE)
110                                                 < 0) {
111                                         phsd->status = HTTP_INVALID_GET_REQUEST;
112                                 } else {
113                                         phsd->status = HTTP_GOT_GET_REQUEST;
114                                         PARA_INFO_LOG("received get request\n");
115                                 }
116                         }
117                         break;
118                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
119                         phsd->status = HTTP_STREAMING;
120                         http_send_ok_msg(sc);
121                         break;
122                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
123                         if (http_send_err_msg(sc) >= 0)
124                                 shutdown_client(sc, hss);
125                         break;
126                 }
127         }
128         if (!FD_ISSET(hss->listen_fd, rfds))
129                 return;
130         sc = accept_sender_client(hss);
131         if (!sc)
132                 return;
133         phsd = para_malloc(sizeof(*phsd));
134         sc->private_data = phsd;
135         phsd->status = HTTP_CONNECTED;
136 }
137
138 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
139 {
140         struct sender_client *sc, *tmp;
141
142         if (hss->listen_fd < 0)
143                 return;
144         para_fd_set(hss->listen_fd, rfds, max_fileno);
145         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
146                 struct private_http_sender_data *phsd = sc->private_data;
147                 if (phsd->status == HTTP_CONNECTED) /* need to recv get request */
148                         para_fd_set(sc->fd, rfds, max_fileno);
149                 if (phsd->status == HTTP_GOT_GET_REQUEST ||
150                                 phsd->status == HTTP_INVALID_GET_REQUEST)
151                         para_fd_set(sc->fd, wfds, max_fileno);
152         }
153 }
154
155 static int http_com_on(__a_unused struct sender_command_data *scd)
156 {
157         return generic_com_on(hss, IPPROTO_TCP);
158 }
159
160 static int http_com_off(__a_unused struct sender_command_data *scd)
161 {
162         generic_com_off(hss);
163         return 1;
164 }
165
166 static int http_com_deny(struct sender_command_data *scd)
167 {
168         generic_com_deny(scd, hss);
169         return 1;
170 }
171
172 static int http_com_allow(struct sender_command_data *scd)
173 {
174         generic_com_allow(scd, hss);
175         return 1;
176 }
177
178 static char *http_info(void)
179 {
180         return get_sender_info(hss, "http");
181 }
182
183 /**
184  * The init function of the http sender.
185  *
186  * \param s Pointer to the http sender struct.
187  *
188  * It initializes all function pointers of \a s, the client list and the access
189  * control list. If the autostart option was given, the tcp port is opened.
190  */
191 void http_send_init(struct sender *s)
192 {
193         int ret;
194         s->info = http_info;
195         s->send = http_send;
196         s->pre_select = http_pre_select;
197         s->post_select = http_post_select;
198         s->shutdown_clients = http_shutdown_clients;
199         s->help = generic_sender_help;
200         s->client_cmds[SENDER_ON] = http_com_on;
201         s->client_cmds[SENDER_OFF] = http_com_off;
202         s->client_cmds[SENDER_DENY] = http_com_deny;
203         s->client_cmds[SENDER_ALLOW] = http_com_allow;
204         s->client_cmds[SENDER_ADD] = NULL;
205         s->client_cmds[SENDER_DELETE] = NULL;
206
207         init_sender_status(hss, conf.http_access_arg, conf.http_access_given,
208                 conf.http_port_arg, conf.http_max_clients_arg,
209                 conf.http_default_deny_given);
210         if (conf.http_no_autostart_given)
211                 return;
212         ret = generic_com_on(hss, IPPROTO_TCP);
213         if (ret < 0)
214                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
215 }