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

📄 ws_tasks.c

📁 基于nucleus实时操作系统的webserver源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/************************************************************************
*                                                                      
*       Copyright (c) 1993-2001 by 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 implicitly accepts the terms of the license.           
*                                                                      
*                                                                      
*************************************************************************

*************************************************************************
*
* FILE NAME                                     VERSION                
*                                                                      
*        ws_tasks.c - WebServ Tasks               1.5              
*                                                                      
* COMPONENT                                                            
*                                                                      
*        Nucleus Web Server Tasking                                    
*                                                                      
* DESCRIPTION                                                          
*                                                                      
*      This file holds the Nucleus Web Server tasks.  It handles task   
*      initialization.  The tasks initialized handle binding a socket,  
*      listening to a socket, accepting a connection, and processing a  
*      connection to handle HTTP 1.1/1.0 connections with a web browser.
*      Also contains support routines for the web serv product.         
*                                                                      
* DATA STRUCTURES                                                      
*                                                                      
*      main_entry_task_ptr         Pointer to TCP client task          
*                                  control block and initialize        
*                                  the network and the webserver.      
*      StrEcho                     Pointer to string echo task         
*                                  control block.                      
*      socketQueue                 Pointer to socket queue             
*                                  control block.                      
*                                                                      
* FUNCTIONS                                                            
*                                                                      
*      WS_Receive_Task             Initializes Nucleus Web Server,      
*                                  creates Web server task, opens       
*                                  socket connection, Binds socket      
*                                  connection, Listens to the socket    
*                                  connection, and Accepts sockets      
*                                  indefinitely and transmits the       
*                                  sockets through a queue to the server
*                                  task.                                
*      WS_Worker_Task              Receives a connection and processes  
*                                  the HTTP request.                    
*      WS_Webserv_Initialize       Initialize WebServ.                  
*      WS_File_Status              Check for external storage of file.                             
*      WS_Send_File                Write the file to socket.         
*      WS_Read_File                Copy the file to an array.        
*      WS_Write_To_Net             Writes data out to the Network.      
*      WS_Flush_Net                Flushes the output buffer.           
*      WS_Read_Net                 Read data from network once a        
*                                  connection has been made.            
*      WS_Get_Time                 Get time and convert it to seconds.  
*      WS_Write_File_System        Write to the mass storage medium.                      
*      WS_Name_File                Places the filename into correct     
*                                  format.                              
*      WS_Hp_Add_Data              Adds data to the hp.data             
*                                  structure item.                      
*      WS_Set_Client_Addr          Gets the Peer names address.         
*      WS_Remove_HTTP_Socket_Entry Removes a socket entry from the      
*                                  doubly linked list.                  
*      WS_Add_Socket_Entry         Adds an entry to the HTTP            
*                                  socket doubly linked list.                
*                                                                      
* DEPENDENCIES                                                         
*
*
*                                                                      
************************************************************************/

#include "webserv/inc/nu_websr.h"
#include "net/inc/netevent.h"


WS_SERVER WS_Master_Server;      /* declare space for the master server structure */


/* Define Application data structures.  */

extern NU_MEMORY_POOL   System_Memory;
NU_TASK                 WS_Receive_Task_CB;
NU_QUEUE                WS_Socket_Queue;
NU_TASK                 WS_Servers[WS_MAX_WORKER_TASKS];

/*  WS_TASKS function prototypes */

static VOID    WS_Receive_Task(UNSIGNED  argc, VOID  *argv);
static VOID    WS_Worker_Task(UNSIGNED argc, VOID  *argv);
static STATUS  WS_Make_Worker_Task(INT index);
static STATUS  WS_Set_Client_Addr(WS_REQUEST  *Req, INT sockfd);
static VOID    WS_Create_Socket_List(WS_SOCK_LIST *free_sock);
static STATUS  WS_Add_Socket_Entry(WS_SOCK_LIST *http_sock, WS_SOCK_LIST *free_sock, INT socket);
static VOID    WS_Remove_Socket_Entry(WS_SOCK_LIST *http_sock, WS_SOCK_LIST *free_sock, WS_SOCKET_STRUCT *hsock_ent);
static STATUS  WS_Hp_Add_Data(CHAR *buf, INT32 size,WS_HTTP_PLUGIN *hp);


/************************************************************************
* FUNCTION                                                             
*                                                                      
*     WS_Webserv_Initialize                                        
*                                                                      
* DESCRIPTION                                                          
*                                                                      
*     Function that allows easy interface to hook in the Nucleus       
*     Webserv product. This function initializes the web server tasks  
*     and the queue.                                                   
*                                                                      
* AUTHOR                                                               
*                                                                      
*     PicoServer Embedded Web Server                                   
*                                                                      
*     Copywrite (c) 1995 1996 1997 CNiT                                
*                                                                      
*     Communication and Information Technology                         
*                                                                      
* INPUTS                                                               
*                                                                      
*     None.                                      
*                                                                      
* OUTPUTS                                                              
*                                                                      
*     None.                                    
*                                                                      
************************************************************************/

