?? webpage.c
字號:
//--------------------------------------------------------------------------
// 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\console\console.h"
#include "router.h"
#pragma DATA_SECTION(DEFAULT, "OBJMEM");
#include "webdata\default.c"
#pragma DATA_SECTION(LOGOBAR, "OBJMEM");
#include "webdata\logobar.c"
#pragma DATA_SECTION(DSPCHIP, "OBJMEM");
#include "webdata\dspchip.c"
#pragma DATA_SECTION(SAMPLE, "OBJMEM");
#include "webdata\sample.c"
static int cgiSample(SOCKET htmlSock, int ContentLength, char *pArgs );
static int cgiInform( SOCKET htmlSock, int ContentLength );
// 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);
}
void RemoveWebFiles(void)
{
efs_destroyfile("index.html");
efs_destroyfile("logobar.gif");
efs_destroyfile("dspchip.gif");
efs_destroyfile("protected/%R%");
efs_destroyfile("protected/sample.cgi");
efs_destroyfile("protected/sample.htm");
efs_destroyfile("inform.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";
//
// 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 );
}
//
// 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 *postdata;
char *name, *value;
int bytes;
int parseIndex;
// Allocate space for the CGI post data
postdata = (char *)mmBulkAlloc(ContentLength+1);
if( !postdata )
goto FATAL;
// Read in the post data from the socket
bytes = recv(htmlSock, postdata, ContentLength, MSG_WAITALL);
if( bytes < 1 )
goto FATAL;
// Setup to parse the post data
parseIndex = 0;
postdata[ContentLength] = '\0';
// Read until we've read the item "ip"
do {
name = cgiParseVars( postdata, &parseIndex );
value = cgiParseVars( postdata, &parseIndex );
} while( strcmp("ip", name ) && parseIndex != -1 );
// Now check the value of "ip"
if (strcmp("ipinfo", value) == 0)
CreateIPUse(htmlSock);
else if (strcmp("sockets", value) == 0)
CreatehtmlSockets(htmlSock);
else if (strcmp("route", value) == 0)
CreateRoute(htmlSock);
FATAL:
// Free the data we've allocated
if( postdata )
mmBulkFree(postdata);
// We always leave the socket open
return( 1 );
}
void CreateIPUse(SOCKET htmlSock)
{
IPN myIP;
IPN yourIP;
char pszmyIP[32];
char pszyourIP[32];
struct sockaddr_in Info;
int InfoLength;
char tmpBuf[128];
HOSTENT *dnsInfo;
char htmlbuf[MAX_RESPONSE_SIZE];
int rc;
InfoLength = sizeof(Info);
getsockname( htmlSock, &Info, &InfoLength );
myIP = Info.sin_addr.s_addr;
NtIPN2Str( myIP, pszmyIP );
InfoLength = sizeof(Info);
getpeername( htmlSock, &Info, &InfoLength );
yourIP = Info.sin_addr.s_addr;
NtIPN2Str( yourIP, pszyourIP );
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -