]> git.tuebingen.mpg.de Git - paraslash.git/blob - http_recv.c
a8c982e66fbe069f39141be3124a4ccd2fd60808
[paraslash.git] / http_recv.c
1 /*
2  * Copyright (C) 2005-2009 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 <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12
13 #include "para.h"
14 #include "error.h"
15 #include "http.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "ggo.h"
19 #include "recv.h"
20 #include "http_recv.cmdline.h"
21 #include "net.h"
22 #include "string.h"
23 #include "fd.h"
24 #include "buffer_tree.h"
25
26 /** the output buffer size of the http receiver */
27 #define BUFSIZE (32 * 1024)
28
29 /**
30  * the possible states of a http receiver node
31  *
32  * \sa receiver_node
33  */
34 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
35
36 /**
37  * Data specific to the http receiver.
38  *
39  * Each running instance of the http receiver reserves space for one such struct.
40  */
41 struct private_http_recv_data {
42         /**
43          * The current status of the http receiver node.
44          *
45          * It gets initialized to \p HTTP_CONNECTED by the open function of the
46          * http receiver.
47          *
48          * \sa receiver::open, receiver_node.
49          */
50         enum http_recv_status status;
51         /**
52          * The file descriptor used for receiving the http stream.
53          *
54          * The pre_select function of the http receiver adds this file descriptor to
55          * the set of file decriptors which are checked for reading/writing (depending
56          * on the current status) by the select loop of the application (para_audiod or
57          * para_recv).
58          *
59          * The post_select function of the http receiver uses \a fd, if ready, to
60          * establish the http connection, and updates \a status according to the new
61          * state of the connection.  As soon as \a status is \p HTTP_STREAMING, \a fd is
62          * going to be only checked for reading. If data is available, it is read into
63          * the output buffer of the receiver node by post_select.
64          *
65          * \sa receiver::pre_select receiver::post_select receiver_node, http_recv_status
66          */
67         int fd;
68         struct btr_pool *btrp;
69 };
70
71 static char *make_request_msg(void)
72 {
73         char *ret, *hn = para_hostname();
74         ret = make_message("%s1.0\nHost: %s\nUser-Agent: para_recv/%s\n\n\n",
75                 HTTP_GET_MSG, hn, PACKAGE_VERSION);
76         free(hn);
77         return ret;
78 }
79
80 static void http_recv_pre_select(struct sched *s, struct task *t)
81 {
82         struct receiver_node *rn = container_of(t, struct receiver_node, task);
83         struct private_http_recv_data *phd = rn->private_data;
84         int ret;
85
86         if (rn->btrn) {
87                 ret = generic_recv_pre_select(s, t);
88                 if (ret <= 0)
89                         return;
90         }
91         t->error = 0;
92         if  (phd->status == HTTP_CONNECTED)
93                 para_fd_set(phd->fd, &s->wfds, &s->max_fileno);
94         else
95                 para_fd_set(phd->fd, &s->rfds, &s->max_fileno);
96 }
97
98 static void http_recv_post_select(struct sched *s, struct task *t)
99 {
100         struct receiver_node *rn = container_of(t, struct receiver_node, task);
101         struct private_http_recv_data *phd = rn->private_data;
102         struct btr_node *btrn = rn->btrn;
103         int ret;
104
105         t->error = 0;
106         if (btrn) {
107                 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
108                 if (ret < 0)
109                         goto err;
110                 if (ret == 0)
111                         return;
112         } else {
113                 if (rn->output_error && *rn->output_error < 0) {
114                         ret = *rn->output_error;
115                         goto err;
116                 }
117         }
118         if (phd->status == HTTP_CONNECTED) {
119                 char *rq;
120                 if (!FD_ISSET(phd->fd, &s->wfds))
121                         return;
122                 rq = make_request_msg();
123                 PARA_INFO_LOG("sending http request\n");
124                 ret = send_va_buffer(phd->fd, "%s", rq);
125                 free(rq);
126                 if (ret < 0)
127                         goto err;
128                 phd->status = HTTP_SENT_GET_REQUEST;
129                 return;
130         }
131         if (!FD_ISSET(phd->fd, &s->rfds))
132                 return;
133         if (phd->status == HTTP_SENT_GET_REQUEST) {
134                 ret = recv_pattern(phd->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG));
135                 if (ret < 0)
136                         goto err;
137                 PARA_INFO_LOG("received ok msg, streaming\n");
138                 phd->status = HTTP_STREAMING;
139                 return;
140         }
141         if (btrn) {
142                 char *buf;
143                 size_t sz;
144
145                 sz = btr_pool_get_buffer(phd->btrp, &buf);
146                 //PARA_CRIT_LOG("max buffer %p: %zu\n", buf, sz);
147                 ret = -E_HTTP_RECV_OVERRUN;
148                 if (sz == 0)
149                         goto err;
150                 //buf = para_malloc(HTTP_RECV_READ_BUF_SIZE);
151                 //sz = HTTP_RECV_READ_BUF_SIZE;
152                 ret = recv_bin_buffer(phd->fd, buf, sz);
153                 if (ret == 0)
154                         ret = -E_RECV_EOF;
155                 if (ret < 0)
156                         goto err;
157                 btr_add_output_pool(phd->btrp, ret, btrn);
158                 return;
159         }
160         ret = -E_HTTP_RECV_OVERRUN;
161         if (rn->loaded >= BUFSIZE)
162                 goto err;
163         ret = recv_bin_buffer(phd->fd, rn->buf + rn->loaded,
164                 BUFSIZE - rn->loaded);
165         if (ret == 0)
166                 ret = -E_RECV_EOF;
167         if (ret < 0)
168                 goto err;
169         rn->loaded += ret;
170         return;
171 err:
172         if (btrn)
173                 btr_remove_node(rn->btrn);
174         t->error = ret;
175 }
176
177 static void http_recv_close(struct receiver_node *rn)
178 {
179         struct private_http_recv_data *phd = rn->private_data;
180
181         close(phd->fd);
182         btr_pool_free(phd->btrp);
183         free(rn->buf);
184         free(rn->private_data);
185 }
186
187 static void *http_recv_parse_config(int argc, char **argv)
188 {
189         struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
190
191         if (!http_recv_cmdline_parser(argc, argv, tmp))
192                 return tmp;
193         free(tmp);
194         return NULL;
195 }
196
197 static int http_recv_open(struct receiver_node *rn)
198 {
199         struct private_http_recv_data *phd;
200         struct http_recv_args_info *conf = rn->conf;
201         int fd, ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, conf->host_arg,
202                 conf->port_arg);
203
204         if (ret < 0)
205                 return ret;
206         fd = ret;
207         ret = mark_fd_nonblocking(fd);
208         if (ret < 0) {
209                 close(fd);
210                 return ret;
211         }
212         rn->buf = para_calloc(BUFSIZE);
213         rn->private_data = phd = para_calloc(sizeof(struct private_http_recv_data));
214         phd->fd = fd;
215         phd->status = HTTP_CONNECTED;
216         phd->btrp = btr_pool_new("http_recv", 320 * 1024);
217         return 1;
218 }
219
220 static void http_recv_free_config(void *conf)
221 {
222         http_recv_cmdline_parser_free(conf);
223 }
224
225 /**
226  * The init function of the http receiver.
227  *
228  * \param r Pointer to the receiver struct to initialize.
229  *
230  * This initializes all function pointers of \a r.
231  */
232 void http_recv_init(struct receiver *r)
233 {
234         struct http_recv_args_info dummy;
235
236         http_recv_cmdline_parser_init(&dummy);
237         r->open = http_recv_open;
238         r->close = http_recv_close;
239         r->pre_select = http_recv_pre_select;
240         r->post_select = http_recv_post_select;
241         r->parse_config = http_recv_parse_config;
242         r->free_config = http_recv_free_config;
243         r->help = (struct ggo_help) {
244                 .short_help = http_recv_args_info_help,
245                 .detailed_help = http_recv_args_info_detailed_help
246         };
247         http_recv_cmdline_parser_free(&dummy);
248 }