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

📄 http_psr.c

📁 基于nucleus实时操作系统的webserver源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/************************************************************************
*                                                                          
*    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          
*                                                                       
*       http_psr.c - HTTP Parser                          1.5      
*                                                                       
* COMPONENT                                                             
*      
*       Nucleus WebServ                                                                 
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       This file holds the routines that are the heart of the Nucleus   
*       Webserv product. The function within are used to maintain all    
*       facets of the Webserv Product.  At this time the following       
*       are not shipping with the Nucleus Webserv product:               
*       Authentication and Encryption.  All other features are           
*       supported.                                                          
*                                                                       
* DATA STRUCTURES                                                       
*                                                                       
*       HTTP_Fs_File            Structure for the embedded file  
*                               system.                          
*                                                                
* FUNCTIONS                                                      
*                                                                
*       HTTP_Make_Mime_Header   Generate HTTP header with the    
*                               correct mime codes.              
*       HTTP_Authenticated_Check                                          
*       HTTP_Check_Timeout      Checks the timeout.              
*       HTTP_Send_File          Send file to browser over        
*                               network.                         
*       HTTP_Process_GET        Processes an HTTP response by    
*                               using the GET method.            
*       HTTP_Process_POST       Processes Post form commands.    
*       HTTP_Client_Error       Error Routine for the client.    
*       HTTP_Parse_Request      Parses HTTP request and calls    
*                               necessary routines required for  
*                               processing the parsed request.   
*       HTTP_Is_SSI             Checks for an SSI(Server Side    
*                               Include) tag.                    
*       HTTP_In_String          Compares string to actual buffer.                                     
*       HTTP_Process_SSI        Process a Get Operation with the 
*                               SSI extension.                  
*       HTTP_Serve_File         This function sends the file to  
*                               the client over the network.     
*       HTTP_Server_Error            Prints an error message from the                               
*                               server.                          
*       HTTP_Response_Header    Sets up the HTTP response.       
*       HTTP_Header_Name_Insert Inserts the name and value into  
*                               the output header of the HTTP    
*                               response.                        
*       HTTP_Initiate_Response  Starts the HTTP response to the  
*                               client.                          
*       HTTP_Auth_Control       Authentication Control routine.  
*       HTTP_Check_Auth_IP      Check if the ip address is       
*                               already authenticated.           
*       HTTP_IP_Match           Compares ip addresses for a      
*                               match.                           
*       HTTP_Send_Client_HTML_Msg Sends HTML error message to      
*                               client.                          
*       HTTP_Redirect_Client    Redirects the  user agent to     
*                               another URL.                     
*       HTTP_Setup_Plgn_Token   Parses the  clients plugin       
*                               arguements into tokens.          
*       HTTP_File_Match         Search for client requested file 
*                               name.                            
*       HTTP_Global_Access      Check what is global access set  
*                               to.                              
*       HTTP_Add_Authenticated  Adds an authenticated user                                       
*                               to the auth structure.           
*       HTTP_Free_Auth          Frees authentication structure   
*                               for a particular ip address.     
*       HTTP_Header_Num_Insert  Places name and numeric value    
*                               token into output header.        
*       HTTP_Register_Plugin    Register plugins.                
*       HTTP_File_Request_Status Checks to see if the file is     
*                               in memory or os-dependent file   
*                               system.                          
*       HTTP_Initialize_Server  Initializes Nucleus Web Server.  
*       HTTP_Find_Token         Finds a token in a string.       
*       HTTP_Bin_To_Hex         Convert binary to two hex        
*                               digits.                          
*       HTTP_Packed_Hex_To_Bin  Convert packed hex to binary.    
*       HTTP_Hex_to_Binary      Convert hex to binary.           
*       HTTP_Ascii_To_Nibble    Converts Ascii Hex nibble to     
*                               binary.                          
*       HTTP_Ascii_Hex_To_Bin   Converts a hex digit into binary.
*       HTTP_Find_Token_Value   Find a the value of a token name.
*       HTTP_Get_Mime_Type      Derives a mime type.             
*       HTTP_Uri_To_Url         Converts the uri to full URL.    
*                                                                       
* DEPENDENCIES                                                          
*                                                                       
*       nu_websrv.h                     
*                                                                       
*************************************************************************/

