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 = ;
char* point = NULL;
char* content_type = NULL;
char lenstr[]; if(resp_head == NULL)
return -; if(status > HTTP_END || status < )
return -; 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,));
// head end
strncat(point, "\r\n\r\n", );
return ;
} int get_response_body(EN_MIME_TYPE type, void* body, char* uri, long len)
{
int fd = ;
void* bodybuf = NULL;
if(body == NULL || uri == NULL)
return -; if(len == )
return ; if(type == HTTP_ELSE)
return -; fd = open(uri, O_RDONLY, );
if(fd == -)
{
close(fd);
return -;
}
// virsual memory map
bodybuf = mmap(, len, PROT_READ, MAP_PRIVATE, fd, );
memcpy(body, bodybuf, len);
munmap(bodybuf, len);
close(fd);
return ;
}
上一篇:mysql组复制安装


下一篇:内置函数---eval、exec、compile