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

📄 dir_plgn.c

📁 基于nucleus实时操作系统的webserver源代码
💻 C
字号:
/************************************************************************
*                                                                          
*    CopyrIght (c)  1993 - 2001 Accelerated Technology, Inc.               
*                                                                          
* PROPRIETARY RIGHTS of Accelerated Technology are involved in the subject 
* matter of this material.  All manufacturing, reproduction, use and sales 
* rights pertaining to this subject matter are governed by the license     
* agreement.  The recipient of this software implicity accepts the terms   
* of the license.                                                          
*                                                                          
*************************************************************************/

/************************************************************************
*                                                                       
* FILE NAME                                             VERSION          
*                                                                       
*       dir_plgn.c - Directory Plugin                     1.5      
*                                                                       
* COMPONENT                                                             
*         
*       Nucleus WebServ                                                              
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       File that contains the DIR plugin information.                   
*                                                                       
* DATA STRUCTURES                                                       
*       
*       None                                                                
*                                                                       
* FUNCTIONS                                                             
*                                                                       
*       DIR_List_Directory      Plugin to list all files in file system. 
*       DIR_Pad                 Converts int to char* and right justifies
*                                                                       
* DEPENDENCIES                                                          
*                                                                       
*       nu_websrv.h       
*                                                                       
*************************************************************************/

#include "webserv/inc/nu_websr.h"

#ifdef WS_LIST_DIR_PLGN

static VOID DIR_Pad(UINT32 number, CHAR* buffer, INT buf_size);

extern WS_FS_FILE * HTTP_Fs_File;

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       DIR_List_Directory                                                   
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       This function gets the structure of the file system and outputs  
*       to the HTTP client the directory structure and the number of     
*       embedded files.  This operation is a plugin and must be          
*       registered within the server before being used.                  
*                                                                       
* INPUTS                                                                
*                                                                       
*       *req                    Pointer to Request structure that
*                               holds all information pertaining 
*                               to  the HTTP request.            
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       None
*                                                                       
*************************************************************************/

#ifdef WS_FS_IN_MEMORY

INT DIR_List_Directory(WS_REQUEST *req)
{
    INT             num_ent = 0;
    CHAR            *file;
    CHAR            outb[WS_OUTBLEN];
    CHAR            temp[10];
    WS_FS_FILE      *f;
    INT32           total = 0;

    /* Build the response header */
    HTTP_Response_Header( req, WS_PROTO_OK );
    HTTP_Header_Name_Insert( WS_CONTENT_TYPE, WS_TYPE_TXT_HTML, req->ws_response );
    HTTP_Initiate_Response(req, WS_PLUGIN_PROTO);

    /* Begin the creation of the HTML page */
    strcpy(outb, "<HTML><HEAD>\n<TITLE>Index of ");
    strcat(outb, "/" );
    strcat(outb, "</TITLE>\n</HEAD><BODY>\n" );
    strcat(outb, "<H1>Directory Index of ");
    strcat(outb, "/");
    strcat(outb, "</H1>\n");

    WS_Write_To_Net(req, outb, strlen( outb ), WS_PLUGIN_DATA);

    strcpy(outb, "<pre>\n");
    f = HTTP_Fs_File;

    /*
     * Loop through, getting information from every file stored in memory,
     * and then create a line of that information on the web page.
     */
    while( f )
    {
        file = f->ws_name; 
        total += f->ws_length;

        /* Convert the length of the file to ASCII */
        DIR_Pad((UINT32)f->ws_length, temp, 10);

        strcat(outb, temp);
        strcat(outb, " bytes    ");

        /* Create an anchor so that user can link straight to file */
        strcat(outb, "<A HREF=\"");
        strcat(outb, file);
        strcat(outb, "\">");
        strcat(outb, file);
        strcat(outb, "</A>\n"); 

        /* Write this line out and begin the next line */
        WS_Write_To_Net(req, outb, strlen(outb), WS_PLUGIN_DATA);
        outb[0]=0;
        
        num_ent++;
        
        f = f->ws_next;
    }
    
    /* Finish the page by printing some statics */    
    strcpy(outb, "\nTotal Entries ");
    strcat(outb, (CHAR *)NU_ITOA(num_ent, temp, 10));
    strcat(outb, "\nTotal Size    ");
    strcat(outb, (CHAR *)NU_ULTOA((UNSIGNED)total, temp, 10));
    strcat(outb, "\n</pre>\n</body>\n</html>");

    /* The page is done, send it on to the user */
    WS_Write_To_Net( req, outb, strlen( outb ), WS_PLUGIN_DATA);
    WS_Write_To_Net( req, NU_NULL, 0, WS_PLUGIN_SEND);

    return(WS_REQ_PROCEED);
}

#else

