改用自行实现的http读取与解析,同时修复部分bug

This commit is contained in:
2025-12-17 18:13:39 +08:00
parent e0a3f0d3f1
commit a73b317547
7 changed files with 76 additions and 30 deletions

View File

@ -14,7 +14,7 @@ add_executable(Run_pluginmanager run_pluginmanager/run_pluginmanager.c)
add_library(Network SHARED network/network.c network/swap.c network/cJSON.c network/http/http_rel.c network/erroprocess/erroprocess.c) add_library(Network SHARED network/network.c network/swap.c network/cJSON.c network/http/http_rel.c network/erroprocess/erroprocess.c)
add_library(Swmem SHARED network/swap.c) add_library(Swmem SHARED network/swap.c)
add_library(Interpre SHARED interpreter/interpreter.c tools/pkgmanager/pkginstall.c) add_library(Interpre SHARED interpreter/interpreter.c tools/pkgmanager/pkginstall.c)
add_library(Log SHARED tools/log/log.c) add_library(Log SHARED tools/log/log.c)
add_library(Toml SHARED tools/toml/toml.c) add_library(Toml SHARED tools/toml/toml.c)
add_library(Quit SHARED tools/quit/quit.c) add_library(Quit SHARED tools/quit/quit.c)

View File

@ -9,6 +9,8 @@
#define NET_MAX_POOL 10 #define NET_MAX_POOL 10
#define NET_MAX_MESSAGE_BUF 1024 #define NET_MAX_MESSAGE_BUF 1024
#define HTTP_BLOCK_SIZE 512
#define MAX_HTTP_LENGTH 20
#define INTER_MAX_BUF 256 #define INTER_MAX_BUF 256

View File

@ -3,28 +3,75 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <bits/fcntl-linux.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include "errno.h"
#include "tools/log/log.h" #include "tools/log/log.h"
#include "http_rel.h" #include "http_rel.h"
#include <netinet/in.h> #include <netinet/in.h>
/* 接收状态辅助结构 */ int write_in_bk(char input,httpbuf* blk){
if(blk->size%HTTP_BLOCK_SIZE == 0)//块满分配新块
const char *http_get_body(const char *buf); {
char *recv_http_request(int cfd); void *tes = realloc(blk->buf,blk->size+HTTP_BLOCK_SIZE);
if(tes = NULL){//无法分配内存清理并返回NULL
/* http_get_body 无需修改,保持原样 */ free(blk->buf);
const char *http_get_body(const char *buf) perror("http rec error");
{ return -1;
if (!buf) return NULL; }
const char *sep = strstr(buf, "\r\n\r\n"); blk->buf = tes;
if (!sep) return NULL; }
const char *body = sep + 4; blk->buf[blk->size] = input;
if (*body == '\0') return NULL; blk->size++;
return body; 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记录 * @brief 初始化HTTP监听socket所有错误通过logmanager记录

View File

@ -1,7 +1,13 @@
#ifndef HTTP_REL #ifndef HTTP_REL
#define HTTP_REL #define HTTP_REL
const char *http_get_body(const char *buf); #include "config.h"
typedef struct httpbuf{
char *buf;
int size;
}httpbuf;//http分块结构体
char *recv_http_request(int cfd); char *recv_http_request(int cfd);
int init_http_network(int port, log_manager *logger); int init_http_network(int port, log_manager *logger);

View File

@ -74,7 +74,6 @@ int rbt_parse_json(const char *json_text, rbt_msg *out)
out->message_type = 'p'; out->message_type = 'p';
/* else 保持 0 */ /* else 保持 0 */
} }
cJSON_Delete(root); cJSON_Delete(root);
return 0; // 成功 return 0; // 成功
} }
@ -99,15 +98,7 @@ int process_message(char *req, log_manager *logger,rbt_msg *swap) {
return -1; return -1;
} }
char *req_buf = recv_http_request(fd); const char *body = recv_http_request(fd);
if(!req_buf) { // 检查返回值
close(fd);
free(req);
return -1;
}
const char *body = http_get_body(req_buf);
free(req_buf);
if(rbt_parse_json(body,swap) == 0) { if(rbt_parse_json(body,swap) == 0) {
logs *log = malloc(sizeof(logs)); logs *log = malloc(sizeof(logs));

View File

@ -33,9 +33,9 @@ int create_swap(const char *name)
fcntl(fd, F_SETFD, flags); fcntl(fd, F_SETFD, flags);
//调整大小 //调整大小
rbt_msg *init_msg = (rbt_msg*)mmap(NULL, sizeof(rbt_msg), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); rbt_msg *init_msg = (rbt_msg*)mmap(NULL, sizeof(rbt_msg), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
char buf[MAX_MESSAGE_BUF] = {'\0'}; char buf[NET_MAX_MESSAGE_BUF] = {'\0'};
//初始化 //初始化
memcpy(init_msg->raw_message,buf,MAX_MESSAGE_BUF); memcpy(init_msg->raw_message,buf,NET_MAX_MESSAGE_BUF);
memcpy(init_msg->nickname,buf,64); memcpy(init_msg->nickname,buf,64);
sem_init(&init_msg->status,1,1); sem_init(&init_msg->status,1,1);
init_msg->raw_message[0] = '\0'; init_msg->raw_message[0] = '\0';

View File

@ -8,7 +8,7 @@
typedef struct logs typedef struct logs
{ {
char log[1024]; char log[4096];
struct logs *next; struct logs *next;
}logs; }logs;