2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file http_recv.c paraslash's http receiver */
27 #include "http_recv.cmdline.h"
33 /** the output buffer size of the http receiver */
34 #define BUFSIZE (32 * 1024)
37 * the possible states of a http receiver node
41 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
44 * data specific to the http receiver
46 * Each running instance of the http receiver reserves space for one such struct.
48 struct private_http_recv_data {
52 * the current status of the http receiver node
54 * It gets initialized to \p HTTP_CONNECTED by the open function of the
57 * \sa receiver::open, receiver_node.
59 enum http_recv_status status;
63 * the file descriptor used for receiving the http stream
65 * The pre_select function of the http receiver adds this file descriptor to
66 * the set of file decriptors which are checked for reading/writing (depending
67 * on the current status) by the select loop of the application (para_audiod or
70 * The post_select function of the http receiver uses \a fd, if ready, to
71 * establish the http connection, and updates \a status according to the new
72 * state of the connection. As soon as \a status is \p HTTP_STREAMING, \a fd is
73 * going to be only checked for reading. If data is available, it is read into
74 * the output buffer of the receiver node by post_select.
76 * \sa receiver::pre_select receiver::post_select receiver_node, http_recv_status
81 static void http_shutdown(void)
86 static char *make_request_msg(void)
88 char *ret, *hn = para_hostname();
89 ret = make_message("%s1.0\nHost: %s\nUser-Agent: para_recv/%s\n\n\n",
90 HTTP_GET_MSG, hn, PACKAGE_VERSION);
95 static void http_recv_pre_select(struct sched *s, struct task *t)
97 struct receiver_node *rn = t->private_data;
98 struct private_http_recv_data *phd = rn->private_data;
101 if (phd->status == HTTP_CONNECTED)
102 para_fd_set(phd->fd, &s->wfds, &s->max_fileno);
104 para_fd_set(phd->fd, &s->rfds, &s->max_fileno);
108 static void http_recv_post_select(struct sched *s, struct task *t)
110 struct receiver_node *rn = t->private_data;
111 struct private_http_recv_data *phd = rn->private_data;
113 t->ret = -E_HTTP_RECV_EOF;
114 if (rn->output_eof && *rn->output_eof)
119 if (phd->status == HTTP_CONNECTED) {
121 if (!FD_ISSET(phd->fd, &s->wfds))
123 rq = make_request_msg();
124 PARA_INFO_LOG("%s", "sending http request\n");
125 t->ret = send_va_buffer(phd->fd, "%s", rq);
128 phd->status = HTTP_SENT_GET_REQUEST;
131 if (!FD_ISSET(phd->fd, &s->rfds))
133 if (phd->status == HTTP_SENT_GET_REQUEST) {
134 t->ret = recv_pattern(phd->fd, HTTP_OK_MSG, MAXLINE);
137 PARA_INFO_LOG("%s", "received ok msg, streaming\n");
139 phd->status = HTTP_STREAMING;
143 if (rn->loaded >= BUFSIZE)
145 t->ret = recv_bin_buffer(phd->fd, rn->buf + rn->loaded,
146 BUFSIZE - rn->loaded);
149 t->ret = -E_HTTP_RECV_EOF;
152 rn->loaded += t->ret;
158 static void http_recv_close(struct receiver_node *rn)
160 struct private_http_recv_data *phd = rn->private_data;
163 free(rn->private_data);
166 static void *http_recv_parse_config(int argc, char **argv)
168 struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
170 if (!http_recv_cmdline_parser(argc, argv, tmp))
176 static int http_recv_open(struct receiver_node *rn)
178 struct private_http_recv_data *phd;
180 struct http_recv_args_info *conf = rn->conf;
181 struct sockaddr_in their_addr;
184 rn->buf = para_calloc(BUFSIZE);
185 rn->private_data = para_calloc(sizeof(struct private_http_recv_data));
186 phd = rn->private_data;
187 ret = get_host_info(conf->host_arg, &he);
195 /* init their_addr */
196 init_sockaddr(&their_addr, conf->port_arg, he);
198 PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg,
200 ret = para_connect(phd->fd, &their_addr);
205 mark_fd_nonblock(phd->fd);
206 phd->status = HTTP_CONNECTED;
209 free(rn->private_data);
215 * the init function of the http receiver
217 * \param r pointer to the receiver struct to initialize
219 * Just initialize all function pointers of \a r.
221 void http_recv_init(struct receiver *r)
223 r->open = http_recv_open;
224 r->close = http_recv_close;
225 r->pre_select = http_recv_pre_select;
226 r->post_select = http_recv_post_select;
227 r->shutdown = http_shutdown;
228 r->parse_config = http_recv_parse_config;