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