iPXE
httpconn.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /**
27  * @file
28  *
29  * Hyper Text Transfer Protocol (HTTP) connection management
30  *
31  */
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <errno.h>
37 #include <byteswap.h>
38 #include <ipxe/tcpip.h>
39 #include <ipxe/uri.h>
40 #include <ipxe/timer.h>
41 #include <ipxe/xfer.h>
42 #include <ipxe/open.h>
43 #include <ipxe/pool.h>
44 #include <ipxe/http.h>
45 
46 /** HTTP pooled connection expiry time */
47 #define HTTP_CONN_EXPIRY ( 10 * TICKS_PER_SEC )
48 
49 /** HTTP connection pool */
50 static LIST_HEAD ( http_connection_pool );
51 
52 /**
53  * Identify HTTP scheme
54  *
55  * @v uri URI
56  * @ret scheme HTTP scheme, or NULL
57  */
58 static struct http_scheme * http_scheme ( struct uri *uri ) {
59  struct http_scheme *scheme;
60 
61  /* Sanity check */
62  if ( ! uri->scheme )
63  return NULL;
64 
65  /* Identify scheme */
67  if ( strcasecmp ( uri->scheme, scheme->name ) == 0 )
68  return scheme;
69  }
70 
71  return NULL;
72 }
73 
74 /**
75  * Free HTTP connection
76  *
77  * @v refcnt Reference count
78  */
79 static void http_conn_free ( struct refcnt *refcnt ) {
80  struct http_connection *conn =
82 
83  /* Free connection */
84  uri_put ( conn->uri );
85  free ( conn );
86 }
87 
88 /**
89  * Close HTTP connection
90  *
91  * @v conn HTTP connection
92  * @v rc Reason for close
93  */
94 static void http_conn_close ( struct http_connection *conn, int rc ) {
95 
96  /* Remove from connection pool, if applicable */
97  pool_del ( &conn->pool );
98 
99  /* Shut down interfaces */
100  intf_shutdown ( &conn->socket, rc );
101  intf_shutdown ( &conn->xfer, rc );
102  if ( rc == 0 ) {
103  DBGC2 ( conn, "HTTPCONN %p closed %s://%s\n",
104  conn, conn->scheme->name, conn->uri->host );
105  } else {
106  DBGC ( conn, "HTTPCONN %p closed %s://%s: %s\n",
107  conn, conn->scheme->name, conn->uri->host,
108  strerror ( rc ) );
109  }
110 }
111 
112 /**
113  * Disconnect idle HTTP connection
114  *
115  * @v pool Pooled connection
116  */
117 static void http_conn_expired ( struct pooled_connection *pool ) {
118  struct http_connection *conn =
119  container_of ( pool, struct http_connection, pool );
120 
121  /* Close connection */
122  http_conn_close ( conn, 0 /* Not an error to close idle connection */ );
123 }
124 
125 /**
126  * Receive data from transport layer interface
127  *
128  * @v http HTTP connection
129  * @v iobuf I/O buffer
130  * @v meta Transfer metadata
131  * @ret rc Return status code
132  */
133 static int http_conn_socket_deliver ( struct http_connection *conn,
134  struct io_buffer *iobuf,
135  struct xfer_metadata *meta ) {
136 
137  /* Mark connection as alive */
138  pool_alive ( &conn->pool );
139 
140  /* Pass on to data transfer interface */
141  return xfer_deliver ( &conn->xfer, iobuf, meta );
142 }
143 
144 /**
145  * Close HTTP connection transport layer interface
146  *
147  * @v http HTTP connection
148  * @v rc Reason for close
149  */
150 static void http_conn_socket_close ( struct http_connection *conn, int rc ) {
151 
152  /* If we are reopenable (i.e. we are a recycled connection
153  * from the connection pool, and we have received no data from
154  * the underlying socket since we were pooled), then suggest
155  * that the client should reopen the connection.
156  */
157  if ( pool_is_reopenable ( &conn->pool ) )
158  pool_reopen ( &conn->xfer );
159 
160  /* Close the connection */
161  http_conn_close ( conn, rc );
162 }
163 
164 /**
165  * Recycle this connection after closing
166  *
167  * @v http HTTP connection
168  */
169 static void http_conn_xfer_recycle ( struct http_connection *conn ) {
170 
171  /* Mark connection as recyclable */
172  pool_recyclable ( &conn->pool );
173  DBGC2 ( conn, "HTTPCONN %p keepalive enabled\n", conn );
174 }
175 
176 /**
177  * Close HTTP connection data transfer interface
178  *
179  * @v conn HTTP connection
180  * @v rc Reason for close
181  */
182 static void http_conn_xfer_close ( struct http_connection *conn, int rc ) {
183 
184  /* Add to the connection pool if keepalive is enabled and no
185  * error occurred.
186  */
187  if ( ( rc == 0 ) && pool_is_recyclable ( &conn->pool ) ) {
188  intf_restart ( &conn->xfer, rc );
189  pool_add ( &conn->pool, &http_connection_pool,
191  DBGC2 ( conn, "HTTPCONN %p pooled %s://%s\n",
192  conn, conn->scheme->name, conn->uri->host );
193  return;
194  }
195 
196  /* Otherwise, close the connection */
197  http_conn_close ( conn, rc );
198 }
199 
200 /** HTTP connection socket interface operations */
204  INTF_OP ( intf_close, struct http_connection *,
206 };
207 
208 /** HTTP connection socket interface descriptor */
210  INTF_DESC_PASSTHRU ( struct http_connection, socket,
212 
213 /** HTTP connection data transfer interface operations */
217  INTF_OP ( intf_close, struct http_connection *,
219 };
220 
221 /** HTTP connection data transfer interface descriptor */
223  INTF_DESC_PASSTHRU ( struct http_connection, xfer,
224  http_conn_xfer_operations, socket );
225 
226 /**
227  * Connect to an HTTP server
228  *
229  * @v xfer Data transfer interface
230  * @v uri Connection URI
231  * @ret rc Return status code
232  *
233  * HTTP connections are pooled. The caller should be prepared to
234  * receive a pool_reopen() message.
235  */
236 int http_connect ( struct interface *xfer, struct uri *uri ) {
237  struct http_connection *conn;
238  struct http_scheme *scheme;
239  struct sockaddr_tcpip server;
240  unsigned int port;
241  int rc;
242 
243  /* Identify scheme */
244  scheme = http_scheme ( uri );
245  if ( ! scheme )
246  return -ENOTSUP;
247 
248  /* Sanity check */
249  if ( ! uri->host )
250  return -EINVAL;
251 
252  /* Identify port */
253  port = uri_port ( uri, scheme->port );
254 
255  /* Look for a reusable connection in the pool. Reuse the most
256  * recent connection in order to accommodate authentication
257  * schemes that break the stateless nature of HTTP and rely on
258  * the same connection being reused for authentication
259  * responses.
260  */
261  list_for_each_entry_reverse ( conn, &http_connection_pool, pool.list ) {
262 
263  /* Sanity checks */
264  assert ( conn->uri != NULL );
265  assert ( conn->uri->host != NULL );
266 
267  /* Reuse connection, if possible */
268  if ( ( scheme == conn->scheme ) &&
269  ( strcmp ( uri->host, conn->uri->host ) == 0 ) &&
270  ( port == uri_port ( conn->uri, scheme->port ) ) ) {
271 
272  /* Remove from connection pool, stop timer,
273  * attach to parent interface, and return.
274  */
275  pool_del ( &conn->pool );
276  intf_plug_plug ( &conn->xfer, xfer );
277  DBGC2 ( conn, "HTTPCONN %p reused %s://%s:%d\n", conn,
278  conn->scheme->name, conn->uri->host, port );
279  return 0;
280  }
281  }
282 
283  /* Allocate and initialise structure */
284  conn = zalloc ( sizeof ( *conn ) );
285  if ( ! conn ) {
286  rc = -ENOMEM;
287  goto err_alloc;
288  }
289  ref_init ( &conn->refcnt, http_conn_free );
290  conn->uri = uri_get ( uri );
291  conn->scheme = scheme;
292  intf_init ( &conn->socket, &http_conn_socket_desc, &conn->refcnt );
293  intf_init ( &conn->xfer, &http_conn_xfer_desc, &conn->refcnt );
294  pool_init ( &conn->pool, http_conn_expired, &conn->refcnt );
295 
296  /* Open socket */
297  memset ( &server, 0, sizeof ( server ) );
298  server.st_port = htons ( port );
299  if ( ( rc = xfer_open_named_socket ( &conn->socket, SOCK_STREAM,
300  ( struct sockaddr * ) &server,
301  uri->host, NULL ) ) != 0 )
302  goto err_open;
303 
304  /* Add filter, if any */
305  if ( scheme->filter && ( ( rc = scheme->filter ( conn ) ) != 0 ) )
306  goto err_filter;
307 
308  /* Attach to parent interface, mortalise self, and return */
309  intf_plug_plug ( &conn->xfer, xfer );
310  ref_put ( &conn->refcnt );
311 
312  DBGC2 ( conn, "HTTPCONN %p created %s://%s:%d\n", conn,
313  conn->scheme->name, conn->uri->host, port );
314  return 0;
315 
316  err_filter:
317  err_open:
318  DBGC2 ( conn, "HTTPCONN %p could not create %s://%s:%d: %s\n", conn,
319  conn->scheme->name, conn->uri->host, port, strerror ( rc ) );
320  http_conn_close ( conn, rc );
321  ref_put ( &conn->refcnt );
322  err_alloc:
323  return rc;
324 }
static struct interface_operation http_conn_socket_operations[]
HTTP connection socket interface operations.
Definition: httpconn.c:201
#define EINVAL
Invalid argument.
Definition: errno.h:428
An object interface operation.
Definition: interface.h:17
TCP/IP socket address.
Definition: tcpip.h:75
struct refcnt refcnt
Reference count.
Definition: http.h:73
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void pool_add(struct pooled_connection *pool, struct list_head *list, unsigned long expiry)
Add connection to pool.
Definition: pool.c:63
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:249
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:343
Data transfer metadata.
Definition: xfer.h:22
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:278
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:205
static struct uri * uri_get(struct uri *uri)
Increment URI reference count.
Definition: uri.h:194
static struct interface_descriptor http_conn_socket_desc
HTTP connection socket interface descriptor.
Definition: httpconn.c:209
timer_init & pool
Definition: pool.h:65
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
Error codes.
Pooled connections.
unsigned int port
Default port.
Definition: http.h:44
int(* filter)(struct http_connection *conn)
Transport-layer filter (if any)
Definition: http.h:50
#define DBGC(...)
Definition: compiler.h:505
static struct interface_operation http_conn_xfer_operations[]
HTTP connection data transfer interface operations.
Definition: httpconn.c:214
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:107
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:208
iPXE timers
A pooled connection.
Definition: pool.h:17
struct interface socket
Transport layer interface.
Definition: http.h:84
Uniform Resource Identifiers.
void pool_del(struct pooled_connection *pool)
Remove connection from pool.
Definition: pool.c:82
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
struct interface xfer
Data transfer interface.
Definition: http.h:86
Data transfer interfaces.
A reference counter.
Definition: refcnt.h:26
int http_connect(struct interface *xfer, struct uri *uri)
Connect to an HTTP server.
Definition: httpconn.c:236
#define ENOMEM
Not enough space.
Definition: errno.h:534
const char * scheme
Scheme.
Definition: uri.h:68
u8 port
Port number.
Definition: CIB_PRM.h:31
static struct http_scheme * http_scheme(struct uri *uri)
Identify HTTP scheme.
Definition: httpconn.c:58
Hyper Text Transport Protocol.
static int http_conn_socket_deliver(struct http_connection *conn, struct io_buffer *iobuf, struct xfer_metadata *meta)
Receive data from transport layer interface.
Definition: httpconn.c:133
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An object interface.
Definition: interface.h:124
static void http_conn_socket_close(struct http_connection *conn, int rc)
Close HTTP connection transport layer interface.
Definition: httpconn.c:150
#define list_for_each_entry_reverse(pos, head, member)
Iterate over entries in a list in reverse order.
Definition: list.h:444
Transport-network layer interface.
void pool_reopen(struct interface *intf)
Reopen a defunct connection.
Definition: pool.c:51
static void http_conn_xfer_close(struct http_connection *conn, int rc)
Close HTTP connection data transfer interface.
Definition: httpconn.c:182
const char * name
Scheme name (e.g.
Definition: http.h:42
int meta(WINDOW *, bool)
uint16_t st_port
TCP/IP port.
Definition: tcpip.h:81
Generalized socket address structure.
Definition: socket.h:96
An object interface descriptor.
Definition: interface.h:55
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
static void http_conn_close(struct http_connection *conn, int rc)
Close HTTP connection.
Definition: httpconn.c:94
#define SOCK_STREAM
Definition: socket.h:24
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:194
Data transfer interface opening.
const char * host
Host name.
Definition: uri.h:76
struct http_scheme * scheme
HTTP scheme.
Definition: http.h:82
#define HTTP_SCHEMES
HTTP scheme table.
Definition: http.h:54
#define HTTP_CONN_EXPIRY
HTTP pooled connection expiry time.
Definition: httpconn.c:47
An HTTP URI scheme.
Definition: http.h:40
#define DBGC2(...)
Definition: compiler.h:522
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct uri * uri
Connection URI.
Definition: http.h:80
static void http_conn_free(struct refcnt *refcnt)
Free HTTP connection.
Definition: httpconn.c:79
unsigned int uri_port(const struct uri *uri, unsigned int default_port)
Get port from URI.
Definition: uri.c:455
A Uniform Resource Identifier.
Definition: uri.h:64
#define INTF_DESC_PASSTHRU(object_type, intf, operations, passthru)
Define an object interface descriptor with pass-through interface.
Definition: interface.h:97
static void http_conn_expired(struct pooled_connection *pool)
Disconnect idle HTTP connection.
Definition: httpconn.c:117
void pool_recycle(struct interface *intf)
Recycle this connection after closing.
Definition: pool.c:41
static void http_conn_xfer_recycle(struct http_connection *conn)
Recycle this connection after closing.
Definition: httpconn.c:169
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:203
struct pooled_connection pool
Pooled connection.
Definition: http.h:88
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
An HTTP connection.
Definition: http.h:71
#define htons(value)
Definition: byteswap.h:135
static LIST_HEAD(http_connection_pool)
HTTP connection pool.
int xfer_open_named_socket(struct interface *xfer, int semantics, struct sockaddr *peer, const char *name, struct sockaddr *local)
Open named socket.
Definition: resolv.c:402
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
String functions.
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:33
static struct interface_descriptor http_conn_xfer_desc
HTTP connection data transfer interface descriptor.
Definition: httpconn.c:222