osx_write: Make it compile on Snow Leopard.
[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 <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "string.h"
15 #include "server.cmdline.h"
16 #include "afh.h"
17 #include "afs.h"
18 #include "server.h"
19 #include "http.h"
20 #include "vss.h"
21 #include "list.h"
22 #include "send.h"
23 #include "close_on_fork.h"
24 #include "net.h"
25 #include "fd.h"
26 #include "chunk_queue.h"
27 #include "acl.h"
28
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"
31
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. */
35         HTTP_CONNECTED,
36         /** Successfully received the get request. */
37         HTTP_GOT_GET_REQUEST,
38         /** Connection is ready for sending audio data. */
39         HTTP_STREAMING,
40         /** We didn't receive a valid get request. */
41         HTTP_INVALID_GET_REQUEST
42 };
43
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;
48 };
49
50 static struct sender_status http_sender_status, *hss = &http_sender_status;
51
52 static int http_send_msg(struct sender_client *sc, const char *msg)
53 {
54         int ret = send_buffer(sc->fd, msg);
55
56         if (ret < 0)
57                 shutdown_client(sc, hss);
58         return ret;
59 }
60
61 static void http_send_ok_msg(struct sender_client *sc)
62 {
63         PARA_INFO_LOG("sending http ok message to fd %d\n", sc->fd);
64         http_send_msg(sc, HTTP_OK_MSG);
65 }
66
67 static int http_send_err_msg(struct sender_client *sc)
68 {
69         PARA_NOTICE_LOG("sending bad request message to fd %d\n", sc->fd);
70         return http_send_msg(sc, HTTP_ERR_MSG);
71 }
72
73 static void http_shutdown_clients(void)
74 {
75         shutdown_clients(hss);
76 }
77
78 static void http_send(long unsigned current_chunk,
79         __a_unused long unsigned chunks_sent, const char *buf, size_t len,
80         const char *header_buf, size_t header_len)
81 {
82         struct sender_client *sc, *tmp;
83
84         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
85                 struct private_http_sender_data *phsd = sc->private_data;
86                 if (phsd->status != HTTP_STREAMING)
87                         continue;
88                 send_chunk(sc, hss, 0, current_chunk, buf, len, header_buf,
89                         header_len);
90         }
91 }
92
93 static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
94 {
95         struct sender_client *sc, *tmp;
96         struct private_http_sender_data *phsd;
97
98         if (hss->listen_fd < 0)
99                 return;
100         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
101                 phsd = sc->private_data;
102                 switch (phsd->status) {
103                 case HTTP_STREAMING: /* nothing to do */
104                         break;
105                 case HTTP_CONNECTED: /* need to recv get request */
106                         if (FD_ISSET(sc->fd, rfds)) {
107                                 if (recv_pattern(sc->fd, HTTP_GET_MSG, MAXLINE)
108                                                 < 0) {
109                                         phsd->status = HTTP_INVALID_GET_REQUEST;
110                                 } else {
111                                         phsd->status = HTTP_GOT_GET_REQUEST;
112                                         PARA_INFO_LOG("received get request\n");
113                                 }
114                         }
115                         break;
116                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
117                         phsd->status = HTTP_STREAMING;
118                         http_send_ok_msg(sc);
119                         break;
120                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
121                         if (http_send_err_msg(sc) >= 0)
122                                 shutdown_client(sc, hss);
123                         break;
124                 }
125         }
126         if (!FD_ISSET(hss->listen_fd, rfds))
127                 return;
128         sc = accept_sender_client(hss);
129         if (!sc)
130                 return;
131         phsd = para_malloc(sizeof(*phsd));
132         sc->private_data = phsd;
133         phsd->status = HTTP_CONNECTED;
134 }
135
136 static void http_pre_select(int *max_fileno, fd_set *rfds, __a_unused fd_set *wfds)
137 {
138         struct sender_client *sc, *tmp;
139
140         if (hss->listen_fd < 0)
141                 return;
142         para_fd_set(hss->listen_fd, rfds, max_fileno);
143         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
144                 struct private_http_sender_data *phsd = sc->private_data;
145                 if (phsd->status == HTTP_CONNECTED) /* need to recv get request */
146                         para_fd_set(sc->fd, rfds, max_fileno);
147         }
148 }
149
150 static int http_com_on(__a_unused struct sender_command_data *scd)
151 {
152         return generic_com_on(hss, IPPROTO_TCP);
153 }
154
155 static int http_com_off(__a_unused struct sender_command_data *scd)
156 {
157         generic_com_off(hss);
158         return 1;
159 }
160
161 static int http_com_deny(struct sender_command_data *scd)
162 {
163         generic_com_deny(scd, hss);
164         return 1;
165 }
166
167 static int http_com_allow(struct sender_command_data *scd)
168 {
169         generic_com_allow(scd, hss);
170         return 1;
171 }
172
173 static char *http_info(void)
174 {
175         return get_sender_info(hss, "http");
176 }
177
178 /**
179  * The init function of the http sender.
180  *
181  * \param s Pointer to the http sender struct.
182  *
183  * It initializes all function pointers of \a s, the client list and the access
184  * control list. If the autostart option was given, the tcp port is opened.
185  */
186 void http_send_init(struct sender *s)
187 {
188         int ret;
189         s->info = http_info;
190         s->send = http_send;
191         s->pre_select = http_pre_select;
192         s->post_select = http_post_select;
193         s->shutdown_clients = http_shutdown_clients;
194         s->help = generic_sender_help;
195         s->client_cmds[SENDER_ON] = http_com_on;
196         s->client_cmds[SENDER_OFF] = http_com_off;
197         s->client_cmds[SENDER_DENY] = http_com_deny;
198         s->client_cmds[SENDER_ALLOW] = http_com_allow;
199         s->client_cmds[SENDER_ADD] = NULL;
200         s->client_cmds[SENDER_DELETE] = NULL;
201
202         init_sender_status(hss, conf.http_access_arg, conf.http_access_given,
203                 conf.http_port_arg, conf.http_max_clients_arg,
204                 conf.http_default_deny_given);
205         if (conf.http_no_autostart_given)
206                 return;
207         ret = generic_com_on(hss, IPPROTO_TCP);
208         if (ret < 0)
209                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
210 }