2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file http_recv.c paraslash's http receiver */
18 #include "http_recv.cmdline.h"
23 /** the output buffer size of the http receiver */
24 #define BUFSIZE (32 * 1024)
27 * the possible states of a http receiver node
31 enum http_recv_status
{HTTP_CONNECTED
, HTTP_SENT_GET_REQUEST
, HTTP_STREAMING
};
34 * data specific to the http receiver
36 * Each running instance of the http receiver reserves space for one such struct.
38 struct private_http_recv_data
{
42 * the current status of the http receiver node
44 * It gets initialized to \p HTTP_CONNECTED by the open function of the
47 * \sa receiver::open, receiver_node.
49 enum http_recv_status status
;
53 * the file descriptor used for receiving the http stream
55 * The pre_select function of the http receiver adds this file descriptor to
56 * the set of file decriptors which are checked for reading/writing (depending
57 * on the current status) by the select loop of the application (para_audiod or
60 * The post_select function of the http receiver uses \a fd, if ready, to
61 * establish the http connection, and updates \a status according to the new
62 * state of the connection. As soon as \a status is \p HTTP_STREAMING, \a fd is
63 * going to be only checked for reading. If data is available, it is read into
64 * the output buffer of the receiver node by post_select.
66 * \sa receiver::pre_select receiver::post_select receiver_node, http_recv_status
71 static void http_shutdown(void)
76 static char *make_request_msg(void)
78 char *ret
, *hn
= para_hostname();
79 ret
= make_message("%s1.0\nHost: %s\nUser-Agent: para_recv/%s\n\n\n",
80 HTTP_GET_MSG
, hn
, PACKAGE_VERSION
);
85 static void http_recv_pre_select(struct sched
*s
, struct task
*t
)
87 struct receiver_node
*rn
= t
->private_data
;
88 struct private_http_recv_data
*phd
= rn
->private_data
;
91 if (phd
->status
== HTTP_CONNECTED
)
92 para_fd_set(phd
->fd
, &s
->wfds
, &s
->max_fileno
);
94 para_fd_set(phd
->fd
, &s
->rfds
, &s
->max_fileno
);
98 static void http_recv_post_select(struct sched
*s
, struct task
*t
)
100 struct receiver_node
*rn
= t
->private_data
;
101 struct private_http_recv_data
*phd
= rn
->private_data
;
103 if (rn
->output_error
&& *rn
->output_error
) {
104 t
->ret
= *rn
->output_error
;
110 if (phd
->status
== HTTP_CONNECTED
) {
112 if (!FD_ISSET(phd
->fd
, &s
->wfds
))
114 rq
= make_request_msg();
115 PARA_INFO_LOG("%s", "sending http request\n");
116 t
->ret
= send_va_buffer(phd
->fd
, "%s", rq
);
119 phd
->status
= HTTP_SENT_GET_REQUEST
;
122 if (!FD_ISSET(phd
->fd
, &s
->rfds
))
124 if (phd
->status
== HTTP_SENT_GET_REQUEST
) {
125 t
->ret
= recv_pattern(phd
->fd
, HTTP_OK_MSG
, MAXLINE
);
128 PARA_INFO_LOG("%s", "received ok msg, streaming\n");
130 phd
->status
= HTTP_STREAMING
;
133 t
->ret
= -E_HTTP_RECV_OVERRUN
;
134 if (rn
->loaded
>= BUFSIZE
)
136 t
->ret
= recv_bin_buffer(phd
->fd
, rn
->buf
+ rn
->loaded
,
137 BUFSIZE
- rn
->loaded
);
140 t
->ret
= -E_RECV_EOF
;
143 rn
->loaded
+= t
->ret
;
149 static void http_recv_close(struct receiver_node
*rn
)
151 struct private_http_recv_data
*phd
= rn
->private_data
;
154 free(rn
->private_data
);
157 static void *http_recv_parse_config(int argc
, char **argv
)
159 struct http_recv_args_info
*tmp
= para_calloc(sizeof(struct http_recv_args_info
));
161 if (!http_recv_cmdline_parser(argc
, argv
, tmp
))
167 static int http_recv_open(struct receiver_node
*rn
)
169 struct private_http_recv_data
*phd
;
170 struct http_recv_args_info
*conf
= rn
->conf
;
171 int ret
= makesock(AF_UNSPEC
, IPPROTO_TCP
, 0, conf
->host_arg
, conf
->port_arg
);
176 rn
->buf
= para_calloc(BUFSIZE
);
177 rn
->private_data
= phd
= para_calloc(sizeof(struct private_http_recv_data
));
180 mark_fd_nonblocking(phd
->fd
);
181 phd
->status
= HTTP_CONNECTED
;
186 * the init function of the http receiver
188 * \param r pointer to the receiver struct to initialize
190 * Just initialize all function pointers of \a r.
192 void http_recv_init(struct receiver
*r
)
194 r
->open
= http_recv_open
;
195 r
->close
= http_recv_close
;
196 r
->pre_select
= http_recv_pre_select
;
197 r
->post_select
= http_recv_post_select
;
198 r
->shutdown
= http_shutdown
;
199 r
->parse_config
= http_recv_parse_config
;