paraslash 0.4.1
[paraslash.git] / http_recv.c
1 /*
2  * Copyright (C) 2005-2009 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 #include <dirent.h>
12
13 #include "para.h"
14 #include "error.h"
15 #include "http.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "ggo.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 /** the output buffer size of the http receiver */
26 #define BUFSIZE (32 * 1024)
27
28 /**
29  * the possible states of a http receiver node
30  *
31  * \sa receiver_node
32  */
33 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
34
35 /**
36  * data specific to the http receiver
37  *
38  * Each running instance of the http receiver reserves space for one such struct.
39  */
40 struct private_http_recv_data {
41 /**
42  *
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  *
55  * the file descriptor used for receiving the http stream
56  *
57  * The pre_select function of the http receiver adds this file descriptor to
58  * the set of file decriptors which are checked for reading/writing (depending
59  * on the current status) by the select loop of the application (para_audiod or
60  * para_recv).
61  *
62  * The post_select function of the http receiver uses \a fd, if ready, to
63  * establish the http connection, and updates \a status according to the new
64  * state of the connection.  As soon as \a status is \p HTTP_STREAMING, \a fd is
65  * going to be only checked for reading. If data is available, it is read into
66  * the output buffer of the receiver node by post_select.
67  *
68  * \sa receiver::pre_select receiver::post_select receiver_node, http_recv_status
69  */
70         int fd;
71 };
72
73 static void http_shutdown(void)
74 {
75         return;
76 }
77
78 static char *make_request_msg(void)
79 {
80         char *ret, *hn = para_hostname();
81         ret = make_message("%s1.0\nHost: %s\nUser-Agent: para_recv/%s\n\n\n",
82                 HTTP_GET_MSG, hn, PACKAGE_VERSION);
83         free(hn);
84         return ret;
85 }
86
87 static void http_recv_pre_select(struct sched *s, struct task *t)
88 {
89         struct receiver_node *rn = container_of(t, struct receiver_node, task);
90         struct private_http_recv_data *phd = rn->private_data;
91
92         t->error = 0;
93         if  (phd->status == HTTP_CONNECTED)
94                 para_fd_set(phd->fd, &s->wfds, &s->max_fileno);
95         else
96                 para_fd_set(phd->fd, &s->rfds, &s->max_fileno);
97 }
98
99
100 static void http_recv_post_select(struct sched *s, struct task *t)
101 {
102         struct receiver_node *rn = container_of(t, struct receiver_node, task);
103         struct private_http_recv_data *phd = rn->private_data;
104
105         if (rn->output_error && *rn->output_error < 0) {
106                 t->error = *rn->output_error;
107                 return;
108         }
109         if  (phd->status == HTTP_CONNECTED) {
110                 char *rq;
111                 if (!FD_ISSET(phd->fd, &s->wfds))
112                         return;
113                 rq = make_request_msg();
114                 PARA_INFO_LOG("sending http request\n");
115                 t->error = send_va_buffer(phd->fd, "%s", rq);
116                 free(rq);
117                 if (t->error >= 0)
118                         phd->status = HTTP_SENT_GET_REQUEST;
119                 return;
120         }
121         if (!FD_ISSET(phd->fd, &s->rfds))
122                 return;
123         if (phd->status == HTTP_SENT_GET_REQUEST) {
124                 t->error = recv_pattern(phd->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG));
125                 if (t->error >= 0) {
126                         PARA_INFO_LOG("received ok msg, streaming\n");
127                         phd->status = HTTP_STREAMING;
128                 }
129                 return;
130         }
131         if (rn->loaded >= BUFSIZE) {
132                 t->error = -E_HTTP_RECV_OVERRUN;
133                 return;
134         }
135         t->error = recv_bin_buffer(phd->fd, rn->buf + rn->loaded,
136                 BUFSIZE - rn->loaded);
137         if (t->error > 0) {
138                 rn->loaded += t->error;
139                 return;
140         }
141         if (!t->error)
142                 t->error = -E_RECV_EOF;
143 }
144
145 static void http_recv_close(struct receiver_node *rn)
146 {
147         struct private_http_recv_data *phd = rn->private_data;
148         close(phd->fd);
149         free(rn->buf);
150         free(rn->private_data);
151 }
152
153 static void *http_recv_parse_config(int argc, char **argv)
154 {
155         struct http_recv_args_info *tmp = para_calloc(sizeof(struct http_recv_args_info));
156
157         if (!http_recv_cmdline_parser(argc, argv, tmp))
158                 return tmp;
159         free(tmp);
160         return NULL;
161 }
162
163 static int http_recv_open(struct receiver_node *rn)
164 {
165         struct private_http_recv_data *phd;
166         struct http_recv_args_info *conf = rn->conf;
167         int fd, ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, conf->host_arg,
168                 conf->port_arg);
169
170         if (ret < 0)
171                 return ret;
172         fd = ret;
173         ret = mark_fd_nonblocking(fd);
174         if (ret < 0) {
175                 close(fd);
176                 return ret;
177         }
178         rn->buf = para_calloc(BUFSIZE);
179         rn->private_data = phd = para_calloc(sizeof(struct private_http_recv_data));
180         phd->fd = fd;
181         phd->status = HTTP_CONNECTED;
182         return 1;
183 }
184
185 /**
186  * the init function of the http receiver
187  *
188  * \param r pointer to the receiver struct to initialize
189  *
190  * Just initialize all function pointers of \a r.
191  */
192 void http_recv_init(struct receiver *r)
193 {
194         struct http_recv_args_info dummy;
195
196         http_recv_cmdline_parser_init(&dummy);
197         r->open = http_recv_open;
198         r->close = http_recv_close;
199         r->pre_select = http_recv_pre_select;
200         r->post_select = http_recv_post_select;
201         r->shutdown = http_shutdown;
202         r->parse_config = http_recv_parse_config;
203         r->help = (struct ggo_help) {
204                 .short_help = http_recv_args_info_help,
205                 .detailed_help = http_recv_args_info_detailed_help
206         };
207         http_recv_cmdline_parser_free(&dummy);
208 }