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

📄 http_psr.c

📁 基于nucleus实时操作系统的webserver源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
        
#ifdef WS_BASIC_AUTH
    if (BSC_Parse_Auth(req)!= NU_SUCCESS)
        return(WPW_BASIC_AUTH_FAILED);
#endif
    
        /* if the user has not been authenticeated
         * allow them the choice to login.
         */
    if( (HTTP_Authenticated_Check(req) == FAILURE) && ((HTTP_Global_Access(req)) == FAILURE))
    {
        HTTP_Auth_Control(p, WS_A_DISABLE, NU_NULL);
        req->ws_server->ws_master.ws_auth_state = 1;
        req->ws_fname = req->ws_server->ws_master.ws_auth_uri;
        HTTP_Send_File(req);
        return(NU_SUCCESS);
    }

    if((*(req->ws_http_ver) == WS_HTTP_11) && (HTTP_In_String("Host: ", line) == 0))
    {
        /*  If a method comes in from an HTTP 1.1 client without the Host tag,
         *  it is necessary to return a Bad Request response.
         */
        strcpy(req->ws_response, HTTP_Text_H400);
        strcpy(buf, HTTP_Text_D400);
        len = strlen(buf);
        HTTP_Initiate_Response(req, WS_PLUGIN_PROTO);
        WS_Write_To_Net(req, buf, (UINT32)len, WS_PLUGIN_DATA);
        WS_Write_To_Net(req, 0, 0, WS_PLUGIN_SEND);
        return(NU_SUCCESS);
    }

    switch(j)
    {
        
    case    WS_GET:
        HTTP_Process_GET(req);
        break;
        
    case    WS_HEAD:
        if( HTTP_File_Match(req->ws_fname, &start, &len, &type) )
            HTTP_Make_Mime_Header(req, len);
        break;
        
    case    WS_POST:
        HTTP_Process_POST(req, line);
        break;
        
    }/* end switch */
    
    return(NU_SUCCESS);
}

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       HTTP_Process_GET                                                      
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       The GET request requests the return or                           
*       URI on the server. This is the most used                         
*       method and is how the client request fixed                       
*       most fixed Web Pages.                                            
*                                                                       
* INPUTS                                                                
*                                                                       
*       *req                    Pointer to Request structure that
*                               holds all information pertaining 
*                               to the HTTP request.             
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       None
*                                                                       
*************************************************************************/
static VOID  HTTP_Process_GET(WS_REQUEST* req)
{
    INT             i;
    UINT32          len;
    WS_STAT         stat;
    INT             (*plugin)(WS_REQUEST *);
    CHAR            *s;
    CHAR            buf[260];

#ifdef WS_REDIRECT_IP
    CHAR            url_buf[WS_URL_LEN];
#endif
    
#ifdef NU_WEBSERV_DEBUG
    printf("GET:%s\n", req->ws_fname);
#endif
    
    s = HTTP_In_String("?",req->ws_fname);
    if( s )
    {
        *s++ = '\0';
        HTTP_Build_Token_Array(s, &req->ws_pg_args);
    }

    /* 
     *  If we reference the root node "/" of our filesystem
     *  redirect the browser to the default URL.
     *  This is usually defined to be either default.htm 
     *  or index.htm
     */
    
    if( (req->ws_fname[0] == '/') && (req->ws_fname[1] == '\0') )
    {
#ifdef WS_REDIRECT_IP
        HTTP_Uri_To_Url(req, WS_DEFAULT_URI, url_buf);
        HTTP_Redirect_Client(req, url_buf);
        return;
#else
        req->ws_fname = WS_DEFAULT_URI;
#endif
    }
    
    req->ws_stat = &stat;
    i = HTTP_File_Request_Status(req); 
    
    switch( i )
    {
        
    case ENOFILE:
        
        /*
         *  Send the HTTP 404 Not Found Reply 
         *  Load the Header Data 
         */
        strcpy(req->ws_response,HTTP_Text_H404);

        /*
         *  Place the not found file name in the data section of the 404
         *  command.
         */
        strcpy(buf, HTTP_Text_D404);
        strcat(buf, req->ws_fname);
        strcat(buf, HTTP_Text_D404c);
        
        len = strlen(buf);

        /*  Write the 404 PLUGIN PROTO */
        HTTP_Initiate_Response(req, WS_PLUGIN_PROTO);

        /*  Write the Plugin Data */
        WS_Write_To_Net(req, buf, len, WS_PLUGIN_DATA);

        /*  Let WS_Write_To_Net know to send the data */
        WS_Write_To_Net(req, 0, 0, WS_PLUGIN_SEND);
        break;
        
    case SUCCESS:
        
        if( req->ws_stat->ws_flags == WS_PLUGIN )
        {
            plugin = req->ws_stat->plugin;
            i = (plugin)(req);

            /* Post plugin error check */
            if(i != WS_REQ_PROCEED)
            {
                strcpy(req->ws_response, HTTP_Text_H500);
                strcpy(buf, HTTP_Text_D500);
                strcat(buf, "Plugin Failure");
                strcat(buf, HTTP_Text_D500A);
                len = strlen(buf);
                HTTP_Initiate_Response(req, WS_PLUGIN_PROTO);
                WS_Write_To_Net(req, buf, len, WS_PLUGIN_DATA);
                WS_Write_To_Net(req, 0, 0, WS_PLUGIN_SEND);
            }
            
            break;
        }

        /* check for server side include */ 
        if( HTTP_Is_SSI(req) == NU_SUCCESS ) 
            HTTP_Process_SSI(req);
        else
            HTTP_Serve_File(req);
        break;
        
    default:
        HTTP_Server_Error(req,"Fallthru get");
        break;
    }       
}

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       HTTP_Serve_File                                                        
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       This function sends a file to the client(web browser).           
*                                                                       
* INPUTS                                                                
*                                                                       
*       *req                    Pointer to Request structure that
*                               holds all information pertaining 
*                               to  the HTTP request.            
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       None
*                                                                       
*************************************************************************/
VOID  HTTP_Serve_File(WS_REQUEST *req)
{
    if( req->ws_stat->ws_flags & WS_INCORE )               
        /* an in-memory file...simply send down the socket */
        HTTP_Send_File(req); 
    else
    {
        /* setup the protocol to generate the response headers for the trasfer */
        
        HTTP_Make_Mime_Header(req, req->ws_stat->ws_size);
        
        /* all that remains  is to copy the data to the socket and return */
        WS_Send_File(req);                           
    }
}

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       HTTP_Process_SSI                                                      
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       This function processed server side includes. Note compressed    
*       files are not handled within this routine.                       
*                                                                       
* INPUTS                                                                
*                                                                       
*       *req                    Pointer to Request structure that
*                               holds all information pertaining 
*                               to  the HTTP request.            
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       None
*                                                                       
*************************************************************************/
static VOID  HTTP_Process_SSI(WS_REQUEST *req)
{
    CHAR        *file;
    CHAR HUGE   *insert;
    CHAR HUGE   *p;
    CHAR HUGE   *q;
    CHAR        plugin_s[WS_SSI_NAME_LEN];
    CHAR        ssierror[WS_SSI_LINE];
    CHAR HUGE   *last_start;
    INT         (*plugin)(WS_REQUEST *);
    INT         i;
    INT         ssilen;
    CHAR        *last_byte;
    CHAR        *ssi_buf = req->ws_rdata->ws_ssi_buf;
    CHAR        buf[260];
    STATUS      status;

    if( (req->ws_stat->ws_flags & WS_INCORE ) == 0 )         
    {
        /* this file has to be read in */
        status = NU_Allocate_Memory(&System_Memory, (VOID*)&file,
                                    (UNSIGNED)req->ws_stat->ws_size, NU_NO_SUSPEND);
        
        if(status != NU_SUCCESS)
        {
            return;
        }

        WS_Read_File(req, file); 
    }
    else
        file = req->ws_stat->ws_address;
    
    last_byte = (CHAR*)((CHAR HUGE*)file + req->ws_stat->ws_size);
    
    /* setup all the protocol for a transfer */
    
    HTTP_Response_Header(req, WS_PROTO_OK);
    HTTP_Header_Name_Insert(WS_CONTENT_TYPE, WS_TYPE_TXT_HTML, req->ws_response);
    /*  Send the SSI Proto Header */
    HTTP_Initiate_Response(req, WS_PLUGIN_PROTO);
    
    last_start = file;
    insert = (HTTP_Find_Token(WS_SSI_TOKEN, last_start, last_byte));
    while( insert )
    {
        
        p = insert;
        q = ssi_buf;

⌨️ 快捷键说明

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