]> git.tuebingen.mpg.de Git - paraslash.git/blob - http_recv.c
REQUIREMENTS: Add pointers to software packages.
[paraslash.git] / http_recv.c
1 /*
2  * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file http_recv.c paraslash's http receiver */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "http.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "recv.h"
18 #include "http_recv.cmdline.h"
19 #include "net.h"
20 #include "string.h"
21 #include "fd.h"
22
23 /** the output buffer size of the http receiver */
24 #define BUFSIZE (32 * 1024)
25
26 /**
27  * the possible states of a http receiver node
28  *
29  * \sa receiver_node
30  */
31 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
32
33 /**
34  * data specific to the http receiver
35  *
36  * Each running instance of the http receiver reserves space for one such struct.
37  */
38 struct private_http_recv_data {
39 /**
40  *
41  *
42  * the current status of the http receiver node
43  *
44  * It gets initialized to \p HTTP_CONNECTED by the open function of the
45  * http receiver.
46  *
47  * \sa receiver::open, receiver_node.
48  */
49         enum http_recv_status status;
50 /**
51  *
52  *
53  * the file descriptor used for receiving the http stream
54  *
55  * The pre_select function of the http receiver adds this file descriptor to
56  * the set of file decriptors which are checked for reading/writing (depending
57  * on the current status) by the select loop of the application (para_audiod or
58  * para_recv).
59  *
60  * The post_select function of the http receiver uses \a fd, if ready, to
61  * establish the http connection, and updates \a status according to the new
62  * state of the connection.  As soon as \a status is \p HTTP_STREAMING, \a fd is
63  * going to be only checked for reading. If data is available, it is read into
64  * the output buffer of the receiver node by post_select.
65  *
66  * \sa receiver::pre_select receiver::post_select receiver_node, http_recv_status
67  */
68         int fd;
69 };
70
71 static void http_shutdown(void)
72 {
73         return;
74 }
75
76 static char *make_request_msg(void)
77 {
78         char *ret, *hn = para_hostname();
79         ret = make_message("%s1.0\nHost: %s\nUser-Agent: para_recv/%s\n\n\n",
80                 HTTP_GET_MSG, hn, PACKAGE_VERSION);
81         free(hn);
82         return ret;
83 }
84
85 static void http_recv_pre_select(struct sched *s, struct task *t)
86 {
87         struct receiver_node *rn = t->private_data;
88         struct private_http_recv_data *phd = rn->private_data;
89
90         t->ret = 1;
91         if  (phd->status == HTTP_CONNECTED)
92                 para_fd_set(phd->fd, &s->wfds, &s->max_fileno);
93         else
94                 para_fd_set(phd->fd, &s->rfds, &s->max_fileno);
95 }
96
97
98 static void http_recv_post_select(struct sched *s, struct task *t)
99 {
100         struct receiver_node *rn = t->private_data;
101         struct private_http_recv_data *phd = rn->private_data;
102
103         t->ret = -E_HTTP_RECV_EOF;
104         if (rn->output_eof && *rn->output_eof)
105                 goto out;
106         t->ret = 1;
107         if (!s->select_ret)
108                 goto out;
109         if  (phd->status == HTTP_CONNECTED) {
110                 char *rq;
111                 if (!FD_ISSET(phd->fd, &s->wfds))
112                         goto out;
113                 rq = make_request_msg();
114                 PARA_INFO_LOG("%s", "sending http request\n");
115                 t->ret = send_va_buffer(phd->fd, "%s", rq);
116                 free(rq);
117                 if (t->ret > 0)
118                         phd->status = HTTP_SENT_GET_REQUEST;
119                 goto out;
120         }
121         if (!FD_ISSET(phd->fd, &s->rfds))
122                 goto out;
123         if (phd->status == HTTP_SENT_GET_REQUEST) {
124                 t->ret = recv_pattern(phd->fd, HTTP_OK_MSG, MAXLINE);
125                 if (t->ret < 0)
126                         goto out;
127                 PARA_INFO_LOG("%s", "received ok msg, streaming\n");
128                 t->ret = 1;
129                 phd->status = HTTP_STREAMING;
130                 goto out;
131         }
132         t->ret = -E_HTTP_RECV_OVERRUN;
133         if (rn->loaded >= BUFSIZE)
134                 goto out;
135         t->ret = recv_bin_buffer(phd->fd, rn->buf + rn->loaded,
136                 BUFSIZE - rn->loaded);
137         if (t->ret <= 0) {
138                 if (!t->ret)
139                         t->ret = -E_HTTP_RECV_EOF;
140                 goto out;
141         }
142         rn->loaded += t->ret;
143 out:
144         if (t->ret < 0)
145                 rn->eof = 1;
146 }
147
148 static void http_recv_close(struct receiver_node *rn)
149 {
150         struct private_http_recv_data *phd = rn->private_data;
151         close(phd->fd);
152         free(rn->buf);
153         free(rn->private_data);
154 }
155
156 static void *http_recv_parse_config(int argc, char **argv)
157 {
158         struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
159
160         if (!http_recv_cmdline_parser(argc, argv, tmp))
161                 return tmp;
162         free(tmp);
163         return NULL;
164 }
165
166 static int http_recv_open(struct receiver_node *rn)
167 {
168         struct private_http_recv_data *phd;
169         struct hostent *he;
170         struct http_recv_args_info *conf = rn->conf;
171         struct sockaddr_in their_addr;
172         int ret;
173
174         rn->buf = para_calloc(BUFSIZE);
175         rn->private_data = para_calloc(sizeof(struct private_http_recv_data));
176         phd = rn->private_data;
177         ret = get_host_info(conf->host_arg, &he);
178         if (ret < 0)
179                 goto err_out;
180         ret = get_stream_socket(AF_INET);
181         if (ret < 0)
182                 goto err_out;
183         phd->fd = ret;
184         /* init their_addr */
185         init_sockaddr(&their_addr, conf->port_arg, he);
186         PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg,
187                 conf->port_arg);
188         ret = PARA_CONNECT(phd->fd, &their_addr);
189         if (ret < 0) {
190                 close(phd->fd);
191                 goto err_out;
192         }
193         mark_fd_nonblock(phd->fd);
194         phd->status = HTTP_CONNECTED;
195         return 1;
196 err_out:
197         free(rn->private_data);
198         free(rn->buf);
199         return ret;
200 }
201
202 /**
203  * the init function of the http receiver
204  *
205  * \param r pointer to the receiver struct to initialize
206  *
207  * Just initialize all function pointers of \a r.
208  */
209 void http_recv_init(struct receiver *r)
210 {
211         r->open = http_recv_open;
212         r->close = http_recv_close;
213         r->pre_select = http_recv_pre_select;
214         r->post_select = http_recv_post_select;
215         r->shutdown = http_shutdown;
216         r->parse_config = http_recv_parse_config;
217 }