/*
** Send file to client
**
** IN:
**      HTTPSRV_SESSION_STRUCT* session - session used for transmission
**      HTTPSRV_STRUCT*         server - server structure
**
** OUT:
**      none
**
** Return Value:
**      none
*/
void httpsrv_sendfile(HTTPSRV_STRUCT *server, HTTPSRV_SESSION_STRUCT *session)
{
    char *ext;
    int  length;
    char *buffer;

    buffer =  session->buffer.data;

    ext = strrchr(session->request.path, '.');
    httpsrv_process_file_type(ext, session);

    /* Check if file has server side includes */
    if ((0 == strcasecmp(ext, ".shtml")) || (0 == strcasecmp(ext, ".shtm")))
    {
        /* 
         * Disable keep-alive for this session otherwise we would have to 
         * wait for session timeout.
         */
        session->keep_alive = 0;
        httpsrv_sendhdr(session, 0, 1);
        #if MQX_USE_IO_OLD
        fseek(session->response.file, session->response.len, IO_SEEK_SET);
        #else
        fseek(session->response.file, session->response.len, SEEK_SET);
        #endif
        length = fread(buffer+session->buffer.offset, 1, HTTPSRV_SES_BUF_SIZE_PRV-session->buffer.offset, session->response.file);
        if (length > 0)
        {
            uint32_t offset;

            offset = httpsrv_sendextstr(server, session, length);
            session->response.len += session->buffer.offset;
            httpsrv_ses_flush(session);
            session->response.len += offset;
        }
    }
    else
    {
        #if MQX_USE_IO_OLD
        httpsrv_sendhdr(session, session->response.file->SIZE, 1);
        fseek(session->response.file, session->response.len, IO_SEEK_SET);
        #else
        httpsrv_sendhdr(session, httpsrv_fsize(session->response.file), 1);
        fseek(session->response.file, session->response.len, SEEK_SET);
        #endif    
        length = fread(buffer+session->buffer.offset, 1, HTTPSRV_SES_BUF_SIZE_PRV-session->buffer.offset, session->response.file);
        if (length > 0)
        {
            session->buffer.offset += length;
            httpsrv_ses_flush(session);
            session->response.len += length;
        }
    }

    if (length <= 0)
    {
        session->state = HTTPSRV_SES_END_REQ;
    }
}
Beispiel #2
0
/*
** Send file to client
**
** IN:
**      HTTPSRV_SESSION_STRUCT* session - session used for transmission
**      HTTPSRV_STRUCT*         server - server structure
**
** OUT:
**      none
**
** Return Value:
**      none
*/
void httpsrv_sendfile(HTTPSRV_STRUCT *server, HTTPSRV_SESSION_STRUCT *session)
{
    char *ext;
    int expand = 0;
    int len;
    char* buf = session->buffer.data;

    ext = strrchr(session->request.path, '.');

    if (ext != NULL)
    {
        httpsrv_process_file_type(ext+1, session);
    }
    else
    {
        session->response.content_type = HTTPSRV_CONTENT_TYPE_OCTETSTREAM;
    }

    /* Check if file has server side includes */
    if ((0 == strcasecmp(ext, ".shtml")) || (0 == strcasecmp(ext, ".shtm")))
    {
        expand = 1;
        /* Disable keep-alive for this session otherwise we would have to wait for session timeout */
        session->keep_alive = 0;
        /* If there will be any expansion there's no way how to calculate correct length
        ** zero length prevents sending Content-Length header field. */
        httpsrv_sendhdr(session, 0, 1);
    }
    else
    {
#if MQX_USE_IO_OLD
        httpsrv_sendhdr(session, session->response.file->SIZE, 1);
#else
        httpsrv_sendhdr(session, httpsrv_fsize(session->response.file), 1);
#endif
    }

    if (expand)
    {
        len = fread(buf+session->buffer.offset, 1, HTTPSRV_SES_BUF_SIZE_PRV-session->buffer.offset, session->response.file);
        if (len > 0)
        {
            len = httpsrv_sendextstr(server, session, buf, len);
            if (!len)
            {
                session->state = HTTPSRV_SES_END_REQ;
            }
            else
            {
                session->response.len += len;
#if MQX_USE_IO_OLD
                fseek(session->response.file, session->response.len, IO_SEEK_SET);
#else
				fseek(session->response.file, session->response.len, SEEK_SET);
#endif
            }
        }
        else
        {
            session->state = HTTPSRV_SES_END_REQ;
        }    
    }
    else
    {
#if MQX_USE_IO_OLD
        fseek(session->response.file, session->response.len, IO_SEEK_SET);
#else
        fseek(session->response.file, session->response.len, SEEK_SET);
#endif        
        len = fread(buf+session->buffer.offset, 1, HTTPSRV_SES_BUF_SIZE_PRV-session->buffer.offset, session->response.file);
        if (len > 0)
        {
            session->buffer.offset += len;
            httpsrv_ses_flush(session);
            session->response.len += len;
        }
        else
        {
            session->state = HTTPSRV_SES_END_REQ;
        }    
    }
}