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

📄 upl_plgn.c

📁 基于nucleus实时操作系统的webserver源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
             return(WS_REQ_PROCEED);
         }
         
         /*  Copy the data into the filemem buffer */
         WS_Mem_Cpy(filemem, fstart, (unsigned int)buf_bytes);
         
         /*  Save the Upload file */
         UPL_Save_Upload_File(req, fname, &filemem, buf_bytes);
         
     } /* if t */
     else
     {
     /*  We should have more data to get.  Get the correct Content Length
     *  of the file and start processing.
         */
         content_length -= (fstart - content_start);
         
         /*  Allocate Memory for the Filemem buffer */
         status = NU_Allocate_Memory(&System_Memory, (VOID **)&filemem,
                                    (UNSIGNED)(content_length + 1), NU_NO_SUSPEND);
         if( status != NU_SUCCESS)
         {
             HTTP_Send_Client_HTML_Msg(req, "File Upload Error-no memory");
             NU_Deallocate_Memory((VOID **)buffer);
             return(WS_REQ_PROCEED);
         }
         
         /*  Allocate Memory for the tempoary buffer buf */
         status = NU_Allocate_Memory(&System_Memory, (VOID **)&buf, 
                                    (UNSIGNED)(WS_MAX_RECV_BYTES + 1), NU_NO_SUSPEND);
         if( status != NU_SUCCESS)
         {
             HTTP_Send_Client_HTML_Msg(req, "File Upload Error-no memory");
             NU_Deallocate_Memory((VOID **)filemem);
             NU_Deallocate_Memory((VOID **)buffer);
             return(WS_REQ_PROCEED);
         }
         
         /*  Set the number of buffer bytes left in the last packet. */    
         buf_bytes = req_buf_end - fstart;
         
         /*  Copy the data into the filemem buffer */
         WS_Mem_Cpy(filemem, fstart, (unsigned int)buf_bytes);
         
         /* There is more data in the upload file
         * we must read some more from the socket
         */
         
         count = content_length - buf_bytes;
         
         while( count > 0  )
         {
             
#ifdef NU_WEBSERV_DEBUG
             printf("reading more data from socket = %d\n", WS_RECEIVE_SIZE);
#endif
             /*  Try to get the remaining data */
             i = WS_Read_Net(req, buf, WS_MAX_RECV_BYTES, (TICKS_PER_SECOND >> 1));
             if( i <= 0 )
             {
#ifdef NU_WEBSERV_DEBUG
                 printf("error reading net\n");
#endif
                 /*  If there was an error then send an error message and  
                 *  break out of the loop and exit.
                 */
                 HTTP_Send_Client_HTML_Msg(req, "File Upload Error-Expected Packet");
                 
                 break;
             }
             
             /*  Copy the next packet into the Filemem buffer */
             WS_Mem_Cpy((CHAR HUGE*)filemem + buf_bytes, buf, (unsigned int)i);
             
#ifdef NU_WEBSERV_DEBUG
             printf("Trying to read %d bytes got %d\n", count, i);
#endif
             /*  Reduce the count */
             count -= i;
             
             /*  Increase the Number of Buffer Bytes */
             buf_bytes += i;
             
         } /*  end while (count > 0) */
         
#ifdef NU_WEBSERV_DEBUG                    
         printf("BUF_BYTES %lu \n", (buf_bytes - strlen(bound)));
#endif
         
         /*  Now look for the boundary token */
         s = HTTP_Find_Token(bound, filemem, (CHAR*)((CHAR HUGE*)filemem + buf_bytes));
         
         /*  If boundary was found then the file-upload was complete.  Then call 
          *  UPL_Save_Upload_File to save the file into the file system. 
          */
         
         if( s )
         {
             /* Remove the last \r\n */
             s -= 2;

             /* Set up size of file and save */
             buf_bytes = s - filemem;
             UPL_Save_Upload_File(req, fname, &filemem, buf_bytes); 
             
         }/* end if */
         else
             HTTP_Send_Client_HTML_Msg(req,
                 "File Upload Error-error in transfer");
         
         /*  Deallocate the buf buffer */
         status = NU_Deallocate_Memory((VOID **)buf);
         if (status != NU_SUCCESS)
             return(status);
    }/*  end else */
    
    /*  Deallocate the Filemem buffer */
    status = NU_Deallocate_Memory((VOID **)filemem);
    if (status != NU_SUCCESS)
        return(status);

    /*  Deallocate the Buffer */
    status = NU_Deallocate_Memory((VOID **)buffer);
    if (status != NU_SUCCESS)
        return(status);
    
    /*  Return from plug-in */
    
    return(WS_REQ_PROCEED);
}

