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

📄 servlet4.c

📁 picoos源码。The RTOS and the TCP/IP stack will be built automatically.
💻 C
字号:
/*
 *  Copyright (c) 2004, 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.
 *
 */

/**
 * @file    servlet4.c
 * @author  Dennis Kuschel
 * @brief   This servlet demonstrates the use of sessions.
 *
 * This software is from http://mycpu.mikrocontroller.net.
 * Please send questions and bug reports to dennis_k@freenet.de.
 */

#include "../src/sys.h"
#include "../src/httpd.h"



/* define the type of the session data */
typedef struct {
  u32_t sc;
  u32_t ln;
  u32_t fs;
  char chars[81];
} CGSESSION_t;


/* function prototypes */
sint_t  my_chargen_servlet2(SERVLET_t so);
static sint_t getCharactersFromUser(SERVLET_t so, CGSESSION_t *cgs);



/*
 *  This servlet generates a character pattern.
 *  It can be confiugured by pressing some hyperlinks
 *  that are embedded into the generated html page.
 *  The servlet uses a session to store its local data.
 */

sint_t my_chargen_servlet2(SERVLET_t so)
{
  CGSESSION_t *cgs;
  char buf[90];
  char *p;
  u32_t sc, ln, fs;
  sint_t i, j, status;
  u32_t x, y, c, d, n;
  static const char ctab[] =
    "1234567890+-*/=?abcdefghijklmnopqrstuvwxyz(#)ABCDEFGHIJKLMNOPQRSTUVWXYZ_[$]{:}.;";

  /* get / create session */
  cgs = (CGSESSION_t*) hsl_getSession(so);
  if (cgs == NULL)
  {
    cgs = (CGSESSION_t*) hsl_newSession(so, sizeof(CGSESSION_t), 120);
    if (cgs == NULL)
      return SLERR_BUSY;

    /* initialize new session */
    cgs->sc = 0;
    cgs->ln = 4;
    cgs->fs = 1;
    strcpy(cgs->chars, ctab);
  }

  /* get type of button that was pushed by the user (if any) */
  n = 0;
  p = hsl_getParameterByName(so, "bu");
  if (p != NULL) sscanf(p, "%u", &n);
  switch (n)
  {
    case 1: if (++cgs->sc >= 78) cgs->sc = 0;   break;
    case 2: if (cgs->sc-- ==  0) cgs->sc = 78;  break;
    case 3: if (++cgs->ln > 300) cgs->ln = 1;   break;
    case 4: if (--cgs->ln ==  0) cgs->ln = 300; break;
    case 5: if (--cgs->fs ==  0) cgs->fs = 1;   break;
    case 6: if (++cgs->fs >   5) cgs->fs = 5;   break;
    case 7: return getCharactersFromUser(so, cgs);
    default:
      break;
  }

  /* read new character pattern (if available) */
  p = hsl_getParameterByName(so, "pat");
  if (p != NULL) 
  {
    if (*p != 0)
    {
      j = 0;
      for (i = 0; i < 80; i++)
      {
        if (p[j] == 0) j = 0;
        cgs->chars[i] = p[j++];
      }
    }
  }

  /* get parameters from current session */
  sc = cgs->sc;
  ln = cgs->ln;
  fs = cgs->fs;

  /* Allocate memory for the html page that will be generated
     by this servlet. Note: The default memory size is only 1KB. */
  status = hsl_setStreamBufSize(so, (ln+5) * 85 + 2048);
  if (status == SLERR_FAIL)
  {
    /* The http demon is not able to provide the requested ammount
       of memory. The servlet can not proceed. Note:
       When we would not test for SLERR_FAIL and would simply
       return status, the user will see a page saying
       "500 Internal Server Error"  */
    hsl_printf(so, "<html><body><br><br>OUT OF MEMORY<br></body></html>\n");
    return SLERR_OK;
  }
  if (status != SLERR_OK)
  {
    /* Print debug message and exit. Note: The http demon will call
       this servlet again as soon as there is enough memory available. */
    printf("mem failed for request sc=%u ln=%u, %u bytes\n", sc, ln, (ln+5) * 85);
    return status;
  }

  /* create html header */
  hsl_printf(so, "<html><head><title>My Chargen Servlet</title></head><body>\n");
  hsl_printf(so, "<a href=\"index.html\">Index</a><br><br>");

  /* insert control links to the html page (with session id)*/
  status = hsl_linkCreate(so, NULL, HSL_LINK_SID);
  if (status == 0)  
    status = hsl_linkAddNumberVal(so, "bu", 1);
  if (status == 0)
    status = hsl_linkInsert(so, "increment startcharacter");
  hsl_printf(so, " &nbsp; ");
  if (status == 0)
    status = hsl_linkCreate(so, NULL, HSL_LINK_SID);
  if (status == 0)  
    status = hsl_linkAddNumberVal(so, "bu", 2);
  if (status == 0)
    status = hsl_linkInsert(so, "decrement startcharacter");
  hsl_printf(so, "<br>\n");
  if (status == 0)
    status = hsl_linkCreate(so, NULL, HSL_LINK_SID);
  if (status == 0)  
    status = hsl_linkAddNumberVal(so, "bu", 3);
  if (status == 0)
    status = hsl_linkInsert(so, "increment linecount");
  hsl_printf(so, " &nbsp; ");
  if (status == 0)
    status = hsl_linkCreate(so, NULL, HSL_LINK_SID);
  if (status == 0)  
    status = hsl_linkAddNumberVal(so, "bu", 4);
  if (status == 0)
    status = hsl_linkInsert(so, "decrement linecount");
  if (status != 0)
    return SLERR_SERVER;

  /* insert buttons to control the character size */
  hsl_printf(so, "<br><br><table><tr><td>\n");
  hsl_printf(so, "<form action=\"%s\" method=\"get\">\n",hsl_getOwnName(so));
  hsl_formAddSessionId(so);
  hsl_printf(so, "<input type=\"hidden\" name=\"bu\" value=\"5\">\n");
  hsl_printf(so, "<input type=\"submit\" value=\"smaller\"%s>\n",
             (fs > 1) ? "" : " disabled" );
  hsl_printf(so, "</form>\n");
  hsl_printf(so, "</td><td>&nbsp;</td><td>\n");
  hsl_printf(so, "<form action=\"%s\" method=\"get\">\n",hsl_getOwnName(so));
  hsl_formAddSessionId(so);
  hsl_printf(so, "<input type=\"hidden\" name=\"bu\" value=\"6\">\n");
  hsl_printf(so, "<input type=\"submit\" value=\"larger\"%s>\n",
             (fs < 5) ? "" : " disabled" );
  hsl_printf(so, "</form>\n");

  /* insert button to change the character string */
  hsl_printf(so, "</td><td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td><td>\n");
  hsl_printf(so, "<form action=\"%s\" method=\"get\">\n",hsl_getOwnName(so));
  hsl_formAddSessionId(so);
  hsl_printf(so, "<input type=\"hidden\" name=\"bu\" value=\"7\">\n");
  hsl_printf(so, "<input type=\"submit\" value=\"change characters\">\n");
  hsl_printf(so, "</form>\n");

  hsl_printf(so, "</td></tr></table>\n");

  /* print informations */
  hsl_printf(so, "<br>\nStart-Character: %u<br>", sc);
  hsl_printf(so, "Line-Count: %u<br>\n", ln);

  /* generate character pattern and insert it into the html page */
  hsl_printf(so, "<code><font size=\"%u\"><br>\n", fs);
  d = sc;
  for (y = 0; y < ln; y++)
  {
    c = d;
    if (++d >= 79) d = 0;
    for (x = 0; x < 79; x++)
    {
      buf[x] = cgs->chars[c];
      if (++c >= 79) c = 0;
    }
    buf[x] = 0;
    hsl_printf(so, "%s<br>\n", buf);
  }
  hsl_printf(so, "</font></code>\n");

  /* create html footer */
  hsl_printf(so, "</body></html>\n");

  return SLERR_OK;
}



