2 * Copyright (C) 2005-2010 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"
27 * the possible states of a http receiver node
31 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
34 * Data specific to the http receiver.
36 * Each running instance of the http receiver reserves space for one such struct.
38 struct private_http_recv_data {
40 * The current status of the http receiver node.
42 * It gets initialized to \p HTTP_CONNECTED by the open function of the
45 * \sa receiver::open, receiver_node.
47 enum http_recv_status status;
49 * The file descriptor used for receiving the http stream.
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
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.
62 * \sa receiver::pre_select receiver::post_select receiver_node, http_recv_status
65 struct btr_pool *btrp;
68 static char *make_request_msg(void)
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);
77 static void http_recv_pre_select(struct sched *s, struct task *t)
79 struct receiver_node *rn = container_of(t, struct receiver_node, task);
80 struct private_http_recv_data *phd = rn->private_data;
83 if (generic_recv_pre_select(s, t) <= 0)
85 if (phd->status == HTTP_CONNECTED)
86 para_fd_set(phd->fd, &s->wfds, &s->max_fileno);
88 para_fd_set(phd->fd, &s->rfds, &s->max_fileno);
91 static void http_recv_post_select(struct sched *s, struct task *t)
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;
101 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
106 if (phd->status == HTTP_CONNECTED) {
108 if (!FD_ISSET(phd->fd, &s->wfds))
110 rq = make_request_msg();
111 PARA_INFO_LOG("sending http request\n");
112 ret = send_va_buffer(phd->fd, "%s", rq);
116 phd->status = HTTP_SENT_GET_REQUEST;
119 if (phd->status == HTTP_SENT_GET_REQUEST) {
120 ret = read_pattern(phd->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG), &s->rfds);
125 PARA_INFO_LOG("received ok msg, streaming\n");
126 phd->status = HTTP_STREAMING;
129 ret = -E_HTTP_RECV_OVERRUN;
130 sz = btr_pool_get_buffer(phd->btrp, &buf);
133 ret = read_nonblock(phd->fd, buf, sz, &s->rfds, &n);
135 btr_add_output_pool(phd->btrp, n, btrn);
139 btr_remove_node(rn->btrn);
143 static void http_recv_close(struct receiver_node *rn)
145 struct private_http_recv_data *phd = rn->private_data;
148 btr_pool_free(phd->btrp);
149 free(rn->private_data);
152 static void *http_recv_parse_config(int argc, char **argv)
154 struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
156 if (!http_recv_cmdline_parser(argc, argv, tmp))
162 static int http_recv_open(struct receiver_node *rn)
164 struct private_http_recv_data *phd;
165 struct http_recv_args_info *conf = rn->conf;
166 int fd, ret = para_connect_simple(IPPROTO_TCP, conf->host_arg,
172 ret = mark_fd_nonblocking(fd);
177 rn->private_data = phd = para_calloc(sizeof(struct private_http_recv_data));
179 phd->status = HTTP_CONNECTED;
180 phd->btrp = btr_pool_new("http_recv", 320 * 1024);
184 static void http_recv_free_config(void *conf)
186 http_recv_cmdline_parser_free(conf);
191 * The init function of the http receiver.
193 * \param r Pointer to the receiver struct to initialize.
195 * This initializes all function pointers of \a r.
197 void http_recv_init(struct receiver *r)
199 struct http_recv_args_info dummy;
201 http_recv_cmdline_parser_init(&dummy);
202 r->open = http_recv_open;
203 r->close = http_recv_close;
204 r->pre_select = http_recv_pre_select;
205 r->post_select = http_recv_post_select;
206 r->parse_config = http_recv_parse_config;
207 r->free_config = http_recv_free_config;
208 r->help = (struct ggo_help) {
209 .short_help = http_recv_args_info_help,
210 .detailed_help = http_recv_args_info_detailed_help
212 http_recv_cmdline_parser_free(&dummy);