📄 http.lst
字号:
C51 COMPILER V8.18 HTTP 04/26/2010 19:38:05 PAGE 1
C51 COMPILER V8.18, COMPILATION OF MODULE HTTP
OBJECT MODULE PLACED IN http.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE http.c LARGE BROWSE DEBUG OBJECTEXTEND
line level source
1 //-----------------------------------------------------------------------------
2 // Net HTTP.C
3 //
4 // This module is the Web Server
5 // It currently serves a html text page and a jpeg image, or handles
6 // a POST message to turn an LED on or off.
7 // The HTTP protocol specification is at http://www.w3.org/Protocols/
8 //-----------------------------------------------------------------------------
9 #include <string.h>
10 #include <stdlib.h>
11 #include <ctype.h> // toupper
12 #include "main.h" // toupper
13 #include "rs232.h" // toupper
14 #include "tcp.h" // toupper
15 #include "http.h" // toupper
16 #include "ip.h" // toupper
17 #include "WebData.h"
18 // These structures keep track of connection information
19
20 bit CONTROL_LED;
21
22 void LightONOFF(bit b);
23
24 /*********************************************************************
25 函数名: void ResetSystem(void)
26 功能: 复位系统
27 输入: None
28 输出: None
29 返回: TRUE or FALSE
30 日期: 2004/02/09
31 *********************************************************************/
32
33 void LightONOFF(bit b)
34 {
35 1 if (b)
36 1 {
37 2 TEST_LED = 0;
38 2 }
39 1 else
40 1 {
41 2 TEST_LED = 1;
42 2 }
43 1 }
44 /*********************************************************************
45 函数名: void ResetSystem(void)
46 功能: 复位系统
47 输入: None
48 输出: None
49 返回: TRUE or FALSE
50 日期: 2004/02/09
51 *********************************************************************/
52 void init_http(void)
53 {
54 1 CONTROL_LED = 0;
55 1 LightONOFF(CONTROL_LED);
C51 COMPILER V8.18 HTTP 04/26/2010 19:38:05 PAGE 2
56 1 }
57 /*********************************************************************
58 函数名: UWORK8 FlashCheck(void)
59 功能: 把整形转成ASCII的字符串
60 输入: none
61 输出: None
62 返回: none
63 日期: 2005/11/19
64 注意:最少为12个字节
65 *********************************************************************/
66 char *itoa(UWORK16 value, char * buf, UWORK8 radix)
67 {
68 1 UWORK16 i;
69 1 char * ptr;
70 1 char * temphold;
71 1
72 1 temphold = buf;
73 1 ptr = buf + 12;
74 1 *--ptr = 0; // Insert NULL char
75 1 do
76 1 {
77 2 i = (value % radix) + 0x30; // First create string in reverse order
78 2 if(i > 0x39) i += 7;
79 2 *--ptr = i;
80 2 value = value / radix;
81 2 } while(value != 0);
82 1 for( ; (*buf++ = *ptr++); ); // Next, move the string 6 places to the left Include NULL character
83 1 return(temphold);
84 1 }
85
86
87 //------------------------------------------------------------------------
88 // This function is the standard string search. The Keil library
89 // does not provide it. It looks for one string in another string
90 // and returns a pointer to it if found, otherwise returns NULL.
91 //------------------------------------------------------------------------
92 char * strstr(char * haystack, char * needle)
93 {
94 1 char *ptr1, *ptr2;
95 1
96 1 // Protect against NULL pointer
97 1 if (*needle == 0) return(haystack);
98 1 for( ; *haystack; haystack++ )
99 1 {
100 2 // Look for needle in haystack. If there is a
101 2 // match then this will continue all the way
102 2 // until ptr1 reaches the NULL at the end of needle
103 2 for(ptr1 = needle, ptr2 = haystack; *ptr1 && (*ptr1 == *ptr2); ++ptr1, ++ptr2);
104 2
105 2 // If there is a match then return pointer to needle in haystack
106 2 if(*ptr1 == 0) return(haystack);
107 2 }
108 1 return NULL; // no matching string found
109 1 }
110
111
112
113 //------------------------------------------------------------------------
114 // This sends an TCP segment to the ip layer. The segment is
115 // is normally either a web page or a graphic.
116 // See "TCP/IP Illustrated, Volume 1" Sect 17.3
117 //------------------------------------------------------------------------
C51 COMPILER V8.18 HTTP 04/26/2010 19:38:05 PAGE 3
118
119 void http_send(UWORK16 flags, UWORK8 *TData, UWORK16 TLen, UWORK8 nr)
120 {
121 1 ST_TCP_FORMAT *strTcp;
122 1 UWORK8 destIP_buf[4];
123 1 UWORK16 TCPHead_len;
124 1
125 1 UWORK8 *TempD;
126 1 TempD = TData;
127 1 TCPHead_len = 20;
128 1 strTcp = (ST_TCP_FORMAT *)&NetSend_buf[20];
129 1 strTcp->usSourcePort = SourcePort; //源端口
130 1 strTcp->usDesPort = StrConnection_buf[nr].usDesPort; //目标端口,在接收TCP时已附值
131 1
132 1
133 1 strTcp->ucSERIESNUM = StrConnection_buf[nr].ucSERIESNUM; //序号为0
134 1 strTcp->ucTRUECODE = StrConnection_buf[nr].ucTRUECODE; //确认号为0
135 1
136 1 memcpy(destIP_buf,(UWORK8 *)&StrConnection_buf[nr].ipaddr, 4); //发送的目标IP地址在TCP接收中已附值
137 1
138 1 strTcp->ucMOVENUM = (TCPHead_len << 10) | flags; //记算TCP的头长度及相关的标志位
139 1 strTcp->ucWINDOWBIG = 1024; //设置窗口大小
140 1 strTcp->ucTCPCHECK = 0; //先设置校验位为0
141 1 strTcp->ucMUSGPOINT = 0; //紧急指针为0
142 1
143 1 memcpy((UWORK8 *)&NetSend_buf[(20 + TCPHead_len)], TData, TLen); //把发送的数据传到发送缓存中
144 1 //下面是给IP打包头
145 1 gstIphead.ucVerAndLen = 0x45; //版本号和长度,各占4位
146 1 gstIphead.ucDs = 0; //区分服务
147 1 gstIphead.usTotalLen = IP_HEAD_LEN + TCPHead_len + TLen; //头加后面的数据
148 1 gstIphead.usID = ++LocalIpID;
149 1 gstIphead.usSegOffset = 0;
150 1
151 1 gstIphead.ucTTL = 0; // max hops
152 1 gstIphead.ucprotocol = TCP;
153 1 gstIphead.usCheckSum = TCPHead_len + TLen; //在计算TCP的校验位时,TC
-P的长度要算两次,此时是借IP的校验位来做第二个TCP的长度
154 1 memcpy(&gstIphead.ucDestIP[0],&IPDestAddress_buf[0],IP_LEN); // 目的IP
155 1 memcpy(&gstIphead.ucSourceIP[0],&IPLocalAddress_buf[0],IP_LEN); // 源IP
156 1 memcpy(&NetSend_buf[0],&gstIphead,20); //把IP的头传给发送缓存区
157 1 strTcp->ucTCPCHECK = 0;
158 1 strTcp->ucTCPCHECK = CheckSum((UWORK16 *)&NetSend_buf[8],TCPHead_len + TLen + 12); // 20 = 12个字节伪头
- + 8个字节UDP头
159 1
160 1 gstIphead.ucTTL = 0x20; //校验和计算完毕,重赋TTL值
161 1 memcpy(&MAC_Remote_buf[0],&gstaRevEthernet.ucaSourceNodID[0],MAC_LEN); // 目的MAC
162 1 // memcpy((UWORK8 *)&MAC_Remote_buf[4],&gstaRevEthernet.ucaDestNodID[0],MAC_LEN); // 目的MAC
163 1 // memcpy(&MAC_Remote_buf[0],&ArpCache_Buf[0].ucaMAC[0],MAC_LEN); // 远程MAC
164 1 IpSend();
165 1 // (Re)start TCP retransmit timer
166 1 StrConnection_buf[nr].timer = TCP_TIMEOUT;
167 1 StrConnection_buf[nr].ucRESERIESNUM = StrConnection_buf[nr].ucSERIESNUM;
168 1 }
169
170
171
172 //------------------------------------------------------------------------
173 // This searches a web page looking for a specified tag. If found,
174 // it replaces the tag with the text in * sub. Tags are fixed length -
175 // The first 4 chars of the tag is always "TAG:" and the rest of it
176 // is always 4 chars for a total of 8 chars.
177 //------------------------------------------------------------------------
C51 COMPILER V8.18 HTTP 04/26/2010 19:38:05 PAGE 4
178 void replace_tag(UWORK8 xdata * start, char * tag, char * sub)
179 {
180 1 UWORK8 idata i, flg;
181 1 UWORK8 xdata * ptr;
182 1
183 1 // Find tag. If not found - just return
184 1 ptr = strstr(start, tag);
185 1 if (ptr == NULL) return;
186 1
187 1 flg = TRUE;
188 1
189 1 // Replace the 8 char tag with the substitute text
190 1 // Pad on the right with spaces
191 1 for (i=0; i < 8; i++)
192 1 {
193 2 if (sub[i] == 0) flg = FALSE;
194 2 if (flg) ptr[i] = sub[i]; else ptr[i] = SPACE;
195 2 }
196 1 }
197
198
199
200 //------------------------------------------------------------------------
201 // This serves up either a HTML page, a JPEG image, or controls an
202 // LED, depending what it gets from the browser. The received header
203 // must contain the word "GET" or "POST" to be considered a valid request.
204 // With HTTP 1.1 where the connection is left open, the header I send
205 // should include content length. With HTTP 1.0 you can just close the
206 // connection after sending the page and the browser knows its done.
207 //
208 // The HTTP protocol specification is at http://www.w3.org/Protocols/
209 //------------------------------------------------------------------------
210 //------------------------------------------------------------------------
211 // This serves up either a HTML page, a JPEG image, or controls an
212 // LED, depending what it gets from the browser. The received header
213 // must contain the word "GET" or "POST" to be considered a valid request.
214 // With HTTP 1.1 where the connection is left open, the header I send
215 // should include content length. With HTTP 1.0 you can just close the
216 // connection after sending the page and the browser knows its done.
217 //
218 // The HTTP protocol specification is at http://www.w3.org/Protocols/
219 //------------------------------------------------------------------------
220 UWORK16 http_server(UWORK8 nr, UWORK8 reSendFlag)
221 {
222 1 UWORK16 idata body_len, hhdr_len, jhdr_len, page_len, jpeg_len;
223 1 UWORK16 idata sent, remaining;
224 1 UWORK8 xdata * outbuf;
225 1 UWORK8 xdata * ptr;
226 1 UWORK8 xdata * tcp_data;
227 1 UWORK8 idata request;
228 1 UWORK16 TCPheader_len, data_len, Total_len;
229 1 ST_IP_HEAD_FORMAT StrIp;
230 1 ST_TCP_FORMAT strTcp;
231 1 static UWORK8 idata post_flg = FALSE;
232 1
233 1 if(FALSE == gbHaveFragment)
234 1 { // 没有分片的数据
235 2 memcpy((UWORK8 *)&StrIp.ucVerAndLen, (UWORK8 *)&gstaRevEthernet.ucaPacket[0], 20); //读取IP头数据
236 2 memcpy((UWORK8 *)&strTcp.usSourcePort, (UWORK8 *)&gstaRevEthernet.ucaPacket[20], 20); //读取TCP头
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -