summaryrefslogtreecommitdiffstats
path: root/src/net/tcp/http.c
blob: 6ae664ad839615c99581e5d8a539f49bc68cf36c (plain) (blame)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
 * Copyright (C) 2007 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

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

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <byteswap.h>
#include <errno.h>
#include <assert.h>
#include <vsprintf.h>
#include <gpxe/async.h>
#include <gpxe/uri.h>
#include <gpxe/buffer.h>
#include <gpxe/download.h>
#include <gpxe/http.h>

static inline struct http_request *
tcp_to_http ( struct tcp_application *app ) {
	return container_of ( app, struct http_request, tcp );
}

/**
 * Mark HTTP request as complete
 *
 * @v http		HTTP request
 * @v rc		Return status code
 *
 */
static void http_done ( struct http_request *http, int rc ) {

	/* Close TCP connection */
	tcp_close ( &http->tcp );

	/* Prevent further processing of any current packet */
	http->rx_state = HTTP_RX_DEAD;

	/* Free up any dynamically allocated storage */
	empty_line_buffer ( &http->linebuf );

	/* If we had a Content-Length, and the received content length
	 * isn't correct, flag an error
	 */
	if ( http->content_length &&
	     ( http->content_length != http->buffer->fill ) ) {
		DBGC ( http, "HTTP %p incorrect length %zd, should be %zd\n",
		       http, http->buffer->fill, http->content_length );
		rc = -EIO;
	}

	/* Mark async operation as complete */
	async_done ( &http->async, rc );
}

/**
 * Convert HTTP response code to return status code
 *
 * @v response		HTTP response code
 * @ret rc		Return status code
 */
static int http_response_to_rc ( unsigned int response ) {
	switch ( response ) {
	case 200:
		return 0;
	case 404:
		return -ENOENT;
	case 403:
		return -EPERM;
	default:
		return -EIO;
	}
}

/**
 * Handle HTTP response
 *
 * @v http		HTTP request
 * @v response		HTTP response
 */
static void http_rx_response ( struct http_request *http, char *response ) {
	char *spc;
	int rc = -EIO;

	DBGC ( http, "HTTP %p response \"%s\"\n", http, response );

	/* Check response starts with "HTTP/" */
	if ( strncmp ( response, "HTTP/", 5 ) != 0 )
		goto err;

	/* Locate and check response code */
	spc = strchr ( response, ' ' );
	if ( ! spc )
		goto err;
	http->response = strtoul ( spc, NULL, 10 );
	if ( ( rc = http_response_to_rc ( http->response ) ) != 0 )
		goto err;

	/* Move to received headers */
	http->rx_state = HTTP_RX_HEADER;
	return;

 err:
	DBGC ( http, "HTTP %p bad response\n", http );
	http_done ( http, rc );
	return;
}

/**
 * Handle HTTP Content-Length header
 *
 * @v http		HTTP request
 * @v value		HTTP header value
 * @ret rc		Return status code
 */
static int http_rx_content_length ( struct http_request *http,
				    const char *value ) {
	char *endp;

	http->content_length = strtoul ( value, &endp, 10 );
	if ( *endp != '\0' ) {
		DBGC ( http, "HTTP %p invalid Content-Length \"%s\"\n",
		       http, value );
		return -EIO;
	}

	return 0;
}

/**
 * An HTTP header handler
 *
 */
struct http_header_handler {
	/** Name (e.g. "Content-Length") */
	const char *header;
	/** Handle received header
	 *
	 * @v http	HTTP request
	 * @v value	HTTP header value
	 * @ret rc	Return status code
	 */
	int ( * rx ) ( struct http_request *http, const char *value );
};

/** List of HTTP header handlers */
struct http_header_handler http_header_handlers[] = {
	{
		.header = "Content-Length",
		.rx = http_rx_content_length,
	},
	{ NULL, NULL }
};

