📄 webpage.c
字号:
/*
* Copyright 2006 by Texas Instruments Incorporated.
* All rights reserved. Property of Texas Instruments Incorporated.
* Restricted rights to use, duplicate or disclose this code are
* granted through contract.
*
* @(#) TCP/IP_Network_Developers_Kit 1.91.00.08 08-22-2006 (ndk-a08)
*/
//--------------------------------------------------------------------------
// IP Stack Client Demo
//--------------------------------------------------------------------------
// webpage.c
//
// Web page and CGI function
//
// Author: Michael A. Denio
// Copyright 2000, 2001 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <std.h>
#include <sys.h>
#include <sem.h>
#include <tsk.h>
#include <netmain.h>
#include <_stack.h>
#include "../../../tools/common/cgi/cgiparse.h"
#include "../../../tools/common/cgi/cgiparsem.h"
#include "../../../tools/common/console/console.h"
#include "client.h"
#pragma DATA_SECTION(DEFAULT, ".far:NDK_OBJMEM");
#include "webdata/default.c"
#pragma DATA_SECTION(LOGOBAR, ".far:NDK_OBJMEM");
#include "webdata/logobar.c"
#pragma DATA_SECTION(DSPCHIP, ".far:NDK_OBJMEM");
#include "webdata/dspchip.c"
#pragma DATA_SECTION(SAMPLE, ".far:NDK_OBJMEM");
#include "webdata/sample.c"
static int cgiSample( SOCKET htmlSock, int ContentLength, char *pArgs );
static int cgiSampleMulti( SOCKET htmlSock, int ContentLength, char *pArgs );
static int cgiInform( SOCKET htmlSock, int ContentLength, char *pArgs );
// This int will be our "%R%" realm file, forcing any file located
// in the same direcotry to be authenticated on realm 1. The system
// supports realms 1 to 4.
//
// Note that we're only going to protect the "protected/" subdir,
// but its also possible to protect the entire web site by putting
// a %R% file in the root. Also, you can have the root protected
// on (say) realm 1, and a subdir on (say) realm 2, allowing for
// "users" (members of realm 1) and "superusers" (members of both
// realm 1 and realm 2).
//
static int OurRealm = 1;
void AddWebFiles(void)
{
efs_createfile("index.html", DEFAULT_SIZE, DEFAULT);
efs_createfile("logobar.gif", LOGOBAR_SIZE, LOGOBAR);
efs_createfile("dspchip.gif", DSPCHIP_SIZE, DSPCHIP);
efs_createfile("inform.cgi", 0, (UINT8 *)cgiInform);
efs_createfile("protected/%R%", 4, (UINT8 *)&OurRealm );
efs_createfile("protected/sample.htm", SAMPLE_SIZE, SAMPLE );
efs_createfile("protected/sample.cgi", 0, (UINT8*)cgiSample);
efs_createfile("protected/sample_multi.cgi", 0, (UINT8*)cgiSampleMulti);
}
void RemoveWebFiles(void)
{
efs_destroyfile("index.html");
efs_destroyfile("logobar.gif");
efs_destroyfile("dspchip.gif");
efs_destroyfile("inform.cgi");
efs_destroyfile("protected/%R%");
efs_destroyfile("protected/sample.htm");
efs_destroyfile("protected/sample.cgi");
efs_destroyfile("protected/sample_multi.cgi");
}
//
// Page Creation Commonly Used Data
//
static const char *pstr_HTML_START =
"<html><body text=#000000 bgcolor=#ffffff>\r\n";
static const char *pstr_HTML_END =
"</body></html>\r\n";
static const char *pstr_TI_START =
"<center><p><image src=\"logobar.gif\"></p>\r\n";
static const char *pstr_TI_END =
"</center></body></html>\r\n";
static const char *pstr_ROW_START =
"<tr>";
static const char *pstr_ROW_END =
"</tr>\r\n";
static const char *pstr_DIVIDER =
"<hr WIDTH=\"100%\"><br>\r\n";
static const char *pstr_TABLE_START =
"<table border cellspacing=0 cellpadding=5>\r\n";
static const char *pstr_TABLE_END =
"</table><br>\r\n";
static const char *pstr_LINK_MAIN =
"<a href=index.html>Return to Main Page</a><br><br>\r\n";
static const char *pstr_LINK_MAIN_SUBDIR =
"<a href=../index.html>Return to Main Page</a><br><br>\r\n";
//
// Page Creation Macro
//
#define html(str) httpSendClientStr(htmlSock, (char *)str)
// We use the following format line in cgiSample()
static const char *tablefmt = "<tr><td>%s</td><td>%s</td></tr>\r\n";
static const char *tablefmtd = "<tr><td>%s</td><td>%d</td></tr>\r\n";
//
// Sample CGI Function
//
// This function processes the sample CGI from off the main
// WEB page on the HTTP server.
//
// CGI Functions must return 1 if the socket is left open,
// and zero if the socket is closed. This example always
// returns 1.
//
static int cgiSample(SOCKET htmlSock, int ContentLength, char *pArgs )
{
char *name = 0, *spam = 0, *pizza = 0, *color = 0;
char *buffer, *key, *value;
int len;
int parseIndex;
char htmlbuf[MAX_RESPONSE_SIZE];
// CGI Functions can now support URI arguments as well if the
// pArgs pointer is not NULL, and the ContentLength were zero,
// we could parse the arguments off of pArgs instead.
// First, allocate a buffer for the request
buffer = (char*) mmBulkAlloc( ContentLength + 1 );
if ( !buffer )
goto ERROR;
// Now read the data from the client
len = recv( htmlSock, buffer, ContentLength, MSG_WAITALL );
if ( len < 1 )
goto ERROR;
// Setup to parse the post data
parseIndex = 0;
buffer[ContentLength] = '\0';
// Process request variables until there are none left
do
{
key = cgiParseVars( buffer, &parseIndex );
value = cgiParseVars( buffer, &parseIndex );
if( !strcmp("name", key) )
name = value;
else if( !strcmp("pizza", key) )
pizza = value;
else if( !strcmp("spam", key) )
spam = value;
else if( !strcmp("color", key) )
color = value;
} while ( parseIndex != -1 );
//
// Output the data we read in...
//
httpSendStatusLine(htmlSock, HTTP_OK, CONTENT_TYPE_HTML);
// CRLF before entity
html( CRLF );
//
// Build our HTML response
//
html(pstr_HTML_START);
html("<h1>Form Information</h1>");
html(pstr_DIVIDER);
html(pstr_TABLE_START);
if( name )
{
sprintf( htmlbuf, tablefmt, "Name:", name );
html( htmlbuf );
}
if( spam )
{
sprintf( htmlbuf, tablefmt, "Likes Spam:", spam );
html( htmlbuf );
}
if( pizza )
{
sprintf( htmlbuf, tablefmt, "Favorite Pizza:", pizza );
html( htmlbuf );
}
if( color )
{
sprintf( htmlbuf, tablefmt, "Favorite Color:", color );
html( htmlbuf );
}
html(pstr_TABLE_END);
html(pstr_DIVIDER);
html(pstr_LINK_MAIN_SUBDIR);
html(pstr_HTML_END);
ERROR:
if( buffer )
mmBulkFree( buffer );
return( 1 );
}
//
// MultiPart Form Sample CGI Function
//
// This function processes the sample CGI from off the main
// WEB page on the HTTP server.
//
// CGI Functions must return 1 if the socket is left open,
// and zero if the socket is closed. This example always
// returns 1.
//
static int cgiSampleMulti(SOCKET htmlSock, int ContentLength, char *pArgs )
{
CGIPARSEREC recs[8];
int len,numrecs,i;
char *buffer;
char htmlbuf[MAX_RESPONSE_SIZE];
// CGI Functions can now support URI arguments as well if the
// pArgs pointer is not NULL, and the ContentLength were zero,
// we could parse the arguments off of pArgs instead.
// First, allocate a buffer for the request
buffer = (char*) mmBulkAlloc( ContentLength );
if ( !buffer )
goto ERROR;
// Now read the data from the client
len = recv( htmlSock, buffer, ContentLength, MSG_WAITALL );
if ( len < 1 )
goto ERROR;
// Parse the form data and return the record count
numrecs = cgiParseMultiVars( buffer, len, recs, 8 );
// Output the data we read in...
httpSendStatusLine(htmlSock, HTTP_OK, CONTENT_TYPE_HTML);
// CRLF before entity
html( CRLF );
//
// Build our HTML response
//
html(pstr_HTML_START);
html("<h1>Form Information</h1>");
html(pstr_DIVIDER);
html(pstr_TABLE_START);
for( i=0; i<numrecs; i++ )
{
sprintf( htmlbuf, tablefmtd, "RECORD NUMBER:", i+1 );
html( htmlbuf );
sprintf( htmlbuf, tablefmt, "Name:", recs[i].Name );
html( htmlbuf );
if(recs[i].Filename)
{
if( !recs[i].Filename[0] )
sprintf( htmlbuf, tablefmt, "Filename:", "<i>NULL</i>" );
else
sprintf( htmlbuf, tablefmt, "Filename:", recs[i].Filename );
html( htmlbuf );
}
if(recs[i].Type)
{
sprintf( htmlbuf, tablefmt, "Content-Type:", recs[i].Type );
html( htmlbuf );
}
sprintf( htmlbuf, tablefmtd, "Data Size:", recs[i].DataSize );
html( htmlbuf );
if( recs[i].DataSize < 32 )
{
if( !recs[i].Data[0] )
sprintf( htmlbuf, tablefmt, "Data:", "<i>NULL</i>" );
else
sprintf( htmlbuf, tablefmt, "Data:", recs[i].Data );
html( htmlbuf );
}
}
html(pstr_TABLE_END);
html(pstr_DIVIDER);
html(pstr_LINK_MAIN_SUBDIR);
html(pstr_HTML_END);
ERROR:
if( buffer )
mmBulkFree( buffer );
return( 1 );
}
//
// Functions to create CGI htmlbuf
//
static void CreateIPUse(SOCKET htmlSock);
static void CreatehtmlSockets(SOCKET htmlSock);
static void CreateRoute(SOCKET htmlSock);
//
// Main CGI Function
//
int cgiInform(SOCKET htmlSock, int ContentLength, char *pArgs)
{
char *postdata = 0;
char *name, *value;
int bytes;
int parseIndex;
//
// Here, lets support either a "get" or a "post". In a
// "get", there is no ContentLength
//
if( !ContentLength )
{
// Since we know we are in "get" mode, the "get" arguments
// must be valid
if( pArgs )
{
value = pArgs;
goto CHECKARGS;
}
// We don't support a get with no arguments
http405(htmlSock);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -