2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file http_recv.c paraslash's http receiver */
10 #include <sys/types.h>
20 #include "http_recv.cmdline.h"
24 #include "buffer_tree.h"
26 /** the output buffer size of the http receiver */
27 #define BUFSIZE (32 * 1024)
30 * the possible states of a http receiver node
34 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
37 * Data specific to the http receiver.
39 * Each running instance of the http receiver reserves space for one such struct.
41 struct private_http_recv_data {
43 * The current status of the http receiver node.
45 * It gets initialized to \p HTTP_CONNECTED by the open function of the
48 * \sa receiver::open, receiver_node.
50 enum http_recv_status status;
52 * The file descriptor used for receiving the http stream.
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
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.
65 * \sa receiver::pre_select receiver::post_select receiver_node, http_recv_status
68 struct btr_pool *btrp;
71 static void http_shutdown(void)
76 static char *make_request_msg(void)
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);
85 static void http_recv_pre_select(struct sched *s, struct task *t)
87 struct receiver_node *rn = container_of(t, struct receiver_node, task);
88 struct private_http_recv_data *phd = rn->private_data;
92 ret = generic_recv_pre_select(s, t);
97 if (phd->status == HTTP_CONNECTED)
98 para_fd_set(phd->fd, &s->wfds, &s->max_fileno);
100 para_fd_set(phd->fd, &s->rfds, &s->max_fileno);
103 static void http_recv_post_select(struct sched *s, struct task *t)
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;
112 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
118 if (rn->output_error && *rn->output_error < 0) {
119 ret = *rn->output_error;
123 if (phd->status == HTTP_CONNECTED) {
125 if (!FD_ISSET(phd->fd, &s->wfds))
127 rq = make_request_msg();
128 PARA_INFO_LOG("sending http request\n");
129 ret = send_va_buffer(phd->fd, "%s", rq);
133 phd->status = HTTP_SENT_GET_REQUEST;
136 if (!FD_ISSET(phd->fd, &s->rfds))
138 if (phd->status == HTTP_SENT_GET_REQUEST) {
139 ret = recv_pattern(phd->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG));
142 PARA_INFO_LOG("received ok msg, streaming\n");
143 phd->status = HTTP_STREAMING;
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;
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);
162 btr_add_output_pool(phd->btrp, ret, btrn);
165 ret = -E_HTTP_RECV_OVERRUN;
166 if (rn->loaded >= BUFSIZE)
168 ret = recv_bin_buffer(phd->fd, rn->buf + rn->loaded,
169 BUFSIZE - rn->loaded);
178 btr_remove_node(rn->btrn);
182 static void http_recv_close(struct receiver_node *rn)
184 struct private_http_recv_data *phd = rn->private_data;
187 btr_pool_free(phd->btrp);
189 free(rn->private_data);
192 static void *http_recv_parse_config(int argc, char **argv)
194 struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
196 if (!http_recv_cmdline_parser(argc, argv, tmp))
202 static int http_recv_open(struct receiver_node *rn)
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,
212 ret = mark_fd_nonblocking(fd);
217 rn->buf = para_calloc(BUFSIZE);
218 rn->private_data = phd = para_calloc(sizeof(struct private_http_recv_data));
220 phd->status = HTTP_CONNECTED;
221 phd->btrp = btr_pool_new("http_recv", 320 * 1024);
225 static void http_recv_free_config(void *conf)
227 http_recv_cmdline_parser_free(conf);
231 * The init function of the http receiver.
233 * \param r Pointer to the receiver struct to initialize.
235 * This initializes all function pointers of \a r.
237 void http_recv_init(struct receiver *r)
239 struct http_recv_args_info dummy;
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
253 http_recv_cmdline_parser_free(&dummy);