/************************************************************************
*                                                                       
*   FUNCTION                                                              
*                                                                       
*       UPL_Save_Upload_File                                                 
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       This function saves a file to file system.
*       The plugin is passed in the request structure.  The     
*       file is checked for a previous version and type.  If it did      
*       not exist it saves it to memory.  If it did exist it saved in    
*       in the location it was defined.                                  
*                                                                       
*   INPUTS                                                                
*                                                                       
*       req                         Pointer to Request structure that    
*                                   holds all information pertaining     
*                                   to  the HTTP request.                
*       fname                       File name of the file to be saved.   
*       filemem                     The pointer to the input buffer that 
*                                   contains the file.                   
*       length                      The length of the file in bytes.     
*                                                                       
*   OUTPUTS                                                               
*                                                                       
*       The function returns WS_FAILURE if it was unable to write the file  
*       to memory or file system.  It returns NU_SUCCESS if    
*       the saving of the file was complete.             
*                                                                        
*************************************************************************/
static STATUS UPL_Save_Upload_File(WS_REQUEST * req, CHAR * fname, CHAR **filemem, INT32 length)
{
    CHAR buf[256];
    
    INT i;

#ifdef NU_WEBSERV_DEBUG 
    printf("ENC_Save_Upload_File %s (%d)\n",fname,length);
#endif
    req->ws_fname = fname;        /* HTTP_File_Request_Status expects it here */

    i = HTTP_File_Request_Status(req);

    /* find out if it exists and if so what kind of file it is */
    switch( i )
    {
        
    case SUCCESS:
        if( req->ws_stat->ws_flags == WS_PLUGIN )
        {
            /* "' fname 'is the name of a plugin.<BR>Upload failed." */
            strcpy(buf,"\' ");
            strcat(buf, fname);
            strcat(buf, "\'is the name of a plugin.<BR>Upload failed.\n");
            
            HTTP_Send_Client_HTML_Msg(req, buf);
            return(WS_FAILURE);
        }
        
        if( req->ws_stat->ws_flags & WS_INCORE )
        {
            i = UPL_Save_File(fname, filemem, length);
            if (i == WS_FAILURE)
                return(i);
            /* Not an Error Send Browser Message that it is complete */
            /* "File: 'fname' saved to memory." */
            strcpy(buf, "File: ");
            strcat(buf, fname);
            strcat(buf, " saved to memory.\n");
            HTTP_Send_Client_HTML_Msg(req, buf);
            
        }
        else
        {
            i = WS_Write_File_System(fname, *filemem, (UINT32)length); 
            if (i == WS_FAILURE)
                return(i);
            /* Not an Error Send Browser Message that it is complete */
            /* "File: 'fname' saved to memory." */
            strcpy(buf, "File: ");
            strcat(buf, fname);
            strcat(buf, " saved to memory.\n");
            HTTP_Send_Client_HTML_Msg(req, buf);
        }
        return(i);
        
    case ENOFILE:                               /* no file by that name */
        
#ifdef WS_FS_IN_MEMORY
        
        /* then save it to memory (or flash or whatever ) */
        
        if( (UPL_Save_File(fname, filemem, length)) == WS_FAILURE )
        {
            /* "Error Saving 'fname' to memory. */
            strcpy(buf, "Error Saving ");
            strcat(buf, fname);
            strcat(buf, " to memory.\n");
            HTTP_Send_Client_HTML_Msg(req, buf);
            return(WS_FAILURE);
        }
        
        /* "File: 'fname' saved to memory. */
        strcpy(buf, "File: ");
        strcat(buf, fname);
        strcat(buf, " saved to memory.\n");
        HTTP_Send_Client_HTML_Msg(req, buf);
        return(NU_SUCCESS);
#else
        
        /* else save it to disk (or what ever Mass storage) */
        
        if( (WS_Write_File_System(fname, *filemem, (UINT32)length)) == WS_FAILURE )
        {
            /* "Failed saving 'fname' to disk. */
            strcpy(buf, "Failed saving ");
            strcat(buf, fname);
            strcat(buf, " to disk.\n");
            HTTP_Send_Client_HTML_Msg(req, buf);
            return(WS_FAILURE);
        }

        /* "File: 'fname' sucessfully saved to disk." */
        strcpy(buf, "File: ");
        strcat(buf, fname);
        strcat(buf, " sucessfully saved to disk.\n");
        HTTP_Send_Client_HTML_Msg(req,buf);
        return(NU_SUCCESS);
#endif
    }/* end switch */
    
    return(NU_SUCCESS);
}

/************************************************************************
*                                                                       
*   FUNCTION                                                              
*                                                                       
*       UPL_Save_File                                                       
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       Function to save an uploaded file into the incore memory file    
*       system.  It checks if file compression is enabled.  If it is     
*       then it checks if it is necessary to compress the file.  A slot to  

⌨️ 快捷键说明

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