/* print the html page that allows the user
   to change the character pattern */
static sint_t getCharactersFromUser(SERVLET_t so, CGSESSION_t *cgs)
{
  sint_t status;

  /* Allocate memory for the html page that will be generated
     by this servlet. Note: The default memory size is only 1KB. */
  status = hsl_setStreamBufSize(so, 2048);
  if (status != SLERR_OK)
    return status;

  /* create html header */
  hsl_printf(so, "<html><head><title>My Chargen Servlet</title></head><body>\n");
  hsl_printf(so, "<a href=\"index.html\">Index</a><br><br>");

  hsl_printf(so, "<form action=\"%s\" method=\"get\">\n",hsl_getOwnName(so));
  hsl_printf(so, "<p>Enter the character pattern:<br>");
  hsl_formAddSessionId(so);
  hsl_printf(so, "<input name=\"pat\" type=\"text\" size=\"60\" maxlength=\"80\" "
                 "value=\"%s\">\n", cgs->chars);
  hsl_printf(so, "</p><br>\n<p>");
  hsl_printf(so, "<input type=\"submit\" value=\"Save\">\n");
  hsl_printf(so, "<input type=\"reset\" value=\"Reset\">\n");
  hsl_printf(so, "</p></form>\n");

  /* create html footer */
  hsl_printf(so, "</body></html>\n");

  return SLERR_OK;
}

⌨️ 快捷键说明

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