/**
 * Handle HTTP header
 *
 * @v http		HTTP request
 * @v header		HTTP header
 */
static void http_rx_header ( struct http_request *http, char *header ) {
	struct http_header_handler *handler;
	char *separator;
	char *value;
	int rc = -EIO;

	/* An empty header line marks the transition to the data phase */
	if ( ! header[0] ) {
		DBGC ( http, "HTTP %p start of data\n", http );
		empty_line_buffer ( &http->linebuf );
		http->rx_state = HTTP_RX_DATA;
		return;
	}

	DBGC ( http, "HTTP %p header \"%s\"\n", http, header );

	/* Split header at the ": " */
	separator = strstr ( header, ": " );
	if ( ! separator )
		goto err;
	*separator = '\0';
	value = ( separator + 2 );

	/* Hand off to header handler, if one exists */
	for ( handler = http_header_handlers ; handler->header ; handler++ ) {
		if ( strcasecmp ( header, handler->header ) == 0 ) {
			if ( ( rc = handler->rx ( http, value ) ) != 0 )
				goto err;
			break;
		}
	}
	return;

 err:
	DBGC ( http, "HTTP %p bad header\n", http );
	http_done ( http, rc );
	return;
}

/**
 * Handle new data arriving via HTTP connection in the data phase
 *
 * @v http		HTTP request
 * @v data		New data
 * @v len		Length of new data
 */
static void http_rx_data ( struct http_request *http,
			   const char *data, size_t len ) {
	int rc;

	/* Fill data buffer */
	if ( ( rc = fill_buffer ( http->buffer, data,
				  http->buffer->fill, len ) ) != 0 ) {
		DBGC ( http, "HTTP %p failed to fill data buffer: %s\n",
		       http, strerror ( rc ) );
		http_done ( http, rc );
		return;
	}

	/* If we have reached the content-length, stop now */
	if ( http->content_length &&
	     ( http->buffer->fill >= http->content_length ) ) {
		http_done ( http, 0 );
	}
}

/**
 * Handle new data arriving via HTTP connection
 *
 * @v http		HTTP request
 * @v data		New data
 * @v len		Length of new data
 */
static void http_newdata ( struct tcp_application *app,
			   void *data, size_t len ) {
	struct http_request *http = tcp_to_http ( app );
	const char *buf = data;
	char *line;
	int rc;

	while ( len ) {
		if ( http->rx_state == HTTP_RX_DEAD ) {
			/* Do no further processing */
			return;
		} else if ( http->rx_state == HTTP_RX_DATA ) {
			/* Once we're into the data phase, just fill
			 * the data buffer
			 */
			http_rx_data ( http, buf, len );
			return;
		} else {
			/* In the other phases, buffer and process a
			 * line at a time
			 */
			if ( ( rc = line_buffer ( &http->linebuf, &buf,
						  &len ) ) != 0 ) {
				DBGC ( http, "HTTP %p could not buffer line: "
				       "%s\n", http, strerror ( rc ) );
				http_done ( http, rc );
				return;
			}
			if ( ( line = buffered_line ( &http->linebuf ) ) ) {
				switch ( http->rx_state ) {
				case HTTP_RX_RESPONSE:
					http_rx_response ( http, line );
					break;
				case HTTP_RX_HEADER:
					http_rx_header ( http, line );
					break;
				default:
					assert ( 0 );
					break;
				}
			}
		}
	}
}

/**
 * Send HTTP data
 *
 * @v app		TCP application
 * @v buf		Temporary data buffer
 * @v len		Length of temporary data buffer
 */
static void http_senddata ( struct tcp_application *app,
			    void *buf, size_t len ) {
	struct http_request *http = tcp_to_http ( app );
	const char *path = http->uri->path;
	const char *host = http->uri->host;

	if ( ! path )
		path = "/";

	len = snprintf ( buf, len,
			 "GET %s HTTP/1.1\r\n"
			 "User-Agent: gPXE/" VERSION "\r\n"
			 "Host: %s\r\n"
			 "\r\n", path, host );

	tcp_send ( app, ( buf + http->tx_offset ), ( len - http->tx_offset ) );
}