#include "webserv/inc/nu_websr.h"
#include "webserv/inc/wpw_auth.h"

static VOID         HTTP_Process_GET(WS_REQUEST* req);
static VOID         HTTP_Process_SSI(WS_REQUEST *req);
static STATUS       HTTP_Is_SSI(WS_REQUEST * req);
static VOID         HTTP_Process_POST(WS_REQUEST * req, CHAR* line);
static INT          HTTP_File_Match(CHAR * s, CHAR ** start, INT32 * length, INT * type);
static INT          HTTP_Authenticated_Check(WS_REQUEST * req);
static INT          HTTP_IP_Match(UINT8 * a, UINT8 * b );
static VOID         HTTP_Free_Auth(WS_AUTH * a);
static VOID         HTTP_Send_File(WS_REQUEST * req);
static INT          HTTP_Add_Authenticated(WS_SERVER *server, CHAR * ipaddr);
static WS_AUTH      *HTTP_Check_Auth_IP(WS_SERVER * server , CHAR * ip );
static INT          HTTP_Global_Access(WS_REQUEST * req);
static VOID         HTTP_Check_Timeout(WS_REQUEST * req);
static unsigned int HTTP_Ascii_Hex_To_Bin(UINT8* s);
static CHAR         *HTTP_Get_Mime_Type(WS_REQUEST * req);
static VOID         HTTP_Make_Mime_Header(WS_REQUEST * req, INT32 length);
static INT          HTTP_Ascii_To_Nibble(UINT8 * s); 
INT         HTTP_Hex_to_Binary(CHAR * s);

struct WS_PLUGIN_STRUCT HTTP_Plugins[WS_MAX_PLUGIN];

/*  The Header for HTTP 400 Bad Request */
static CHAR HTTP_Text_H400[]="\
HTTP/1.1 400 Bad Request\r\n\
Content-type: text/html\r\n";

/*  The Message Body for HTTP 400 Bad Request */
static CHAR HTTP_Text_D400[]="\
<HEAD><TITLE>400 Bad Request</TITLE></HEAD>\r\n\
<BODY><H1>400 Bad Request</H1>\r\n</BODY>";

/*  The Header for HTTP 404 not found message */
static CHAR HTTP_Text_H404[]="\
HTTP/1.1 404 Not Found\r\n\
Content-type: text/html\r\n";

/*  The Message Body for HTTP 404 not found */
static CHAR HTTP_Text_D404[]="\
<HEAD><TITLE>404 Not Found</TITLE></HEAD>\r\n\
<BODY><H1>404 Not Found</H1>\r\n\
Url '";
static CHAR HTTP_Text_D404c[]="' not found on server<P>\r\n</BODY>";

/*  The Message Header for HTTP 500 Internal Server Error */
static CHAR HTTP_Text_H500[]="\
HTTP/1.1 500 Internal Server Error\r\n\
Content-type: text/html\r\n";

/*  The Message Body for HTTP 500 Internal Server Error */
static CHAR HTTP_Text_D500[]="\
<HEAD><TITLE>500 Internal Server Error</TITLE></HEAD>\r\n\
<BODY><H1>500 Internal Server Error</H1>\r\n\
Error: '";

static CHAR HTTP_Text_D500A[]="' </BODY>";

/*  The Message Header for HTTP 501 Not Implemented */
static CHAR HTTP_Text_H501[]="\
HTTP/1.1 501 Not Implemented\r\n\
Content-type: text/html\r\n";

