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