[btr] Add the node type parameter to btr_node_status().
[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 *
44 *
45 * the current status of the http receiver node
46 *
47 * It gets initialized to \p HTTP_CONNECTED by the open function of the
48 * http receiver.
49 *
50 * \sa receiver::open, receiver_node.
51 */
52 enum http_recv_status status;
53 /**
54 *
55 *
56 * the file descriptor used for receiving the http stream
57 *
58 * The pre_select function of the http receiver adds this file descriptor to
59 * the set of file decriptors which are checked for reading/writing (depending
60 * on the current status) by the select loop of the application (para_audiod or
61 * para_recv).
62 *
63 * The post_select function of the http receiver uses \a fd, if ready, to
64 * establish the http connection, and updates \a status according to the new
65 * state of the connection. As soon as \a status is \p HTTP_STREAMING, \a fd is
66 * going to be only checked for reading. If data is available, it is read into
67 * the output buffer of the receiver node by post_select.
68 *
69 * \sa receiver::pre_select receiver::post_select receiver_node, http_recv_status
70 */
71 int fd;
72 };
73
74 static void http_shutdown(void)
75 {
76 return;
77 }
78
79 static char *make_request_msg(void)
80 {
81 char *ret, *hn = para_hostname();
82 ret = make_message("%s1.0\nHost: %s\nUser-Agent: para_recv/%s\n\n\n",
83 HTTP_GET_MSG, hn, PACKAGE_VERSION);
84 free(hn);
85 return ret;
86 }
87
88 static void http_recv_pre_select(struct sched *s, struct task *t)
89 {
90 struct receiver_node *rn = container_of(t, struct receiver_node, task);
91 struct private_http_recv_data *phd = rn->private_data;
92
93 t->error = 0;
94 if (phd->status == HTTP_CONNECTED)
95 para_fd_set(phd->fd, &s->wfds, &s->max_fileno);
96 else
97 para_fd_set(phd->fd, &s->rfds, &s->max_fileno);
98 }
99
100 #define HTTP_RECV_READ_BUF_SIZE 4096
101
102 static void http_recv_post_select(struct sched *s, struct task *t)
103 {
104 struct receiver_node *rn = container_of(t, struct receiver_node, task);
105 struct private_http_recv_data *phd = rn->private_data;
106 struct btr_node *btrn = rn->btrn;
107 int ret;
108
109 t->error = 0;
110 if (btrn) {
111 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
112 if (ret < 0)
113 goto err;
114 if (ret == 0)
115 return;
116 } else {
117 if (rn->output_error && *rn->output_error < 0) {
118 ret = *rn->output_error;
119 goto err;
120 }
121 }
122 if (phd->status == HTTP_CONNECTED) {
123 char *rq;
124 if (!FD_ISSET(phd->fd, &s->wfds))
125 return;
126 rq = make_request_msg();
127 PARA_INFO_LOG("sending http request\n");
128 ret = send_va_buffer(phd->fd, "%s", rq);
129 free(rq);
130 if (ret < 0)
131 goto err;
132 phd->status = HTTP_SENT_GET_REQUEST;
133 return;
134 }
135 if (!FD_ISSET(phd->fd, &s->rfds))
136 return;
137 if (phd->status == HTTP_SENT_GET_REQUEST) {
138 ret = recv_pattern(phd->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG));
139 if (ret < 0)
140 goto err;
141 PARA_INFO_LOG("received ok msg, streaming\n");
142 phd->status = HTTP_STREAMING;
143 return;
144 }
145 if (btrn) {
146 char *buf;
147
148 buf = para_malloc(HTTP_RECV_READ_BUF_SIZE);
149 ret = recv_bin_buffer(phd->fd, buf, HTTP_RECV_READ_BUF_SIZE);
150 if (ret == 0)
151 ret = -E_RECV_EOF;
152 if (ret < 0) {
153 free(buf);
154 goto err;
155 }
156 btr_add_output(buf, ret, btrn);
157 return;
158 }
159 ret = -E_HTTP_RECV_OVERRUN;
160 if (rn->loaded >= BUFSIZE)
161 goto err;
162 ret = recv_bin_buffer(phd->fd, rn->buf + rn->loaded,
163 BUFSIZE - rn->loaded);
164 if (ret == 0)
165 ret = -E_RECV_EOF;
166 if (ret < 0)
167 goto err;
168 rn->loaded += ret;
169 return;
170 err:
171 if (btrn)
172 btr_remove_node(rn->btrn);
173 t->error = ret;
174 }
175
176 static void http_recv_close(struct receiver_node *rn)
177 {
178 struct private_http_recv_data *phd = rn->private_data;
179
180 close(phd->fd);
181 free(rn->buf);
182 free(rn->private_data);
183 }
184
185 static void *http_recv_parse_config(int argc, char **argv)
186 {
187 struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
188
189 if (!http_recv_cmdline_parser(argc, argv, tmp))
190 return tmp;
191 free(tmp);
192 return NULL;
193 }
194
195 static int http_recv_open(struct receiver_node *rn)
196 {
197 struct private_http_recv_data *phd;
198 struct http_recv_args_info *conf = rn->conf;
199 int fd, ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, conf->host_arg,
200 conf->port_arg);
201
202 if (ret < 0)
203 return ret;
204 fd = ret;
205 ret = mark_fd_nonblocking(fd);
206 if (ret < 0) {
207 close(fd);
208 return ret;
209 }
210 rn->buf = para_calloc(BUFSIZE);
211 rn->private_data = phd = para_calloc(sizeof(struct private_http_recv_data));
212 phd->fd = fd;
213 phd->status = HTTP_CONNECTED;
214 if (conf->buffer_tree_given)
215 rn->btrn = btr_new_node("receiver", NULL, NULL, NULL);
216 return 1;
217 }
218
219 /**
220 * the init function of the http receiver
221 *
222 * \param r pointer to the receiver struct to initialize
223 *
224 * Just initialize all function pointers of \a r.
225 */
226 void http_recv_init(struct receiver *r)
227 {
228 struct http_recv_args_info dummy;
229
230 http_recv_cmdline_parser_init(&dummy);
231 r->open = http_recv_open;
232 r->close = http_recv_close;
233 r->pre_select = http_recv_pre_select;
234 r->post_select = http_recv_post_select;
235 r->shutdown = http_shutdown;
236 r->parse_config = http_recv_parse_config;
237 r->help = (struct ggo_help) {
238 .short_help = http_recv_args_info_help,
239 .detailed_help = http_recv_args_info_detailed_help
240 };
241 http_recv_cmdline_parser_free(&dummy);
242 }