📄 http.lst
字号:
C51 COMPILER V7.06 HTTP 10/09/2006 21:51:55 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE HTTP
OBJECT MODULE PLACED IN http.OBJ
COMPILER INVOKED BY: D:\Program Files\Keil\C51\BIN\C51.EXE http.c BROWSE DEBUG OBJECTEXTEND
stmt 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 <reg51.h>
13 #include <net.h>
14 #include <cksum.h>
15 #include <ip.h>
16 #include <tcp.h>
17 #include <http.h>
18
19
20 // These structures keep track of connection information
21 extern CONNECTION xdata conxn[];
22
23 extern ULONG code my_ipaddr;
24 extern char xdata text[];
25 extern char code html_header[];
26 extern char code web_page[];
27 extern UCHAR idata rcve_buf_allocated;
28 bit CONTROL_LED;
29 void LightONOFF(bit b);
30
31 void init_http(void)
32 {
33 1 CONTROL_LED = 0;
34 1 LightONOFF(CONTROL_LED);
35 1 }
36
37 //------------------------------------------------------------------------
38 // This function converts an integer to an ASCII string. It is a
39 // normally provided as a standard library function but the Keil
40 // libraries do not include it. Caution: The string passed to this
41 // must be at least 12 bytes long
42 //------------------------------------------------------------------------
43 char * itoa(UINT value, char * buf, UCHAR radix)
44 {
45 1 UINT i;
46 1 char * ptr;
47 1 char * temphold;
48 1
49 1 temphold = buf;
50 1 ptr = buf + 12;
51 1 *--ptr = 0; // Insert NULL char
52 1 do
53 1 {
54 2 // First create string in reverse order
55 2 i = (value % radix) + 0x30;
C51 COMPILER V7.06 HTTP 10/09/2006 21:51:55 PAGE 2
56 2 if(i > 0x39) i += 7;
57 2 *--ptr = i;
58 2 value = value / radix;
59 2 } while(value != 0);
60 1
61 1 // Next, move the string 6 places to the left
62 1 // Include NULL character
63 1 for( ; (*buf++ = *ptr++); );
64 1 return(temphold);
65 1 }
66
67
68 //------------------------------------------------------------------------
69 // This function is the standard string search. The Keil library
70 // does not provide it. It looks for one string in another string
71 // and returns a pointer to it if found, otherwise returns NULL.
72 //------------------------------------------------------------------------
73 char * strstr(char * haystack, char * needle)
74 {
75 1 char *ptr1, *ptr2;
76 1
77 1 // Protect against NULL pointer
78 1 if (*needle == 0) return(haystack);
79 1 for( ; *haystack; haystack++ )
80 1 {
81 2 // Look for needle in haystack. If there is a
82 2 // match then this will continue all the way
83 2 // until ptr1 reaches the NULL at the end of needle
84 2 for(ptr1 = needle, ptr2 = haystack; *ptr1 && (*ptr1 == *ptr2); ++ptr1, ++ptr2);
85 2
86 2 // If there is a match then return pointer to needle in haystack
87 2 if(*ptr1 == 0) return(haystack);
88 2 }
89 1 return NULL; // no matching string found
90 1 }
91
92
93
94 //------------------------------------------------------------------------
95 // This sends an TCP segment to the ip layer. The segment is
96 // is normally either a web page or a graphic.
97 // See "TCP/IP Illustrated, Volume 1" Sect 17.3
98 //------------------------------------------------------------------------
99 void http_send(UCHAR xdata * outbuf, UINT len, UCHAR nr)
100 {
101 1 TCP_HEADER xdata * tcp;
102 1 IP_HEADER xdata * ip;
103 1 ULONG idata sum;
104 1 UINT idata result;
105 1
106 1 // Fill in TCP segment header
107 1 tcp = (TCP_HEADER xdata *)(outbuf + 34);
108 1 ip = (IP_HEADER xdata *)(outbuf + 14);
109 1
110 1 tcp->source_port = HTTP_PORT;
111 1 tcp->dest_port = conxn[nr].port;
112 1 tcp->sequence = conxn[nr].my_sequence;
113 1 tcp->ack_number = conxn[nr].his_sequence;
114 1
115 1 // Header is always 20 bytes long
116 1 tcp->flags = 0x5000 | FLG_ACK | FLG_PSH;
117 1 tcp->window = 1024;
C51 COMPILER V7.06 HTTP 10/09/2006 21:51:55 PAGE 3
118 1 tcp->checksum = 0;
119 1 tcp->urgent_ptr = 0;
120 1
121 1 // Compute checksum including 12 bytes of pseudoheader
122 1 // Must pre-fill 2 items in ip header to do this
123 1 ip->dest_ipaddr = conxn[nr].ipaddr;
124 1 ip->source_ipaddr = my_ipaddr;
125 1
126 1 // Sum source_ipaddr, dest_ipaddr, and entire TCP message
127 1 sum = (ULONG)cksum(outbuf + 26, 8 + len);
128 1
129 1 // Add in the rest of pseudoheader which is
130 1 // protocol id and TCP segment length
131 1 sum += (ULONG)0x0006;
132 1 sum += (ULONG)len;
133 1
134 1 // In case there was a carry, add it back around
135 1 result = (UINT)(sum + (sum >> 16));
136 1 tcp->checksum = ~result;
137 1
138 1 ip_send(outbuf, conxn[nr].ipaddr, TCP_TYPE, len);
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 void replace_tag(UCHAR xdata * start, char * tag, char * sub)
153 {
154 1 UCHAR idata i, flg;
155 1 UCHAR xdata * ptr;
156 1
157 1 // Find tag. If not found - just return
158 1 ptr = strstr(start, tag);
159 1 if (ptr == NULL) return;
160 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -