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

📄 servlet5.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    servlet5.c
 * @author  Dennis Kuschel
 * @brief   This servlet demonstrates the use of sessions with login.
 *
 * 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 MAX_USERNAME_LEN  20


#define SLS_PRINTLOGIN 0
#define SLS_DOLOGIN    1
#define SLS_LOGGEDIN   2


/* define the type of the session data */
typedef struct {
  char   username[MAX_USERNAME_LEN+1];
  sint_t currentPage;
} CGSESSION_t;


/* function prototypes */
sint_t  my_login_servlet(SERVLET_t so);
static sint_t printInitialFrame(SERVLET_t so);
static sint_t printLoginScreen(SERVLET_t so, sint_t wrongpass);
static sint_t checkUser(SERVLET_t so, const char* user, const char *pass);
static sint_t printPageFramework(SERVLET_t so, CGSESSION_t *cgs);
static sint_t handlePage1(SERVLET_t so, CGSESSION_t *cgs);
static sint_t handlePage2(SERVLET_t so, CGSESSION_t *cgs);
static sint_t handlePage3(SERVLET_t so, CGSESSION_t *cgs);
static sint_t handlePage4(SERVLET_t so, CGSESSION_t *cgs);
static void printMenuPoint(SERVLET_t so, CGSESSION_t *cgs, sint_t nbr, char *text);



/*
 *  This servlet shows how to use sessions
 *  to manage a user login.
 */

sint_t my_login_servlet(SERVLET_t so)
{
  CGSESSION_t *cgs;
  char *user, *pass, *tmp;
  sint_t page;

  /* get / create session */
  cgs = (CGSESSION_t*) hsl_getSession(so);
  if (cgs == NULL)
  {
    /* We do not have a session yet. We need to ask the
       user for its name and password now. */

    /* try to get username and password from URI */
    user = hsl_getParameterByName(so, "login");
    pass = hsl_getParameterByName(so, "passw");

    /* print login screen if not yet done */
    if ((user == NULL) || (pass == NULL))
    {
      /* print initial frame and return
         (this hides the login+password in the address bar) */
      if (hsl_getParameterByName(so, "framed") == NULL)
        return printInitialFrame(so);

      /* print login screen and return */
      return printLoginScreen(so, 0);
    }

    /* check login data */
    if (checkUser(so, user, pass) <= 0)
    {
      /* wrong password */
      return printLoginScreen(so, 1);
    }

    /* Ok, user is logged in. Now create the session. */
    cgs = (CGSESSION_t*) hsl_newSession(so, sizeof(CGSESSION_t), 300);
    if (cgs == NULL)
      return SLERR_BUSY;

    /* initialize new session */
    strcpy(cgs->username, user);
    cgs->currentPage = 1;
  }

  /* try to get page parameter and change the current html subpage */
  tmp = hsl_getParameterByName(so, "page");
  if (tmp != NULL)
  {
    page = 1;
    sscanf(tmp, "%i", &page);
    if ((page < 0) || (page > 4))
      return SLERR_OK; /* Wrong page number. Ignore this HTTP request. */

    if (page == 0)
    {
      /* got logout request: now we have to destroy the session */
      hsl_destroySession(so);
      /* print login screen again and return */
      return printLoginScreen(so, 0);
    }

    cgs->currentPage = page;
  }

  /* generate and print currently selected html subpage */
  return printPageFramework(so, cgs);
}



/* Generate a frameset. This is to hide the login data in the address bar. */
static sint_t printInitialFrame(SERVLET_t so)
{
  hsl_printf(so, "<html><head><title>Login Demo Servlet</title></head>\n");
  hsl_printf(so, "<frameset>\n");
  hsl_printf(so, "<frame src=\"");
  if (hsl_linkCreate(so, NULL, HSL_LINK_RAW) == SLERR_OK)
  {
    hsl_linkAddStringVal(so, "framed", "yes");
    hsl_linkInsert(so, NULL);
  }
  hsl_printf(so, "\" name=\"topframe\">\n",hsl_getOwnName(so));
  hsl_printf(so, "</frameset></html>\n");
  return SLERR_OK;
}


