]> git.tuebingen.mpg.de Git - paraslash.git/blob - http_recv.c
audiod: fix nasty thinko
[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 "list.h"
25 #include "sched.h"
26 #include "recv.h"
27 #include "http_recv.cmdline.h"
28 #include "error.h"
29 #include "net.h"
30 #include "string.h"
31 #include "fd.h"
32
33 /** the output buffer size of the http receiver */
34 #define BUFSIZE (32 * 1024)
35
36 /**
37  * the possible states of a http receiver node
38  *
39  * \sa receiver_node
40  */
41 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
42
43 /**
44  * data specific to the http receiver
45  *
46  * Each running instance of the http receiver reserves space for one such struct.
47  */
48 struct private_http_recv_data {
49 /**
50  *
51  *
52  * the current status of the http receiver node
53  *
54  * It gets initialized to #HTTP_CONNECTED by the open function of the
55  * http receiver.
56  *
57  * \sa receiver::open, receiver_node.
58  */
59         enum http_recv_status status;
60 /**
61  *
62  *
63  * the file descriptor used for receiving the http stream
64  *
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
68  * para_recv).
69  *
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 #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.
75  *
76  * \sa receiver::pre_select receiver::post_select receiver_node
77  */
78         int fd;
79 };
80
81 static void http_shutdown(void)
82 {
83         return;
84 }
85
86 static char *make_request_msg(void)
87 {
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, VERSION);
91         free(hn);
92         return ret;
93 }
94
95 static void http_recv_pre_select(struct sched *s, struct task *t)
96 {
97         struct receiver_node *rn = t->private_data;
98         struct private_http_recv_data *phd = rn->private_data;
99
100         t->ret = 1;
101         if  (phd->status == HTTP_CONNECTED)
102                 para_fd_set(phd->fd, &s->wfds, &s->max_fileno);
103         else
104                 para_fd_set(phd->fd, &s->rfds, &s->max_fileno);
105 }
106
107
108 static void http_recv_post_select(struct sched *s, struct task *t)
109 {
110         struct receiver_node *rn = t->private_data;
111         struct private_http_recv_data *phd = rn->private_data;
112
113         t->ret = 1;
114         if (!s->select_ret) /* we're not interested in timeouts */
115                 return;
116         if  (phd->status == HTTP_CONNECTED) {
117                 char *rq;
118                 if (!FD_ISSET(phd->fd, &s->wfds))
119                         return; /* nothing to do */
120                 rq = make_request_msg();
121                 PARA_NOTICE_LOG("%s", "sending http request\n");
122                 t->ret = send_va_buffer(phd->fd, "%s", rq);
123                 free(rq);
124                 if (t->ret > 0)
125                         phd->status = HTTP_SENT_GET_REQUEST;
126                 return;
127         }
128         if (!FD_ISSET(phd->fd, &s->rfds))
129                 return; /* nothing to do */
130         if (phd->status == HTTP_SENT_GET_REQUEST) {
131                 t->ret = recv_pattern(phd->fd, HTTP_OK_MSG, MAXLINE);
132                 if (t->ret < 0)
133                         return;
134                 PARA_NOTICE_LOG("%s", "received ok msg, streaming\n");
135                 phd->status = HTTP_STREAMING;
136                 return;
137         }
138         t->ret = -E_OVERRUN;
139         /* already streaming */
140         if (rn->loaded >= BUFSIZE)
141                 return;
142         t->ret = recv_bin_buffer(phd->fd, rn->buf + rn->loaded,
143                 BUFSIZE - rn->loaded);
144         if (t->ret <= 0) {
145                 rn->eof = 1;
146                 if (!t->ret)
147                         t->ret = -E_HTTP_RECV_EOF;
148                 return;
149         }
150         rn->loaded += t->ret;
151 }
152
153 static void http_recv_close(struct receiver_node *rn)
154 {
155         struct private_http_recv_data *phd = rn->private_data;
156         close(phd->fd);
157         free(rn->buf);
158         free(rn->private_data);
159 }
160
161 static void *http_recv_parse_config(int argc, char **argv)
162 {
163         struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
164
165         if (!http_recv_cmdline_parser(argc, argv, tmp))
166                 return tmp;
167         free(tmp);
168         return NULL;
169 }
170
171 static int http_recv_open(struct receiver_node *rn)
172 {
173         struct private_http_recv_data *phd;
174         struct hostent *he;
175         struct http_recv_args_info *conf = rn->conf;
176         struct sockaddr_in their_addr;
177         int ret;
178
179         rn->buf = para_calloc(BUFSIZE);
180         rn->private_data = para_calloc(sizeof(struct private_http_recv_data));
181         phd = rn->private_data;
182         PARA_NOTICE_LOG("phd = %p, rn = %p\n", phd, rn);
183         ret = -E_HOST_INFO;
184         if (!(he = get_host_info(conf->host_arg)))
185                 goto err_out;
186         /* get new socket */
187         ret = -E_SOCKET;
188         if ((phd->fd = get_socket()) < 0)
189                 goto err_out;
190         /* init their_addr */
191         init_sockaddr(&their_addr, conf->port_arg, he);
192         /* connect */
193         PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg,
194                 conf->port_arg);
195         ret = para_connect(phd->fd, &their_addr);
196         if (ret < 0)
197                 goto err_out;
198         phd->status = HTTP_CONNECTED;
199         return 1;
200 err_out:
201         free(rn->private_data);
202         free(rn->buf);
203         return ret;
204 }
205
206 /**
207  * the init function of the http receiver
208  *
209  * \param r pointer to the receiver struct to initialize
210  *
211  * Just initialize all function pointers of \a r.
212  */
213 void http_recv_init(struct receiver *r)
214 {
215         r->open = http_recv_open;
216         r->close = http_recv_close;
217         r->pre_select = http_recv_pre_select;
218         r->post_select = http_recv_post_select;
219         r->shutdown = http_shutdown;
220         r->parse_config = http_recv_parse_config;
221 }