summaryrefslogtreecommitdiffstats
path: root/src/proto/http.c
blob: 6f47fc14dba801dcc6efc23ec481cf3df9ca3932 (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
#include "proto.h"
#include "tcp.h"
#include "url.h"
#include "etherboot.h"

/* The block size is currently chosen to be 512 bytes. This means, we can
   allocate the receive buffer on the stack, but it results in a noticeable
   performance penalty.
   This is what needs to be done in order to increase the block size:
     - size negotiation needs to be implemented in TCP
     - the buffer needs to be allocated on the heap
     - path MTU discovery needs to be implemented
*/ /***/ /* FIXME */
#define BLOCKSIZE TFTP_DEFAULTSIZE_PACKET

/**************************************************************************
SEND_TCP_CALLBACK - Send data using TCP
**************************************************************************/
struct send_recv_state {
	struct buffer *recv_buffer;
	char *send_buffer;
	int send_length;
	int bytes_sent;
	int bytes_received;
	enum { RESULT_CODE, HEADER, DATA, ERROR, MOVED } recv_state;
	int rc;
	char *url;
};

static int send_tcp_request(int length, void *buffer, void *ptr) {
	struct send_recv_state *state = (struct send_recv_state *)ptr;

	if (length > state->send_length - state->bytes_sent)
		length = state->send_length - state->bytes_sent;
	memcpy(buffer, state->send_buffer + state->bytes_sent, length);
	state->bytes_sent += length;
	return (length);
}

/**************************************************************************
RECV_TCP_CALLBACK - Receive data using TCP
**************************************************************************/
static int recv_tcp_request(int length, const void *buffer, void *ptr) {
	struct send_recv_state *state = (struct send_recv_state *)ptr;

	/* Assume that the lines in an HTTP header do not straddle a packet */
	/* boundary. This is probably a reasonable assumption */
	if (state->recv_state == RESULT_CODE) {
		while (length > 0) {
			/* Find HTTP result code */
			if (*(const char *)buffer == ' ') {
				const char *ptr = ((const char *)buffer) + 1;
				int rc = strtoul(ptr, &ptr, 10);
				if (ptr >= (const char *)buffer + length) {
					state->recv_state = ERROR;
					DBG ( "HTTP got bad result code\n" );
					return 0;
				}
				state->rc = rc;
				state->recv_state = HEADER;
				DBG ( "HTTP got result code %d\n", rc );
				goto header;
			}
			++(const char *)buffer;
			length--;
		}
		state->recv_state = ERROR;
		DBG ( "HTTP got no result code\n" );
		return 0;
	}
	if (state->recv_state == HEADER) {
	header: while (length > 0) {
			/* Check for HTTP redirect */
			if (state->rc >= 300 && state->rc < 400 &&
			    !memcmp(buffer, "Location: ", 10)) {
				char *p;
				
				state->url = p = ( char * ) buffer + 10;
				while ( *p > ' ' ) {
					p++;
				}
				*p = '\0';
				state->recv_state = MOVED;
				DBG ( "HTTP got redirect to %s\n",
				      state->url );
				return 1;
			}
			/* Find beginning of line */
			while (length > 0) {
				length--;
				if (*((const char *)buffer)++ == '\n')
					break;
			}
			/* Check for end of header */
			if (length >= 2 && !memcmp(buffer, "\r\n", 2)) {
				state->recv_state = DATA;
				buffer += 2;
				length -= 2;
				break;
			}
		}
	}
	if (state->recv_state == DATA) {
		DBG2 ( "HTTP received %d bytes\n", length );
		if ( ! fill_buffer ( state->recv_buffer, buffer,
				     state->bytes_received, length ) )
			return 0;
		state->bytes_received += length;
	}
	return 1;
}

/**************************************************************************
HTTP_GET - Get data using HTTP
**************************************************************************/
static int http ( char *url, struct sockaddr_in *server __unused,
		  char *file __unused, struct buffer *buffer ) {
	struct protocol *proto;
	struct sockaddr_in http_server = *server;
	char *filename;
	static const char GET[] = "GET /%s HTTP/1.0\r\n\r\n";
	struct send_recv_state state;
	int length;

	state.rc = -1;
	state.url = url;
	state.recv_buffer = buffer;
	while ( 1 ) {
		length = strlen ( filename ) + strlen ( GET );
		{
			char send_buf[length];

			sprintf ( send_buf, GET, filename );
			state.send_buffer = send_buf;
			state.send_length = strlen ( send_buf );
			state.bytes_sent = 0;
			
			state.bytes_received = 0;
			state.recv_state = RESULT_CODE;
			
			tcp_transaction ( server->sin_addr.s_addr,
					  server->sin_port, &state,
					  send_tcp_request, recv_tcp_request );
		}

		if ( state.recv_state == MOVED ) {
			if ( ! parse_url ( state.url, &proto,
					   &http_server, &filename ) ) {
				printf ( "Invalid redirect URL %s\n",
					 state.url );
				return 0;
			}
			continue;
		}
		
		break;
	}

	if ( state.rc != 200 ) {
		printf ( "Failed to download %s (rc = %d)\n",
			 state.url, state.rc );
		return 0;
	}

	return 1;
}

static struct protocol http_protocol __protocol = {
	.name = "http",
	.default_port = 80,
	.load = http,
};