/* Print the login screen. */
static sint_t printLoginScreen(SERVLET_t so, sint_t wrongpass)
{
  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;

  hsl_printf(so, "<html><head><title>Login Screen</title></head>\n");
  hsl_printf(so, "<body>\n");
  hsl_printf(so, "<a href=\"index.html\" target=\"_top\">Index</a><br><br>");

  if (wrongpass != 0)
  {
    hsl_printf(so, "\n<h3>Wrong password. Try again.</h3>\n");
  }
  else
  {
    hsl_printf(so, "<br>\n");
  }

  hsl_printf(so, "<form action=\"%s\" method=\"get\">\n",hsl_getOwnName(so));
  hsl_formAddSessionId(so);
  hsl_printf(so, "<table border=\"0\" cellpadding=\"0\" cellspacing=\"4\">\n");
  hsl_printf(so, "<tr><td align=\"left\">Login:</td>\n");
  hsl_printf(so, "<td><input name=\"login\" type=\"text\" "
                 "size=\"20\" maxlength=\"20\"></td></tr>\n");
  hsl_printf(so, "<tr><td align=\"left\">Password:</td>\n");
  hsl_printf(so, "<td><input name=\"passw\" type=\"password\" "
                 "size=\"20\" maxlength=\"20\"></td></tr>\n");
  hsl_printf(so, "<tr><td>&nbsp;</td><td>\n");
  hsl_printf(so, "<input type=\"submit\" value=\"Login\">\n");
  hsl_printf(so, "</td></tr></table><br>\n");
  hsl_printf(so, "</form><br>\n");

  hsl_printf(so, "</body></html>\n");
  return SLERR_OK;
}


/* This function tests if a username/password-pair is valid. */
static sint_t checkUser(SERVLET_t so, const char* user, const char *pass)
{
  if ((strcmp(user, "test") == 0) && (strcmp(pass, "1234") == 0))
    return 1;  /* ok, login is valid */
  if ((strcmp(user, "homer") == 0) && (strcmp(pass, "qwerty") == 0))
    return 1;  /* ok, login is valid */
  return 0; /* login failed */
}


/* First page. Note: pages can contain active content. */
static sint_t handlePage1(SERVLET_t so, CGSESSION_t *cgs)
{
  hsl_printf(so, "<br><br><b>This is Page 1</b><br><br>\n");
  hsl_printf(so, "Click on the links above to navigate.<br>\n");
  hsl_printf(so, "And don't forget to logout!<br>\n");
  return SLERR_OK;
}


/* Second page. Note: pages can contain active content. */
static sint_t handlePage2(SERVLET_t so, CGSESSION_t *cgs)
{
  hsl_printf(so, "<br><br><b>.... This is Page 2 ....</b><br><br>\n");
  return SLERR_OK;
}


/* Third page. Note: pages can contain active content. */
static sint_t handlePage3(SERVLET_t so, CGSESSION_t *cgs)
{
  hsl_printf(so, "<br><br><b>---=== This is Page 3 ===---</b><br><br>\n");
  return SLERR_OK;
}


/* Fourth page. Note: pages can contain active content. */
static sint_t handlePage4(SERVLET_t so, CGSESSION_t *cgs)
{
  hsl_printf(so, "<br><br><b>(((( This is Page 4 ))))</b><br><br>\n");
  return SLERR_OK;
}


/* this function is called by func. printPageFramework() */
static void printMenuPoint(SERVLET_t so, CGSESSION_t *cgs, sint_t nbr, char *text)
{
  if (cgs->currentPage == nbr)
  {
    hsl_printf(so, "%s", text);
  }
  else
  {
    if (hsl_linkCreate(so, NULL, HSL_LINK_SID) == SLERR_OK)
    {
      hsl_linkAddNumberVal(so, "page", nbr);
      hsl_linkInsert(so, text);
    }
  }
  hsl_printf(so, "</font></td><td align=\"center\"><font size=\"2\" color=\"#000099\">\n");
}


/* This func prints the menu framework and inserts the
   currently selected page. */
static sint_t printPageFramework(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, 4096);
  if (status != SLERR_OK)
    return status;

  hsl_printf(so, "<html><head><title>User: %s</title></head>\n", cgs->username);
  hsl_printf(so, "<body bgcolor=\"#FFFFCC\" text=\"#000000\">\n");
  hsl_printf(so, "<table width=\"100%%\" cellspacing=\"0\"><tr>\n");
  hsl_printf(so, "<td high=\"100\" valign=\"center\" bgcolor=\"#99CCFF\"><table width=\"100%%\">\n");
  hsl_printf(so, "<tr><td align=\"center\"><font size=\"2\" color=\"#000099\">\n");

  printMenuPoint(so, cgs, 1, "Page 1");
  printMenuPoint(so, cgs, 2, "Page 2");
  printMenuPoint(so, cgs, 3, "Page 3");
  printMenuPoint(so, cgs, 4, "Page 4");
  printMenuPoint(so, cgs, 0, "Logout");

  hsl_printf(so, "</font></td></tr></table>\n");
  hsl_printf(so, "<tr><td><font color=\"#000000\">\n");

  hsl_printf(so, "User: %s<br>\n", cgs->username);

  /* handle the pages */
  switch(cgs->currentPage)
  {
    default:
    case 1: status = handlePage1(so, cgs); break;
    case 2: status = handlePage2(so, cgs); break;
    case 3: status = handlePage3(so, cgs); break;
    case 4: status = handlePage4(so, cgs); break;
  }

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

  return status;
}

⌨️ 快捷键说明

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