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

📄 userdir.c

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 C
字号:
/*____________________________________________________________________________*\

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 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 ``AS IS'' AND ANY EXPRESSED 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 AUTHORS OR ITS CONTRIBUTORS 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.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Plugins/UserDir.c,v $
 * $Date: 2003/05/13 18:42:15 $
 *
 Description:
	User directory mapping.

\*____________________________________________________________________________*/
/* $SourceTop:$ */

#include <string.h>
#include <assert.h>

#include "Pi3API.h"

#define KEY_CONF_USERDIRECTORYROOT		"UserDirectoryRoot"
#define KEY_CONF_HTMLDIRECTORY			"HTMLDirectory"

/*____________________________________________________________________________*\
 *
 Description:
	Documentation
\*____________________________________________________________________________*/
#if 0
	/*
	** HTML documentation for this handler
	*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
	UserDirectory

Description:
	Remap user directories.

	Example
<B><PRE>
	UserDirectoryRoot="/home/"
	HTMLDirectory="/public_html"

	/~jroy/hello.html --> /home/jroy/public_html/hello.html

</PRE></B>

Options:
<H5>Overview</H5>
<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)

<TR>
<TD>UserDirectoryRoot
<TD>+
<TD>A directory path
<TD>Root of user directories
<TD>UserDirectoryRoot="/home/"

<TR>
<TD>HTMLDirectory
<TD>+
<TD>A directory name
<TD>User directory with web files
<TD>HTMLDirectory="/public_html"

</TABLE>
<STRONG>-</STRONG> in the <IT>default</IT> indicates no default<BR>
<STRONG>+</STRONG> in the <IT>default</IT> indicates the field is mandatory<BR>

<!--
Dont forget the slashes
<H4>Description of Options</H4>
<H5>
	Pi3Expression
</H5>
Pi3Expression to write in the debug log.
-->

Phase:
	MAPPING

Returns:
	PIAPI_COMPLETED if the directory was mapped, otherwise PIAPI_CONTINUE.

Note:
Example:
	<PRE>
	&lt;Object&gt;
		Name UserDirectory
		Class UserDirectoryClass
		UserDirectoryRoot "/home/"
		HTMLDirectory "/public_html"
	&lt;/Object&gt;

	&lt;Object&gt;
		...
		Handle UserDirectory
		...
	&lt;/Object&gt;
	</PRE>
/*___+++HTMLDOC_END+++___*/
#endif

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
struct _UserDirectory
{
	char *pRoot;
	int iRootLen;
	char *pDirectory;
	int iDirectoryLen;
};
typedef struct _UserDirectory UserDirectory;

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int UserDirectory_fnParameter( void *pData, const char *pVar, const char *pVal,
	const char *pWhere )
{
	PIObject *pObject = (PIObject *)pData;
	UserDirectory *pUserDirectory = (UserDirectory *)PIObject_getUserData( pObject );
	(void)pUserDirectory;
	assert( pVar );
	assert( pVal );

	if ( !PIUtil_stricmp( pVar, KEY_CONF_USERDIRECTORYROOT) )
		{
		pUserDirectory->pRoot = PIUtil_strdup( pVal );
		pUserDirectory->iRootLen = strlen( pUserDirectory->pRoot );
		}
	else if ( !PIUtil_stricmp( pVar, KEY_CONF_HTMLDIRECTORY) )
		{
		pUserDirectory->pDirectory = PIUtil_strdup( pVal );
		pUserDirectory->iDirectoryLen = strlen( pUserDirectory->pDirectory );
		}
	else
		{
		/* --- Configuration error --- */
		PILog_addMessage( PIObject_getDB( pObject ),
			PIObject_getConfigurationDB( pObject ),
			PILOG_ERROR, "UserDirectory: %sUnknown directive '%s'.",
			pWhere, pVar );
		return 0;
		};
	return 1;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int UserDirectory_init( UserDirectory *pUserDirectory, PIObject *pObject, int iArgc,
	const char *ppArgv[] )
{
	int iRet = PIObject_readParameters( pObject, iArgc, ppArgv,
		UserDirectory_fnParameter, pObject );

	/* --- set defaults --- */
	if ( iRet )
		{
		if ( !pUserDirectory->pRoot )	
			{
			PILog_addMessage(
				PIObject_getDB( pObject ),	
				PIObject_getConfigurationDB( pObject ),
				PILOG_ERROR, "%s", "UserDirectory: 'UserDirectoryRoot' \
not specified." );
			iRet = 0;
			}
		else if ( !pUserDirectory->pDirectory )	
			{
			PILog_addMessage(
				PIObject_getDB( pObject ),	
				PIObject_getConfigurationDB( pObject ),
				PILOG_ERROR, "%s", "UserDirectory: 'HTMLDirectory' \
not specified." );
			iRet = 0;
			};
		};

	return iRet;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int UserDirectory_handle( PIHTTP *pPIHTTP, UserDirectory *pUserDirectory )
{
	const char *pPath;
	int i;
	enum { BUF_SIZE=2047 };
	char szBuffer[BUF_SIZE+1];
	char *pBuffer = szBuffer;
	int iSize;

	assert( pUserDirectory );
	assert( pUserDirectory->pRoot );
	assert( pUserDirectory->pDirectory );

	/* ---
	Get Path
	--- */
	pPath = (const char *)PIDB_lookup( pPIHTTP->pResponseDB, PIDBTYPE_STRING,
		KEY_INT_PATH, 0 );

	/* ---
	See if this path is not a user path
	--- */
	if ( !pPath || *pPath!='/' || pPath[1]!='~' )
		{
		return PIAPI_CONTINUE;
		};

	/* ---
	Scan to first '/' character
	--- */
	pPath = &( pPath[2] );
	for( i=0; pPath[i] && pPath[i]!='/'; i++  );

	/* ---
	Write in root 
	--- */
	iSize = BUF_SIZE;
	if ( !iSize ) { goto buffer_error; };
	strncpy( pBuffer, pUserDirectory->pRoot, iSize );
	iSize -= pUserDirectory->iRootLen;
	pBuffer = &( pBuffer[pUserDirectory->iRootLen] );

	/* ---
	Then user name
	--- */
	if ( iSize<i ) { goto buffer_error; };
	if ( i )
		{
		strncat( pBuffer, pPath, i );
		iSize -= i;
		pBuffer = &( pBuffer[i] );
		}; 

	/* ---
	Then HTML directory
	--- */
	if ( iSize<pUserDirectory->iDirectoryLen ) { goto buffer_error; };
	if ( pUserDirectory->iDirectoryLen )
		{
		strcat( pBuffer, pUserDirectory->pDirectory );
		iSize -= pUserDirectory->iDirectoryLen;
		pBuffer = &( pBuffer[pUserDirectory->iDirectoryLen] );
		};

	/* ---
	Then the path that followed the user directory
	--- */
	pPath = &( pPath[i] );
	i = strlen( pPath );
	if ( iSize<i ) { goto buffer_error; };
	if ( i )
		{
		strcat( pBuffer, pPath );
		};

	/* ---
	Done, replace path
	--- */
	PIDB_replace( pPIHTTP->pResponseDB, PIDBTYPE_STRING, KEY_INT_PATH,
		szBuffer, 0 );

	/* 
	** NOTE: Verify that path is world readable? (UNIX only)
	*/

	return PIAPI_COMPLETED;

buffer_error:
	
	HTTPCore_logError( pPIHTTP, "UserDirectory: Internal error remapping \
to user directory. Path components too long for internal buffer." );

	/* ---
	Internal redirect to error handler
	--- */
	return HTTPUtil_doHTTPError( pPIHTTP, ST_INTERNALERROR );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int UserDirectory_onClassLoad( PIClass_LoadAction eLoad, int i,
	const char *a[] )
{
	return PIAPI_COMPLETED;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int UserDirectory_constructor( PIObject *pObj,
	int iArgc, const char *ppArgv[] )
{
	UserDirectory *pUserDirectory = PIUtil_malloc( sizeof( UserDirectory ) );
	memset( pUserDirectory, 0, sizeof( UserDirectory ) );
	PIObject_setUserData( pObj, pUserDirectory );
	if ( !UserDirectory_init( pUserDirectory, pObj, iArgc, ppArgv ) )
		{
		return PIAPI_ERROR;
		};
	return PIAPI_COMPLETED;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int UserDirectory_copyConstructor( PIObject *o, int i, const char *a[] )
{
	return PIAPI_ERROR;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int UserDirectory_execute( PIObject *pObj,
	int iArgc, const char *ppArgv[] )
{
	PIHTTP *pPIHTTP;

	if ( iArgc<1 )
		{ return PIAPI_ERROR; };

	pPIHTTP = (PIHTTP *)*ppArgv;

	if ( pPIHTTP->ciPhase!=PH_MAPPING )
		{ return PIAPI_ERROR; };

	return UserDirectory_handle( pPIHTTP, 
		(UserDirectory *)PIObject_getUserData( pObj ) ); 
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int UserDirectory_destructor( PIObject *pObj, int i, const char *a[] )
{
	UserDirectory *pUserDirectory = (UserDirectory *)PIObject_getUserData( pObj );
	if ( pUserDirectory->pRoot )
		{ PIUtil_free( pUserDirectory->pRoot ); };
	if ( pUserDirectory->pDirectory )
		{ PIUtil_free( pUserDirectory->pDirectory ); };
	PIUtil_free( pUserDirectory );
	return PIAPI_COMPLETED;
}

#if 0
/*___+++CNF_BEGIN+++___*/
	<Class>
		Name UserDirectoryClass
		Type LogicExtension
		Library Plugins
		OnClassLoad UserDirectory_onClassLoad
		Constructor UserDirectory_constructor
		CopyConstructor UserDirectory_copyConstructor
		Destructor UserDirectory_destructor
		Execute UserDirectory_execute
	</Class>

	<Object>
		Name UserDirectory
		Class UserDirectoryClass
	</Object>

/*___+++CNF_END+++___*/
#endif

⌨️ 快捷键说明

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