/**
 * HTTP data acknowledged
 *
 * @v app		TCP application
 * @v len		Length of acknowledged data
 */
static void http_acked ( struct tcp_application *app, size_t len ) {
	struct http_request *http = tcp_to_http ( app );

	http->tx_offset += len;
}

/**
 * HTTP connection closed by network stack
 *
 * @v app		TCP application
 */
static void http_closed ( struct tcp_application *app, int rc ) {
	struct http_request *http = tcp_to_http ( app );

	DBGC ( http, "HTTP %p connection closed: %s\n",
	       http, strerror ( rc ) );
	
	http_done ( http, rc );
}

/** HTTP TCP operations */
static struct tcp_operations http_tcp_operations = {
	.closed		= http_closed,
	.acked		= http_acked,
	.newdata	= http_newdata,
	.senddata	= http_senddata,
};

/**
 * Reap asynchronous operation
 *
 * @v async		Asynchronous operation
 */
static void http_reap ( struct async *async ) {
	struct http_request *http =
		container_of ( async, struct http_request, async );

	free ( http );
}

/**
 * Handle name resolution completion
 *
 * @v async		HTTP asynchronous operation
 * @v signal		SIGCHLD
 */
static void http_sigchld ( struct async *async, enum signal signal __unused ) {
	struct http_request *http =
		container_of ( async, struct http_request, async );
	struct sockaddr_tcpip *st = ( struct sockaddr_tcpip * ) &http->server;
	int rc;

	/* Reap child */
	async_wait ( async, &rc, 1 );

	/* If name resolution failed, abort now */
	if ( rc != 0 ) {
		http_done ( http, rc );
		return;
	}

	/* Otherwise, start the HTTP connection */
	http->tcp.tcp_op = &http_tcp_operations;
	st->st_port = htons ( uri_port ( http->uri, HTTP_PORT ) );
	if ( ( rc = tcp_connect ( &http->tcp, st, 0 ) ) != 0 ) {
		DBGC ( http, "HTTP %p could not open TCP connection: %s\n",
		       http, strerror ( rc ) );
		http_done ( http, rc );
		return;
	}
}

/** HTTP asynchronous operations */
static struct async_operations http_async_operations = {
	.reap = http_reap,
	.signal = {
		[SIGCHLD] = http_sigchld,
	},
};

/**
 * Initiate a HTTP connection
 *
 * @v uri		Uniform Resource Identifier
 * @v buffer		Buffer into which to download file
 * @v parent		Parent asynchronous operation
 * @ret rc		Return status code
 */
int http_get ( struct uri *uri, struct buffer *buffer, struct async *parent ) {
	struct http_request *http = NULL;
	int rc;

	/* Sanity check */
	if ( ! uri->host ) {
		rc = -EINVAL;
		goto err;
	}

	/* Allocate and populate HTTP structure */
	http = malloc ( sizeof ( *http ) );
	if ( ! http )
		return -ENOMEM;
	memset ( http, 0, sizeof ( *http ) );
	http->uri = uri;
	http->buffer = buffer;
	async_init ( &http->async, &http_async_operations, parent );


#warning "Quick name resolution hack"
	extern int dns_resolv ( const char *name,
				struct sockaddr *sa,
				struct async *parent );
		
	if ( ( rc = dns_resolv ( uri->host, &http->server,
				 &http->async ) ) != 0 )
		goto err;


	return 0;

 err:
	DBGC ( http, "HTTP %p could not create request: %s\n", 
	       http, strerror ( rc ) );
	async_uninit ( &http->async );
	free ( http );
	return rc;
}

/** HTTP download protocol */
struct download_protocol http_download_protocol __download_protocol = {
	.name = "http",
	.start_download = http_get,
};