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