STATUS WS_Webserv_Initialize(VOID)
{
    VOID           *pointer;
    STATUS         status;
    INT            task_num = 0;

    status = HTTP_Initialize_Server();
    if (status != NU_SUCCESS)
    {
#ifdef NU_WEBSERV_DEBUG
        printf ("Can not allocate memory for file structure.\n");
#endif
        return(status);
    }

    /* Create the Application tasks. */
    
    status = NU_Allocate_Memory(&System_Memory, &pointer, 2000,
                                NU_NO_SUSPEND);
    
    if (status != NU_SUCCESS)
    {
        
#ifdef NU_WEBSERV_DEBUG
        printf ("Can not create memory for WS_Receive_Task.\n");
#endif
        return(status);
    }
    
    /*  Create Nucleus Web Server Receiving Task  */
    status = NU_Create_Task(&WS_Receive_Task_CB, "NUWebSrv",WS_Receive_Task, 0, 
                            NU_NULL, pointer, 2000, TM_PRIORITY + 2, 1000,
                            NU_PREEMPT, NU_START);
    
    if (status != NU_SUCCESS)
    {
#ifdef NU_WEBSERV_DEBUG
        printf ("Cannot create WS_Receive_Task\n");
#endif
        return(status);
    }
    
    status = NU_Allocate_Memory(&System_Memory, &pointer, 
                            WS_QSIZE * sizeof(UNSIGNED), NU_NO_SUSPEND);
    
    if (status != NU_SUCCESS)
    {
#ifdef NU_WEBSERV_DEBUG
        printf ("Can not create memory for SocQueue.\n");
#endif 
        return(status);
    }
    
    status = NU_Create_Queue(&WS_Socket_Queue, "SocketQ", pointer, 
                            WS_QSIZE, NU_FIXED_SIZE, 1, NU_FIFO);
    
    if (status != NU_SUCCESS)
    {
#ifdef NU_WEBSERV_DEBUG
        printf ("Can not create Socket Queue.\n");
#endif
        return(status);
    }
    
    for(;task_num < WS_MAX_WORKER_TASKS;task_num++)
    {
        status = WS_Make_Worker_Task(task_num);
        
        if(status != NU_SUCCESS)
        {
#ifdef NU_WEBSERV_DEBUG
        printf ("Can not create Worker Task.\n");
#endif
            return(status);
        }
    }
    
    return(NU_SUCCESS);
}


/*************************************************************************                                                                       
* FUNCTION                                                              
*                                                                       
*      WS_Make_Worker_Task                                              
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*      This function creates the WS_Worker_Task.                      
*                                                                       
* AUTHOR                                                                
*                                                                       
*      Doug Phillips, Accelerated Technology Inc                                                                 
*                                                                       
* INPUTS                                                                
*                                                                       
*      index                       Number of server entry.              
*                                                                       
* OUTPUTS                                                               
*                                                                       
*      None.                                                            
*                                                                       
*************************************************************************/

static STATUS WS_Make_Worker_Task(INT index)
{
    VOID        *pointer;
    STATUS      status;
    CHAR        server[8];

    server[0] = 's';
    server[1] = 'e';
    server[2] = 'r';
    server[3] = 'v';
    server[4] = 'e';
    server[5] = 'r';
    server[6] = '_';
    server[7] = (CHAR)(65 + index);

    status = NU_Allocate_Memory(&System_Memory, &pointer, WS_STACK_SIZE, 
                                NU_NO_SUSPEND);

    if (status != NU_SUCCESS) 
    {
#ifdef NU_WEBSERV_DEBUG
        printf ("no mem for server req\n");
#endif
        return(status);
    }

    /* Allows for multiple server entries.  The value should be modified if 
	 * more than one server is required.
     */
    status = NU_Create_Task(&WS_Servers[index], server, WS_Worker_Task, 
                            (UNSIGNED)index, NU_NULL, pointer, WS_STACK_SIZE,
                            (TM_PRIORITY + 5), 0, NU_PREEMPT, NU_START);
    
    if (status != NU_SUCCESS) 
    {
#ifdef  NU_WEBSERV_DEBUG
	printf ("Cannot create server_request = %d\n",status);
#endif
        return(status);
    }

    return(NU_SUCCESS);
}


/************************************************************************
* FUNCTION                                                             
*                                                                      
*     WS_Receive_Task                                        
*                                                                      
* DESCRIPTION                                                          

⌨️ 快捷键说明

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