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

📄 http_psr.c

📁 基于nucleus实时操作系统的webserver源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
*                                                                       
*       buffer                  String that holds the clients    
*                               plugin arguements.         
*       token_info              Pointer to the array of tokens
*                                                                       
* OUTPUTS                                                               
*                                                                       
*                                                                       
*************************************************************************/
VOID HTTP_Build_Token_Array(CHAR HUGE *buffer, WS_TOKEN_INFO *token_info)
{
    INT16       buf_count = 0;
    INT16       arr_count = 0;
    INT16       *token_array;

    buffer = TLS_Normalize_Ptr(buffer);
    
    token_array = token_info->ws_token_array;
    token_info->ws_token_string = (CHAR*)buffer;

    /* Be sure to add token to end of list */
    while((token_array[arr_count] != -1) && (arr_count < WS_TOKEN_HEAP))
        arr_count++;

    /* To prevent including a line feed or cariage return in
     * the array, we must state that such characters end the
     * buffer.
     */
    while((buffer[buf_count] > 31) && (arr_count < WS_TOKEN_HEAP))
    {
        token_array[arr_count++] = buf_count;

        while((buffer[buf_count] > 31) && (buffer[buf_count] != '='))
            buf_count++;
        if(buffer[buf_count] > 31)
            buffer[buf_count++] = '\0';
        else
            break;

        /* Set a null terminator between the name and value
         * to allow for easy retrieval later
         */
        while((buffer[buf_count] > 31) && (buffer[buf_count] != '&'))
            buf_count++;

        /* Verify that this is not a terminating character */
        if(buffer[buf_count] > 31)
            buffer[buf_count++] = '\0';
        else
            buffer[buf_count] = '\0';
    }

    token_array[arr_count] = -1;
}

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       HTTP_Token_Value_by_Name                                                 
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       Function finds the value associated with a token name.  It compares     
*       a name string until it finds the correct token name and returns  
*       the token value.  If the name is not found, NULL is returned   
*                                                                       
* INPUTS                                                                
*                                                                       
*       *name                   Name of the token name to find.  
*       info                    Token structure that holds pointers
*                               to the buffer containing the token    
*                               arguments (Name and Value) .
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       *token_string[index]   The token value
*       NULL
*                                                                       
*************************************************************************/
CHAR* HTTP_Token_Value_by_Name(CHAR *name, WS_REQUEST *req)
{
    INT         count;
    CHAR HUGE   *token_string;
    INT16       *token_array;
    INT16       index;
    CHAR HUGE   *line;

    token_string = req->ws_pg_args.ws_token_string;

    /* Find the begining of the token arguments */
    if(req->ws_pg_args.ws_token_array[0] == -1)
    {
        line = (CHAR HUGE*)req->ws_rdata->ws_lbuf + req->ws_nbytes;
        TLS_Normalize_Ptr(line);
        
        /* Just in case we didn't receive what we were expecting */
        count = WS_Read_Net(req, (CHAR*)line, (WS_RECEIVE_SIZE - req->ws_nbytes),
                            (TICKS_PER_SECOND >> 1));

        if (count <= 0)
        {
            /* An Error has occured */
            return(NU_NULL);
        }

        req->ws_nbytes = req->ws_nbytes + count;
        
        /* If there is a header attached, move past to the arguments */
        if(HTTP_In_String(WS_CONTENT_LENGTH, line))
        {        
            line = HTTP_In_String("\r\n\r\n",line);
            if(line == NU_NULL)
            {
                HTTP_Client_Error(req, "bad POST");
            }

            line = line + 4;
        }
        
        HTTP_Build_Token_Array(line, &req->ws_pg_args);
        token_string = req->ws_pg_args.ws_token_string;
    }
    
    if(token_string != NU_NULL)
    {
        token_array = req->ws_pg_args.ws_token_array;

        for(count = 0; (token_array[count] != -1) && (count < WS_TOKEN_HEAP); count++)
        {
            index = token_array[count];
            
            /* Check each token name for a match */
            if(strcmp(name, (CHAR *)&token_string[index]) == 0)
            {
                /* A match was found, move past name to token value */
                while(token_string[index])
                    index++;
                index++;

                /* Return the token value */
                return((CHAR *)&token_string[index]);
            }
        }
    }

    /* There was an error in processing, return a null pointer */
    return(NU_NULL);
}

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       HTTP_Token_Value_by_Number                                                 
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       Function finds the value associated with a token number.  
*       The number is the position the token name and value occur
*       when sent by the client.  If the name is not found, 
*       NULL is returned   
*                                                                       
* INPUTS                                                                
*                                                                       
*       number                  Position of token in buffer.  
*       info                    Token structure that holds pointers
*                               to the buffer containing the token    
*                               arguments (Name and Value) .
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       *token_string[index]   The token value
*       NULL
*                                                                       
*************************************************************************/
CHAR* HTTP_Token_Value_by_Number(INT number, WS_REQUEST *req)
{
    INT         count;
    CHAR HUGE   *token_string;
    INT16       index;
    INT16       *token_array;
    CHAR HUGE   *line;

    token_array = req->ws_pg_args.ws_token_array;
    token_string = req->ws_pg_args.ws_token_string;

    /* Find the begining of the token arguments */
    if(req->ws_pg_args.ws_token_array[0] == -1)
    {
        line = (CHAR HUGE*)req->ws_rdata->ws_lbuf + req->ws_nbytes;
        TLS_Normalize_Ptr(line);
        
        /* Just in case we didn't receive what we were expecting */
        count = WS_Read_Net(req, (CHAR *)line, (WS_RECEIVE_SIZE - req->ws_nbytes),
                            (TICKS_PER_SECOND >> 1));

        if (count <= 0)
        {
            /* An Error has occured */
            return(NU_NULL);
        }
        
        req->ws_nbytes = req->ws_nbytes + count;
        /* If there is a header attached, move past to the arguments */
        if(HTTP_In_String(WS_CONTENT_LENGTH, line))
        {        
            line = HTTP_In_String("\r\n\r\n",line);
            if(line == FAILURE )
            {
                HTTP_Client_Error(req, "bad POST");
            }
            
            line = line + 4;
        }
        
        HTTP_Build_Token_Array(line, &req->ws_pg_args);
        token_string = req->ws_pg_args.ws_token_string;
    }
    
    /* Find the begining of the token arguments */
    if(token_string && (number < WS_TOKEN_HEAP))
    {
        index = token_array[number];
        
        /* Move past the name to the token value */
        while(token_string[index])
            index++;
        index++;

        /* Return the token value */
        return( (CHAR *)&token_string[index] );
    }
    
    /* There was an error in processing, return a null pointer */
    return(NU_NULL);
}

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       HTTP_File_Match                                                    
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       This function searches through the in core based file system   
*       for a client requested filename.                                 
*                                                                       
* INPUTS                                                                
*                                                                       
*       *s                      Filename to be examined.             
*       **start                 Pointer to start location in memory  
*                               of the file.                         
*       *length                 Pointer to the length of the file.   
*       *type                   Pointer to the type of file(ie gif,  
*                               htm, jpeg...                         
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       SUCCESS                 The filename was found in the
*                               file system structure
*       FAILURE                 The filename was not found             
*                                                                       
*************************************************************************/
static INT HTTP_File_Match(CHAR *s, CHAR **start, INT32 *length, INT *type)
{
    WS_FS_FILE  *f;
    CHAR        *t;

    f = HTTP_Fs_File;

    if( f == NU_NULL )
        return (FAILURE);
    while ( *s == '/' )
        s++;                        /* bump past any leading /'s */
    
    f = HTTP_Fs_File;
    while ( f )
    {
        
        if( f->ws_length)
        {
            t = f->ws_name;
            while ( *t == '/' )
                t++;
            if( strcmp(s,t) == 0 )
            {
                *start = f->ws_addr;
                *length = f->ws_length;
                *type   = f->ws_type;
                
                return(SUCCESS);
            }
        }
        f = f->ws_next;
    }

⌨️ 快捷键说明

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