]> git.tuebingen.mpg.de Git - paraslash.git/blob - http_recv.c
recv: Make ->btrp and ->fd generic.
[paraslash.git] / http_recv.c
1 /*
2  * Copyright (C) 2005-2011 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 "recv.h"
19 #include "http_recv.cmdline.h"
20 #include "net.h"
21 #include "string.h"
22 #include "fd.h"
23 #include "buffer_tree.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         t->error = 0;
64         if (generic_recv_pre_select(s, t) <= 0)
65                 return;
66         if  (phd->status == HTTP_CONNECTED)
67                 para_fd_set(rn->fd, &s->wfds, &s->max_fileno);
68         else
69                 para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
70 }
71
72 /*
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.
76  */
77 static void http_recv_post_select(struct sched *s, struct task *t)
78 {
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;
82         int ret;
83         char *buf;
84         size_t sz, n;
85
86         t->error = 0;
87         ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
88         if (ret < 0)
89                 goto err;
90         if (ret == 0)
91                 return;
92         if (phd->status == HTTP_CONNECTED) {
93                 char *rq;
94                 if (!FD_ISSET(rn->fd, &s->wfds))
95                         return;
96                 rq = make_request_msg();
97                 PARA_INFO_LOG("sending http request\n");
98                 ret = send_va_buffer(rn->fd, "%s", rq);
99                 free(rq);
100                 if (ret < 0)
101                         goto err;
102                 phd->status = HTTP_SENT_GET_REQUEST;
103                 return;
104         }
105         if (phd->status == HTTP_SENT_GET_REQUEST) {
106                 ret = read_pattern(rn->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG), &s->rfds);
107                 if (ret < 0)
108                         goto err;
109                 if (ret == 0)
110                         return;
111                 PARA_INFO_LOG("received ok msg, streaming\n");
112                 phd->status = HTTP_STREAMING;
113                 return;
114         }
115         ret = -E_HTTP_RECV_OVERRUN;
116         sz = btr_pool_get_buffer(rn->btrp, &buf);
117         if (sz == 0)
118                 goto err;
119         ret = read_nonblock(rn->fd, buf, sz, &s->rfds, &n);
120         if (n > 0)
121                 btr_add_output_pool(rn->btrp, n, btrn);
122         if (ret >= 0)
123                 return;
124 err:
125         btr_remove_node(rn->btrn);
126         t->error = ret;
127 }
128
129 static void http_recv_close(struct receiver_node *rn)
130 {
131         close(rn->fd);
132         btr_pool_free(rn->btrp);
133         free(rn->private_data);
134 }
135
136 static void *http_recv_parse_config(int argc, char **argv)
137 {
138         struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
139
140         if (!http_recv_cmdline_parser(argc, argv, tmp))
141                 return tmp;
142         free(tmp);
143         return NULL;
144 }
145
146 static int http_recv_open(struct receiver_node *rn)
147 {
148         struct private_http_recv_data *phd;
149         struct http_recv_args_info *conf = rn->conf;
150         int fd, ret = para_connect_simple(IPPROTO_TCP, conf->host_arg,
151                                                        conf->port_arg);
152
153         if (ret < 0)
154                 return ret;
155         fd = ret;
156         ret = mark_fd_nonblocking(fd);
157         if (ret < 0) {
158                 close(fd);
159                 return ret;
160         }
161         rn->private_data = phd = para_calloc(sizeof(struct private_http_recv_data));
162         rn->fd = fd;
163         phd->status = HTTP_CONNECTED;
164         rn->btrp = btr_pool_new("http_recv", 320 * 1024);
165         return 1;
166 }
167
168 static void http_recv_free_config(void *conf)
169 {
170         http_recv_cmdline_parser_free(conf);
171         free(conf);
172 }
173
174 /**
175  * The init function of the http receiver.
176  *
177  * \param r Pointer to the receiver struct to initialize.
178  *
179  * This initializes all function pointers of \a r.
180  */
181 void http_recv_init(struct receiver *r)
182 {
183         struct http_recv_args_info dummy;
184
185         http_recv_cmdline_parser_init(&dummy);
186         r->open = http_recv_open;
187         r->close = http_recv_close;
188         r->pre_select = http_recv_pre_select;
189         r->post_select = http_recv_post_select;
190         r->parse_config = http_recv_parse_config;
191         r->free_config = http_recv_free_config;
192         r->help = (struct ggo_help) {
193                 .short_help = http_recv_args_info_help,
194                 .detailed_help = http_recv_args_info_detailed_help
195         };
196         http_recv_cmdline_parser_free(&dummy);
197 }