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 {
45 * the current status of the http receiver node
47 * It gets initialized to \p HTTP_CONNECTED by the open function of the
50 * \sa receiver::open, receiver_node.
52 enum http_recv_status status;
56 * the file descriptor used for receiving the http stream
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
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.
69 * \sa receiver::pre_select receiver::post_select receiver_node, http_recv_status
74 static void http_shutdown(void)
79 static char *make_request_msg(void)
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);
88 static void http_recv_pre_select(struct sched *s, struct task *t)
90 struct receiver_node *rn = container_of(t, struct receiver_node, task);
91 struct private_http_recv_data *phd = rn->private_data;
94 if (phd->status == HTTP_CONNECTED)
95 para_fd_set(phd->fd, &s->wfds, &s->max_fileno);
97 para_fd_set(phd->fd, &s->rfds, &s->max_fileno);
100 #define HTTP_RECV_MAX_PENDING (1024 * 1024)
101 #define HTTP_RECV_READ_BUF_SIZE 4096
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 http_recv_args_info *conf = rn->conf;
109 if (rn->output_error && *rn->output_error < 0) {
110 t->error = *rn->output_error;
113 if (phd->status == HTTP_CONNECTED) {
115 if (!FD_ISSET(phd->fd, &s->wfds))
117 rq = make_request_msg();
118 PARA_INFO_LOG("sending http request\n");
119 t->error = send_va_buffer(phd->fd, "%s", rq);
123 phd->status = HTTP_SENT_GET_REQUEST;
126 if (!FD_ISSET(phd->fd, &s->rfds))
128 if (phd->status == HTTP_SENT_GET_REQUEST) {
129 t->error = recv_pattern(phd->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG));
132 PARA_INFO_LOG("received ok msg, streaming\n");
133 phd->status = HTTP_STREAMING;
136 if (conf->buffer_tree_given) {
139 if (btr_no_children(rn->btrn)) {
140 t->error = -E_HTTP_RECV_NO_CHILD;
143 if (btr_bytes_pending(rn->btrn) > HTTP_RECV_MAX_PENDING) {
144 t->error = -E_HTTP_RECV_OVERRUN;
147 buf = para_malloc(HTTP_RECV_READ_BUF_SIZE);
148 t->error = recv_bin_buffer(phd->fd, buf, HTTP_RECV_READ_BUF_SIZE);
150 t->error = -E_RECV_EOF;
155 btr_add_output(buf, t->error, rn->btrn);
158 t->error = -E_HTTP_RECV_OVERRUN;
159 if (rn->loaded >= BUFSIZE)
161 t->error = recv_bin_buffer(phd->fd, rn->buf + rn->loaded,
162 BUFSIZE - rn->loaded);
164 t->error = -E_RECV_EOF;
167 rn->loaded += t->error;
170 if (conf->buffer_tree_given) {
171 btr_del_node(rn->btrn);
176 static void http_recv_close(struct receiver_node *rn)
178 struct private_http_recv_data *phd = rn->private_data;
179 struct http_recv_args_info *conf = rn->conf;
181 if (conf->buffer_tree_given)
182 btr_del_node(rn->btrn);
185 free(rn->private_data);
188 static void *http_recv_parse_config(int argc, char **argv)
190 struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
192 if (!http_recv_cmdline_parser(argc, argv, tmp))
198 static int http_recv_open(struct receiver_node *rn)
200 struct private_http_recv_data *phd;
201 struct http_recv_args_info *conf = rn->conf;
202 int fd, ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, conf->host_arg,
208 ret = mark_fd_nonblocking(fd);
213 rn->buf = para_calloc(BUFSIZE);
214 rn->private_data = phd = para_calloc(sizeof(struct private_http_recv_data));
216 phd->status = HTTP_CONNECTED;
217 if (conf->buffer_tree_given)
218 rn->btrn = btr_new_node("receiver", NULL, NULL, NULL);
223 * the init function of the http receiver
225 * \param r pointer to the receiver struct to initialize
227 * Just initialize all function pointers of \a r.
229 void http_recv_init(struct receiver *r)
231 struct http_recv_args_info dummy;
233 http_recv_cmdline_parser_init(&dummy);
234 r->open = http_recv_open;
235 r->close = http_recv_close;
236 r->pre_select = http_recv_pre_select;
237 r->post_select = http_recv_post_select;
238 r->shutdown = http_shutdown;
239 r->parse_config = http_recv_parse_config;
240 r->help = (struct ggo_help) {
241 .short_help = http_recv_args_info_help,
242 .detailed_help = http_recv_args_info_detailed_help
244 http_recv_cmdline_parser_free(&dummy);