]> git.tuebingen.mpg.de Git - paraslash.git/blob - http_send.c
server.c: Clarify fatal log message.
[paraslash.git] / http_send.c
1 /*
2  * Copyright (C) 2005-2008 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 struct private_http_sender_data {
45         enum http_client_status status;
46 };
47
48 static struct sender_status http_sender_status, *hss = &http_sender_status;
49
50 static int http_send_msg(struct sender_client *sc, const char *msg)
51 {
52         int ret = send_buffer(sc->fd, msg);
53
54         if (ret < 0)
55                 shutdown_client(sc, hss);
56         return ret;
57 }
58
59 static void http_send_ok_msg(struct sender_client *sc)
60 {
61         PARA_INFO_LOG("sending http ok message to fd %d\n", sc->fd);
62         http_send_msg(sc, HTTP_OK_MSG);
63 }
64
65 static int http_send_err_msg(struct sender_client *sc)
66 {
67         PARA_NOTICE_LOG("sending bad request message to fd %d\n", sc->fd);
68         return http_send_msg(sc, HTTP_ERR_MSG);
69 }
70
71 static void http_shutdown_clients(void)
72 {
73         shutdown_clients(hss);
74 }
75
76 static void http_send(long unsigned current_chunk,
77         __a_unused long unsigned chunks_sent, const char *buf, size_t len)
78 {
79         struct sender_client *sc, *tmp;
80
81         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
82                 struct private_http_sender_data *phsd = sc->private_data;
83                 if (phsd->status != HTTP_STREAMING)
84                         continue;
85                 send_chunk(sc, hss, 0, current_chunk, buf, len);
86         }
87 }
88
89 static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
90 {
91         struct sender_client *sc, *tmp;
92         struct private_http_sender_data *phsd;
93
94         if (hss->listen_fd < 0)
95                 return;
96         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
97                 phsd = sc->private_data;
98                 switch (phsd->status) {
99                 case HTTP_STREAMING: /* nothing to do */
100                         break;
101                 case HTTP_CONNECTED: /* need to recv get request */
102                         if (FD_ISSET(sc->fd, rfds)) {
103                                 if (recv_pattern(sc->fd, HTTP_GET_MSG, MAXLINE)
104                                                 < 0) {
105                                         phsd->status = HTTP_INVALID_GET_REQUEST;
106                                 } else {
107                                         phsd->status = HTTP_GOT_GET_REQUEST;
108                                         PARA_INFO_LOG("%s",
109                                                 "received get request\n");
110                                 }
111                         }
112                         break;
113                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
114                         phsd->status = HTTP_STREAMING;
115                         http_send_ok_msg(sc);
116                         break;
117                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
118                         if (http_send_err_msg(sc) >= 0)
119                                 shutdown_client(sc, hss);
120                         break;
121                 }
122         }
123         if (!FD_ISSET(hss->listen_fd, rfds))
124                 return;
125         sc = accept_sender_client(hss);
126         if (!sc)
127                 return;
128         phsd = para_malloc(sizeof(*phsd));
129         sc->private_data = phsd;
130         phsd->status = HTTP_CONNECTED;
131 }
132
133 static void http_pre_select(int *max_fileno, fd_set *rfds, __a_unused fd_set *wfds)
134 {
135         struct sender_client *sc, *tmp;
136
137         if (hss->listen_fd < 0)
138                 return;
139         para_fd_set(hss->listen_fd, rfds, max_fileno);
140         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
141                 struct private_http_sender_data *phsd = sc->private_data;
142                 if (phsd->status == HTTP_CONNECTED) /* need to recv get request */
143                         para_fd_set(sc->fd, rfds, max_fileno);
144         }
145 }
146
147 static int http_com_on(__a_unused struct sender_command_data *scd)
148 {
149         return generic_com_on(hss, IPPROTO_TCP);
150 }
151
152 static int http_com_off(__a_unused struct sender_command_data *scd)
153 {
154         generic_com_off(hss);
155         return 1;
156 }
157
158 static int http_com_deny(struct sender_command_data *scd)
159 {
160         generic_com_deny(scd, hss);
161         return 1;
162 }
163
164 static int http_com_allow(struct sender_command_data *scd)
165 {
166         generic_com_allow(scd, hss);
167         return 1;
168 }
169
170 static char *http_info(void)
171 {
172         return get_sender_info(hss, "http");
173 }
174
175 /**
176  * The init function of the http sender.
177  *
178  * \param s Pointer to the http sender struct.
179  *
180  * It initializes all function pointers of \a s, the client list and the access
181  * control list. If the autostart option was given, the tcp port is opened.
182  */
183 void http_send_init(struct sender *s)
184 {
185         int ret;
186         s->info = http_info;
187         s->send = http_send;
188         s->pre_select = http_pre_select;
189         s->post_select = http_post_select;
190         s->shutdown_clients = http_shutdown_clients;
191         s->help = generic_sender_help;
192         s->client_cmds[SENDER_ON] = http_com_on;
193         s->client_cmds[SENDER_OFF] = http_com_off;
194         s->client_cmds[SENDER_DENY] = http_com_deny;
195         s->client_cmds[SENDER_ALLOW] = http_com_allow;
196         s->client_cmds[SENDER_ADD] = NULL;
197         s->client_cmds[SENDER_DELETE] = NULL;
198
199         init_sender_status(hss, conf.http_access_arg, conf.http_access_given,
200                 conf.http_port_arg, conf.http_max_clients_arg,
201                 conf.http_default_deny_given);
202         if (conf.http_no_autostart_given)
203                 return;
204         ret = generic_com_on(hss, IPPROTO_TCP);
205         if (ret < 0)
206                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
207 }