http.lst
来自「cf8020+cp2200(网络)的驱动实现」· LST 代码 · 共 291 行
LST
291 行
C51 COMPILER V7.09 HTTP 07/17/2007 17:36:30 PAGE 1
C51 COMPILER V7.09, COMPILATION OF MODULE HTTP
OBJECT MODULE PLACED IN HTTP.obj
COMPILER INVOKED BY: F:\Keil\C51\BIN\C51.EXE tcp\HTTP.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\HTTP.lst) OBJECT(HTTP.ob
-j)
line level source
1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2002 Jim Brady
3 // Do not use commercially without author's permission
4 // Last revised August 2002
5 // Net HTTP.C
6 //
7 // This module is the Web Server
8 // It currently serves a html text page and a jpeg image, or handles
9 // a POST message to turn an LED on or off.
10 // The HTTP protocol specification is at http://www.w3.org/Protocols/
11 //-----------------------------------------------------------------------------
12
13 #include <stdlib.h>
14 #include <ctype.h> // toupper
15 #include "net.h"
16
17 #include "cksum.h"
18 #include "ip.h"
19 #include "tcp.h"
20 #include "http.h"
21 #include "utils.h"
22
23
24 // These structures keep track of connection information
25 extern CONNECTION conxn[];
26
27 extern ulong my_ipaddr;
28 extern char text[];
29 extern char html_header[];
30 extern char web_page[];
31 extern char jpeg_header[];
32 extern UCHAR photo1_jpeg[];
33 extern UCHAR rcve_buf_allocated;
34 extern UCHAR debug;
35 //bit CONTROL_LED;
36 //void LightONOFF(bit b);
37
38 char cnt = 0;
39
40 void init_http(void)
41 {
42 1 // CONTROL_LED = 0;
43 1 // LightONOFF(CONTROL_LED);
44 1 }
45
46
47
48 //------------------------------------------------------------------------
49 // This function is the standard string search. The Keil library
50 // does not provide it. It looks for one string in another string
51 // and returns a pointer to it if found, otherwise returns NULL.
52 //------------------------------------------------------------------------
53 /*char * strstr(char * haystack, char * needle)
54 {
C51 COMPILER V7.09 HTTP 07/17/2007 17:36:30 PAGE 2
55 char *ptr1, *ptr2;
56
57 // Protect against NULL pointer
58 if (*needle == 0) return(haystack);
59 for( ; *haystack; haystack++ )
60 {
61 // Look for needle in haystack. If there is a
62 // match then this will continue all the way
63 // until ptr1 reaches the NULL at the end of needle
64 for(ptr1 = needle, ptr2 = haystack; *ptr1 && (*ptr1 == *ptr2); ++ptr1, ++ptr2);
65
66 // If there is a match then return pointer to needle in haystack
67 if(*ptr1 == 0) return(haystack);
68 }
69 return NULL; // no matching string found
70 }*/
71
72
73
74 //------------------------------------------------------------------------
75 // This sends an TCP segment to the ip layer. The segment is
76 // is normally either a web page or a graphic.
77 // See "TCP/IP Illustrated, Volume 1" Sect 17.3
78 //------------------------------------------------------------------------
79 void http_send(UCHAR * outbuf, uint len, UCHAR nr)
80 {
81 1 TCP_HEADER * tcp;
82 1 IP_HEADER * ip;
83 1 ulong sum;
84 1 uint result;
85 1
86 1 // Fill in TCP segment header
87 1 tcp = (TCP_HEADER *)(outbuf + 34);
88 1 ip = (IP_HEADER *)(outbuf + 14);
89 1
90 1 tcp->source_port = HTTP_PORT;
91 1 tcp->dest_port = conxn[nr].port;
92 1 tcp->sequence = conxn[nr].my_sequence;
93 1 tcp->ack_number = conxn[nr].his_sequence;
94 1
95 1 // Header is always 20 bytes long
96 1 tcp->flags = 0x5000 | FLG_ACK | FLG_PSH;
97 1 tcp->window = 1024;
98 1 tcp->checksum = 0;
99 1 tcp->urgent_ptr = 0;
100 1
101 1 // Compute checksum including 12 bytes of pseudoheader
102 1 // Must pre-fill 2 items in ip header to do this
103 1 ip->dest_ipaddr = conxn[nr].ipaddr;
104 1 ip->source_ipaddr = my_ipaddr;
105 1
106 1 #ifdef __LITTLEENDIAN__
tcp->flags = ntohs(tcp->flags);
tcp->window = ntohs(tcp->window);
tcp->source_port = ntohs(tcp->source_port);
tcp->sequence = ntohl(tcp->sequence);
tcp->dest_port = ntohs(tcp->dest_port);
tcp->ack_number = ntohl(tcp->ack_number);
ip->source_ipaddr = ntohl(my_ipaddr);
#endif
115 1
116 1 // Sum source_ipaddr, dest_ipaddr, and entire TCP message
C51 COMPILER V7.09 HTTP 07/17/2007 17:36:30 PAGE 3
117 1 sum = (ulong)cksum(outbuf + 26, 8 + len);
118 1
119 1 // Add in the rest of pseudoheader which is
120 1 // protocol id and TCP segment length
121 1 sum += (ulong)0x0006;
122 1 sum += (ulong)len;
123 1
124 1 // In case there was a carry, add it back around
125 1 result = (uint)(sum + (sum >> 16));
126 1 tcp->checksum = ~result;
127 1
128 1 #ifdef __LITTLEENDIAN__
tcp->checksum = ntohs(tcp->checksum);
// ip_send(outbuf, ntohl(conxn[nr].ipaddr), TCP_TYPE, len);
conxn[nr].ipaddr = ntohl(conxn[nr].ipaddr);
#endif
133 1 // if (debug) serial_send("TCP: Sending msg to IP layer\r");
134 1 ip_send(outbuf, conxn[nr].ipaddr, TCP_TYPE, len);
135 1 #ifdef __LITTLEENDIAN__
free(outbuf);
#endif
138 1
139 1
140 1 // (Re)start TCP retransmit timer
141 1 conxn[nr].timer = TCP_TIMEOUT;
142 1 }
143
144
145
146 //------------------------------------------------------------------------
147 // This searches a web page looking for a specified tag. If found,
148 // it replaces the tag with the text in * sub. Tags are fixed length -
149 // The first 4 chars of the tag is always "TAG:" and the rest of it
150 // is always 4 chars for a total of 8 chars.
151 //------------------------------------------------------------------------
152
153
154
155
156 //------------------------------------------------------------------------
157 // This serves up either a HTML page, a JPEG image, or controls an
158 // LED, depending what it gets from the browser. The received header
159 // must contain the word "GET" or "POST" to be considered a valid request.
160 // With HTTP 1.1 where the connection is left open, the header I send
161 // should include content length. With HTTP 1.0 you can just close the
162 // connection after sending the page and the browser knows its done.
163 //
164 // The HTTP protocol specification is at http://www.w3.org/Protocols/
165 //------------------------------------------------------------------------
166 uint http_server(UCHAR * inbuf, uint header_len, UCHAR nr, UCHAR resend, int data_len)
167 {
168 1 UCHAR i;
169 1 uint body_len, hhdr_len, jhdr_len, page_len, jpeg_len;
170 1 uint sent, remaining;
171 1 UCHAR * outbuf;
172 1 UCHAR * ptr;
173 1 UCHAR * tcp_data;
174 1 UCHAR request;
175 1 static UCHAR post_flg = FALSE;
176 1
177 1 // Make sure this is a valid connection
178 1 if (nr == NO_CONNECTION)
C51 COMPILER V7.09 HTTP 07/17/2007 17:36:30 PAGE 4
179 1 return 0;
180 1
181 1 // Compute start of TCP data
182 1
183 1 // Save first 20 chars and seq number just in case
184 1 // we need to re-generate page
185 1 // TODO: if post, then save switch state infomation
186 1
187 1 if (!resend)
188 1 {
189 2 tcp_data = inbuf + 34 + header_len;
190 2 memcpy((char*)conxn[nr].query, tcp_data, 20);
191 2 conxn[nr].old_sequence = conxn[nr].my_sequence;
192 2 }
193 1 // If this is a resend, set sequence number to what it was
194 1 // the last time we sent this
195 1 else
196 1 {
197 2 tcp_data = inbuf;
198 2 conxn[nr].my_sequence = conxn[nr].old_sequence;
199 2 }
200 1
201 1
202 1 // Start off with no request
203 1
204 1 if(data_len>0)
205 1 {
206 2 //printf(tcp_data);
207 2
208 2 jpeg_len = data_len;//6194;
209 2 body_len = jpeg_len;
210 2
211 2 // Free memory holding received message. The message from the
212 2 // browser can be 500+ bytes long so this is a significant
213 2 // chunk out of the available malloc space of 1500 bytes
214 2 if (!resend) {rcve_buf_allocated = FALSE;}
215 2
216 2 // First send the header and enough of the jpeg to make 1000 bytes.
217 2 // The value of 1000 is arbitrary, but must be stay under 1500.
218 2 /* if (body_len < 1000)
219 2 remaining = body_len;
220 2 else
221 2 remaining = 1000; */
222 2 // outbuf = (UCHAR *)malloc(54 + data_len + 1);
223 2 outbuf = TX_BUFF;
224 2 if (outbuf == NULL)
225 2 {
226 3 // if (debug) serial_send("TCP: Oops, out of memory\r");
227 3 return 0;
228 3 }
229 2
230 2 memcpy(outbuf + 54, tcp_data, data_len);
231 2
232 2 http_send(outbuf, 20 + data_len, nr);
233 2
234 2 //#ifdef __LITTLEENDIAN__
235 2
236 2 conxn[nr].my_sequence += data_len;
237 2 //#endif
238 2 }
239 1 return 0;
240 1 // }
C51 COMPILER V7.09 HTTP 07/17/2007 17:36:30 PAGE 5
241 1
242 1 // Return number of bytes sent, not including TCP header
243 1 return(body_len);
244 1 }
*** WARNING C280 IN LINE 168 OF TCP\HTTP.C: 'i': unreferenced local variable
*** WARNING C280 IN LINE 169 OF TCP\HTTP.C: 'hhdr_len': unreferenced local variable
*** WARNING C280 IN LINE 169 OF TCP\HTTP.C: 'jhdr_len': unreferenced local variable
*** WARNING C280 IN LINE 169 OF TCP\HTTP.C: 'page_len': unreferenced local variable
*** WARNING C280 IN LINE 170 OF TCP\HTTP.C: 'sent': unreferenced local variable
*** WARNING C280 IN LINE 170 OF TCP\HTTP.C: 'remaining': unreferenced local variable
*** WARNING C280 IN LINE 172 OF TCP\HTTP.C: 'ptr': unreferenced local variable
*** WARNING C280 IN LINE 174 OF TCP\HTTP.C: 'request': unreferenced local variable
*** WARNING C280 IN LINE 175 OF TCP\HTTP.C: 'post_flg': unreferenced local variable
245
246
247
248
249
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 996 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 2 48
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 9 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?