📄 http.lst
字号:
196 2 tcp_data = inbuf;
197 2 conxn[nr].my_sequence = conxn[nr].old_sequence;
198 2 }
199 1
200 1 // Start off with no request
201 1 request = NONE;
202 1
203 1 // TODO: Calling strstr() on a large buffer takes a lot of time
204 1 // so perhaps we could speed things up by limiting the search
205 1 // range to the portion of the buffer where the item is expected
206 1 // to be found
207 1
208 1 // If it is a POST, then set a flag to start looking for the post
209 1 // data of interest, which is the string "switch=". It may arrive
210 1 // in a later segment (Netscape seems to split up the POST message)
211 1 if (strstr(tcp_data, "POST") != NULL) post_flg = TRUE;
212 1
213 1 // See if this is a GET message
214 1 else if (strstr(tcp_data, "GET") != NULL)
215 1 {
216 2 post_flg = FALSE;
217 2 if (strstr(tcp_data, "photo1") != NULL) request = GET_JPEG;
218 2 else if (strstr(tcp_data, "index") != NULL) request = GET_PAGE;
219 2 else if (strstr(tcp_data, "/ ") != NULL) request = GET_PAGE;
220 2 }
221 1
222 1 // If POST flag is "armed" then look for the "switch=" string
223 1 // and if found, turn the LED on or off according to whether
224 1 // the browser is sending a 1 or a 0.
225 1 if (post_flg)
226 1 {
227 2 ptr = strstr(tcp_data, "switch=");
228 2 if (ptr != NULL)
229 2 {
230 3 // Move to space after equals sign
231 3 // Set control indicator accordingly
232 3 post_flg = FALSE;
233 3 request = POST_PAGE;
234 3 ptr += 7;
235 3 if (*ptr == '1') {CONTROL_LED=0x0;}
236 3 else if (*ptr == '0') {CONTROL_LED=0x1;}
237 3 LightONOFF(CONTROL_LED);
238 3 P36=!CONTROL_LED;
239 3 }
240 2 }
241 1
C51 COMPILER V7.07 HTTP 11/25/2003 15:47:46 PAGE 5
242 1 if ((request == GET_PAGE) || (request == POST_PAGE))
243 1 {
244 2 // Figure out sizes
245 2 hhdr_len = strlen(html_header);
246 2 page_len = strlen(web_page);
247 2 body_len = hhdr_len + page_len;
248 2
249 2 // Free memory holding received message. The message from the
250 2 // browser can be 500+ bytes long so this is a significant
251 2 // chunk out of the available malloc space of 1500 bytes
252 2 if (!resend) {free(inbuf); rcve_buf_allocated = FALSE;}
253 2
254 2 // Allocate memory for entire outgoing message including
255 2 // 14 byte Ethernet + 20 byte IP + 20 byte TCP headers
256 2 // Allow 1 byte for NULL char at the end
257 2 outbuf = (UCHAR xdata *)malloc(54 + body_len + 1);
258 2 if (outbuf == NULL)
259 2 {
260 3 if (debug) serial_send("TCP: Oops, out of memory\r");
261 3 return 0;
262 3 }
263 2
264 2 // Copy page data. This moves data from flash into RAM. It is
265 2 // an undesirable process, but must get data into RAM to replace
266 2 // tags
267 2 memcpy(outbuf + 54, html_header, hhdr_len);
268 2 memcpy(outbuf + 54 + hhdr_len, web_page, page_len);
269 2
270 2 outbuf[54 + body_len] = 0; // Append NULL
271 2
272 2 // Replace length tag with actual value
273 2 itoa(page_len, text, 10);
274 2 replace_tag(outbuf + 54, "TAG:LEN1", text);
275 2
276 2 // Replace CPU temperature tag with actual value
277 2 itoa(cpu_temperature/100, text, 10);
278 2 i=strlen(text);
279 2 text[i]='.';
280 2 i++;
281 2 itoa(cpu_temperature%100, text+i, 10);
282 2 replace_tag(outbuf + 54, "TAG:TMP1", text);
283 2
284 2 itoa(air_temperature/1000, text, 10);
285 2 i=strlen(text);
286 2 text[i]='.';
287 2 i++;
288 2 itoa(air_temperature%1000, text+i, 10);
289 2 replace_tag(outbuf + 54, "TAG:TMP2", text);
290 2
291 2 // Replace CPU voltage tag with actual value X 100
292 2 // Insert decimal point between first and second digits
293 2 itoa(cpu_voltage/1000, text, 10);
294 2 i=strlen(text);
295 2 text[i]='.';
296 2 i++;
297 2 itoa(cpu_voltage%1000, text+i, 10);
298 2 replace_tag(outbuf + 54, "TAG:VOL1", text);
299 2
300 2 // Insert the word CHECKED into the html so the browser will
301 2 // check the correct LED state indicator box
302 2 if (CONTROL_LED == OFF) replace_tag(outbuf + 54, "TAG:CHK1", "CHECKED");
303 2 else replace_tag(outbuf + 54, "TAG:CHK2", "CHECKED");
C51 COMPILER V7.07 HTTP 11/25/2003 15:47:46 PAGE 6
304 2
305 2 // Segment length = body_len + 20
306 2 http_send(outbuf, body_len + 20, nr);
307 2 conxn[nr].my_sequence += body_len;
308 2 }
309 1 else if (request == GET_JPEG)
310 1 {
311 2 // Ths browser has requested the jpeg image. First figure out
312 2 // sizes - cannot use sizeof() for jpeg here because it is in
313 2 // another module. Just directly specify length of it
314 2 jhdr_len = strlen(jpeg_header);
315 2 jpeg_len = 5705;//6194;
316 2 body_len = jhdr_len + jpeg_len;
317 2
318 2 // Free memory holding received message. The message from the
319 2 // browser can be 500+ bytes long so this is a significant
320 2 // chunk out of the available malloc space of 1500 bytes
321 2 if (!resend) {free(inbuf); rcve_buf_allocated = FALSE;}
322 2
323 2 // First send the header and enough of the jpeg to make 1000 bytes.
324 2 // The value of 1000 is arbitrary, but must be stay under 1500.
325 2 if (body_len < 1000) remaining = body_len; else remaining = 1000;
326 2 outbuf = (UCHAR xdata *)malloc(54 + remaining + 1);
327 2 if (outbuf == NULL)
328 2 {
329 3 if (debug) serial_send("TCP: Oops, out of memory\r");
330 3 return 0;
331 3 }
332 2
333 2 memcpy(outbuf + 54, jpeg_header, jhdr_len);
334 2 memcpy(outbuf + 54 + jhdr_len, photo1_jpeg, remaining - jhdr_len);
335 2
336 2 outbuf[54 + remaining] = 0; // Append NULL
337 2
338 2 // Replace jpeg length tag with actual value
339 2 itoa(jpeg_len, text, 10);
340 2 replace_tag(outbuf + 54, "TAG:LEN2", text);
341 2
342 2 http_send(outbuf, remaining + 20, nr);
343 2 sent = remaining - jhdr_len;
344 2 conxn[nr].my_sequence += remaining;
345 2
346 2 // Send the rest of the jpeg file in 1000 byte chunks. This sends about
347 2 // 6 segments of 1000 bytes back to back, but we should actually process
348 2 // acks from the other end to make sure we are not sending more than the
349 2 // other end can receive. Most systems can handle 6K
350 2 while (sent < jpeg_len)
351 2 {
352 3 remaining = jpeg_len - sent;
353 3 if (remaining > 1000) remaining = 1000;
354 3 outbuf = (UCHAR xdata *)malloc(54 + remaining + 1);
355 3 if (outbuf == NULL)
356 3 {
357 4 if (debug) serial_send("TCP: Oops, out of memory\r");
358 4 return 0;
359 4 }
360 3
361 3 memcpy(outbuf + 54, photo1_jpeg + sent, remaining);
362 3
363 3 outbuf[54 + remaining] = 0; // Append NULL
364 3 http_send(outbuf, remaining + 20, nr);
365 3 sent += remaining;
C51 COMPILER V7.07 HTTP 11/25/2003 15:47:46 PAGE 7
366 3 conxn[nr].my_sequence += remaining;
367 3 }
368 2 }
369 1 else
370 1 {
371 2 // The incoming HTTP message did not warrant a response
372 2 if (debug) serial_send("HTTP: No response sent\r");
373 2 return 0;
374 2 }
375 1
376 1 // Return number of bytes sent, not including TCP header
377 1 return(body_len);
378 1 }
379
380
381
382
383
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 2037 ----
CONSTANT SIZE = 184 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 39
IDATA SIZE = 1 23
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -