📄 http.lst
字号:
161 1 flg = TRUE;
162 1
163 1 // Replace the 8 char tag with the substitute text
164 1 // Pad on the right with spaces
165 1 for (i=0; i < 8; i++)
166 1 {
167 2 if (sub[i] == 0) flg = FALSE;
168 2 if (flg) ptr[i] = sub[i]; else ptr[i] = SPACE;
169 2 }
170 1 }
171
172
173
174 //------------------------------------------------------------------------
175 // This serves up either a HTML page, a JPEG image, or controls an
176 // LED, depending what it gets from the browser. The received header
177 // must contain the word "GET" or "POST" to be considered a valid request.
178 // With HTTP 1.1 where the connection is left open, the header I send
179 // should include content length. With HTTP 1.0 you can just close the
C51 COMPILER V7.06 HTTP 10/09/2006 21:51:55 PAGE 4
180 // connection after sending the page and the browser knows its done.
181 //
182 // The HTTP protocol specification is at http://www.w3.org/Protocols/
183 //------------------------------------------------------------------------
184 UINT http_server(UCHAR xdata * inbuf, UINT header_len, UCHAR nr, UCHAR resend)
185 {
186 1 UINT idata body_len, hhdr_len, page_len;
187 1 UCHAR xdata * outbuf;
188 1 UCHAR xdata * ptr;
189 1 UCHAR xdata * tcp_data;
190 1 UCHAR idata request;
191 1 static UCHAR idata post_flg = FALSE;
192 1
193 1 // Make sure this is a valid connection
194 1 if (nr == NO_CONNECTION) return 0;
195 1
196 1 // Compute start of TCP data
197 1
198 1 // Save first 20 chars and seq number just in case
199 1 // we need to re-generate page
200 1 // TODO: if post, then save switch state infomation
201 1 if (!resend)
202 1 {
203 2 tcp_data = inbuf + 34 + header_len;
204 2 memcpy(conxn[nr].query, tcp_data, 20);
205 2 conxn[nr].old_sequence = conxn[nr].my_sequence;
206 2 }
207 1 // If this is a resend, set sequence number to what it was
208 1 // the last time we sent this
209 1 else
210 1 {
211 2 tcp_data = inbuf;
212 2 conxn[nr].my_sequence = conxn[nr].old_sequence;
213 2 }
214 1
215 1 // Start off with no request
216 1 request = NONE;
217 1
218 1 // TODO: Calling strstr() on a large buffer takes a lot of time
219 1 // so perhaps we could speed things up by limiting the search
220 1 // range to the portion of the buffer where the item is expected
221 1 // to be found
222 1
223 1 // If it is a POST, then set a flag to start looking for the post
224 1 // data of interest, which is the string "switch=". It may arrive
225 1 // in a later segment (Netscape seems to split up the POST message)
226 1 if (strstr(tcp_data, "POST") != NULL) post_flg = TRUE;
227 1
228 1 // See if this is a GET message
229 1 else if (strstr(tcp_data, "GET") != NULL)
230 1 {
231 2 post_flg = FALSE;
232 2 if (strstr(tcp_data, "photo1") != NULL) request = GET_JPEG;
233 2 else if (strstr(tcp_data, "index") != NULL) request = GET_PAGE;
234 2 else if (strstr(tcp_data, "/ ") != NULL) request = GET_PAGE;
235 2 }
236 1
237 1 // If POST flag is "armed" then look for the "switch=" string
238 1 // and if found, turn the LED on or off according to whether
239 1 // the browser is sending a 1 or a 0.
240 1 if (post_flg)
241 1 {
C51 COMPILER V7.06 HTTP 10/09/2006 21:51:55 PAGE 5
242 2 ptr = strstr(tcp_data, "switch=");
243 2 if (ptr != NULL)
244 2 {
245 3 // Move to space after equals sign
246 3 // Set control indicator accordingly
247 3 post_flg = FALSE;
248 3 request = POST_PAGE;
249 3 ptr += 7;
250 3 if (*ptr == '1') {CONTROL_LED=0x0;}
251 3 else if (*ptr == '0') {CONTROL_LED=0x1;}
252 3 LightONOFF(CONTROL_LED);
253 3 }
254 2 }
255 1
256 1 if ((request == GET_PAGE) || (request == POST_PAGE))
257 1 {
258 2 // Figure out sizes
259 2 hhdr_len = strlen(html_header);
260 2 page_len = strlen(web_page);
261 2 body_len = hhdr_len + page_len;
262 2
263 2 // Free memory holding received message. The message from the
264 2 // browser can be 500+ bytes long so this is a significant
265 2 // chunk out of the available malloc space of 1500 bytes
266 2 if (!resend) {free(inbuf); rcve_buf_allocated = FALSE;}
267 2
268 2 // Allocate memory for entire outgoing message including
269 2 // 14 byte Ethernet + 20 byte IP + 20 byte TCP headers
270 2 // Allow 1 byte for NULL char at the end
271 2 outbuf = (UCHAR xdata *)malloc(54 + body_len + 1);
272 2 if (outbuf == NULL)
273 2 {
274 3
275 3 return 0;
276 3 }
277 2
278 2 // Copy page data. This moves data from flash into RAM. It is
279 2 // an undesirable process, but must get data into RAM to replace
280 2 // tags
281 2 memcpy(outbuf + 54, html_header, hhdr_len);
282 2 memcpy(outbuf + 54 + hhdr_len, web_page, page_len);
283 2
284 2 outbuf[54 + body_len] = 0; // Append NULL
285 2
286 2 // Replace length tag with actual value
287 2 itoa(page_len, text, 10);
288 2 replace_tag(outbuf + 54, "TAG:LEN1", text);
289 2
290 2 // Insert the word CHECKED into the html so the browser will
291 2 // check the correct LED state indicator box
292 2 if (CONTROL_LED == OFF) replace_tag(outbuf + 54, "TAG:CHK1", "CHECKED");
293 2 else replace_tag(outbuf + 54, "TAG:CHK2", "CHECKED");
294 2
295 2 // Segment length = body_len + 20
296 2 http_send(outbuf, body_len + 20, nr);
297 2 conxn[nr].my_sequence += body_len;
298 2 }
299 1 else
300 1 {
301 2 // The incoming HTTP message did not warrant a response
302 2 return 0;
303 2 }
C51 COMPILER V7.06 HTTP 10/09/2006 21:51:55 PAGE 6
304 1
305 1 // Return number of bytes sent, not including TCP header
306 1 return(body_len);
307 1 }
308
309
310
311
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1652 ----
CONSTANT SIZE = 68 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 53
IDATA SIZE = 1 15
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -