📄 webserve.lst
字号:
212 /* *
213 /* Arguments : *
214 /* TSOCK *ts:Point to socket. *
215 /* *
216 /* Return : *
217 /* Return 0 to force close current socket *
218 /* Comment : *
219 /* If there is space in the transmit buffer, send HTTP data. *
220 /* *
221 /************************************************************************/
222 char http_data(TSOCK *ts)
223 {
224 1 APPDATA *adp;
225 1 int len,n,buf_length,i_tmp;
226 1 unsigned char flag;
227 1 char *start, *end,tmp;
228 1
229 1 adp = (APPDATA *)ts->app;
230 1
231 1 /* if no file specified yet, just return*/
232 1 if (adp->file_length && (len = buff_freelen(&ts->txb)) >= EGI_BUFFLEN)
233 1 {
234 2 if (adp->egi) /* If EGI is active.. */
235 2 {
236 3
237 3 /* move as much data into the egibuff as possible*/
238 3 buf_length = mini (adp->file_length - adp->count, EGI_BUFFLEN);
239 3 vfat_mem_cpy(egibuff, &adp->in[adp->count], buf_length);
240 3 // adp->count += buf_length;
C51 COMPILER V7.50 WEBSERVE 10/12/2006 15:31:42 PAGE 5
241 3 egibuff[buf_length] = 0;
242 3 if (buf_length <= 0)
243 3 { /* If end of file, close it.. */
244 4 adp->in = 0;
245 4 adp->file_length = 0;
246 4 adp->count = 0;
247 4 return 0;/*..and start closing connection */
248 4 }
249 3 else
250 3 {
251 4 flag=1;
252 4 len=0;
253 4 while (flag)
254 4 {
255 5 /* Check for start of EGI tag */
256 5 if ((start = strstr(&egibuff[len], EGI_STARTS)) == 0)
257 5 {
258 6 start = strchr(&egibuff[buf_length-EGI_STARTLEN], '<');
259 6 flag=0;
260 6 }
261 5 else //Have Egi tag
262 5 flag=1;
263 5 if (start==&egibuff[len] && (end=strstr(&egibuff[len], EGI_ENDS))!=0)
264 5 { /* If tag is at start of buffer.. */
265 6 n = (int)(end - start) + EGI_ENDSLEN - 1;
266 6 adp->count += n;
267 6 i_tmp=adp->count;
268 6 tmp=egibuff[len+n];
269 6 egibuff[len+n] = 0; /* ..call handler */
270 6 adp->egi(ts, &egibuff[len]);
271 6 egibuff[len+n] = tmp;
272 6 len += n;
273 6 if (adp->count!=i_tmp)
274 6 return 1;
275 6 }
276 5 else if (start)/* If tag not at start of buffer.. */
277 5 { /* ..send file up to tag */
278 6 n = (int)(start - &egibuff[len]);
279 6 if (n==0)
280 6 break;
281 6 i_tmp=buff_in(&ts->txb, (BYTE *)&egibuff[len], (WORD)n);
282 6 adp->count += i_tmp;
283 6 if (i_tmp!=n) //buff full
284 6 flag=0;
285 6 len+= n;
286 6 }
287 5 else
288 5 {
289 6 n=buff_in(&ts->txb, (BYTE *)&egibuff[len], (WORD)buf_length-len);
290 6 adp->count += n;
291 6 flag=0;
292 6 }
293 5 }
294 4 }
295 3 }
296 2 else
297 2 if (buff_freelen(&ts->txb) > 0)
298 2 {/* If end of file, close it.. */
299 3
300 3 buff_infile(&ts->txb, adp);
301 3
302 3 if (adp->count == adp->file_length)
C51 COMPILER V7.50 WEBSERVE 10/12/2006 15:31:42 PAGE 6
303 3 {/* If end of file, close it.. */
304 4 adp->file_length = 0;
305 4 adp->count = 0;
306 4 return 0; /* ..and start closing connection */
307 4 }
308 3 }
309 2 }
310 1 return 1;
311 1 }
312
313 /************************************************************************
314 /* Function Name : buff_infile *
315 /* *
316 /* Arguments : *
317 /* CBUFF *bp: Point to CBUFF. *
318 /* APPDATA *adp:Point to APPDATA. *
319 /* *
320 /* Return : *
321 /* Byte count copy to buffer. *
322 /* Comment : *
323 /* Load file into buffer, return byte count. *
324 /* *
325 /************************************************************************/
326 WORD buff_infile(CBUFF *bp, APPDATA *adp)
327 {
328 1 WORD in, n, n1, n2=0;
329 1 unsigned int len, count=0;
330 1
331 1 /* how many bytes left in the file to copy */
332 1 len = adp->file_length - adp->count;
333 1
334 1 /*
335 1 After adding bytes to the buffer pointed by bp, the bp->in
336 1 is adjusted with a straight addition. For an example, if
337 1 n bytes are added to the buffer, the bp->in is updated as
338 1 bp->in += n.
339 1 This obviously creates the problem that bp->in may point to
340 1 a location beyond the end of the data array.
341 1
342 1 the following checks if bp->in is at a position
343 1 beyond the end of the data array. If it is, this operation
344 1 wraps bp->in around
345 1
346 1 e.g. if the size of the data array is 0x0800 (2K),
347 1 so bp->len -1 is 0x07FF. When bp->in is in the range
348 1 0x0000 to 0x7FF, the following operation has no effect.
349 1 However, when bp->in is 0x0800, the following operation will
350 1 change bp->in to 0x0000. When bp->in is 0x08AB, the following
351 1 operation changes it to 0x00AB.
352 1
353 1
354 1 it is also noted that bp->in is a long, while the index into the
355 1 buffer is an unsigned integer. The following operation changes
356 1 the long into a WORD.
357 1 */
358 1
359 1 in = (WORD)bp->in & (bp->len-1); /* Mask I/P ptr to buffer area. */
360 1
361 1 n = minw(len, buff_freelen(bp)); /* Get max allowable length. */
362 1
363 1 n1 = minw(n, (WORD)(bp->len - in)); /* Length up to end of buff. */
364 1
C51 COMPILER V7.50 WEBSERVE 10/12/2006 15:31:42 PAGE 7
365 1 if (n1)
366 1 {
367 2 /* If len <= n1, copy len bytes. If len > n1, copy n1 bytes.*/
368 2 n2 = len<=n1 ? len : n1;
369 2 vfat_mem_cpy(&bp->b_data[in], &adp->in[adp->count], n2);
370 2 adp->count += n2;
371 2
372 2 count += n2;
373 2
374 2 n = n - n2; /* Space left.*/
375 2
376 2 if (len ==n2)
377 2 /* All bytes are copied.*/
378 2 n2 = 0;
379 2 else
380 2 {
381 3 /* Number of bytes left to copy.*/
382 3 n2 = len - n2;
383 3 }
384 2 }
385 1 /* n2 is the number of bytes left to copy. n is the max free space.*/
386 1 if (n2 && n) /* There is something to copy and there is space.*/
387 1 {
388 2 /* If n2 <= n, copy n2 bytes. If n2 > n, copy n bytes.*/
389 2 n2 = (n2 <= n) ? n2 : n;
390 2
391 2 /* Start copy from the beginning of the data pointer.*/
392 2 vfat_mem_cpy(bp->b_data, &adp->in[adp->count], n2);
393 2
394 2 adp->count += n2;
395 2
396 2 count += n2;
397 2 }
398 1 bp->in += count; /* Bump I/P pointer */
399 1 return((WORD)count);
400 1 }
401
402 /************************************************************************
403 /* Function Name : egi_execstr *
404 /* *
405 /* Arguments : *
406 /* TSOCK xdata *ts:Point to socket. *
407 /* char xdata *str:Point to EGI file name string. *
408 /* *
409 /* Return : *
410 /* If success return 1; *
411 /* If fail(not exist) return 0; *
412 /* Comment : *
413 /* Execute the EGI function corresponding to a string, *
414 /* return 0 if not found. *
415 /* *
416 /************************************************************************/
417 int egi_execstr(TSOCK xdata *ts, char xdata *str)
418 {
419 1 int ok=0, n=0;
420 1 /*Search for EGI functions*/
421 1 while (egifuncs[n].func && !(ok=!strcmp(str, egifuncs[n].name)))
422 1 n++;
423 1
424 1 if (ok)
425 1 egifuncs[n].func(ts, (unsigned char xdata *)0);
426 1 return(ok);
C51 COMPILER V7.50 WEBSERVE 10/12/2006 15:31:42 PAGE 8
427 1 }
428
429 /************************************************************************
430 /* Function Name : Init_HTML_pages *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -