2 * Copyright (C) 2005-2010 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file http_send.c paraslash's http sender */
10 #include <sys/types.h>
17 #include "server.cmdline.h"
25 #include "close_on_fork.h"
28 #include "chunk_queue.h"
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"
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. */
38 /** Successfully received the get request. */
40 /** Connection is ready for sending audio data. */
42 /** We didn't receive a valid get request. */
43 HTTP_INVALID_GET_REQUEST
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
;
52 static struct sender_status http_sender_status
, *hss
= &http_sender_status
;
54 static int http_send_msg(struct sender_client
*sc
, const char *msg
)
56 int ret
= send_buffer(sc
->fd
, msg
);
59 shutdown_client(sc
, hss
);
63 static void http_send_ok_msg(struct sender_client
*sc
)
65 PARA_INFO_LOG("sending http ok message to fd %d\n", sc
->fd
);
66 http_send_msg(sc
, HTTP_OK_MSG
);
69 static int http_send_err_msg(struct sender_client
*sc
)
71 PARA_NOTICE_LOG("sending bad request message to fd %d\n", sc
->fd
);
72 return http_send_msg(sc
, HTTP_ERR_MSG
);
75 static void http_shutdown_clients(void)
77 shutdown_clients(hss
);
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
)
84 struct sender_client
*sc
, *tmp
;
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
)
90 send_chunk(sc
, hss
, 0, current_chunk
, buf
, len
, header_buf
,
95 static void http_post_select(fd_set
*rfds
, __a_unused fd_set
*wfds
)
97 struct sender_client
*sc
, *tmp
;
98 struct private_http_sender_data
*phsd
;
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 */
106 case HTTP_CONNECTED
: /* need to recv get request */
107 ret
= read_pattern(sc
->fd
, HTTP_GET_MSG
, MAXLINE
, rfds
);
109 phsd
->status
= HTTP_INVALID_GET_REQUEST
;
111 phsd
->status
= HTTP_GOT_GET_REQUEST
;
112 PARA_INFO_LOG("received get request\n");
115 case HTTP_GOT_GET_REQUEST
: /* need to send ok msg */
116 phsd
->status
= HTTP_STREAMING
;
117 http_send_ok_msg(sc
);
119 case HTTP_INVALID_GET_REQUEST
: /* need to send err msg */
120 if (http_send_err_msg(sc
) >= 0)
121 shutdown_client(sc
, hss
);
125 sc
= accept_sender_client(hss
, rfds
);
128 phsd
= para_malloc(sizeof(*phsd
));
129 sc
->private_data
= phsd
;
130 phsd
->status
= HTTP_CONNECTED
;
133 static void http_pre_select(int *max_fileno
, fd_set
*rfds
, fd_set
*wfds
)
135 struct sender_client
*sc
, *tmp
;
137 if (hss
->listen_fd
< 0)
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 if (phsd
->status
== HTTP_GOT_GET_REQUEST
||
145 phsd
->status
== HTTP_INVALID_GET_REQUEST
)
146 para_fd_set(sc
->fd
, wfds
, max_fileno
);
150 static int http_com_on(__a_unused
struct sender_command_data
*scd
)
152 return generic_com_on(hss
, IPPROTO_TCP
);
155 static int http_com_off(__a_unused
struct sender_command_data
*scd
)
157 generic_com_off(hss
);
161 static int http_com_deny(struct sender_command_data
*scd
)
163 generic_com_deny(scd
, hss
);
167 static int http_com_allow(struct sender_command_data
*scd
)
169 generic_com_allow(scd
, hss
);
173 static char *http_info(void)
175 return get_sender_info(hss
, "http");
179 * The init function of the http sender.
181 * \param s Pointer to the http sender struct.
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.
186 void http_send_init(struct sender
*s
)
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
;
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
)
207 ret
= generic_com_on(hss
, IPPROTO_TCP
);
209 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));