📄 http1.lst
字号:
C51 COMPILER V7.09 HTTP1 06/28/2007 09:51:20 PAGE 1
C51 COMPILER V7.09, COMPILATION OF MODULE HTTP1
OBJECT MODULE PLACED IN HTTP1.obj
COMPILER INVOKED BY: F:\Keil\C51\BIN\C51.EXE tcp\HTTP1.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\HTTP1.lst) OBJECT(HTTP1
-.obj)
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
13 #include <stdlib.h>
14 #include <ctype.h> // toupper
15 #include "net1.h"
16
17 #include "cksum.h"
18 #include "ip1.h"
19 #include "tcp1.h"
20 #include "http1.h"
21 #include "utils.h"
22
23
24 // These structures keep track of connection information
25 extern CONNECTION conxn[];
26
27 extern ulong my_ipaddr;
28 extern char text[];
29 extern char html_header[];
30 extern char web_page[];
31 extern char jpeg_header[];
32 extern UCHAR photo1_jpeg[];
33 extern UCHAR rcve_buf_allocated;
34 extern UCHAR debug;
35 //bit CONTROL_LED;
36 //void LightONOFF(bit b);
37
38 char cnt = 0;
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 {
C51 COMPILER V7.09 HTTP1 06/28/2007 09:51:20 PAGE 2
55 char *ptr1, *ptr2;
56
57 // Protect against NULL pointer
58 if (*needle == 0) return(haystack);
59 for( ; *haystack; haystack++ )
60 {
61 // Look for needle in haystack. If there is a
62 // match then this will continue all the way
63 // until ptr1 reaches the NULL at the end of needle
64 for(ptr1 = needle, ptr2 = haystack; *ptr1 && (*ptr1 == *ptr2); ++ptr1, ++ptr2);
65
66 // If there is a match then return pointer to needle in haystack
67 if(*ptr1 == 0) return(haystack);
68 }
69 return NULL; // no matching string found
70 }*/
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 * outbuf, uint len, UCHAR nr)
80 {
81 1 TCP_HEADER * tcp;
82 1 IP_HEADER * ip;
83 1 ulong sum;
84 1 uint result;
85 1
86 1 // Fill in TCP segment header
87 1 tcp = (TCP_HEADER *)(outbuf + 34);
88 1 ip = (IP_HEADER *)(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 #ifdef __LITTLEENDIAN__
107 1 tcp->flags = ntohs(tcp->flags);
108 1 tcp->window = ntohs(tcp->window);
109 1 tcp->source_port = ntohs(tcp->source_port);
110 1 tcp->sequence = ntohl(tcp->sequence);
111 1 tcp->dest_port = ntohs(tcp->dest_port);
112 1 tcp->ack_number = ntohl(tcp->ack_number);
113 1 ip->source_ipaddr = ntohl(my_ipaddr);
114 1 #endif
115 1
116 1 // Sum source_ipaddr, dest_ipaddr, and entire TCP message
C51 COMPILER V7.09 HTTP1 06/28/2007 09:51:20 PAGE 3
117 1 sum = (ulong)cksum(outbuf + 26, 8 + len);
118 1
119 1 // Add in the rest of pseudoheader which is
120 1 // protocol id and TCP segment length
121 1 sum += (ulong)0x0006;
122 1 sum += (ulong)len;
123 1
124 1 // In case there was a carry, add it back around
125 1 result = (uint)(sum + (sum >> 16));
126 1 tcp->checksum = ~result;
127 1
128 1 #ifdef __LITTLEENDIAN__
129 1 tcp->checksum = ntohs(tcp->checksum);
130 1 #endif
131 1 // if (debug) serial_send("TCP: Sending msg to IP layer\r");
132 1 ip_send1(outbuf, ntohl(conxn[nr].ipaddr), TCP_TYPE, len);
133 1
134 1 #ifdef __LITTLEENDIAN__
135 1 free(outbuf);
136 1 #endif
137 1
138 1
139 1 // (Re)start TCP retransmit timer
140 1 conxn[nr].timer = TCP_TIMEOUT;
141 1 }
142
143
144
145 //------------------------------------------------------------------------
146 // This searches a web page looking for a specified tag. If found,
147 // it replaces the tag with the text in * sub. Tags are fixed length -
148 // The first 4 chars of the tag is always "TAG:" and the rest of it
149 // is always 4 chars for a total of 8 chars.
150 //------------------------------------------------------------------------
151 void replace_tag(UCHAR * start, char * tag, char * sub)
152 {
153 1 // UCHAR i, flg;
154 1 //UCHAR * ptr;
155 1
156 1 // Find tag. If not found - just return
157 1
158 1 }
*** WARNING C280 IN LINE 151 OF TCP\HTTP1.C: 'start': unreferenced local variable
*** WARNING C280 IN LINE 151 OF TCP\HTTP1.C: 'tag': unreferenced local variable
*** WARNING C280 IN LINE 151 OF TCP\HTTP1.C: 'sub': unreferenced local variable
159
160
161
162 //------------------------------------------------------------------------
163 // This serves up either a HTML page, a JPEG image, or controls an
164 // LED, depending what it gets from the browser. The received header
165 // must contain the word "GET" or "POST" to be considered a valid request.
166 // With HTTP 1.1 where the connection is left open, the header I send
167 // should include content length. With HTTP 1.0 you can just close the
168 // connection after sending the page and the browser knows its done.
169 //
170 // The HTTP protocol specification is at http://www.w3.org/Protocols/
171 //------------------------------------------------------------------------
172 uint http_server(UCHAR * inbuf, uint header_len, UCHAR nr, UCHAR resend, int data_len)
173 {
174 1 UCHAR i;
175 1 uint body_len, hhdr_len, jhdr_len, page_len, jpeg_len;
C51 COMPILER V7.09 HTTP1 06/28/2007 09:51:20 PAGE 4
176 1 uint sent, remaining;
177 1 UCHAR * outbuf;
178 1 UCHAR * ptr;
179 1 UCHAR * tcp_data;
180 1 UCHAR request;
181 1 static UCHAR post_flg = FALSE;
182 1
183 1 // Make sure this is a valid connection
184 1 if (nr == NO_CONNECTION)
185 1 return 0;
186 1
187 1 // Compute start of TCP data
188 1
189 1 // Save first 20 chars and seq number just in case
190 1 // we need to re-generate page
191 1 // TODO: if post, then save switch state infomation
192 1
193 1 if (!resend)
194 1 {
195 2 tcp_data = inbuf + 34 + header_len;
196 2 memcpy((char*)conxn[nr].query, tcp_data, 20);
197 2 conxn[nr].old_sequence = conxn[nr].my_sequence;
198 2 }
199 1 // If this is a resend, set sequence number to what it was
200 1 // the last time we sent this
201 1 else
202 1 {
203 2 tcp_data = inbuf;
204 2 conxn[nr].my_sequence = conxn[nr].old_sequence;
205 2 }
206 1
207 1
208 1 // Start off with no request
209 1 /* request = NONE;
210 1
211 1 // TODO: Calling strstr() on a large buffer takes a lot of time
212 1 // so perhaps we could speed things up by limiting the search
213 1 // range to the portion of the buffer where the item is expected
214 1 // to be found
215 1
216 1 // If it is a POST, then set a flag to start looking for the post
217 1 // data of interest, which is the string "switch=". It may arrive
218 1 // in a later segment (Netscape seems to split up the POST message)
219 1 if (strstr(tcp_data, "POST") != NULL) post_flg = TRUE;
220 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -