summaryrefslogtreecommitdiff
path: root/roms/ipxe/src/net/tcp/httpconn.c
blob: 7e4877b7bc28a40ab2cf85b7ea702bbdee0c51d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

/**
 * @file
 *
 * Hyper Text Transfer Protocol (HTTP) connection management
 *
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/tcpip.h>
#include <ipxe/uri.h>
#include <ipxe/timer.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/pool.h>
#include <ipxe/http.h>

/** HTTP pooled connection expiry time */
#define HTTP_CONN_EXPIRY ( 10 * TICKS_PER_SEC )

/** HTTP connection pool */
static LIST_HEAD ( http_connection_pool );

/**
 * Identify HTTP scheme
 *
 * @v uri		URI
 * @ret scheme		HTTP scheme, or NULL
 */
static struct http_scheme * http_scheme ( struct uri *uri ) {
	struct http_scheme *scheme;

	/* Sanity check */
	if ( ! uri->scheme )
		return NULL;

	/* Identify scheme */
	for_each_table_entry ( scheme, HTTP_SCHEMES ) {
		if ( strcmp ( uri->scheme, scheme->name ) == 0 )
			return scheme;
	}

	return NULL;
}

/**
 * Free HTTP connection
 *
 * @v refcnt		Reference count
 */
static void http_conn_free ( struct refcnt *refcnt ) {
	struct http_connection *conn =
		container_of ( refcnt, struct http_connection, refcnt );

	/* Free connection */
	uri_put ( conn->uri );
	free ( conn );
}

/**
 * Close HTTP connection
 *
 * @v conn		HTTP connection
 * @v rc		Reason for close
 */
static void http_conn_close ( struct http_connection *conn, int rc ) {

	/* Remove from connection pool, if applicable */
	pool_del ( &conn->pool );

	/* Shut down interfaces */
	intf_shutdown ( &conn->socket, rc );
	intf_shutdown ( &conn->xfer, rc );
	if ( rc == 0 ) {
		DBGC2 ( conn, "HTTPCONN %p closed %s://%s\n",
			conn, conn->scheme->name, conn->uri->host );
	} else {
		DBGC ( conn, "HTTPCONN %p closed %s://%s: %s\n",
		       conn, conn->scheme->name, conn->uri->host,
		       strerror ( rc ) );
	}
}

/**
 * Disconnect idle HTTP connection
 *
 * @v pool		Pooled connection
 */
static void http_conn_expired ( struct pooled_connection *pool ) {
	struct http_connection *conn =
		container_of ( pool, struct http_connection, pool );

	/* Close connection */
	http_conn_close ( conn, 0 /* Not an error to close idle connection */ );
}

/**
 * Receive data from transport layer interface
 *
 * @v http		HTTP connection
 * @v iobuf		I/O buffer
 * @v meta		Transfer metadata
 * @ret rc		Return status code
 */
static int http_conn_socket_deliver ( struct http_connection *conn,
				      struct io_buffer *iobuf,
				      struct xfer_metadata *meta ) {

	/* Mark connection as alive */
	pool_alive ( &conn->pool );

	/* Pass on to data transfer interface */
	return xfer_deliver ( &conn->xfer, iobuf, meta );
}

/**
 * Close HTTP connection transport layer interface
 *
 * @v http		HTTP connection
 * @v rc		Reason for close
 */
static void http_conn_socket_close ( struct http_connection *conn, int rc ) {

	/* If we are reopenable (i.e. we are a recycled connection
	 * from the connection pool, and we have received no data from
	 * the underlying socket since we were pooled), then suggest
	 * that the client should reopen the connection.
	 */
	if ( pool_is_reopenable ( &conn->pool ) )
		pool_reopen ( &conn->xfer );

	/* Close the connection */
	http_conn_close ( conn, rc );
}

/**
 * Recycle this connection after closing
 *
 * @v http		HTTP connection
 */
static void http_conn_xfer_recycle ( struct http_connection *conn ) {

	/* Mark connection as recyclable */
	pool_recyclable ( &conn->pool );
	DBGC2 ( conn, "HTTPCONN %p keepalive enabled\n", conn );
}

/**
 * Close HTTP connection data transfer interface
 *
 * @v conn		HTTP connection
 * @v rc		Reason for close
 */
