Browse Source

Update to use buffer rather than processor

tags/v0.9.3
Michael Brown 19 years ago
parent
commit
5fce946de4
1 changed files with 11 additions and 40 deletions
  1. 11
    40
      src/proto/http.c

+ 11
- 40
src/proto/http.c View File

17
 SEND_TCP_CALLBACK - Send data using TCP
17
 SEND_TCP_CALLBACK - Send data using TCP
18
 **************************************************************************/
18
 **************************************************************************/
19
 struct send_recv_state {
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
 	char *send_buffer;
21
 	char *send_buffer;
24
-	char *recv_buffer;
25
 	int send_length;
22
 	int send_length;
26
-	int recv_length;
27
 	int bytes_sent;
23
 	int bytes_sent;
28
-	int block;
29
 	int bytes_received;
24
 	int bytes_received;
30
 	enum { RESULT_CODE, HEADER, DATA, ERROR, MOVED } recv_state;
25
 	enum { RESULT_CODE, HEADER, DATA, ERROR, MOVED } recv_state;
31
 	int rc;
26
 	int rc;
106
 		}
101
 		}
107
 	}
102
 	}
108
 	if (state->recv_state == DATA) {
103
 	if (state->recv_state == DATA) {
109
-		state->bytes_received += length;
110
 		DBG2 ( "HTTP received %d bytes\n", length );
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
 	return 1;
110
 	return 1;
130
 }
111
 }
132
 /**************************************************************************
113
 /**************************************************************************
133
 HTTP_GET - Get data using HTTP
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
 	struct protocol *proto;
118
 	struct protocol *proto;
142
 	struct sockaddr_in http_server = *server;
119
 	struct sockaddr_in http_server = *server;
143
 	char *filename;
120
 	char *filename;
144
 	static const char GET[] = "GET /%s HTTP/1.0\r\n\r\n";
121
 	static const char GET[] = "GET /%s HTTP/1.0\r\n\r\n";
145
-	static char recv_buffer[BLOCKSIZE];
146
 	struct send_recv_state state;
122
 	struct send_recv_state state;
147
 	int length;
123
 	int length;
148
 
124
 
149
 	state.rc = -1;
125
 	state.rc = -1;
150
-	state.block = 0;
151
-	state.recv_buffer = recv_buffer;
152
 	state.url = url;
126
 	state.url = url;
153
-	state.process = process;
127
+	state.recv_buffer = buffer;
154
 	while ( 1 ) {
128
 	while ( 1 ) {
155
 		length = strlen ( filename ) + strlen ( GET );
129
 		length = strlen ( filename ) + strlen ( GET );
156
 		{
130
 		{
164
 			state.bytes_received = 0;
138
 			state.bytes_received = 0;
165
 			state.recv_state = RESULT_CODE;
139
 			state.recv_state = RESULT_CODE;
166
 			
140
 			
167
-			state.recv_length = 0;
168
 			tcp_transaction ( server->sin_addr.s_addr,
141
 			tcp_transaction ( server->sin_addr.s_addr,
169
 					  server->sin_port, &state,
142
 					  server->sin_port, &state,
170
 					  send_tcp_request, recv_tcp_request );
143
 					  send_tcp_request, recv_tcp_request );
183
 		break;
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
 		printf ( "Failed to download %s (rc = %d)\n",
160
 		printf ( "Failed to download %s (rc = %d)\n",
192
 			 state.url, state.rc );
161
 			 state.url, state.rc );
193
 		return 0;
162
 		return 0;
194
 	}
163
 	}
164
+
165
+	return 1;
195
 }
166
 }
196
 
167
 
197
 static struct protocol http_protocol __protocol = {
168
 static struct protocol http_protocol __protocol = {

Loading…
Cancel
Save