2 * Copyright (C) 2005-2012 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>
19 #include "http_recv.cmdline.h"
23 #include "buffer_tree.h"
26 * the possible states of a http receiver node
30 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
33 * Data specific to the http receiver.
35 * Each running instance of the http receiver reserves space for one such struct.
37 struct private_http_recv_data {
39 * The current status of the http receiver node.
41 * It gets initialized to \p HTTP_CONNECTED by the open function of the
44 * \sa receiver::open, receiver_node.
46 enum http_recv_status status;
49 static char *make_request_msg(void)
51 char *ret, *hn = para_hostname();
52 ret = make_message("%s1.0\nHost: %s\nUser-Agent: para_recv/%s\n\n\n",
53 HTTP_GET_MSG, hn, PACKAGE_VERSION);
58 static void http_recv_pre_select(struct sched *s, struct task *t)
60 struct receiver_node *rn = container_of(t, struct receiver_node, task);
61 struct private_http_recv_data *phd = rn->private_data;
64 if (generic_recv_pre_select(s, t) <= 0)
66 if (phd->status == HTTP_CONNECTED)
67 para_fd_set(rn->fd, &s->wfds, &s->max_fileno);
69 para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
73 * Establish the http connection. If already established, fill the buffer pool
74 * area with data read from the socket. In any case, update the state of the
75 * connection if necessary.
77 static void http_recv_post_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;
81 struct btr_node *btrn = rn->btrn;
87 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
92 if (phd->status == HTTP_CONNECTED) {
94 if (!FD_ISSET(rn->fd, &s->wfds))
96 rq = make_request_msg();
97 PARA_INFO_LOG("sending http request\n");
98 ret = send_va_buffer(rn->fd, "%s", rq);
102 phd->status = HTTP_SENT_GET_REQUEST;
105 if (phd->status == HTTP_SENT_GET_REQUEST) {
106 ret = read_pattern(rn->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG), &s->rfds);
111 PARA_INFO_LOG("received ok msg, streaming\n");
112 phd->status = HTTP_STREAMING;
115 ret = -E_HTTP_RECV_OVERRUN;
116 iovcnt = btr_pool_get_buffers(rn->btrp, iov);
119 ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
122 if (num_bytes <= iov[0].iov_len) /* only the first buffer was filled */
123 btr_add_output_pool(rn->btrp, num_bytes, btrn);
124 else { /* both buffers contain data */
125 btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
126 btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len, btrn);
131 btr_remove_node(rn->btrn);
135 static void http_recv_close(struct receiver_node *rn)
138 btr_pool_free(rn->btrp);
139 free(rn->private_data);
142 static void *http_recv_parse_config(int argc, char **argv)
144 struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
146 if (!http_recv_cmdline_parser(argc, argv, tmp))
152 static int http_recv_open(struct receiver_node *rn)
154 struct private_http_recv_data *phd;
155 struct http_recv_args_info *conf = rn->conf;
156 int fd, ret = para_connect_simple(IPPROTO_TCP, conf->host_arg,
162 ret = mark_fd_nonblocking(fd);
167 rn->private_data = phd = para_calloc(sizeof(struct private_http_recv_data));
169 phd->status = HTTP_CONNECTED;
170 rn->btrp = btr_pool_new("http_recv", 320 * 1024);
174 static void http_recv_free_config(void *conf)
176 http_recv_cmdline_parser_free(conf);
181 * The init function of the http receiver.
183 * \param r Pointer to the receiver struct to initialize.
185 * This initializes all function pointers of \a r.
187 void http_recv_init(struct receiver *r)
189 struct http_recv_args_info dummy;
191 http_recv_cmdline_parser_init(&dummy);
192 r->open = http_recv_open;
193 r->close = http_recv_close;
194 r->pre_select = http_recv_pre_select;
195 r->post_select = http_recv_post_select;
196 r->parse_config = http_recv_parse_config;
197 r->free_config = http_recv_free_config;
198 r->help = (struct ggo_help) {
199 .short_help = http_recv_args_info_help,
200 .detailed_help = http_recv_args_info_detailed_help
202 http_recv_cmdline_parser_free(&dummy);