示例#1
0
/************************************************************************
* NAME: fnet_http_ssi_send
*
* DESCRIPTION: 
************************************************************************/
static fnet_size_t fnet_http_ssi_send (struct fnet_http_if * http)
{
    fnet_size_t                 result = 0u;
    fnet_size_t                 read_result = 0u;
    fnet_index_t                ssi_head_index = 0u;
    fnet_index_t                ssi_tail_index = 0u;
    struct fnet_http_session_if *session =  http->session_active; 
    fnet_uint8_t                *buffer = session->buffer;
    fnet_bool_t                 next = FNET_FALSE;
    
    while ((result < sizeof(session->buffer)) && (next == FNET_FALSE))
    {
        if((http->ssi.state != FNET_HTTP_SSI_INCLUDING) /* Read from file if not in including. */
           &&((read_result = fnet_fs_fread(buffer, 1u, session->send_param.file_desc)) == 0u) )
        {
            break; /*EOF*/
        }
        
        switch (http->ssi.state)
        {
            case FNET_HTTP_SSI_WAIT_HEAD:
                if(*buffer == fnet_http_ssi_head[ssi_head_index])
                {
                    ssi_head_index++;
                    if(ssi_head_index == sizeof(fnet_http_ssi_head))
                    { /* Head is found */
                        
                        if(result >= sizeof(fnet_http_ssi_head)) 
                        { /* Found in the middle */
                            fnet_fs_fseek (session->send_param.file_desc, -((fnet_int32_t)sizeof(fnet_http_ssi_head)), FNET_FS_SEEK_CUR);
                            next = FNET_TRUE; /* break */
                            result -= sizeof(fnet_http_ssi_head); /* Correct result */
                        }
                        else
                        {
                            http->ssi.state = FNET_HTTP_SSI_WAIT_TAIL;
                        }
                    }
                }
                else
                {
                    ssi_head_index = 0u;
                }
                break;
                
            case FNET_HTTP_SSI_WAIT_TAIL:
                if(*buffer == fnet_http_ssi_tail[ssi_tail_index])
                {
                    ssi_tail_index++;
                    if(ssi_tail_index == sizeof(fnet_http_ssi_tail))
                    { /* Tail is found */
                        const struct fnet_http_ssi  *ssi_ptr = 0;
                        fnet_char_t                *ssi_name = (fnet_char_t*)&session->buffer[sizeof(fnet_http_ssi_head)];
                        fnet_char_t                *ssi_param;

                        http->ssi.send = 0;
                        
                        session->buffer[buffer + 1 - session->buffer - sizeof(fnet_http_ssi_tail)] = '\0'; /* Mark end of the SSI. */

                        /* Find SSI parameters. */
                        if((ssi_param = fnet_strchr( (fnet_char_t*)session->buffer, ' ' )) !=0)
                        {
                            *ssi_param = '\0';  /* Mark end of the SSI name. */
                            ssi_param ++;       /* Point to the begining of params. */
                        }
                        
                        if(http->ssi.ssi_table)
                        /* SSI table is initialized.*/
                        {
                            /* Find SSI handler */
    	                    for(ssi_ptr = http->ssi.ssi_table; (ssi_ptr->name) && (ssi_ptr->send); ssi_ptr++)
    	                    {
    		                    if (!fnet_strcmp( ssi_name, 
    		                                        ssi_ptr->name))                    
    		                    {				 
    		                        http->ssi.send = ssi_ptr->send;
    		                        break;
    	                        }
    	                    }
	                    }
                       
                        read_result = 0u; /* Eliminate the include. */
                        if(http->ssi.send)
                        { /* SSI Handler is found. */
                            if((ssi_ptr->handle == 0) || (ssi_ptr->handle(ssi_param, &session->response.cookie) == FNET_OK))
                            {
                                buffer = session->buffer; /* Reset */
                                result = 0u;
                                
                                http->ssi.state = FNET_HTTP_SSI_INCLUDING;
                            }
                            else
                            {
                                http->ssi.state = FNET_HTTP_SSI_WAIT_HEAD;
                            }
                        }
                        else
                        {
                            http->ssi.state = FNET_HTTP_SSI_WAIT_HEAD;
                        }
                    }
                }
                else
                {
                    ssi_tail_index = 0u;
                }
                break;
            case FNET_HTTP_SSI_INCLUDING:
                {
                    fnet_bool_t eof;
                    read_result = http->ssi.send(session->buffer, sizeof(session->buffer), &eof, &session->response.cookie);
                    if((read_result == 0u) || (eof == FNET_TRUE))
                    {
                        http->ssi.state = FNET_HTTP_SSI_WAIT_HEAD;
                    }
                    
                    next = FNET_TRUE; /* break */
                }
                break;
            default:
                break;
        }
        buffer+=read_result;
        result+=read_result;
    }
    
    if(read_result && (next == FNET_FALSE) && ssi_head_index && (http->ssi.state == FNET_HTTP_SSI_WAIT_HEAD) )
    { /* Potential SSI is splitted => parse in the next itteration */
        result-=ssi_head_index; /* adjust result */
        fnet_fs_fseek(session->send_param.file_desc, -(fnet_int32_t)ssi_head_index, FNET_FS_SEEK_CUR);
    }
    
    return result;
}
示例#2
0
文件: fapp_fs.c 项目: rschuck/K64f
/************************************************************************
* NAME: fapp_fs_view_cmd
*
* DESCRIPTION: 
*************************************************************************/
static void fapp_fs_view_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t ** argv )
{
    FNET_FS_FILE    file;
    fnet_char_t    *path = argv[1];
    fnet_char_t    *path_end;
    fnet_size_t     size_cd = fnet_strlen (fapp_fs_current_path);
    fnet_size_t     size_path;
    fnet_char_t    splitter[] = {FNET_FS_SPLITTER,'\0'};
    fnet_uint8_t    data;
    struct          fnet_fs_dirent dirent;    

    FNET_COMP_UNUSED_ARG(desc);
    FNET_COMP_UNUSED_ARG(argc);
        
	if (*path != FNET_FS_SPLITTER) /* Realative path.*/
	{
	    /* Add splitter if not yet.*/
	    if(fapp_fs_current_path[size_cd-1U] != FNET_FS_SPLITTER) 
        {
	        fnet_strncat( &fapp_fs_current_path[0], splitter, FAPP_FS_DIR_PATH_MAX);
        }
	        
	    fnet_strncat( &fapp_fs_current_path[0], path, FAPP_FS_DIR_PATH_MAX);
	    path = fapp_fs_current_path; 
	}    
    else /* Full path. */
    {
        /* Strip possible repetitive leading slashes. */
        while ((path[0] == FNET_FS_SPLITTER) && (path[1] == FNET_FS_SPLITTER)) 
        {
            path++;	        
        }
    }
    
    /* Strip possible ending slashes. */
    if((size_path = fnet_strlen(path)) > 0U)
    {
        path_end = &path[size_path-1U]; 
        while(*path_end == FNET_FS_SPLITTER)
        {
            *path_end = '\0';
            path_end--;	        
        }
    }
    
    /* Open file. */
    file = fnet_fs_fopen(path,"r");
    
    if (file)
    {
        /* Print file info.*/
        fnet_fs_finfo (file, &dirent);
        
        fnet_shell_println(desc, FAPP_FS_DIR_STR, "Content of:", dirent.d_size, dirent.d_name);
        
        while(fnet_fs_fread(&data, sizeof(data), file))
        {
            fnet_shell_putchar(desc, data);
        }
                  
        /* Close file. */    
        fnet_fs_fclose(file);                  
    }
    else
    {
        fnet_shell_println(desc, FAPP_FS_VIEW_ERR, argv[1]);
    }
    
    /* Restore cur path. */
    fapp_fs_current_path[size_cd] = '\0'; 
                 
}
示例#3
0
/************************************************************************
* NAME: fnet_http_ssi_send
*
* DESCRIPTION: 
************************************************************************/
static unsigned long fnet_http_ssi_send (struct fnet_http_if * http)
{
    unsigned long result = 0;
    unsigned long read_result = 0;
    int ssi_head_index = 0;
    int ssi_tail_index = 0;
    char * buffer = http->buffer;
    int next = 0;
    
    while (result<sizeof(http->buffer) && (next == 0))
    {
        if(http->ssi.state != FNET_HTTP_SSI_INCLUDING) /* Read from file if not in including. */
            if((read_result = fnet_fs_fread(buffer, 1, http->send_param.file_desc)) == 0)
                break; /*EOF*/
        
        switch (http->ssi.state)
        {
            case FNET_HTTP_SSI_WAIT_HEAD:
                if(*buffer == fnet_http_ssi_head[ssi_head_index])
                {
                    ssi_head_index++;
                    if(ssi_head_index == sizeof(fnet_http_ssi_head))
                    { /* Head is found */
                        
                        if(result >= sizeof(fnet_http_ssi_head)) 
                        { /* Found in the middle */
                            fnet_fs_fseek (http->send_param.file_desc, -sizeof(fnet_http_ssi_head), FNET_FS_SEEK_CUR);
                            next = 1; /* break */
                            result -= sizeof(fnet_http_ssi_head); /* Correct result */
                        }
                        else
                            http->ssi.state = FNET_HTTP_SSI_WAIT_TAIL;
                        
                    }
                }
                else
                    ssi_head_index = 0;
                break;
                
            case FNET_HTTP_SSI_WAIT_TAIL:
                if(*buffer == fnet_http_ssi_tail[ssi_tail_index])
                {
                    ssi_tail_index++;
                    if(ssi_tail_index == sizeof(fnet_http_ssi_tail))
                    { /* Tail is found */
                        const struct fnet_http_ssi *ssi_ptr = 0;
                        char * ssi_name = &http->buffer[sizeof(fnet_http_ssi_head)];
                        char * ssi_param;

                        http->ssi.send = 0;
                        
                        http->buffer[buffer + 1 - http->buffer - sizeof(fnet_http_ssi_tail)] = '\0'; /* Mark end of the SSI. */

                        /* Find SSI parameters. */
                        if((ssi_param = fnet_strchr( http->buffer, ' ' )) !=0)
                        {
                            *ssi_param = '\0';  /* Mark end of the SSI name. */
                            ssi_param ++;       /* Point to the begining of params. */
                        }
                        
                        if(http->ssi.ssi_table)
                        /* SSI table is initialized.*/
                        {
                            /* Find SSI handler */
    	                    for(ssi_ptr = http->ssi.ssi_table; ssi_ptr->name && ssi_ptr->send; ssi_ptr++)
    	                    {
    		                    if (!fnet_strcmp( ssi_name, 
    		                                        ssi_ptr->name))                    
    		                    {				 
    		                        http->ssi.send = ssi_ptr->send;
    		                        break;
    	                        }
    	                    }
	                    }
                       
                        read_result = 0; /* Eliminate the include. */
                        if(http->ssi.send)
                        { /* SSI Handler is found. */
                            if((ssi_ptr->handle == 0) || (ssi_ptr->handle(ssi_param, &http->response.cookie) == FNET_OK))
                            {
                                buffer = http->buffer; /* Reset */
                                result = 0;
                                
                                http->ssi.state = FNET_HTTP_SSI_INCLUDING;
                            }
                            else
                                http->ssi.state = FNET_HTTP_SSI_WAIT_HEAD;
                        }
                        else
                            http->ssi.state = FNET_HTTP_SSI_WAIT_HEAD;
                    }

                }
                else
                    ssi_tail_index = 0;
                break;
            case FNET_HTTP_SSI_INCLUDING:
                {
                    char eof;
                    read_result = (unsigned long) http->ssi.send(http->buffer, sizeof(http->buffer), &eof, &http->response.cookie);
                    if((read_result == 0) || (eof == 1))
                        http->ssi.state = FNET_HTTP_SSI_WAIT_HEAD;
                    
                    next = 1; /* break */
                }
                break;
        
        }
        buffer+=read_result;
        result+=read_result;
    }
    
    if(read_result && (next == 0) && ssi_head_index && (http->ssi.state == FNET_HTTP_SSI_WAIT_HEAD) )
    { /* Potential SSI is splitted => parse in the next itteration */
        result-=ssi_head_index; /* adjust result */
        fnet_fs_fseek (http->send_param.file_desc, -ssi_head_index, FNET_FS_SEEK_CUR);
    }
    
    return result;
}