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