string.c: Fix typos in comment.
[paraslash.git] / http_recv.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file http_recv.c paraslash's http receiver */
4
5 #include <regex.h>
6 #include <netinet/in.h>
7 #include <sys/socket.h>
8 #include <sys/types.h>
9 #include <arpa/inet.h>
10 #include <sys/un.h>
11 #include <netdb.h>
12 #include <lopsub.h>
13
14 #include "recv_cmd.lsg.h"
15 #include "para.h"
16 #include "error.h"
17 #include "http.h"
18 #include "list.h"
19 #include "sched.h"
20 #include "buffer_tree.h"
21 #include "recv.h"
22 #include "net.h"
23 #include "string.h"
24 #include "fd.h"
25
26 /**
27 * the possible states of a http receiver node
28 *
29 * \sa \ref receiver_node.
30 */
31 enum http_recv_status {HTTP_CONNECTED, HTTP_SENT_GET_REQUEST, HTTP_STREAMING};
32
33 /**
34 * Data specific to the http receiver.
35 *
36 * Each running instance of the http receiver reserves space for one such struct.
37 */
38 struct private_http_recv_data {
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 \ref receiver::open, \ref receiver_node.
46 */
47 enum http_recv_status status;
48 };
49
50 static char *make_request_msg(void)
51 {
52 char *ret, *hn = para_hostname();
53 ret = make_message("%s1.0\nHost: %s\nUser-Agent: para_recv/%s\n\n\n",
54 HTTP_GET_MSG, hn, PACKAGE_VERSION);
55 free(hn);
56 return ret;
57 }
58
59 static void http_recv_pre_select(struct sched *s, void *context)
60 {
61 struct receiver_node *rn = context;
62 struct private_http_recv_data *phd = rn->private_data;
63
64 if (generic_recv_pre_select(s, rn) <= 0)
65 return;
66 if (phd->status == HTTP_CONNECTED)
67 para_fd_set(rn->fd, &s->wfds, &s->max_fileno);
68 else
69 para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
70 }
71
72 /*
73 * Establish the http connection. If already established, fill the buffer pool
74 * area with data read from the socket. In any case, update the state of the
75 * connection if necessary.
76 */
77 static int http_recv_post_select(struct sched *s, void *context)
78 {
79 struct receiver_node *rn = context;
80 struct private_http_recv_data *phd = rn->private_data;
81 struct btr_node *btrn = rn->btrn;
82 int ret, iovcnt;
83 struct iovec iov[2];
84 size_t num_bytes;
85
86 ret = task_get_notification(rn->task);
87 if (ret < 0)
88 goto out;
89 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
90 if (ret < 0)
91 goto out;
92 if (ret == 0)
93 return 0;
94 if (phd->status == HTTP_CONNECTED) {
95 char *rq;
96 if (!FD_ISSET(rn->fd, &s->wfds))
97 return 0;
98 rq = make_request_msg();
99 PARA_INFO_LOG("sending http request\n");
100 ret = write_va_buffer(rn->fd, "%s", rq);
101 free(rq);
102 if (ret < 0)
103 goto out;
104 phd->status = HTTP_SENT_GET_REQUEST;
105 return 0;
106 }
107 if (phd->status == HTTP_SENT_GET_REQUEST) {
108 ret = read_pattern(rn->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG), &s->rfds);
109 if (ret < 0) {
110 PARA_ERROR_LOG("did not receive HTTP OK message\n");
111 goto out;
112 }
113 if (ret == 0)
114 return 0;
115 PARA_INFO_LOG("received ok msg, streaming\n");
116 phd->status = HTTP_STREAMING;
117 return 0;
118 }
119 ret = -E_HTTP_RECV_OVERRUN;
120 iovcnt = btr_pool_get_buffers(rn->btrp, iov);
121 if (iovcnt == 0)
122 goto out;
123 ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
124 if (num_bytes == 0)
125 goto out;
126 if (num_bytes <= iov[0].iov_len) /* only the first buffer was filled */
127 btr_add_output_pool(rn->btrp, num_bytes, btrn);
128 else { /* both buffers contain data */
129 btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
130 btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len, btrn);
131 }
132 out:
133 if (ret < 0) {
134 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
135 btr_remove_node(&rn->btrn);
136 }
137 return ret;
138 }
139
140 static void http_recv_close(struct receiver_node *rn)
141 {
142 close(rn->fd);
143 btr_pool_free(rn->btrp);
144 free(rn->private_data);
145 }
146
147 static int http_recv_open(struct receiver_node *rn)
148 {
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);
154
155 if (ret < 0)
156 return ret;
157 fd = ret;
158 ret = mark_fd_nonblocking(fd);
159 if (ret < 0) {
160 close(fd);
161 return ret;
162 }
163 rn->private_data = phd = para_calloc(sizeof(struct private_http_recv_data));
164 rn->fd = fd;
165 phd->status = HTTP_CONNECTED;
166 rn->btrp = btr_pool_new("http_recv", 320 * 1024);
167 return 1;
168 }
169
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,
176 };