/*  The Message Body for HTTP 501 Not Implemented */
static CHAR HTTP_Text_D501[]="\
<HEAD><TITLE>501 Method Not Implemented</TITLE></HEAD>\r\n\
<BODY><H1>501 Method Not Implemented</H1>\r\n</BODY>";


/*  Upload.htm file that will be saved to file system if in Memory File System
 *  Is not to be Used.
 */
#ifndef WS_FS_IN_MEMORY
static CHAR *HTTP_Upload_Htm[]= {
    "<html>\r\n",
    "<head>\r\n",
    "<title>Nucleus Webserv Example of Uploading a File Via HTTP</title>\r\n",
    "</head>\r\n",
    "<body bgcolor=\"#FFFFFF\">\r\n\
    <h3><font color=\"#980040\" size=\"5\">ATI's Example of a Post Operation for a\
    File Upload!</font></h3>\r\n",
    "<h2><font color=\"#980040\" size=\"4\">To Upload a file via the Web, fill out the required\
    elements below:</font></h2>\r\n",
    "<FORM ENCTYPE=\"multipart/form-data\" ACTION=\"/upload\" METHOD=POST>\r\n",
    "<font color=\"#980040\" size=\"3\">Filename To Be Saved As On Server:</font>\
    <input name=\"save-as-filename\" size=35><p>\r\n",
    "<font color=\"#980040\" size=\"3\">Filename On Your Computer:</font>\
    <INPUT NAME=\"upload-file\" TYPE=\"file\"><p>\r\n",
    "<INPUT TYPE=\"submit\" VALUE=\"Send File\">\r\n",
    "</FORM>\r\n",
    "</body>\r\n",
    "</html>\r\n",
    ""};
#endif
    
extern WS_MIME_TABLE MIME_Mime_Table[];
extern NU_MEMORY_POOL    System_Memory;
 WS_FS_FILE Embed_Fs[];
WS_FS_FILE * HTTP_Fs_File;

extern WS_SERVER WS_Master_Server;

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       HTTP_Parse_Request                                               
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       We parse the HTTP request and call                               
*       the correct handler. Note that HEAD is exactly                   
*       like GET except no date is returned (only the                    
*       response headers).                                               
*                                                                       
* INPUTS                                                                
*                                                                       
*       *line                   Contains the HTTP characters that
*                               are to be parsed.                
*       *req                    Pointer to Request structure that
*                               holds all information pertaining 
*                               to the HTTP request.             
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       NU_SUCCESS
*                                                                       
*************************************************************************/
INT HTTP_Parse_Request(WS_REQUEST *req, CHAR *line)
{
    INT16       j;
    INT32       len;
    CHAR HUGE   *s;
    WS_SERVER   *p;
    CHAR        buf[260];
    INT16       token_list[WS_TOKEN_HEAP + 1];
    CHAR        *start;
    INT         type;

    req->ws_pg_args.ws_token_array = token_list;
    req->ws_pg_args.ws_token_array[0] = -1;
    req->ws_pg_args.ws_token_string = NU_NULL;

    p = &WS_Master_Server; 

    j = ((INT16)(line[0] + line[1] + line[2]));
    req->ws_method = j;

    switch( j )
    {
        
    case WS_GET:
    case WS_POST:
    case WS_HEAD:
        break;
        
    default:
#ifdef NU_WEBSERV_DEBUG 
        printf("Could not decode request\n");
        printf("C:%s\n", line);
#endif 
        
        /*  If a method comes in that is not supported send the 501 method
         *  Not implemented message.
         */
        strcpy(req->ws_response, HTTP_Text_H501);
        strcpy(buf, HTTP_Text_D501);
        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);
    }
    
    HTTP_Check_Timeout(req);           /* check for authentication timeout */
    
    s = line;
    while (*s != ' ')
        s++;
    s++;
    req->ws_fname = (CHAR*)s;
    while (*s != ' ')
        s++;
    *s = '\0';
    line = (CHAR*)s + 1;

⌨️ 快捷键说明

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