首页 > 其他 > 详细

http server v0.1_http_reponse.c

时间:2014-01-16 21:02:47      阅读:344      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
#include <string.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

#include "mime.h"
#include "http_common.h"
#include "http_webapp.h"
static STR_STATUS_MAP resp_map[]={HTTP_STATUS_MAP(HTTP_MAP_GEN)};

/*----------------------------------------------------------------------------




-----------------------------------------------------------------------------*/
int get_response_head(EN_REP_STATUS status, EN_MIME_TYPE type, char* resp_head, int content_len)
{
    int     stat_len = 0;
    char*     point     = NULL;
    char*   content_type     = NULL;
    char     lenstr[10];

    if(resp_head == NULL)
        return -1;

    if(status > HTTP_END || status < 0)
        return -1;

    point = resp_head;
    // response status
    stat_len = strlen(resp_map[status].head);
    strncpy(point, resp_map[status].head, stat_len);
    // server version    
    strcat(point, HTTP_RESP_HEAD_SERVER);
    // content type
    content_type = get_resp_type(type);
    strcat(point, HTTP_RESP_HEAD_CONTENT_TYPE);
    strcat(point, content_type);
    // content length
    strcat(point, HTTP_RESP_HEAD_CONTENT_LENGTH);
    bzero(lenstr, sizeof(lenstr));
    strcat(point, itoa(content_len,lenstr,10));
    // head end
    strncat(point, "\r\n\r\n", 4);
    return 0;         
}


int get_response_body(EN_MIME_TYPE type, void* body, char* uri, long len)
{
    int fd = 0;
    void* bodybuf = NULL;
    if(body == NULL  || uri == NULL)
        return -1;
        
    if(len == 0)
        return 0;
    
    if(type == HTTP_ELSE)
        return -1;
        
    fd = open(uri, O_RDONLY, 0);
    if(fd == -1)
    {
        close(fd);
        return -1;
    }
    // virsual memory map
    bodybuf = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
    memcpy(body, bodybuf, len);
    munmap(bodybuf, len);    
    close(fd);
    return 0;
}
bubuko.com,布布扣

http server v0.1_http_reponse.c

原文:http://www.cnblogs.com/unixshell/p/3518997.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!