📄 http.lst
字号:
C51 COMPILER V8.08 HTTP 11/04/2008 18:45:34 PAGE 1
C51 COMPILER V8.08, COMPILATION OF MODULE HTTP
OBJECT MODULE PLACED IN Http.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.exe Http.c DB OE
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 #include <string.h>
13 #include "global.h"
14 //#include <stdlib.h>
15 #include <ctype.h> // toupper
16 #include "C8051f340.h"
17 ///#include "net.h"
18 #include "cksum.h"
19 #include "analog.h"
20 #include "ip.h"
21 #include "tcp.h"
22 #include "http.h"
23
24
25 // These structures keep track of connection information
26 extern CONNECTION xdata conxn[];
27
28 extern ULONG code my_ipaddr;
29 extern char xdata text[];
30 extern UINT idata cpu_temperature;
31 extern UINT idata air_temperature;
32 extern UINT idata cpu_voltage;
33 extern char code html_header[];
34 extern char code web_page[];
35 extern char code jpeg_header[];
36 extern UCHAR code photo1_jpeg[];
37 extern UCHAR idata rcve_buf_allocated;
38 extern UCHAR idata debug;
39 bit CONTROL_LED;
40 void LightONOFF(bit b);
41 extern char xdata outbuf1[];
42
43 void init_http(void)
44 {
45 1 CONTROL_LED = 0;
46 1 LightONOFF(CONTROL_LED);
47 1 }
48
49 char * itoa(UINT value, char * buf, UCHAR radix) //整型数据转换为字符型数据
50 {
51 1 UINT i;
52 1 char * ptr;
53 1 char * temphold;
54 1
55 1 temphold = buf;
C51 COMPILER V8.08 HTTP 11/04/2008 18:45:34 PAGE 2
56 1 ptr = buf + 12;
57 1 *--ptr = 0; // Insert NULL char
58 1 do
59 1 {
60 2 // First create string in reverse order
61 2 i = (value % radix) + 0x30;
62 2 if(i > 0x39) i += 7;
63 2 *--ptr = i;
64 2 value = value / radix;
65 2 } while(value != 0);
66 1
67 1 // Next, move the string 6 places to the left
68 1 // Include NULL character
69 1 for( ; (*buf++ = *ptr++); );
70 1 return(temphold);
71 1 }
72
73 //------------------------------------------------------------------------
74 // This function is the standard string search. The Keil library
75 // does not provide it. It looks for one string in another string
76 // and returns a pointer to it if found, otherwise returns NULL.
77 //------------------------------------------------------------------------
78 char * strstr(char * haystack, char * needle)
79 {
80 1 char *ptr1, *ptr2;
81 1
82 1 // Protect against NULL pointer
83 1 if (*needle == 0) return(haystack);
84 1 for( ; *haystack; haystack++ )
85 1 {
86 2 // Look for needle in haystack. If there is a
87 2 // match then this will continue all the way
88 2 // until ptr1 reaches the NULL at the end of needle
89 2 for(ptr1 = needle, ptr2 = haystack; *ptr1 && (*ptr1 == *ptr2); ++ptr1, ++ptr2);
90 2
91 2 // If there is a match then return pointer to needle in haystack
92 2 if(*ptr1 == 0) return(haystack);
93 2 }
94 1 return NULL; // no matching string found
95 1 }
96
97
98
99 //------------------------------------------------------------------------
100 // This sends an TCP segment to the ip layer. The segment is
101 // is normally either a web page or a graphic.
102 // See "TCP/IP Illustrated, Volume 1" Sect 17.3
103 //------------------------------------------------------------------------
104 void http_send(UCHAR xdata * outbuf, UINT len, UCHAR nr)
105 {
106 1 TCP_HEADER xdata * tcp;
107 1 IP_HEADER xdata * ip;
108 1 ULONG idata sum;
109 1 UINT idata result;
110 1
111 1 // Fill in TCP segment header
112 1 tcp = (TCP_HEADER xdata *)(outbuf + 34);
113 1 ip = (IP_HEADER xdata *)(outbuf + 14);
114 1
115 1 tcp->source_port = HTTP_PORT;
116 1 tcp->dest_port = conxn[nr].port;
117 1 tcp->sequence = conxn[nr].my_sequence;
C51 COMPILER V8.08 HTTP 11/04/2008 18:45:34 PAGE 3
118 1 tcp->ack_number = conxn[nr].his_sequence;
119 1
120 1 // Header is always 20 bytes long
121 1 tcp->flags = 0x5000 | FLG_ACK | FLG_PSH;
122 1 tcp->window = 1024;
123 1 tcp->checksum = 0;
124 1 tcp->urgent_ptr = 0;
125 1
126 1 // Compute checksum including 12 bytes of pseudoheader
127 1 // Must pre-fill 2 items in ip header to do this
128 1 ip->dest_ipaddr = conxn[nr].ipaddr;
129 1 ip->source_ipaddr = my_ipaddr;
130 1
131 1 // Sum source_ipaddr, dest_ipaddr, and entire TCP message
132 1 sum = (ULONG)cksum(outbuf + 26, 8 + len);
133 1
134 1 // Add in the rest of pseudoheader which is
135 1 // protocol id and TCP segment length
136 1 sum += (ULONG)0x0006;
137 1 sum += (ULONG)len;
138 1
139 1 // In case there was a carry, add it back around
140 1 result = (UINT)(sum + (sum >> 16));
141 1 tcp->checksum = ~result;
142 1
143 1 ip_send(outbuf, conxn[nr].ipaddr, TCP_TYPE, len);
144 1
145 1 // (Re)start TCP retransmit timer
146 1 conxn[nr].timer = TCP_TIMEOUT;
147 1 }
148
149
150
151 //------------------------------------------------------------------------
152 // This searches a web page looking for a specified tag. If found,
153 // it replaces the tag with the text in * sub. Tags are fixed length -
154 // The first 4 chars of the tag is always "TAG:" and the rest of it
155 // is always 4 chars for a total of 8 chars.
156 //------------------------------------------------------------------------
157 void replace_tag(UCHAR xdata * start, char * tag, char * sub)
158 {
159 1 UCHAR idata i, flg;
160 1 UCHAR xdata * ptr;
161 1
162 1 // Find tag. If not found - just return
163 1 ptr = strstr(start, tag);
164 1 if (ptr == NULL) return;
165 1
166 1 flg = TRUE;
167 1
168 1 // Replace the 8 char tag with the substitute text
169 1 // Pad on the right with spaces
170 1 for (i=0; i < 8; i++)
171 1 {
172 2 if (sub[i] == 0) flg = FALSE;
173 2 if (flg) ptr[i] = sub[i]; else ptr[i] = SPACE;
174 2 }
175 1 }
176
177
178
179 //------------------------------------------------------------------------
C51 COMPILER V8.08 HTTP 11/04/2008 18:45:34 PAGE 4
180 // This serves up either a HTML page, a JPEG image, or controls an
181 // LED, depending what it gets from the browser. The received header
182 // must contain the word "GET" or "POST" to be considered a valid request.
183 // With HTTP 1.1 where the connection is left open, the header I send
184 // should include content length. With HTTP 1.0 you can just close the
185 // connection after sending the page and the browser knows its done.
186 //
187 // The HTTP protocol specification is at http://www.w3.org/Protocols/
188 //------------------------------------------------------------------------
189 UINT http_server(UCHAR xdata * inbuf, UINT header_len, UCHAR nr, UCHAR resend)
190 {
191 1 UCHAR i;
192 1 UINT idata body_len, hhdr_len, jhdr_len, page_len, jpeg_len;
193 1 UINT idata sent, remaining;
194 1 UCHAR xdata * outbuf;
195 1 UCHAR xdata * ptr;
196 1 UCHAR xdata * tcp_data;
197 1 UCHAR idata request;
198 1 static UCHAR idata post_flg = FALSE;
199 1
200 1 // Make sure this is a valid connection
201 1 if (nr == NO_CONNECTION) return 0;
202 1
203 1 // Compute start of TCP data
204 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -