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