static void http_conn_xfer_close ( struct http_connection *conn, int rc ) {

	/* Add to the connection pool if keepalive is enabled and no
	 * error occurred.
	 */
	if ( ( rc == 0 ) && pool_is_recyclable ( &conn->pool ) ) {
		intf_restart ( &conn->xfer, rc );
		pool_add ( &conn->pool, &http_connection_pool,
			   HTTP_CONN_EXPIRY );
		DBGC2 ( conn, "HTTPCONN %p pooled %s://%s\n",
			conn, conn->scheme->name, conn->uri->host );
		return;
	}

	/* Otherwise, close the connection */
	http_conn_close ( conn, rc );
}

/** HTTP connection socket interface operations */
static struct interface_operation http_conn_socket_operations[] = {
	INTF_OP ( xfer_deliver, struct http_connection *,
		  http_conn_socket_deliver ),
	INTF_OP ( intf_close, struct http_connection *,
		  http_conn_socket_close ),
};

/** HTTP connection socket interface descriptor */
static struct interface_descriptor http_conn_socket_desc =
	INTF_DESC_PASSTHRU ( struct http_connection, socket,
			     http_conn_socket_operations, xfer );

/** HTTP connection data transfer interface operations */
static struct interface_operation http_conn_xfer_operations[] = {
	INTF_OP ( pool_recycle, struct http_connection *,
		  http_conn_xfer_recycle ),
	INTF_OP ( intf_close, struct http_connection *,
		  http_conn_xfer_close ),
};

/** HTTP connection data transfer interface descriptor */
static struct interface_descriptor http_conn_xfer_desc =
	INTF_DESC_PASSTHRU ( struct http_connection, xfer,
			     http_conn_xfer_operations, socket );

/**
 * Connect to an HTTP server
 *
 * @v xfer		Data transfer interface
 * @v uri		Connection URI
 * @ret rc		Return status code
 *
 * HTTP connections are pooled.  The caller should be prepared to
 * receive a pool_reopen() message.
 */
int http_connect ( struct interface *xfer, struct uri *uri ) {
	struct http_connection *conn;
	struct http_scheme *scheme;
	struct sockaddr_tcpip server;
	struct interface *socket;
	int rc;

	/* Identify scheme */
	scheme = http_scheme ( uri );
	if ( ! scheme )
		return -ENOTSUP;

	/* Sanity check */
	if ( ! uri->host )
		return -EINVAL;

	/* Look for a reusable connection in the pool */
	list_for_each_entry ( conn, &http_connection_pool, pool.list ) {

		/* Sanity checks */
		assert ( conn->uri != NULL );
		assert ( conn->uri->host != NULL );

		/* Reuse connection, if possible */
		if ( ( scheme == conn->scheme ) &&
		     ( strcmp ( uri->host, conn->uri->host ) == 0 ) ) {

			/* Remove from connection pool, stop timer,
			 * attach to parent interface, and return.
			 */
			pool_del ( &conn->pool );
			intf_plug_plug ( &conn->xfer, xfer );
			DBGC2 ( conn, "HTTPCONN %p reused %s://%s\n",
				conn, conn->scheme->name, conn->uri->host );
			return 0;
		}
	}

	/* Allocate and initialise structure */
	conn = zalloc ( sizeof ( *conn ) );
	ref_init ( &conn->refcnt, http_conn_free );
	conn->uri = uri_get ( uri );
	conn->scheme = scheme;
	intf_init ( &conn->socket, &http_conn_socket_desc, &conn->refcnt );
	intf_init ( &conn->xfer, &http_conn_xfer_desc, &conn->refcnt );
	pool_init ( &conn->pool, http_conn_expired, &conn->refcnt );

	/* Open socket */
	memset ( &server, 0, sizeof ( server ) );
	server.st_port = htons ( uri_port ( uri, scheme->port ) );
	socket = &conn->socket;
	if ( scheme->filter &&
	     ( ( rc = scheme->filter ( socket, uri->host, &socket ) ) != 0 ) )
		goto err_filter;
	if ( ( rc = xfer_open_named_socket ( socket, SOCK_STREAM,
					     ( struct sockaddr * ) &server,
					     uri->host, NULL ) ) != 0 )
		goto err_open;

	/* Attach to parent interface, mortalise self, and return */
	intf_plug_plug ( &conn->xfer, xfer );
	ref_put ( &conn->refcnt );

	DBGC2 ( conn, "HTTPCONN %p created %s://%s:%d\n", conn,
		conn->scheme->name, conn->uri->host, ntohs ( server.st_port ) );
	return 0;

 err_open:
 err_filter:
	DBGC2 ( conn, "HTTPCONN %p could not create %s://%s: %s\n",
		conn, conn->scheme->name, conn->uri->host, strerror ( rc ) );
	http_conn_close ( conn, rc );
	ref_put ( &conn->refcnt );
	return rc;
}