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