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

📄 upl_plgn.c

📁 基于nucleus实时操作系统的webserver源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
*       place the file in memory is looked for.  Once it has found its   
*       slot in memory it write the file to that memory location.        
*                                                                       
*   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. It returns NU_SUCCESS if the saving of the file to       
*       memory was complete.                                             
*                                                                       
*************************************************************************/
static STATUS UPL_Save_File(CHAR *fname, CHAR **filemem, INT32 length)
{
    
#ifdef WS_FILE_COMPRESSION
    INT32       i, j;
    CHAR        *n;
#endif
    INT32      actual_length = 0;
    CHAR        * nbuf;
    WS_FS_FILE  * f, *g, *h;
    INT16       compressed;
    CHAR        * s,*t;
    INT         gif, jpg, ssi;
    STATUS      status;
    
    compressed = 0; 
    gif = 0;
    jpg = 0;
    ssi = 0;
    
#ifdef NU_WEBSERV_DEBUG
    printf("memory: save:%s (%x) %d\n", fname, *filemem, length);
#endif 
    
#ifdef WS_FILE_COMPRESSION
    
#ifdef NU_WEBSERV_DEBUG
    printf("COMPRESSION enabled\n");
#endif
    
    /*  Get the Filename Extension */
    n = fname;
    while (*n != '.')
    {
        if (*(n + 1) == NU_NULL)
        {
            n = fname;
            break;
        }
        n++;
    }
    
    n++;
    /*  Gifs, ssi's and Jpegs are already compressed as far as they can be     */
    /*  Do not want to take a chance that the compression algorithm gives an   */
    /*  incorrect size.  SSI's should not be compressed because of the dynamic */
    /*  of the calls.                                                          */
    if (strcmp(n,"gif")== 0)
        gif = 1;
    else if (strcmp(n,"jpg")== 0)
        jpg = 1;
    else if (strcmp(n,"jpeg")==0)
        jpg = 1;
    else if (strcmp(n,"ssi")==0)
        ssi = 1;
    else
    {
        jpg = 0;
        gif = 0;
        ssi = 0;
    }
    
    /* find out what the compressed size would be */
    i = CFS_Compress(WS_DONT_OUTPUT, *filemem, NU_NULL, length);
    
#ifdef  NU_WEBSERV_DEBUG
    printf("compressed length = %d\n",i);
#endif
    
    /* dont bother compressing unless there is some size benefit */
    /* or if the file is a jpeg or gif file */
    if( ((i + WS_CHD_SZ ) < (length -16)) && !((gif) || (jpg)|| (ssi)))
    {
        
        
#ifdef NU_WEBSERV_DEBUG
        printf("compression version selected\n");
#endif
        status = NU_Allocate_Memory(&System_Memory, (VOID*)&nbuf, (UNSIGNED)(i + WS_CHD_SZ), NU_NO_SUSPEND);
        if(status != NU_SUCCESS)
            return(WS_FAILURE);

        /* do the real compression */
        j = CFS_Compress(WS_DO_OUTPUT, *filemem, nbuf, length);
        
        
        if( i != j )
        {
#ifdef NU_WEBSERV_DEBUG
            printf("Compression Phase error\n");
#endif
        }
        /*  Store off the actual file length */
        actual_length = length;
        length = j;      /* new length (after compression) */
        
        NU_Deallocate_Memory(*filemem);
        *filemem = nbuf;
        
        compressed = WS_COMPRESSED;
    }
#endif /* WS_FILE_COMPRESSION */
    
    /* see if the file exists already */
    
    s = fname;
    f = h = HTTP_Fs_File;
    g = NU_NULL;
    
    while ( f )
    {
        
    /* TRY:
    * 
    * 1. see if it fits in the old slot
    *    (if we are overwriting an existing file)
    *
    * 2. else see if it fits in an unused
    *    (previously deleated slot)
    *
    * 3. If we have no choice we allocate new space 
    *    for the uploaded file
        */
        
        if( f->ws_clength)
        {
            t= f->ws_name;
            while ( *t == '/' )
                t++;
            if( strcmp(s,t)==0 )
            {
                
                /* FILE EXISTS in incore FS  */
                /*  Make sure file is no bigger than the existing file length */
                
                
                f->ws_clength = 0; /* delete old file */
                f->ws_name[0]='\0';
                
                /* will it fit in old slot ? */
                if( length <= f->ws_length )
                {
                    g = f;            /* yes! found our slot */
                    /*  Set the memory to all 0's */
                    UTL_Zero(g->ws_addr, (UINT32)length);
                }
                else if( (f->ws_type & WS_COMPILED) == 0 )
                {
                    
                /* a malloced file slot
                 * delete it  (new file wont fit)
                 */
                    
                    h->ws_next = f->ws_next; /* fix links */
                    NU_Deallocate_Memory(f->ws_addr);
                }
                else if ((f->ws_type & WS_COMPILED) == 1)
                {
                    
                    /*  Clear the File Area and allocate a new area */
                    
                    /*  Reset the Pointers to the next so it is not
                    *  within this area
                    */
                    h->ws_next = f->ws_next;
                    g = NU_NULL;
                }
            }
        }
        else
        {
            if( f->ws_clength  == 0 )
            {                                       /* a previously deleated file */
                
                if( length <= f->ws_length)
                    g = f;                           /* found our slot */
            }
        }
        h = f; /* previous link */
        f = f->ws_next;
    }
    
    if( g )
    {
        
    /* we found a place for the file 
    * will reuse existing file slot
        */
        
        /* save the file */
        
#ifdef NU_WEBSERV_DEBUG
        printf("put new file in old fileslot\n");
#endif
        if (compressed)
        {
            /*  Compressed File tag */
            memcpy(g->ws_addr, WS_CHD, WS_CHD_SZ);
            WS_Mem_Cpy(g->ws_addr + WS_CHD_SZ, *filemem, (unsigned int)length);
        }
        else
            /*  Don't add compressed File tag */
            WS_Mem_Cpy(g->ws_addr, *filemem, (unsigned int)length);
        
        strcpy(g->ws_name, fname);
        g->ws_clength = length;
        g->ws_type &= ~WS_COMPRESSED;
        g->ws_type |= compressed;
    }
    else
    {   
        /* no luck, must allocate space */
        
#ifdef NU_WEBSERV_DEBUG
        printf("allocate new slot for file\n");
#endif
        
        status = NU_Allocate_Memory(&System_Memory, (VOID*)&nbuf, 
           (UNSIGNED)(WS_CHD_SZ + length + sizeof(WS_FS_FILE)), NU_NO_SUSPEND);
        if(status != NU_SUCCESS)
            return(WS_FAILURE);
        
        g = (WS_FS_FILE *) nbuf;
        g->ws_addr = (nbuf+ (sizeof(WS_FS_FILE)));
        strcpy(g->ws_name,"/");
        strcat(g->ws_name,fname);
        if (compressed)
        {
            WS_Mem_Cpy(g->ws_addr, WS_CHD, (unsigned int)WS_CHD_SZ);
            WS_Mem_Cpy(g->ws_addr + WS_CHD_SZ, *filemem, (unsigned int)length);
            g->ws_type = 0;
            g->ws_type &= ~WS_COMPRESSED;
            g->ws_type |= compressed;
            g->ws_length = actual_length;
            g->ws_clength = length + WS_CHD_SZ;
        }
        else
        {
            
            WS_Mem_Cpy(g->ws_addr, *filemem, (unsigned int)length);
            g->ws_type = 0;
            g->ws_type &= ~WS_COMPRESSED;
            g->ws_type |= compressed;
            g->ws_length = length;
            g->ws_clength = length;
        }
        
        h->ws_next = g;
        g->ws_next = NU_NULL;
    }
    
    
    return(NU_SUCCESS);
}

#endif /* WS_FILE_UPLOAD_PLGN */

⌨️ 快捷键说明

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