stdin: Use buffer pools.
[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 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 = container_of(t, struct receiver_node, task);
88         struct private_http_recv_data *phd = rn->private_data;
89         int ret;
90
91         if (rn->btrn) {
92                 ret = generic_recv_pre_select(s, t);
93                 if (ret <= 0)
94                         return;
95         }
96         t->error = 0;
97         if  (phd->status == HTTP_CONNECTED)
98                 para_fd_set(phd->fd, &s->wfds, &s->max_fileno);
99         else
100                 para_fd_set(phd->fd, &s->rfds, &s->max_fileno);
101 }
102
103 static void http_recv_post_select(struct sched *s, struct task *t)
104 {
105         struct receiver_node *rn = container_of(t, struct receiver_node, task);
106         struct private_http_recv_data *phd = rn->private_data;
107         struct btr_node *btrn = rn->btrn;
108         int ret;
109
110         t->error = 0;
111         if (btrn) {
112                 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
113                 if (ret < 0)
114                         goto err;
115                 if (ret == 0)
116                         return;
117         } else {
118                 if (rn->output_error && *rn->output_error < 0) {
119                         ret = *rn->output_error;
120                         goto err;
121                 }
122         }
123         if (phd->status == HTTP_CONNECTED) {
124                 char *rq;
125                 if (!FD_ISSET(phd->fd, &s->wfds))
126                         return;
127                 rq = make_request_msg();
128                 PARA_INFO_LOG("sending http request\n");
129                 ret = send_va_buffer(phd->fd, "%s", rq);
130                 free(rq);
131                 if (ret < 0)
132                         goto err;
133                 phd->status = HTTP_SENT_GET_REQUEST;
134                 return;
135         }
136         if (!FD_ISSET(phd->fd, &s->rfds))
137                 return;
138         if (phd->status == HTTP_SENT_GET_REQUEST) {
139                 ret = recv_pattern(phd->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG));
140                 if (ret < 0)
141                         goto err;
142                 PARA_INFO_LOG("received ok msg, streaming\n");
143                 phd->status = HTTP_STREAMING;
144                 return;
145         }
146         if (btrn) {
147                 char *buf;
148                 size_t sz;
149
150                 sz = btr_pool_get_buffer(phd->btrp, &buf);
151                 //PARA_CRIT_LOG("max buffer %p: %zu\n", buf, sz);
152                 ret = -E_HTTP_RECV_OVERRUN;
153                 if (sz == 0)
154                         goto err;
155                 //buf = para_malloc(HTTP_RECV_READ_BUF_SIZE);
156                 //sz = HTTP_RECV_READ_BUF_SIZE;
157                 ret = recv_bin_buffer(phd->fd, buf, sz);
158                 if (ret == 0)
159                         ret = -E_RECV_EOF;
160                 if (ret < 0)
161                         goto err;
162                 btr_add_output_pool(phd->btrp, ret, btrn);
163                 return;
164         }
165         ret = -E_HTTP_RECV_OVERRUN;
166         if (rn->loaded >= BUFSIZE)
167                 goto err;
168         ret = recv_bin_buffer(phd->fd, rn->buf + rn->loaded,
169                 BUFSIZE - rn->loaded);
170         if (ret == 0)
171                 ret = -E_RECV_EOF;
172         if (ret < 0)
173                 goto err;
174         rn->loaded += ret;
175         return;
176 err:
177         if (btrn)
178                 btr_remove_node(rn->btrn);
179         t->error = ret;
180 }
181
182 static void http_recv_close(struct receiver_node *rn)
183 {
184         struct private_http_recv_data *phd = rn->private_data;
185
186         close(phd->fd);
187         btr_pool_free(phd->btrp);
188         free(rn->buf);
189         free(rn->private_data);
190 }
191
192 static void *http_recv_parse_config(int argc, char **argv)
193 {
194         struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
195
196         if (!http_recv_cmdline_parser(argc, argv, tmp))
197                 return tmp;
198         free(tmp);
199         return NULL;
200 }
201
202 static int http_recv_open(struct receiver_node *rn)
203 {
204         struct private_http_recv_data *phd;
205         struct http_recv_args_info *conf = rn->conf;
206         int fd, ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, conf->host_arg,
207                 conf->port_arg);
208
209         if (ret < 0)
210                 return ret;
211         fd = ret;
212         ret = mark_fd_nonblocking(fd);
213         if (ret < 0) {
214                 close(fd);
215                 return ret;
216         }
217         rn->buf = para_calloc(BUFSIZE);
218         rn->private_data = phd = para_calloc(sizeof(struct private_http_recv_data));
219         phd->fd = fd;
220         phd->status = HTTP_CONNECTED;
221         phd->btrp = btr_pool_new("http_recv", 320 * 1024);
222         return 1;
223 }
224
225 static void http_recv_free_config(void *conf)
226 {
227         http_recv_cmdline_parser_free(conf);
228 }
229
230 /**
231  * The init function of the http receiver.
232  *
233  * \param r Pointer to the receiver struct to initialize.
234  *
235  * This initializes all function pointers of \a r.
236  */
237 void http_recv_init(struct receiver *r)
238 {
239         struct http_recv_args_info dummy;
240
241         http_recv_cmdline_parser_init(&dummy);
242         r->open = http_recv_open;
243         r->close = http_recv_close;
244         r->pre_select = http_recv_pre_select;
245         r->post_select = http_recv_post_select;
246         r->shutdown = http_shutdown;
247         r->parse_config = http_recv_parse_config;
248         r->free_config = http_recv_free_config;
249         r->help = (struct ggo_help) {
250                 .short_help = http_recv_args_info_help,
251                 .detailed_help = http_recv_args_info_detailed_help
252         };
253         http_recv_cmdline_parser_free(&dummy);
254 }