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