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