|
@@ -17,15 +17,10 @@
|
17
|
17
|
SEND_TCP_CALLBACK - Send data using TCP
|
18
|
18
|
**************************************************************************/
|
19
|
19
|
struct send_recv_state {
|
20
|
|
- int ( * process ) ( unsigned char *data,
|
21
|
|
- unsigned int blocknum,
|
22
|
|
- unsigned int len, int eof );
|
|
20
|
+ struct buffer *recv_buffer;
|
23
|
21
|
char *send_buffer;
|
24
|
|
- char *recv_buffer;
|
25
|
22
|
int send_length;
|
26
|
|
- int recv_length;
|
27
|
23
|
int bytes_sent;
|
28
|
|
- int block;
|
29
|
24
|
int bytes_received;
|
30
|
25
|
enum { RESULT_CODE, HEADER, DATA, ERROR, MOVED } recv_state;
|
31
|
26
|
int rc;
|
|
@@ -106,25 +101,11 @@ static int recv_tcp_request(int length, const void *buffer, void *ptr) {
|
106
|
101
|
}
|
107
|
102
|
}
|
108
|
103
|
if (state->recv_state == DATA) {
|
109
|
|
- state->bytes_received += length;
|
110
|
104
|
DBG2 ( "HTTP received %d bytes\n", length );
|
111
|
|
- while (length > 0) {
|
112
|
|
- int copy_length = BLOCKSIZE - state->recv_length;
|
113
|
|
- if (copy_length > length)
|
114
|
|
- copy_length = length;
|
115
|
|
- memcpy(state->recv_buffer + state->recv_length,
|
116
|
|
- buffer, copy_length);
|
117
|
|
- if ((state->recv_length += copy_length) == BLOCKSIZE) {
|
118
|
|
- DBG2 ( "HTTP processing %d bytes\n",
|
119
|
|
- BLOCKSIZE );
|
120
|
|
- if (!state->process(state->recv_buffer,
|
121
|
|
- ++state->block,
|
122
|
|
- BLOCKSIZE, 0))
|
123
|
|
- state->recv_length = 0;
|
124
|
|
- }
|
125
|
|
- length -= copy_length;
|
126
|
|
- buffer += copy_length;
|
127
|
|
- }
|
|
105
|
+ if ( ! fill_buffer ( state->recv_buffer, buffer,
|
|
106
|
+ state->bytes_received, length ) )
|
|
107
|
+ return 0;
|
|
108
|
+ state->bytes_received += length;
|
128
|
109
|
}
|
129
|
110
|
return 1;
|
130
|
111
|
}
|
|
@@ -132,25 +113,18 @@ static int recv_tcp_request(int length, const void *buffer, void *ptr) {
|
132
|
113
|
/**************************************************************************
|
133
|
114
|
HTTP_GET - Get data using HTTP
|
134
|
115
|
**************************************************************************/
|
135
|
|
-static int http ( char *url,
|
136
|
|
- struct sockaddr_in *server __unused,
|
137
|
|
- char *file __unused,
|
138
|
|
- int ( * process ) ( unsigned char *data,
|
139
|
|
- unsigned int blocknum,
|
140
|
|
- unsigned int len, int eof ) ) {
|
|
116
|
+static int http ( char *url, struct sockaddr_in *server __unused,
|
|
117
|
+ char *file __unused, struct buffer *buffer ) {
|
141
|
118
|
struct protocol *proto;
|
142
|
119
|
struct sockaddr_in http_server = *server;
|
143
|
120
|
char *filename;
|
144
|
121
|
static const char GET[] = "GET /%s HTTP/1.0\r\n\r\n";
|
145
|
|
- static char recv_buffer[BLOCKSIZE];
|
146
|
122
|
struct send_recv_state state;
|
147
|
123
|
int length;
|
148
|
124
|
|
149
|
125
|
state.rc = -1;
|
150
|
|
- state.block = 0;
|
151
|
|
- state.recv_buffer = recv_buffer;
|
152
|
126
|
state.url = url;
|
153
|
|
- state.process = process;
|
|
127
|
+ state.recv_buffer = buffer;
|
154
|
128
|
while ( 1 ) {
|
155
|
129
|
length = strlen ( filename ) + strlen ( GET );
|
156
|
130
|
{
|
|
@@ -164,7 +138,6 @@ static int http ( char *url,
|
164
|
138
|
state.bytes_received = 0;
|
165
|
139
|
state.recv_state = RESULT_CODE;
|
166
|
140
|
|
167
|
|
- state.recv_length = 0;
|
168
|
141
|
tcp_transaction ( server->sin_addr.s_addr,
|
169
|
142
|
server->sin_port, &state,
|
170
|
143
|
send_tcp_request, recv_tcp_request );
|
|
@@ -183,15 +156,13 @@ static int http ( char *url,
|
183
|
156
|
break;
|
184
|
157
|
}
|
185
|
158
|
|
186
|
|
- if ( state.rc == 200 ) {
|
187
|
|
- DBG2 ( "HTTP processing %d bytes\n", state.recv_length );
|
188
|
|
- return process ( recv_buffer, ++state.block,
|
189
|
|
- state.recv_length, 1 );
|
190
|
|
- } else {
|
|
159
|
+ if ( state.rc != 200 ) {
|
191
|
160
|
printf ( "Failed to download %s (rc = %d)\n",
|
192
|
161
|
state.url, state.rc );
|
193
|
162
|
return 0;
|
194
|
163
|
}
|
|
164
|
+
|
|
165
|
+ return 1;
|
195
|
166
|
}
|
196
|
167
|
|
197
|
168
|
static struct protocol http_protocol __protocol = {
|