修复http解析
This commit is contained in:
@ -1,5 +1,9 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "http_rel.h"
|
#include "http_rel.h"
|
||||||
|
|
||||||
const char *http_get_body(const char *buf)
|
const char *http_get_body(const char *buf)
|
||||||
@ -18,7 +22,59 @@ const char *http_get_body(const char *buf)
|
|||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *resave_http(int fd)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
/* 一次性读完一个 HTTP 请求,返回 malloc 得到的完整字符串(含头+体),调用者 free。
|
||||||
|
返回 NULL 表示读取出错或内存不足。
|
||||||
|
*/
|
||||||
|
char *recv_http_request(int cfd)
|
||||||
|
{
|
||||||
|
char head[8192];
|
||||||
|
int body_len = 0;
|
||||||
|
|
||||||
|
/* 1. 读头部到 \r\n\r\n */
|
||||||
|
size_t n = 0;
|
||||||
|
while (n < sizeof(head)) {
|
||||||
|
if (read(cfd, head + n, 1) != 1) goto err;
|
||||||
|
if (n >= 3 && memcmp(head + n - 3, "\r\n\r", 3) == 0) {
|
||||||
|
if (read(cfd, head + n, 1) != 1) goto err; /* 再吃一个 \n */
|
||||||
|
++n;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
++n;
|
||||||
|
}
|
||||||
|
head[n] = 0;
|
||||||
|
|
||||||
|
/* 2. 解析 Content-Length */
|
||||||
|
const char *p = head;
|
||||||
|
while ((p = strchr(p, '\n')) != NULL) {
|
||||||
|
if (sscanf(p, "\nContent-Length: %d", &body_len) == 1) break;
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. 读 body */
|
||||||
|
char *body = NULL;
|
||||||
|
if (body_len > 0) {
|
||||||
|
body = malloc(body_len + 1);
|
||||||
|
if (!body) goto err;
|
||||||
|
size_t got = 0;
|
||||||
|
while (got < (size_t)body_len) {
|
||||||
|
ssize_t rd = read(cfd, body + got, body_len - got);
|
||||||
|
if (rd <= 0) { free(body); goto err; }
|
||||||
|
got += rd;
|
||||||
|
}
|
||||||
|
body[got] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 4. 拼成完整字符串 */
|
||||||
|
char *full = malloc(n + (body_len ? body_len : 0) + 1);
|
||||||
|
if (!full) { free(body); goto err; }
|
||||||
|
memcpy(full, head, n);
|
||||||
|
if (body_len) memcpy(full + n, body, body_len);
|
||||||
|
full[n + body_len] = 0;
|
||||||
|
free(body);
|
||||||
|
return full;
|
||||||
|
|
||||||
|
err:
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -2,5 +2,6 @@
|
|||||||
#define HTTP_REL
|
#define HTTP_REL
|
||||||
|
|
||||||
const char *http_get_body(const char *buf);
|
const char *http_get_body(const char *buf);
|
||||||
|
char *recv_http_request(int cfd);
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -99,9 +99,14 @@ ssize_t read_req(int fd, void *buf)
|
|||||||
int process_message(char *req,log_manager *logger)
|
int process_message(char *req,log_manager *logger)
|
||||||
{
|
{
|
||||||
//TODO 修改管道命令解析
|
//TODO 修改管道命令解析
|
||||||
if(req[0]!='s')
|
if(req ==NULL )
|
||||||
return 0;
|
return 0;
|
||||||
const char *body = http_get_body(req);
|
int fd;
|
||||||
|
char front,rear;
|
||||||
|
sscanf(req,"%s/%d/%s",&rear,&fd,&rear);
|
||||||
|
char *req_buf = recv_http_request(fd);
|
||||||
|
const char *body = http_get_body(req_buf);
|
||||||
|
free(req_buf);
|
||||||
rbt_msg message;
|
rbt_msg message;
|
||||||
rbt_parse_json(body,&message);
|
rbt_parse_json(body,&message);
|
||||||
make_swap((void*)&message);
|
make_swap((void*)&message);
|
||||||
|
Reference in New Issue
Block a user