⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slindex.c

📁 picoos源码。The RTOS and the TCP/IP stack will be built automatically.
💻 C
字号:
/*
 *  Copyright (c) 2005, Dennis Kuschel.
 *  All rights reserved. 
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *   3. The name of the author may not be used to endorse or promote
 *      products derived from this software without specific prior written
 *      permission. 
 *
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 *  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 *  INDIRECT,  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/memp.h"
#include "lwip/tcpip.h"
#include "lwip/ip_addr.h"
#include "lwip/stats.h"
#include "lwip/sockets.h"
#include "arch/perf.h"

#include "../../../e-soft/src/httpd.h"
#include "../../../e-soft/src/filesys.h"

#include <picoos.h>


/*
 *  This servlet overloads the index.html file.
 *
 *  When the index.html is requested by a web browser, the servlet
 *  opens the file index.html, searches the file for the string
 *  "{*}" and replaces it with the servers IP address. The modified
 *  html file is then forwarded to the web browser.
 *
 */

#define INDEX_HTML_1  "index.html"
#define INDEX_HTML_2  "index.htm"


/*-------------------------------------------------------------------------*/


/* exported functions */
sint_t index_servlet(SERVLET_t so);

/* imported variables */
extern struct ip_addr  ipaddr;


/*-------------------------------------------------------------------------*/


static char *ip2str(char *buf, struct ip_addr addr)
{
  u32_t s_addr = addr.addr;
  char inv[3];
  char *rp;
  u8_t *ap;
  u8_t rem;
  u8_t n;
  u8_t i;

  rp = buf;
  ap = (u8_t *)&s_addr;
  for(n = 0; n < 4; n++) {
    i = 0;
    do {
      rem = *ap % (u8_t)10;
      *ap /= (u8_t)10;
      inv[i++] = '0' + rem;
    } while(*ap);
    while(i--)
      *rp++ = inv[i];
    *rp++ = '.';
    ap++;
  }
  *--rp = 0;
  return buf;
}


sint_t  index_servlet(SERVLET_t so)
{
  #define BUFSIZE 100
  const char *filename;
  char ibuffer[BUFSIZE];
  char obuffer[BUFSIZE];
  char ipbuf[20];
  sint_t status, ifile, ibytes, iptr, optr;
  sint_t bufsize = 0;
  char c, cl1, cl2, cl3;
  struct fsys_stat fstat;

  filename = INDEX_HTML_1;
  if (fsys_stat(filename, &fstat) >= 0)
  {
    bufsize = fstat.st_size + 1000;
  }
  else
  {
    filename = INDEX_HTML_2;
    if (fsys_stat(filename, &fstat) >= 0)
    {
      bufsize = fstat.st_size + 1000;
    }
  }   

  ifile = fsys_open(filename, FSO_RDONLY);
  if ((ifile < 0) || (bufsize == 0))
  {
    hsl_printf(so, "<html><body><br><b>index.html not found!</b><br><br>");
    hsl_printf(so, "<a href=\"stats\">click here to see the statistics</a>");
    hsl_printf(so, "<br></body></html>\n");
    if (ifile >= 0) fsys_close(ifile);
    return SLERR_OK;
  }

  status = hsl_setStreamBufSize(so, bufsize);
  if (status == SLERR_FAIL)
  {
    hsl_printf(so, "<html><body><br><br>OUT OF MEMORY<br></body></html>\n");
    return SLERR_OK;
  }
  if (status != SLERR_OK)
    return status;

  /* copy index.html to the HTTP client, but replace
     the string "{*}" with our local IP address */
  iptr = 0;
  optr = 0;
  ibytes = 0;
  cl1 = cl2 = cl3 = 0;
  ip2str(ipbuf, ipaddr);
  for (;;)
  {
    if (iptr >= ibytes)
    {
      status = fsys_read(ifile, ibuffer, BUFSIZE);
      if (status <= 0)
        break;
      ibytes = status;
      iptr = 0;
    }
    c = ibuffer[iptr++];
    cl3 = cl2; cl2 = cl1; cl1 = c;

    if ((cl3 == '{') && (cl2 == '*') && (cl1 == '}'))
    {
      if (optr >= 2) optr -= 2;
      strcpy(obuffer + optr, ipbuf);
      optr += strlen(ipbuf);
    }
    else
    {
      obuffer[optr++] = c;
    }
    if (((optr >= BUFSIZE / 2) && (c != '{') && (c != '*')) ||
        (optr >= BUFSIZE-1))
    {
      obuffer[optr] = 0;
      hsl_printf(so, "%s", obuffer);
      optr = 0;
    }
  }
  if (optr != 0)
  {
    obuffer[optr] = 0;
    hsl_printf(so, "%s", obuffer);
  }

  fsys_close(ifile);
  return SLERR_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -