改用自行实现的http读取与解析,同时修复部分bug
This commit is contained in:
@ -3,28 +3,75 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <bits/fcntl-linux.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "errno.h"
|
||||
#include "tools/log/log.h"
|
||||
#include "http_rel.h"
|
||||
#include <netinet/in.h>
|
||||
|
||||
/* 接收状态辅助结构 */
|
||||
|
||||
const char *http_get_body(const char *buf);
|
||||
char *recv_http_request(int cfd);
|
||||
|
||||
/* http_get_body 无需修改,保持原样 */
|
||||
const char *http_get_body(const char *buf)
|
||||
{
|
||||
if (!buf) return NULL;
|
||||
const char *sep = strstr(buf, "\r\n\r\n");
|
||||
if (!sep) return NULL;
|
||||
const char *body = sep + 4;
|
||||
if (*body == '\0') return NULL;
|
||||
return body;
|
||||
int write_in_bk(char input,httpbuf* blk){
|
||||
if(blk->size%HTTP_BLOCK_SIZE == 0)//块满分配新块
|
||||
{
|
||||
void *tes = realloc(blk->buf,blk->size+HTTP_BLOCK_SIZE);
|
||||
if(tes = NULL){//无法分配内存,清理并返回NULL
|
||||
free(blk->buf);
|
||||
perror("http rec error");
|
||||
return -1;
|
||||
}
|
||||
blk->buf = tes;
|
||||
}
|
||||
blk->buf[blk->size] = input;
|
||||
blk->size++;
|
||||
return 0;
|
||||
}
|
||||
char *recv_http_request(int cfd){
|
||||
httpbuf buf;
|
||||
buf.buf = NULL;
|
||||
// cppcheck-suppress missingReturn
|
||||
buf.buf = (char*)malloc(HTTP_BLOCK_SIZE);
|
||||
char input;
|
||||
for(;;)
|
||||
{
|
||||
read(cfd,&input,1);
|
||||
switch(input){//截取头部
|
||||
case '\n':
|
||||
if(buf.buf[buf.size-1] == '\r'&&buf.buf[buf.size-2] =='n'&&buf.buf[buf.size-3] == '\r')
|
||||
{
|
||||
buf.size-3;
|
||||
buf.buf = (char*)realloc(buf.buf,buf.size);//截取头部
|
||||
goto HEAD_RCV;//跳转到头部解析
|
||||
}
|
||||
else
|
||||
{
|
||||
if(write_in_bk(input,&buf)==-1)
|
||||
abort();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if(write_in_bk(input,&buf)==-1)
|
||||
abort();//缓存头部
|
||||
break;
|
||||
}
|
||||
}
|
||||
HEAD_RCV:
|
||||
//获取http体长度
|
||||
char length[MAX_HTTP_LENGTH] = {' '};
|
||||
int index = buf.size-1;
|
||||
int tobewrite = MAX_HTTP_LENGTH-1;
|
||||
while(buf.buf[index]!=' ')
|
||||
{
|
||||
length[tobewrite] = buf.buf[index];
|
||||
index--;tobewrite--;
|
||||
}
|
||||
int bodylength = atoi(length);
|
||||
free(buf.buf);//释放头部
|
||||
buf.buf = malloc(bodylength);
|
||||
read(cfd,buf.buf,bodylength);
|
||||
return buf.buf;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 初始化HTTP监听socket,所有错误通过logmanager记录
|
||||
|
||||
Reference in New Issue
Block a user