2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file http_recv.c paraslash's http receiver */
10 #include <netinet/in.h>
11 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
18 #include "recv_cmd.lsg.h"
24 #include "buffer_tree.h"
31 * the possible states of a http receiver node
33 * \sa \ref receiver_node.
35 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
38 * Data specific to the http receiver.
40 * Each running instance of the http receiver reserves space for one such struct.
42 struct private_http_recv_data {
44 * The current status of the http receiver node.
46 * It gets initialized to \p HTTP_CONNECTED by the open function of the
49 * \sa \ref receiver::open, \ref receiver_node.
51 enum http_recv_status status;
54 static char *make_request_msg(void)
56 char *ret, *hn = para_hostname();
57 ret = make_message("%s1.0\nHost: %s\nUser-Agent: para_recv/%s\n\n\n",
58 HTTP_GET_MSG, hn, PACKAGE_VERSION);
63 static void http_recv_pre_select(struct sched *s, void *context)
65 struct receiver_node *rn = context;
66 struct private_http_recv_data *phd = rn->private_data;
68 if (generic_recv_pre_select(s, rn) <= 0)
70 if (phd->status == HTTP_CONNECTED)
71 para_fd_set(rn->fd, &s->wfds, &s->max_fileno);
73 para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
77 * Establish the http connection. If already established, fill the buffer pool
78 * area with data read from the socket. In any case, update the state of the
79 * connection if necessary.
81 static int http_recv_post_select(struct sched *s, void *context)
83 struct receiver_node *rn = context;
84 struct private_http_recv_data *phd = rn->private_data;
85 struct btr_node *btrn = rn->btrn;
90 ret = task_get_notification(rn->task);
93 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
98 if (phd->status == HTTP_CONNECTED) {
100 if (!FD_ISSET(rn->fd, &s->wfds))
102 rq = make_request_msg();
103 PARA_INFO_LOG("sending http request\n");
104 ret = write_va_buffer(rn->fd, "%s", rq);
108 phd->status = HTTP_SENT_GET_REQUEST;
111 if (phd->status == HTTP_SENT_GET_REQUEST) {
112 ret = read_pattern(rn->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG), &s->rfds);
117 PARA_INFO_LOG("received ok msg, streaming\n");
118 phd->status = HTTP_STREAMING;
121 ret = -E_HTTP_RECV_OVERRUN;
122 iovcnt = btr_pool_get_buffers(rn->btrp, iov);
125 ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
128 if (num_bytes <= iov[0].iov_len) /* only the first buffer was filled */
129 btr_add_output_pool(rn->btrp, num_bytes, btrn);
130 else { /* both buffers contain data */
131 btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
132 btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len, btrn);
136 btr_remove_node(&rn->btrn);
140 static void http_recv_close(struct receiver_node *rn)
143 btr_pool_free(rn->btrp);
144 free(rn->private_data);
147 static int http_recv_open(struct receiver_node *rn)
149 struct private_http_recv_data *phd;
150 struct lls_parse_result *lpr = rn->lpr;
151 const char *r_i = RECV_CMD_OPT_STRING_VAL(HTTP, HOST, lpr);
152 uint32_t r_p = RECV_CMD_OPT_UINT32_VAL(HTTP, PORT, lpr);
153 int fd, ret = para_connect_simple(IPPROTO_TCP, r_i, r_p);
158 ret = mark_fd_nonblocking(fd);
163 rn->private_data = phd = para_calloc(sizeof(struct private_http_recv_data));
165 phd->status = HTTP_CONNECTED;
166 rn->btrp = btr_pool_new("http_recv", 320 * 1024);
170 /** See \ref recv_init(). */
171 const struct receiver lsg_recv_cmd_com_http_user_data = {
172 .open = http_recv_open,
173 .close = http_recv_close,
174 .pre_select = http_recv_pre_select,
175 .post_select = http_recv_post_select,