zoukankan      html  css  js  c++  java
  • http server v0.1_http_reponse.c

    #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, "
    
    ", 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;
    }
  • 相关阅读:
    网络模块axios的简单应用
    UWP App国际化的两种实现
    C# 显示函数调用方的详细信息
    UWP SVG 转 Glyph
    UWP 区分设备类型
    Flutter 星标已正式超过React Native
    查看Github星标排行榜
    博客园部分非公开api
    模块化(零):综述
    模块化一:提取模块
  • 原文地址:https://www.cnblogs.com/unixshell/p/3518997.html
Copyright © 2011-2022 走看看