home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / WEBSERVE / PI3 / PI3WEB.EXE / DEVEL / Plugins / UserDir.c < prev   
C/C++ Source or Header  |  1997-10-19  |  11KB  |  439 lines

  1. /*____________________________________________________________________________*\
  2.  
  3.  Copyright (c) 1997 John Roy. All rights reserved.
  4.  
  5.  These sources, libraries and applications are
  6.  FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  7.  as long as the following conditions are adhered to.
  8.  
  9.  Redistribution and use in source and binary forms, with or without
  10.  modification, are permitted provided that the following conditions
  11.  are met:
  12.  
  13.  1. Redistributions of source code must retain the above copyright
  14.     notice, this list of conditions and the following disclaimer. 
  15.  
  16.  2. Redistributions in binary form must reproduce the above copyright
  17.     notice, this list of conditions and the following disclaimer in
  18.     the documentation and/or other materials provided with the
  19.     distribution.
  20.  
  21.  3. Redistributions of any form whatsoever and all advertising materials 
  22.     mentioning features must contain the following
  23.     acknowledgment:
  24.     "This product includes software developed by John Roy
  25.     (http://www.johnroy.com/pi3/)."
  26.  
  27.  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28.  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29.  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  30.  IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  31.  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32.  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33.  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34.  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35.  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36.  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  37.  OF THE POSSIBILITY OF SUCH DAMAGE.
  38.  
  39.  *____________________________________________________________________________*|
  40.  *
  41.  * $Source: UserDir.c$
  42.  * $Date: Sun Aug 10 06:37:33 1997$
  43.  *
  44.  Description:
  45.     User directory mapping.
  46.  
  47. \*____________________________________________________________________________*/
  48. /* $SourceTop:$ */
  49.  
  50. #include <string.h>
  51. #include <assert.h>
  52.  
  53. #include "Pi3API.h"
  54.  
  55. #define KEY_CONF_USERDIRECTORYROOT        "UserDirectoryRoot"
  56. #define KEY_CONF_HTMLDIRECTORY            "HTMLDirectory"
  57.  
  58. /*____________________________________________________________________________*\
  59.  *
  60.  Description:
  61.     Documentation
  62. \*____________________________________________________________________________*/
  63. #if 0
  64.     /*
  65.     ** HTML documentation for this handler
  66.     */
  67. /*___+++HTMLDOC_BEGIN+++___*/
  68. Name:
  69.     UserDirectory
  70.  
  71. Description:
  72.     Remap user directories.
  73.  
  74.     Example
  75. <B><PRE>
  76.     UserDirectoryRoot="/home/"
  77.     HTMLDirectory="/public_html"
  78.  
  79.     /~jroy/hello.html --> /home/jroy/public_html/hello.html
  80.  
  81. </PRE></B>
  82.  
  83. Options:
  84. <H5>Overview</H5>
  85. <TABLE BORDER=1>
  86. <TH>Option
  87. <TH>Default
  88. <TH>Values
  89. <TH>Short Description
  90. <TH>Example(s)
  91.  
  92. <TR>
  93. <TD>UserDirectoryRoot
  94. <TD>+
  95. <TD>A directory path
  96. <TD>Root of user directories
  97. <TD>UserDirectoryRoot="/home/"
  98.  
  99. <TR>
  100. <TD>HTMLDirectory
  101. <TD>+
  102. <TD>A directory name
  103. <TD>User directory with web files
  104. <TD>HTMLDirectory="/public_html"
  105.  
  106. </TABLE>
  107. <STRONG>-</STRONG> in the <IT>default</IT> indicates no default<BR>
  108. <STRONG>+</STRONG> in the <IT>default</IT> indicates the field is mandatory<BR>
  109.  
  110. <!--
  111. Dont forget the slashes
  112. <H4>Description of Options</H4>
  113. <H5>
  114.     Pi3Expression
  115. </H5>
  116. Pi3Expression to write in the debug log.
  117. -->
  118.  
  119. Phase:
  120.     MAPPING
  121.  
  122. Returns:
  123.     PIAPI_COMPLETED if the directory was mapped, otherwise PIAPI_CONTINUE.
  124.  
  125. Note:
  126. Example:
  127.     <PRE>
  128.     <Object>
  129.         Name UserDirectory
  130.         Class UserDirectoryClass
  131.         UserDirectoryRoot "/home/"
  132.         HTMLDirectory "/public_html"
  133.     </Object>
  134.  
  135.     <Object>
  136.         ...
  137.         Handle UserDirectory
  138.         ...
  139.     </Object>
  140.     </PRE>
  141. /*___+++HTMLDOC_END+++___*/
  142. #endif
  143.  
  144. /*____________________________________________________________________________*\
  145.  *
  146.  Description:
  147. \*____________________________________________________________________________*/
  148. struct _UserDirectory
  149. {
  150.     char *pRoot;
  151.     int iRootLen;
  152.     char *pDirectory;
  153.     int iDirectoryLen;
  154. };
  155. typedef struct _UserDirectory UserDirectory;
  156.  
  157. /*____________________________________________________________________________*\
  158.  *
  159.  Function:
  160.  Synopsis:
  161.  Description:
  162. \*____________________________________________________________________________*/
  163. int UserDirectory_fnParameter( void *pData, const char *pVar, const char *pVal,
  164.     const char *pWhere )
  165. {
  166.     PIObject *pObject = (PIObject *)pData;
  167.     UserDirectory *pUserDirectory = (UserDirectory *)PIObject_getUserData( pObject );
  168.     (void)pUserDirectory;
  169.     assert( pVar );
  170.     assert( pVal );
  171.  
  172.     if ( !PIUtil_stricmp( pVar, KEY_CONF_USERDIRECTORYROOT) )
  173.         {
  174.         pUserDirectory->pRoot = PIUtil_strdup( pVal );
  175.         pUserDirectory->iRootLen = strlen( pUserDirectory->pRoot );
  176.         }
  177.     else if ( !PIUtil_stricmp( pVar, KEY_CONF_HTMLDIRECTORY) )
  178.         {
  179.         pUserDirectory->pDirectory = PIUtil_strdup( pVal );
  180.         pUserDirectory->iDirectoryLen = strlen( pUserDirectory->pDirectory );
  181.         }
  182.     else
  183.         {
  184.         /* --- Configuration error --- */
  185.         PILog_addMessage( PIObject_getDB( pObject ),
  186.             PIObject_getConfigurationDB( pObject ),
  187.             PILOG_ERROR, "UserDirectory: %sUnknown directive '%s'.",
  188.             pWhere, pVar );
  189.         return 0;
  190.         };
  191.     return 1;
  192. }
  193.  
  194. /*____________________________________________________________________________*\
  195.  *
  196.  Function:
  197.  Synopsis:
  198.  Description:
  199. \*____________________________________________________________________________*/
  200. int UserDirectory_init( UserDirectory *pUserDirectory, PIObject *pObject, int iArgc,
  201.     const char *ppArgv[] )
  202. {
  203.     int iRet = PIObject_readParameters( pObject, iArgc, ppArgv,
  204.         UserDirectory_fnParameter, pObject );
  205.  
  206.     /* --- set defaults --- */
  207.     if ( iRet )
  208.         {
  209.         if ( !pUserDirectory->pRoot )    
  210.             {
  211.             PILog_addMessage(
  212.                 PIObject_getDB( pObject ),    
  213.                 PIObject_getConfigurationDB( pObject ),
  214.                 PILOG_ERROR, "%s", "UserDirectory: 'UserDirectoryRoot' \
  215. not specified." );
  216.             iRet = 0;
  217.             }
  218.         else if ( !pUserDirectory->pDirectory )    
  219.             {
  220.             PILog_addMessage(
  221.                 PIObject_getDB( pObject ),    
  222.                 PIObject_getConfigurationDB( pObject ),
  223.                 PILOG_ERROR, "%s", "UserDirectory: 'HTMLDirectory' \
  224. not specified." );
  225.             iRet = 0;
  226.             };
  227.         };
  228.  
  229.     return iRet;
  230. }
  231.  
  232. /*____________________________________________________________________________*\
  233.  *
  234.  Function:
  235.  Synopsis:
  236.  Description:
  237. \*____________________________________________________________________________*/
  238. int UserDirectory_handle( PIHTTP *pPIHTTP, UserDirectory *pUserDirectory )
  239. {
  240.     const char *pPath;
  241.     int i;
  242.     enum { BUF_SIZE=2047 };
  243.     char szBuffer[BUF_SIZE+1];
  244.     char *pBuffer = szBuffer;
  245.     int iSize;
  246.  
  247.     assert( pUserDirectory );
  248.     assert( pUserDirectory->pRoot );
  249.     assert( pUserDirectory->pDirectory );
  250.  
  251.     /* ---
  252.     Get Path
  253.     --- */
  254.     pPath = (const char *)PIDB_lookup( pPIHTTP->pResponseDB, PIDBTYPE_STRING,
  255.         KEY_INT_PATH, 0 );
  256.  
  257.     /* ---
  258.     See if this path is not a user path
  259.     --- */
  260.     if ( !pPath || *pPath!='/' || pPath[1]!='~' )
  261.         {
  262.         return PIAPI_CONTINUE;
  263.         };
  264.  
  265.     /* ---
  266.     Scan to first '/' character
  267.     --- */
  268.     pPath = &( pPath[2] );
  269.     for( i=0; pPath[i] && pPath[i]!='/'; i++  );
  270.  
  271.     /* ---
  272.     Write in root 
  273.     --- */
  274.     iSize = BUF_SIZE;
  275.     if ( !iSize ) { goto buffer_error; };
  276.     strncpy( pBuffer, pUserDirectory->pRoot, iSize );
  277.     iSize -= pUserDirectory->iRootLen;
  278.     pBuffer = &( pBuffer[pUserDirectory->iRootLen] );
  279.  
  280.     /* ---
  281.     Then user name
  282.     --- */
  283.     if ( iSize<i ) { goto buffer_error; };
  284.     if ( i )
  285.         {
  286.         strncat( pBuffer, pPath, i );
  287.         iSize -= i;
  288.         pBuffer = &( pBuffer[i] );
  289.         }; 
  290.  
  291.     /* ---
  292.     Then HTML directory
  293.     --- */
  294.     if ( iSize<pUserDirectory->iDirectoryLen ) { goto buffer_error; };
  295.     if ( pUserDirectory->iDirectoryLen )
  296.         {
  297.         strcat( pBuffer, pUserDirectory->pDirectory );
  298.         iSize -= pUserDirectory->iDirectoryLen;
  299.         pBuffer = &( pBuffer[pUserDirectory->iDirectoryLen] );
  300.         };
  301.  
  302.     /* ---
  303.     Then the path that followed the user directory
  304.     --- */
  305.     pPath = &( pPath[i] );
  306.     i = strlen( pPath );
  307.     if ( iSize<i ) { goto buffer_error; };
  308.     if ( i )
  309.         {
  310.         strcat( pBuffer, pPath );
  311.         };
  312.  
  313.     /* ---
  314.     Done, replace path
  315.     --- */
  316.     PIDB_replace( pPIHTTP->pResponseDB, PIDBTYPE_STRING, KEY_INT_PATH,
  317.         szBuffer, 0 );
  318.  
  319.     /* 
  320.     ** NOTE: Verify that path is world readable? (UNIX only)
  321.     */
  322.  
  323.     return PIAPI_COMPLETED;
  324.  
  325. buffer_error:
  326.     
  327.     HTTPCore_logError( pPIHTTP, "UserDirectory: Internal error remapping \
  328. to user directory. Path components too long for internal buffer." );
  329.  
  330.     /* ---
  331.     Internal redirect to error handler
  332.     --- */
  333.     return HTTPUtil_doHTTPError( pPIHTTP, ST_INTERNALERROR );
  334. }
  335.  
  336. /*____________________________________________________________________________*\
  337.  *
  338.  Function:
  339.  Synopsis:
  340.  Description:
  341. \*____________________________________________________________________________*/
  342. PUBLIC_PIAPI int UserDirectory_onClassLoad( PIClass_LoadAction eLoad, int i,
  343.     const char *a[] )
  344. {
  345.     return PIAPI_COMPLETED;
  346. }
  347.  
  348. /*____________________________________________________________________________*\
  349.  *
  350.  Function:
  351.  Synopsis:
  352.  Description:
  353. \*____________________________________________________________________________*/
  354. PUBLIC_PIAPI int UserDirectory_constructor( PIObject *pObj,
  355.     int iArgc, const char *ppArgv[] )
  356. {
  357.     UserDirectory *pUserDirectory = PIUtil_malloc( sizeof( UserDirectory ) );
  358.     memset( pUserDirectory, 0, sizeof( UserDirectory ) );
  359.     PIObject_setUserData( pObj, pUserDirectory );
  360.     if ( !UserDirectory_init( pUserDirectory, pObj, iArgc, ppArgv ) )
  361.         {
  362.         return PIAPI_ERROR;
  363.         };
  364.     return PIAPI_COMPLETED;
  365. }
  366.  
  367. /*____________________________________________________________________________*\
  368.  *
  369.  Function:
  370.  Synopsis:
  371.  Description:
  372. \*____________________________________________________________________________*/
  373. PUBLIC_PIAPI int UserDirectory_copyConstructor( PIObject *o, int i, const char *a[] )
  374. {
  375.     return PIAPI_ERROR;
  376. }
  377.  
  378. /*____________________________________________________________________________*\
  379.  *
  380.  Function:
  381.  Synopsis:
  382.  Description:
  383. \*____________________________________________________________________________*/
  384. PUBLIC_PIAPI int UserDirectory_execute( PIObject *pObj,
  385.     int iArgc, const char *ppArgv[] )
  386. {
  387.     PIHTTP *pPIHTTP;
  388.  
  389.     if ( iArgc<1 )
  390.         { return PIAPI_ERROR; };
  391.  
  392.     pPIHTTP = (PIHTTP *)*ppArgv;
  393.  
  394.     if ( pPIHTTP->ciPhase!=PH_MAPPING )
  395.         { return PIAPI_ERROR; };
  396.  
  397.     return UserDirectory_handle( pPIHTTP, 
  398.         (UserDirectory *)PIObject_getUserData( pObj ) ); 
  399. }
  400.  
  401. /*____________________________________________________________________________*\
  402.  *
  403.  Function:
  404.  Synopsis:
  405.  Description:
  406. \*____________________________________________________________________________*/
  407. PUBLIC_PIAPI int UserDirectory_destructor( PIObject *pObj, int i, const char *a[] )
  408. {
  409.     UserDirectory *pUserDirectory = (UserDirectory *)PIObject_getUserData( pObj );
  410.     if ( pUserDirectory->pRoot )
  411.         { PIUtil_free( pUserDirectory->pRoot ); };
  412.     if ( pUserDirectory->pDirectory )
  413.         { PIUtil_free( pUserDirectory->pDirectory ); };
  414.     PIUtil_free( pUserDirectory );
  415.     return PIAPI_COMPLETED;
  416. }
  417.  
  418. #if 0
  419. /*___+++CNF_BEGIN+++___*/
  420.     <Class>
  421.         Name UserDirectoryClass
  422.         Type LogicExtension
  423.         Library Plugins
  424.         OnClassLoad UserDirectory_onClassLoad
  425.         Constructor UserDirectory_constructor
  426.         CopyConstructor UserDirectory_copyConstructor
  427.         Destructor UserDirectory_destructor
  428.         Execute UserDirectory_execute
  429.     </Class>
  430.  
  431.     <Object>
  432.         Name UserDirectory
  433.         Class UserDirectoryClass
  434.     </Object>
  435.  
  436. /*___+++CNF_END+++___*/
  437. #endif
  438.  
  439.