Merge branch 't/aes'
[paraslash.git] / http_recv.c
1 /*
2 * Copyright (C) 2005-2013 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 <netinet/in.h>
11 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
14 #include <sys/un.h>
15 #include <netdb.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "http.h"
20 #include "list.h"
21 #include "sched.h"
22 #include "ggo.h"
23 #include "buffer_tree.h"
24 #include "recv.h"
25 #include "http_recv.cmdline.h"
26 #include "net.h"
27 #include "string.h"
28 #include "fd.h"
29
30 /**
31 * the possible states of a http receiver node
32 *
33 * \sa receiver_node
34 */
35 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
36
37 /**
38 * Data specific to the http receiver.
39 *
40 * Each running instance of the http receiver reserves space for one such struct.
41 */
42 struct private_http_recv_data {
43 /**
44 * The current status of the http receiver node.
45 *
46 * It gets initialized to \p HTTP_CONNECTED by the open function of the
47 * http receiver.
48 *
49 * \sa receiver::open, receiver_node.
50 */
51 enum http_recv_status status;
52 };
53
54 static char *make_request_msg(void)
55 {
56 char *ret, *hn = para_hostname();
57 ret = make_message("%s1.0\nHost: %s\nUser-Agent: para_recv/%s\n\n\n",
58 HTTP_GET_MSG, hn, PACKAGE_VERSION);
59 free(hn);
60 return ret;
61 }
62
63 static void http_recv_pre_select(struct sched *s, struct task *t)
64 {
65 struct receiver_node *rn = container_of(t, struct receiver_node, task);
66 struct private_http_recv_data *phd = rn->private_data;
67
68 if (generic_recv_pre_select(s, t) <= 0)
69 return;
70 if (phd->status == HTTP_CONNECTED)
71 para_fd_set(rn->fd, &s->wfds, &s->max_fileno);
72 else
73 para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
74 }
75
76 /*
77 * Establish the http connection. If already established, fill the buffer pool
78 * area with data read from the socket. In any case, update the state of the
79 * connection if necessary.
80 */
81 static int http_recv_post_select(struct sched *s, struct task *t)
82 {
83 struct receiver_node *rn = container_of(t, struct receiver_node, task);
84 struct private_http_recv_data *phd = rn->private_data;
85 struct btr_node *btrn = rn->btrn;
86 int ret, iovcnt;
87 struct iovec iov[2];
88 size_t num_bytes;
89
90 ret = task_get_notification(t);
91 if (ret < 0)
92 goto out;
93 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
94 if (ret < 0)
95 goto out;
96 if (ret == 0)
97 return 0;
98 if (phd->status == HTTP_CONNECTED) {
99 char *rq;
100 if (!FD_ISSET(rn->fd, &s->wfds))
101 return 0;
102 rq = make_request_msg();
103 PARA_INFO_LOG("sending http request\n");
104 ret = write_va_buffer(rn->fd, "%s", rq);
105 free(rq);
106 if (ret < 0)
107 goto out;
108 phd->status = HTTP_SENT_GET_REQUEST;
109 return 0;
110 }
111 if (phd->status == HTTP_SENT_GET_REQUEST) {
112 ret = read_pattern(rn->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG), &s->rfds);
113 if (ret < 0)
114 goto out;
115 if (ret == 0)
116 return 0;
117 PARA_INFO_LOG("received ok msg, streaming\n");
118 phd->status = HTTP_STREAMING;
119 return 0;
120 }
121 ret = -E_HTTP_RECV_OVERRUN;
122 iovcnt = btr_pool_get_buffers(rn->btrp, iov);
123 if (iovcnt == 0)
124 goto out;
125 ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
126 if (num_bytes == 0)
127 goto out;
128 if (num_bytes <= iov[0].iov_len) /* only the first buffer was filled */
129 btr_add_output_pool(rn->btrp, num_bytes, btrn);
130 else { /* both buffers contain data */
131 btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
132 btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len, btrn);
133 }
134 out:
135 if (ret < 0)
136 btr_remove_node(&rn->btrn);
137 return ret;
138 }
139
140 static void http_recv_close(struct receiver_node *rn)
141 {
142 close(rn->fd);
143 btr_pool_free(rn->btrp);
144 free(rn->private_data);
145 }
146
147 static void *http_recv_parse_config(int argc, char **argv)
148 {
149 struct http_recv_args_info *tmp = para_calloc(sizeof(*tmp));
150
151 http_recv_cmdline_parser(argc, argv, tmp);
152 return tmp;
153 }
154
155 static int http_recv_open(struct receiver_node *rn)
156 {
157 struct private_http_recv_data *phd;
158 struct http_recv_args_info *conf = rn->conf;
159 int fd, ret = para_connect_simple(IPPROTO_TCP, conf->host_arg,
160 conf->port_arg);
161
162 if (ret < 0)
163 return ret;
164 fd = ret;
165 ret = mark_fd_nonblocking(fd);
166 if (ret < 0) {
167 close(fd);
168 return ret;
169 }
170 rn->private_data = phd = para_calloc(sizeof(struct private_http_recv_data));
171 rn->fd = fd;
172 phd->status = HTTP_CONNECTED;
173 rn->btrp = btr_pool_new("http_recv", 320 * 1024);
174 return 1;
175 }
176
177 static void http_recv_free_config(void *conf)
178 {
179 http_recv_cmdline_parser_free(conf);
180 free(conf);
181 }
182
183 /**
184 * The init function of the http receiver.
185 *
186 * \param r Pointer to the receiver struct to initialize.
187 *
188 * This initializes all function pointers of \a r.
189 */
190 void http_recv_init(struct receiver *r)
191 {
192 struct http_recv_args_info dummy;
193
194 http_recv_cmdline_parser_init(&dummy);
195 r->open = http_recv_open;
196 r->close = http_recv_close;
197 r->pre_select = http_recv_pre_select;
198 r->post_select = http_recv_post_select;
199 r->parse_config = http_recv_parse_config;
200 r->free_config = http_recv_free_config;
201 r->help = (struct ggo_help)DEFINE_GGO_HELP(http_recv);
202 http_recv_cmdline_parser_free(&dummy);
203 }