📄 http_file.c
字号:
/*
* $Id: fttp_file.c,v 1.0 2003/02/21 00:11:04 csm Exp $
* $Copyright: (c) 2002, 2003 Broadcom Corp.
* All Rights Reserved.$
*/
/***********************************************************************
* Impacct Technology Corporation. Copyright 2001 - 2100. *
* ALL RIGHTS RESERVED *
*----------------------------------------------------------------------*
* Project : HTTP
* Creator : Gary Chen / iMpacct 11/8/2001
* File : fttp_file.c
* Description: To handle file access for .htm files
*
* History:
* 4-17-2002, Carl
* 1. Redefined the structures from web conversion program
* 2. Change the codes to suit for new design.
*
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sys_config.h>
#include "http_file.h"
extern int web_page_table_total, web_submit_table_total, web_get_table_total;
extern HTML_TBL web_page_tables [];
extern HTML_VAR_TBL web_get_tables [];
extern HTML_CGI_TBL web_submit_tables [];
extern char szPageContent [];
extern ULONG ulLoopContinue;
static HTTP_STAT_T stat_POST = {33206, 4678, 1018854968, 1018700980};
static int inLoopMode;
/* Implement the callback function for binary search
*
*/
int http_page_compare (LPSTR psz, int i)
{
return strcmp (web_page_tables [i].szURI, psz);
}
int http_get_compare (LPSTR psz, int i)
{
return strcmp (web_get_tables [i].pszKey, psz);
}
int http_submit_compare (LPSTR psz, int i)
{
return strcmp (web_submit_tables [i].pszURI, psz);
return 1;
}
/*----------------------------------------------------------------------
* ROUTINE NAME - http_open
*-----------------------------------------------------------------------
* DESCRIPTION: To open a html file
* CALLED BY :
* INPUT : points to filename
* OUTPUT : return file handle, or -1 if not found
*----------------------------------------------------------------------*/
HTTP_FILE *http_open (LPSTR pszFilename)
{
INT i;
// LPSTR pszNewFilename = pszFilename;
if (pszFilename [0] == '.')
pszFilename += 2;
// strlwr (pszNewFilename);
// strlwr (pszFilename);
// (+) Carl, 5-2-2002
// To prevent the case problem when we write an program to control something! I convert
// all the uppercases to lowercases.
WebLib_strlwr (pszFilename);
// (-) Carl
i = BinarySearch (pszFilename, http_page_compare, web_page_table_total);
if (i != -1)
{
HTTP_FILE *handle;
if (handle = (HTTP_FILE *) httpd_malloc (sizeof (HTTP_FILE)))
{
handle->tag = HTTP_FILE_TAG;
handle->file_pos = 0;
handle->file_size = web_page_tables [i].pStat->st_size;
handle->file_type = web_page_tables [i].Attr;
handle->file_ptr = web_page_tables [i].pszContent;
handle->stat_ptr = web_page_tables [i].pStat;
handle->file_dest_size = web_page_tables [i].dwSizePage;
#ifdef WIN32
web_page_tables [i].pStat->st_mtime = time((time_t*) 0);
#endif
return (handle);
}
dprintf("<http_open>No space to allocate memory!\n");
}
return((HTTP_FILE *)-1);
}
/* skip nextline */
LPSTR MoveToNextLine (LPSTR cp)
{
return strchr (cp, '\n') + 1;
}
char *http_get_token(char *srce, char *dest)
{
++srce;
while (*srce != '#')
*dest++=*srce++;
*dest = 0;
srce++;
return(srce);
}
/*----------------------------------------------------------------------
* ROUTINE NAME - http_read
*-----------------------------------------------------------------------
* DESCRIPTION: To read data from html file
* CALLED BY :
* INPUT : HTTP_FILE *handle - points a file handle
* char *dest - destination address
* ULONG size - read size
* OUTPUT : -1 if illegal handle
* : actual size to read
*----------------------------------------------------------------------*/
int http_read (HTTP_FILE *handle, LPSTR *ppszDest)
{
LPSTR pszDest;
LPSTR pszSrce, pszEnd;
INT code = HTTP_CODE_NORMAL;
INT total = 0;
*ppszDest = NULL;
if (handle->tag != HTTP_FILE_TAG)
return (-1);
pszDest = (LPSTR)httpd_malloc (handle->file_dest_size);
if (pszDest == NULL)
return -1;
*ppszDest = pszDest;
pszSrce = handle->file_ptr;
pszEnd = pszSrce + strlen (pszSrce);
// Image files
if (handle->file_type != PAGE_IsHtml)
{
// In case, the destination size is equal to original
memcpy (pszDest, pszSrce, handle->stat_ptr->st_size);
return handle->stat_ptr->st_size;
}
// Html files
while (*pszSrce != 0 && pszSrce < pszEnd)
{
pszDest = *ppszDest + total;
if (code == HTTP_CODE_NORMAL)
{
if ((*pszSrce == HTTP_1stChar1 && *(pszSrce + 1) == HTTP_2ndChar1) ||
(*pszSrce == HTTP_1stChar2 && *(pszSrce + 1) == HTTP_2ndChar2))
{
if (memcmp (pszSrce + 2, HTTP_CODE_START, strlen(HTTP_CODE_START)) == 0)
{
pszSrce = MoveToNextLine (pszSrce);
code = HTTP_CODE_PARSING;
continue;
}
}
}
else
{
if ((*pszSrce == HTTP_1stEnd1 && *(pszSrce + 1) == HTTP_2ndEnd1) ||
(*pszSrce == HTTP_1stEnd2 && *(pszSrce + 1) == HTTP_2ndEnd2))
{
if (memcmp (pszSrce + 2, HTTP_CODE_END_1, strlen (HTTP_CODE_END_1)) == 0 ||
memcmp (pszSrce + 2, HTTP_CODE_END_2, strlen (HTTP_CODE_END_2)) == 0)
{
pszSrce = MoveToNextLine (pszSrce);
code = HTTP_CODE_NORMAL;
continue;
}
}
// Parsing the content
if (http_parsing_content (&pszSrce, pszDest, &total, 0))
continue;
}
*pszDest = *pszSrce++;
total++;
}
// Free large memory after interpreted
if ((handle->file_dest_size - total) >= 1024)
*ppszDest = realloc (*ppszDest, total);
return total;
}
/*----------------------------------------------------------------------
* ROUTINE NAME - http_close
*-----------------------------------------------------------------------
* DESCRIPTION: To close an open html file
* CALLED BY :
* INPUT : HTTP_FILE *handle - points a file handle
* OUTPUT : -1 if illegal handle
* : 0 if OK
*----------------------------------------------------------------------*/
int http_close (HTTP_FILE *handle)
{
if (handle->tag!=HTTP_FILE_TAG)
return(-1);
httpd_free (handle);
return(0);
}
/*----------------------------------------------------------------------
* ROUTINE NAME - http_stat
*-----------------------------------------------------------------------
* DESCRIPTION: To get a html file information
* CALLED BY :
* INPUT : char *filename - points to filename
* HTTP_STAT_T *info - file information
* OUTPUT : 0 if OK, or -1 if not found
*----------------------------------------------------------------------*/
int http_stat (char *pszFilename, HTTP_STAT_T *info, int method)
{
int i;
// strlwr (pszFilename);
// (+) Carl, 5-2-2002, the reason is the same as above memtioned.
WebLib_strlwr (pszFilename);
// (-) Carl, 5-2-2002
if (method != 0)
{
i = BinarySearch (pszFilename, http_submit_compare, web_submit_table_total);
if (i != -1)
{
memcpy (info, &stat_POST, sizeof (HTTP_STAT_T));
info->st_size = i;
return 0;
}
return -1;
}
i = BinarySearch (pszFilename, http_page_compare, web_page_table_total);
if (i != -1)
{
memcpy (info, web_page_tables [i].pStat, sizeof(HTTP_STAT_T));
return 0;
}
return -1;
}
static char szRefresh [] = "<html><head><meta http-equiv=\"refresh\" content=\"0; url=%s\"</head></html>";
static char szGenericError_head [] = "<html>\n<head>\n<meta http-equiv=\"Content-Language\" content=\"zh-tw\">\n"
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n"
"<title>Generic Error</title>\n</head>\n<body>\n<p align=\"center\">\n"
"<font color=\"#FF0000\" size=\"6\">";
static char szGenericError_tail [] = "</font></p>\n</body>\n</html>\n";
int http_submit (PHTTP_INFO pInfo, PWEB_ENVIR pEnvir)
{
int iRet;
LPSTR psz;
if (pEnvir->pszMultipartForm == NULL)
{
psz = http_envir_get_string (pEnvir->pszEnvir, "page");
if (psz == NULL || strcmp (psz, szPageContent) != 0)
return CGI_BadRequest;
}
// Call to POST routine
iRet = web_submit_tables [pInfo->ulIndex].pfnVarPost (pEnvir);
// Process returned condition
if (iRet == CGI_OK)
{
if (pEnvir->ulSizeOut == 0 && pEnvir->szRefreshHtml [0] == 0 &&
pEnvir->szHtml [0] == 0)
strcpy (pEnvir->szHtml,
http_envir_get_string (pEnvir->pszEnvir, ENVIR_Path));
if (pEnvir->ulSizeOut == 0)
{
if (pEnvir->szRefreshHtml [0] != 0)
{
pEnvir->ulSizeOut = strlen (szRefresh) + strlen (pEnvir->szRefreshHtml);
pEnvir->pszOutData = (LPSTR)httpd_malloc (pEnvir->ulSizeOut + 256);
if (pEnvir->pszOutData == NULL)
iRet = CGI_Unavailable;
else
{
http_update_modified_time (pEnvir->szRefreshHtml);
sprintf (pEnvir->pszOutData, szRefresh, pEnvir->szRefreshHtml);
}
}
else
if (pEnvir->szHtml [0] != 0)
{
// Get data from http_open, http_read and http_close sequence.
HTTP_FILE *hf;
hf = http_open (pEnvir->szHtml);
if (hf != (HTTP_FILE *) -1)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -