📄 http.lst
字号:
C51 COMPILER V7.06 HTTP 03/01/2004 10:51:53 PAGE 1
C51 COMPILER V7.06, 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
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 <stdio.h>
12 #include <ctype.h> // toupper
13 #include <reg52.h>
14 #include "net.h"
15 //#include "serial.h"
16 #include "cksum.h"
17 #include "analog.h"
18 #include "ip.h"
19 #include "tcp.h"
20 #include "http.h"
21
22
23 // These structures keep track of connection information
24 extern CONNECTION xdata conxn[];
25
26 extern ULONG code my_ipaddr;
27 extern char xdata text[];
28 extern UINT idata cpu_temperature;
29 extern UINT idata air_temperature;
30 extern UINT idata cpu_voltage;
31 extern char code html_header[];
32 extern char code web_page[];
33 extern char code jpeg_header[];
34 extern UCHAR code photo1_jpeg[];
35 extern UCHAR idata rcve_buf_allocated;
36 extern UCHAR idata debug;
37 bit CONTROL_LED;
38 void LightONOFF(bit b);
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 {
55 1 char *ptr1, *ptr2;
C51 COMPILER V7.06 HTTP 03/01/2004 10:51:53 PAGE 2
56 1
57 1 // Protect against NULL pointer
58 1 if (*needle == 0) return(haystack);
59 1 for( ; *haystack; haystack++ )
60 1 {
61 2 // Look for needle in haystack. If there is a
62 2 // match then this will continue all the way
63 2 // until ptr1 reaches the NULL at the end of needle
64 2 for(ptr1 = needle, ptr2 = haystack; *ptr1 && (*ptr1 == *ptr2); ++ptr1, ++ptr2);
65 2
66 2 // If there is a match then return pointer to needle in haystack
67 2 if(*ptr1 == 0) return(haystack);
68 2 }
69 1 return NULL; // no matching string found
70 1 }
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 xdata * outbuf, UINT len, UCHAR nr)
80 {
81 1 TCP_HEADER xdata * tcp;
82 1 IP_HEADER xdata * ip;
83 1 ULONG idata sum;
84 1 UINT idata result;
85 1
86 1 // Fill in TCP segment header
87 1 tcp = (TCP_HEADER xdata *)(outbuf + 34);
88 1 ip = (IP_HEADER xdata *)(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 // Sum source_ipaddr, dest_ipaddr, and entire TCP message
107 1 sum = (ULONG)cksum(outbuf + 26, 8 + len);
108 1
109 1 // Add in the rest of pseudoheader which is
110 1 // protocol id and TCP segment length
111 1 sum += (ULONG)0x0006;
112 1 sum += (ULONG)len;
113 1
114 1 // In case there was a carry, add it back around
115 1 result = (UINT)(sum + (sum >> 16));
116 1 tcp->checksum = ~result;
117 1
C51 COMPILER V7.06 HTTP 03/01/2004 10:51:53 PAGE 3
118 1 // if (debug) printf("TCP: Sending msg to IP layer\n");
119 1 ip_send(outbuf, conxn[nr].ipaddr, TCP_TYPE, len);
120 1
121 1 // (Re)start TCP retransmit timer
122 1 conxn[nr].timer = TCP_TIMEOUT;
123 1 }
124
125
126
127 //------------------------------------------------------------------------
128 // This searches a web page looking for a specified tag. If found,
129 // it replaces the tag with the text in * sub. Tags are fixed length -
130 // The first 4 chars of the tag is always "TAG:" and the rest of it
131 // is always 4 chars for a total of 8 chars.
132 //------------------------------------------------------------------------
133 void replace_tag(UCHAR xdata * start, char * tag, char * sub)
134 {
135 1 UCHAR idata i, flg;
136 1 UCHAR xdata * ptr;
137 1
138 1 // Find tag. If not found - just return
139 1 ptr = strstr(start, tag);
140 1 if (ptr == NULL) return;
141 1
142 1 flg = TRUE;
143 1
144 1 // Replace the 8 char tag with the substitute text
145 1 // Pad on the right with spaces
146 1 for (i=0; i < 8; i++)
147 1 {
148 2 if (sub[i] == 0) flg = FALSE;
149 2 if (flg) ptr[i] = sub[i]; else ptr[i] = SPACE;
150 2 }
151 1 }
152
153
154
155 //------------------------------------------------------------------------
156 // This serves up either a HTML page, a JPEG image, or controls an
157 // LED, depending what it gets from the browser. The received header
158 // must contain the word "GET" or "POST" to be considered a valid request.
159 // With HTTP 1.1 where the connection is left open, the header I send
160 // should include content length. With HTTP 1.0 you can just close the
161 // connection after sending the page and the browser knows its done.
162 //
163 // The HTTP protocol specification is at http://www.w3.org/Protocols/
164 //------------------------------------------------------------------------
165 UINT http_server(UCHAR xdata * inbuf, UINT header_len, UCHAR nr, UCHAR resend)
166 {
167 1 UCHAR i;
168 1 UINT idata body_len, hhdr_len, jhdr_len, page_len, jpeg_len;
169 1 UINT idata sent, remaining;
170 1 UCHAR xdata * outbuf;
171 1 UCHAR xdata * ptr;
172 1 UCHAR xdata * tcp_data;
173 1 UCHAR idata request;
174 1 static UCHAR idata post_flg = FALSE;
175 1
176 1 // Make sure this is a valid connection
177 1 if (nr == NO_CONNECTION) return 0;
178 1
179 1 // Compute start of TCP data
C51 COMPILER V7.06 HTTP 03/01/2004 10:51:53 PAGE 4
180 1
181 1 // Save first 20 chars and seq number just in case
182 1 // we need to re-generate page
183 1 // TODO: if post, then save switch state infomation
184 1 if (!resend)
185 1 {
186 2 tcp_data = inbuf + 34 + header_len;
187 2 memcpy(conxn[nr].query, tcp_data, 20);
188 2 conxn[nr].old_sequence = conxn[nr].my_sequence;
189 2 }
190 1 // If this is a resend, set sequence number to what it was
191 1 // the last time we sent this
192 1 else
193 1 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -