Compare commits

...

2 Commits

Author SHA1 Message Date
73a94417b0 deleted: .vscode/settings.json 2025-12-17 18:14:19 +08:00
a73b317547 改用自行实现的http读取与解析,同时修复部分bug 2025-12-17 18:13:39 +08:00
8 changed files with 76 additions and 39 deletions

View File

@ -1,9 +0,0 @@
{
"cmake.sourceDirectory": "/home/jianf/program/chat_rebot-connect-with-onebot-standard-/c",
"cpp-check-lint.--enable": true,
"cpp-check-lint.cppcheck.--executable": "cppcheck",
"cpp-check-lint.cppcheck.--language=": "c",
"cpp-check-lint.cppcheck.--inline-suppr": true,
"cpp-check-lint.cppcheck.--onsave": true,
"cpp-check-lint.cppcheck.--quick_fix": true
}

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);
/* http_get_body 无需修改,保持原样 */
const char *http_get_body(const char *buf)
{ {
if (!buf) return NULL; void *tes = realloc(blk->buf,blk->size+HTTP_BLOCK_SIZE);
const char *sep = strstr(buf, "\r\n\r\n"); if(tes = NULL){//无法分配内存清理并返回NULL
if (!sep) return NULL; free(blk->buf);
const char *body = sep + 4; perror("http rec error");
if (*body == '\0') return NULL; return -1;
return body;
} }
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记录 * @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;