2 * Copyright (C) 2005-2006 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 */
25 #include "http_recv.cmdline.h"
30 /** the output buffer size of the http receiver */
31 #define BUFSIZE (32 * 1024)
34 * the possible states of a http receiver node
38 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
41 * data specific to the http receiver
43 * Each running instance of the http receiver reserves space for one such struct.
45 struct private_http_recv_data {
49 * the current status of the http receiver node
51 * It gets initialized to #HTTP_CONNECTED by the open function of the
54 * \sa receiver::open, receiver_node.
56 enum http_recv_status status;
60 * the file descriptor used for receiving the http stream
62 * The pre_select function of the http receiver adds this file descriptor to
63 * the set of file decriptors which are checked for reading/writing (depending
64 * on the current status) by the select loop of the application (para_audiod or
67 * The post_select function of the http receiver uses \a fd, if ready, to
68 * establish the http connection, and updates \a status according to the new
69 * state of the connection. As soon as \a status is #HTTP_STREAMING, \a fd is
70 * going to be only checked for reading. If data is available, it is read into
71 * the output buffer of the receiver node by post_select.
73 * \sa receiver::pre_select receiver::post_select receiver_node
78 static void http_shutdown(void)
83 static char *make_request_msg(void)
85 char *ret, *hn = para_hostname();
86 ret = make_message("%s\nHost: %s\nUser-Agent: para_recv/%s\n\n\n",
87 HTTP_GET_MSG, hn, VERSION);
92 static int http_pre_select(struct receiver_node *rn, fd_set *rfds, fd_set *wfds,
93 __unused struct timeval *timeout)
95 struct private_http_recv_data *phd = rn->private_data;
97 if (phd->status == HTTP_CONNECTED)
98 FD_SET(phd->fd, wfds);
100 FD_SET(phd->fd, rfds);
104 static int http_post_select(struct receiver_node *rn, int select_ret,
105 fd_set *rfds, fd_set *wfds)
108 struct private_http_recv_data *phd = rn->private_data;
110 if (!select_ret) /* we're not interested in timeouts */
112 if (phd->status == HTTP_CONNECTED) {
114 if (!FD_ISSET(phd->fd, wfds))
115 return 1; /* nothing to do */
116 rq = make_request_msg();
117 PARA_NOTICE_LOG("%s", "sending http request\n");
118 ret = send_va_buffer(phd->fd, "%s", rq);
121 return E_SEND_HTTP_REQUEST;
122 phd->status = HTTP_SENT_GET_REQUEST;
125 if (!FD_ISSET(phd->fd, rfds))
126 return 1; /* nothing to do */
127 if (phd->status == HTTP_SENT_GET_REQUEST) {
128 ret = recv_pattern(phd->fd, HTTP_OK_MSG, MAXLINE);
130 return -E_MISSING_OK;
131 PARA_NOTICE_LOG("%s", "received ok msg, streaming\n");
132 phd->status = HTTP_STREAMING;
135 /* already streaming */
136 if (rn->loaded >= BUFSIZE) {
137 PARA_ERROR_LOG("%s", "buffer overrun\n");
140 ret = recv_bin_buffer(phd->fd, rn->buf + rn->loaded, BUFSIZE - rn->loaded);
142 PARA_NOTICE_LOG("recv returned %d/%zd\n", ret, BUFSIZE - rn->loaded);
143 return ret < 0? -E_HTTP_RECV_BUF : 0;
149 static void http_recv_close(struct receiver_node *rn)
151 struct private_http_recv_data *phd = rn->private_data;
154 free(rn->private_data);
157 static void *http_recv_parse_config(int argc, char **argv)
159 struct gengetopt_args_info *tmp = para_calloc(sizeof(struct gengetopt_args_info));
161 if (!http_recv_cmdline_parser(argc, argv, tmp))
167 static int http_recv_open(struct receiver_node *rn)
169 struct private_http_recv_data *phd;
171 struct gengetopt_args_info *conf = rn->conf;
172 struct sockaddr_in their_addr;
175 rn->buf = para_calloc(BUFSIZE);
176 rn->private_data = para_calloc(sizeof(struct private_http_recv_data));
177 phd = rn->private_data;
180 if (!(he = get_host_info(conf->host_arg)))
184 if ((phd->fd = get_socket()) < 0)
186 /* init their_addr */
187 init_sockaddr(&their_addr, conf->port_arg, he);
189 PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg,
191 ret = para_connect(phd->fd, &their_addr);
194 phd->status = HTTP_CONNECTED;
197 free(rn->private_data);
203 * the init function of the http receiver
205 * \param r pointer to the receiver struct to initialize
207 * Just initialize all function pointers of \a r.
209 void http_recv_init(struct receiver *r)
211 r->open = http_recv_open;
212 r->close = http_recv_close;
213 r->pre_select = http_pre_select;
214 r->post_select = http_post_select;
215 r->shutdown = http_shutdown;
216 r->parse_config = http_recv_parse_config;