INT DIR_List_Directory(WS_REQUEST *req )
{
    INT             num_ent = 0;
    CHAR            outb[WS_OUTBLEN];
    CHAR            temp[40];
    CHAR            *dir;
    CHAR            dir_temp[40];
    STATUS          status;
    FAL_DIR         statobj;
    INT32           total = 0;
    tm_time         fal_time;
   
    /* Build the response header */
    HTTP_Response_Header( req, WS_PROTO_OK );
    HTTP_Header_Name_Insert( WS_CONTENT_TYPE, WS_TYPE_TXT_HTML, req->ws_response );
    HTTP_Initiate_Response(req, WS_PLUGIN_PROTO);

    /* Get the directory that the user would like a listing of */
    dir = HTTP_Token_Value_by_Name("dir", req);
    if(!dir)
    {
        dir_temp[0] = 'A' + FAL_Get_Curr_Drive();
        dir_temp[1] = ':';
        dir_temp[2] = '\0';
        dir = dir_temp;
    }

    /* Begin the creation of the HTML page */
    strcpy(outb, "<HTML><HEAD>\n<TITLE>Index of ");
    strcat(outb, dir );
    strcat(outb, "</TITLE>\n</HEAD><BODY>\n" );
    strcat(outb, "<H1>Directory Index of ");
    strcat(outb, dir);
    strcat(outb, "</H1>\n");

    WS_Write_To_Net(req, outb, strlen( outb ), WS_PLUGIN_DATA);

    /* Set up the search pattern and send it off to FAL */
    strcpy(outb, dir);
    strcat(outb, "\\*.*");
    status = FAL_Find_First(outb, &statobj, NU_NULL);

    /* If the directory exists, continue */
    if(status == NU_SUCCESS)
    {
        FAL_Set_Curr_Dir(dir);
        strcpy(outb, "<pre>");
        
        /*
         * Loop through, getting information from every file stored in memory,
         * and then create a line of that information on the web page.
         */
        while( status == NU_SUCCESS )
        {
            fal_time = FAL_Object_Mod_Time(&statobj);
                
            /* Print the date */
            DIR_Pad(fal_time.tm_year, temp, 8);
            strcat(outb, temp);
            strcat(outb, "-");
            DIR_Pad(fal_time.tm_mon, temp, 3);
            strcat(outb, temp);
            strcat(outb, "-");
            DIR_Pad(fal_time.tm_mday, temp, 3);
            strcat(outb, temp);
            
            /* Print the time */
            DIR_Pad(fal_time.tm_hour, temp, 6);
            strcat(outb, temp);
            strcat(outb, ":");
            DIR_Pad(fal_time.tm_min, temp, 3);
            strcat(outb, temp);
            strcat(outb, ":");
            DIR_Pad(fal_time.tm_sec, temp, 3);
            strcat(outb, temp);
            strcat(outb, "   ");

            /* If this is a directory, label it so */
            if(FAL_Get_Attribute(statobj) & ADIRENT) 
                strcat(outb, "      &lt;DIR&gt; ");
            else
            {
                /* If it is not a directory, print the size of the file */
                total += FAL_Object_File_Length(&statobj);
                DIR_Pad(FAL_Object_File_Length(&statobj), temp, 10);
                strcat(outb, temp);
            }

            /* Begin the anchor for the user to link through */
            strcat(outb,"  <A HREF=\"");
            
            /* If this is a directory, set the link to come back to this plugin */
            if(FAL_Get_Attribute(statobj) & ADIRENT) 
            {   
                strcat(outb, "dir");
                strcat(outb, "?");
                FAL_Store_Name(temp, &statobj);
                strcat(dir, temp);
                strcat(outb, dir);
            }
            else
            {
                FAL_Store_Name(temp, &statobj);
                strcat(outb, temp);
            }            
            
            strcat(outb, "\"> ");
            
            /* Print the file name */
            strcat(outb, temp);
            strcat(outb, "</A>\n"); 

            /* Get the next file in the directory */
            status = FAL_Find_Next(&statobj, NU_NULL);

            /* Write this line out and begin the next line */
            WS_Write_To_Net(req, outb, strlen(outb), WS_PLUGIN_DATA);
            outb[0] = 0;
            
            num_ent++;
            
        }
        
        FAL_Find_Close(NU_NULL, &statobj);
    }

    /* Finish the page by printing some statics */    
    strcpy(outb, "\nTotal Entries ");
    strcat(outb, (CHAR *)NU_ITOA(num_ent, temp, 10));
    strcat(outb, "\nTotal Size    ");
    strcat(outb, (CHAR *)NU_ULTOA(total, temp, 10));
    strcat(outb, "\n</pre>\n</body>\n</html>");

    /* The page is done, send it on to the user */
    WS_Write_To_Net( req, outb, (INT)strlen( outb ), WS_PLUGIN_DATA);
    WS_Write_To_Net( req, NU_NULL, 0, WS_PLUGIN_SEND);

    return(WS_REQ_PROCEED);
}
#endif

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       DIR_Pad                                                   
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       This function converts an integer to an ASCII string.  It
*       right justifies the string using spaces for padding.
*                                                                       
* INPUTS                                                                
*                                                                       
*       number                  Integer to be converted 
*       *buffer                 Pointer to the buffer where the
*                               new string will reside.
*       buf_size                Size of the buffer.
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       None
*                                                                       
*************************************************************************/

static VOID DIR_Pad(UINT32 number, CHAR* buffer, INT buf_size)
{
    INT     x;
    INT     i;

    /* Move to the last position in the array */
    buf_size--;

    /* Convert the number and place it into the buffer */
    NU_ULTOA(number, buffer, 10);

    /* Make the last character in the string a null terminator */
    buffer[buf_size--] = '\0';
    x = strlen(buffer);

    /* 
     * Shift the string to the end of the array, there by making it
     * right justified.
     */
    for(i = buf_size; x; i--)
        buffer[i] = buffer[--x];

    /* Pad the begining of the string with spaces */
    while(i > -1)
        buffer[i--] = ' ';

}

#endif /* WS_LIST_DIR_PLGN */

⌨️ 快捷键说明

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