Makefile.in: kill sample_conf variable
[paraslash.git] / http_recv.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3 *
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.
8 *
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.
13 *
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.
17 */
18
19 /** \file http_recv.c paraslash's http receiver */
20
21 #include "para.h"
22
23 #include "http.h"
24 #include "recv.h"
25 #include "http_recv.cmdline.h"
26 #include "error.h"
27 #include "net.h"
28 #include "string.h"
29
30 /** the output buffer size of the http receiver */
31 #define BUFSIZE (32 * 1024)
32
33 /**
34 * the possible states of a http receiver node
35 *
36 * \sa receiver_node
37 */
38 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
39
40 /**
41 * data specific to the http receiver
42 *
43 * Each running instance of the http receiver reserves space for one such struct.
44 */
45 struct private_http_recv_data {
46 /**
47 *
48 *
49 * the current status of the http receiver node
50 *
51 * It gets initialized to #HTTP_CONNECTED by the open function of the
52 * http receiver.
53 *
54 * \sa receiver::open, receiver_node.
55 */
56 enum http_recv_status status;
57 /**
58 *
59 *
60 * the file descriptor used for receiving the http stream
61 *
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
65 * para_recv).
66 *
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.
72 *
73 * \sa receiver::pre_select receiver::post_select receiver_node
74 */
75 int fd;
76 };
77
78 static void http_shutdown(void)
79 {
80 return;
81 }
82
83 static char *make_request_msg(void)
84 {
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);
88 free(hn);
89 return ret;
90 }
91
92 static int http_pre_select(struct receiver_node *rn, fd_set *rfds, fd_set *wfds,
93 __a_unused struct timeval *timeout)
94 {
95 struct private_http_recv_data *phd = rn->private_data;
96
97 if (phd->status == HTTP_CONNECTED)
98 FD_SET(phd->fd, wfds);
99 else
100 FD_SET(phd->fd, rfds);
101 return phd->fd;
102 }
103
104 static int http_post_select(struct receiver_node *rn, int select_ret,
105 fd_set *rfds, fd_set *wfds)
106 {
107 int ret;
108 struct private_http_recv_data *phd = rn->private_data;
109
110 if (!select_ret) /* we're not interested in timeouts */
111 return 1;
112 if (phd->status == HTTP_CONNECTED) {
113 char *rq;
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);
119 free(rq);
120 if (ret < 0)
121 return E_SEND_HTTP_REQUEST;
122 phd->status = HTTP_SENT_GET_REQUEST;
123 return 1;
124 }
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);
129 if (ret < 0)
130 return -E_MISSING_OK;
131 PARA_NOTICE_LOG("%s", "received ok msg, streaming\n");
132 phd->status = HTTP_STREAMING;
133 return 1;
134 }
135 /* already streaming */
136 if (rn->loaded >= BUFSIZE) {
137 PARA_ERROR_LOG("%s", "buffer overrun\n");
138 return -E_OVERRUN;
139 }
140 ret = recv_bin_buffer(phd->fd, rn->buf + rn->loaded, BUFSIZE - rn->loaded);
141 if (ret <= 0) {
142 PARA_NOTICE_LOG("recv returned %d/%zd\n", ret, BUFSIZE - rn->loaded);
143 return ret < 0? -E_HTTP_RECV_BUF : 0;
144 }
145 rn->loaded += ret;
146 return 1;
147 }
148
149 static void http_recv_close(struct receiver_node *rn)
150 {
151 struct private_http_recv_data *phd = rn->private_data;
152 close(phd->fd);
153 free(rn->buf);
154 free(rn->private_data);
155 }
156
157 static void *http_recv_parse_config(int argc, char **argv)
158 {
159 struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
160
161 if (!http_recv_cmdline_parser(argc, argv, tmp))
162 return tmp;
163 free(tmp);
164 return NULL;
165 }
166
167 static int http_recv_open(struct receiver_node *rn)
168 {
169 struct private_http_recv_data *phd;
170 struct hostent *he;
171 struct http_recv_args_info *conf = rn->conf;
172 struct sockaddr_in their_addr;
173 int ret;
174
175 rn->buf = para_calloc(BUFSIZE);
176 rn->private_data = para_calloc(sizeof(struct private_http_recv_data));
177 phd = rn->private_data;
178 ret = -E_HOST_INFO;
179 if (!(he = get_host_info(conf->host_arg)))
180 goto err_out;
181 /* get new socket */
182 ret = -E_SOCKET;
183 if ((phd->fd = get_socket()) < 0)
184 goto err_out;
185 /* init their_addr */
186 init_sockaddr(&their_addr, conf->port_arg, he);
187 /* connect */
188 PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg,
189 conf->port_arg);
190 ret = para_connect(phd->fd, &their_addr);
191 if (ret < 0)
192 goto err_out;
193 phd->status = HTTP_CONNECTED;
194 return 1;
195 err_out:
196 free(rn->private_data);
197 free(rn->buf);
198 return ret;
199 }
200
201 /**
202 * the init function of the http receiver
203 *
204 * \param r pointer to the receiver struct to initialize
205 *
206 * Just initialize all function pointers of \a r.
207 */
208 void http_recv_init(struct receiver *r)
209 {
210 r->open = http_recv_open;
211 r->close = http_recv_close;
212 r->pre_select = http_pre_select;
213 r->post_select = http_post_select;
214 r->shutdown = http_shutdown;
215 r->parse_config = http_recv_parse_config;
216 }