Compare commits
2 Commits
55f3e1411b
...
73a94417b0
| Author | SHA1 | Date | |
|---|---|---|---|
| 73a94417b0 | |||
| a73b317547 |
@ -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(Swmem SHARED network/swap.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(Quit SHARED tools/quit/quit.c)
|
||||
|
||||
|
||||
@ -9,6 +9,8 @@
|
||||
|
||||
#define NET_MAX_POOL 10
|
||||
#define NET_MAX_MESSAGE_BUF 1024
|
||||
#define HTTP_BLOCK_SIZE 512
|
||||
#define MAX_HTTP_LENGTH 20
|
||||
|
||||
#define INTER_MAX_BUF 256
|
||||
|
||||
|
||||
@ -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记录
|
||||
|
||||
@ -1,7 +1,13 @@
|
||||
#ifndef 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);
|
||||
int init_http_network(int port, log_manager *logger);
|
||||
|
||||
|
||||
@ -74,7 +74,6 @@ int rbt_parse_json(const char *json_text, rbt_msg *out)
|
||||
out->message_type = 'p';
|
||||
/* else 保持 0 */
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
return 0; // 成功
|
||||
}
|
||||
@ -99,15 +98,7 @@ int process_message(char *req, log_manager *logger,rbt_msg *swap) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *req_buf = recv_http_request(fd);
|
||||
if(!req_buf) { // 检查返回值
|
||||
close(fd);
|
||||
free(req);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *body = http_get_body(req_buf);
|
||||
free(req_buf);
|
||||
const char *body = recv_http_request(fd);
|
||||
|
||||
if(rbt_parse_json(body,swap) == 0) {
|
||||
logs *log = malloc(sizeof(logs));
|
||||
|
||||
@ -33,9 +33,9 @@ int create_swap(const char *name)
|
||||
fcntl(fd, F_SETFD, flags);
|
||||
//调整大小
|
||||
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);
|
||||
sem_init(&init_msg->status,1,1);
|
||||
init_msg->raw_message[0] = '\0';
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
|
||||
typedef struct logs
|
||||
{
|
||||
char log[1024];
|
||||
char log[4096];
|
||||
struct logs *next;
|
||||
}logs;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user