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

📄 ws_tasks.c

📁 基于nucleus实时操作系统的webserver源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
    {
#ifdef NU_WEBSERV_DEBUG
        printf("can't create %s \n",fname);
#endif
        return WS_FAILURE;
    }    
#else
    UNUSED_PARAMETER(fname);
    UNUSED_PARAMETER(filemem);
    UNUSED_PARAMETER(length);
    return WS_FAILURE;
#endif
}

/************************************************************************
* FUNCTION                                                             
*                                                                      
*     WS_Write_To_Net                                        
*                                                                      
* DESCRIPTION                                                          
*                                                                      
*     Function to output data to the network.                          
*                                                                      
* AUTHOR                                                               
*                                                                      
*     Don Sharer, Accelerated Technology                                    
*                                                                      
* INPUTS                                                               
*                                                                      
*       req                            Pointer to Request structure that
*                                      holds all information pertaining 
*                                      to the HTTP request.             
*       buf                            The buffer that holds the        
*                                      information to be written out on 
*                                      the network.                     
*       sz                             The size of the buffer that is to
*                                      be written out.                  
*       mode                           The action for the function to take.   
*                                                                      
* OUTPUTS                                                              
*                                                                      
*     None.                                    
*                                                                      
************************************************************************/
STATUS WS_Write_To_Net(WS_REQUEST * req, CHAR HUGE * buf, UINT32 sz, STATUS mode)
{
    CHAR HUGE   *tmpptr;
    INT32       length;
    INT32       buf_size;
    STATUS      status = NU_SUCCESS;
    
    switch(mode)
    {
        
    case WS_FILETRNSFR:
    case WS_REDIRECTION:
        /*  Process sending data that either has a content
         *  length or no entity body.
         */
        while( (sz + req->ws_obufcnt) > WS_OUT_BUFSZ - 1 ) 
        {
            if( req->ws_obufcnt ) 
            {
                memcpy(&req->ws_rdata->ws_obuf[req->ws_obufcnt], buf,
                       (unsigned int)(WS_OUT_BUFSZ - req->ws_obufcnt));
                NU_Send(req->ws_sd, req->ws_rdata->ws_obuf, WS_OUT_BUFSZ, 0);
                sz -= (WS_OUT_BUFSZ - req->ws_obufcnt);
                buf += (WS_OUT_BUFSZ - req->ws_obufcnt);
                req->ws_obufcnt = 0;
                NU_Send(req->ws_sd, req->ws_rdata->ws_obuf, req->ws_obufcnt, 0);
                req->ws_obufcnt = 0;
            }
            else
            {
                while (sz > WS_MAX_SEND_SIZE)
                {
                    NU_Send(req->ws_sd, (CHAR*)buf, WS_MAX_SEND_SIZE, 0);
                    sz -= WS_MAX_SEND_SIZE;
                    buf += WS_MAX_SEND_SIZE;
                }

                NU_Send(req->ws_sd, (CHAR*)buf, (UINT16)sz, 0);
                sz = 0;
            }
        }
        
        if( sz ) 
        {
            WS_Mem_Cpy(&req->ws_rdata->ws_obuf[req->ws_obufcnt], buf, sz);
            req->ws_obufcnt = (INT16)(req->ws_obufcnt + sz);
        }
        break;
        
    case WS_PLUGIN_PROTO:
        /*  Set up the HTTP header proto-type to the buf */
        req->ws_hp.ws_header = (CHAR *)buf;
        req->ws_hp.ws_header_size = (INT)sz;
        
        /*  0 out the \r\n */
        req->ws_hp.ws_header[req->ws_hp.ws_header_size] = 0;
        req->ws_hp.ws_header[req->ws_hp.ws_header_size - 1] = 0;   
        req->ws_hp.ws_header[req->ws_hp.ws_header_size - 2] = 0;
        req->ws_hp.ws_header_size -= 2;
        req->ws_first = 1;
        req->ws_hp.ws_tsize = 0;
        req->ws_hp.ws_data = 0;
        req->ws_hp.ws_header_set = 1;
        break;

    case WS_PLUGIN_DATA:
        
        if(sz > WS_OUT_BUFSZ)
            buf_size = sz + 1;
        else
            buf_size = WS_OUT_BUFSZ;
        
        if(req->ws_hp.ws_header_set)
        {
            /*  Add the Data to the plug-in data structure to get the total content size */
            if(req->ws_first)
            {
                NU_Allocate_Memory(&System_Memory, (VOID **)&req->ws_hp.ws_data, (UNSIGNED)buf_size,
                                    NU_NO_SUSPEND);
                
                WS_Mem_Cpy(req->ws_hp.ws_data, buf, sz);
                
                req->ws_hp.ws_tsize += sz;
                req->ws_first = 0;
                req->ws_hp.ws_free_buf_sz = buf_size - sz;

            }
            else
                status = WS_Hp_Add_Data((CHAR*)buf, sz, &req->ws_hp);
            
        }
        else
        {
            if(!req->ws_hp.ws_no_pluginhead)
            {
                /*  Set the Header for the Plugin */
                HTTP_Response_Header(req, WS_PROTO_OK);
                HTTP_Header_Name_Insert(WS_CONTENT_TYPE, WS_TYPE_TXT_HTML, req->ws_response);
                
                /* Set the Header Buffer Response */
                req->ws_hp.ws_header = (CHAR *)req->ws_response;
                req->ws_hp.ws_header_size = strlen(req->ws_response);
                req->ws_first = 1;
                
            }
            
            if(req->ws_first)
            {
                /*  Allocate the Initial Entity data buffer */
                NU_Allocate_Memory(&System_Memory, (VOID **)&req->ws_hp.ws_data, WS_OUT_BUFSZ,
                    NU_NO_SUSPEND);
                
                WS_Mem_Cpy(req->ws_hp.ws_data, buf, sz);
                
                req->ws_hp.ws_tsize += sz;
                req->ws_hp.ws_free_buf_sz = buf_size - sz;
                req->ws_first = 0;
                req->ws_hp.ws_no_pluginhead = 1;
            }
            else
                /*  If not the first then continue adding the
                 *  data to the current hp.data field.
                 */
                status = WS_Hp_Add_Data((CHAR*)buf, sz,&req->ws_hp);

        }

        /* There is not enough memory to finish this request */
        if(status != NU_SUCCESS)
        {
            req->ws_obufcnt = 0;
            NU_Deallocate_Memory(req->ws_hp.ws_data);
            req->ws_hp.ws_header = NU_NULL;
            req->ws_hp.ws_data = NU_NULL;
            
            req->ws_hp.ws_tsize = 0;
            req->ws_hp.ws_free_buf_sz = 0;
            req->ws_hp.ws_header_size = 0;
            req->ws_hp.ws_header_set = 0;
            req->ws_hp.ws_no_pluginhead = 0;
        }
        
        break;
        
    case WS_PLUGIN_SEND:
        /* Get ready to send the data */

        /* NULL Terminate the Data */
        tmpptr = (CHAR HUGE*)(req->ws_hp.ws_data) + req->ws_hp.ws_tsize;
        *tmpptr = NU_NULL;

        /*  nsert the CONTENT Length Field */
        HTTP_Header_Num_Insert(WS_CONTENT_LENGTH, req->ws_hp.ws_tsize, req->ws_hp.ws_header);

        /* Add the Entity Body \r\n */
        strcat(req->ws_hp.ws_header,"\r\n");

        /* Calculate the Header Size */
        req->ws_hp.ws_header_size = strlen(req->ws_hp.ws_header);
        req->ws_hp.ws_header[req->ws_hp.ws_header_size] = 0;
        
        
        /* Store the Header */
        WS_Strn_Cpy(&req->ws_rdata->ws_obuf[req->ws_obufcnt], req->ws_hp.ws_header,
                (UINT32)req->ws_hp.ws_header_size);
        req->ws_rdata->ws_obuf[req->ws_hp.ws_header_size] = 0;
        req->ws_obufcnt = (INT16)(req->ws_obufcnt + req->ws_hp.ws_header_size);
        
        length = WS_OUT_BUFSZ - req->ws_obufcnt;
        
        if(length >= req->ws_hp.ws_tsize)
            length = req->ws_hp.ws_tsize;
        
        /* Copy from the content data the number of bytes to get the Max WS_OUT_BUFSZ */
        tmpptr = &req->ws_rdata->ws_obuf[req->ws_obufcnt];
        WS_Strn_Cpy((CHAR*)tmpptr, req->ws_hp.ws_data, (UINT32)length);
        
        if(WS_OUT_BUFSZ > (length + req->ws_hp.ws_header_size))
        {
            /*  Send the First Packet */
            NU_Send(req->ws_sd,req->ws_rdata->ws_obuf,
                    (UINT16)(length + req->ws_hp.ws_header_size), 0);
        }
        else
        {
            /*  Start sending the large data if it is the size of WS_OUT_BUFSZ */
            NU_Send(req->ws_sd, req->ws_rdata->ws_obuf, WS_OUT_BUFSZ,0);
            tmpptr = (CHAR*)((CHAR HUGE*)req->ws_hp.ws_data + length);
            req->ws_hp.ws_tsize -= length;
            
            while(req->ws_hp.ws_tsize > WS_MAX_SEND_SIZE)
            {
                /*  Transfer the WS_OUT_BUFSZ until under 32768 */
                NU_Send(req->ws_sd, (CHAR*)tmpptr, WS_MAX_SEND_SIZE, 0);
                
                /*  Decrement the size by the WS_OUT_BUFSZ */
                req->ws_hp.ws_tsize -= WS_MAX_SEND_SIZE;
                
                tmpptr = tmpptr + WS_MAX_SEND_SIZE;
            }
            
            /*  Send the Rest of the Data */
            NU_Send(req->ws_sd, (CHAR*)tmpptr, (UINT16)req->ws_hp.ws_tsize, 0);
        }
        
        req->ws_obufcnt = 0;
        NU_Deallocate_Memory(req->ws_hp.ws_data);
        req->ws_hp.ws_header = NU_NULL;
        req->ws_hp.ws_data = NU_NULL;
        
        req->ws_hp.ws_tsize = 0;
        req->ws_hp.ws_free_buf_sz = 0;
        req->ws_hp.ws_header_size = 0;
        req->ws_hp.ws_header_set = 0;
        req->ws_hp.ws_no_pluginhead = 0;
        
        break;
    }
    
    return status;
}


/************************************************************************
* FUNCTION                                                             
*                                                                      
*     WS_Hp_Add_Data                                        
*                                                                      
* DESCRIPTION                                                          
*                                                                      
*     Reallocates the hp.data structure field and adds the new data.   
*                                                                      
* AUTHOR                                                               
*                                                                      
*     Don Sharer, Accelerated Technology                                      
*                                                                      
* INPUTS                                                               
*                                                                      
*     CHAR              *buf
*     INT               size
*     WS_HTTP_PLUGIN    *hp                                      
*                                                                      
* OUTPUTS                                                              
*                                                                      
*     STATUS           

⌨️ 快捷键说明

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