2020-07-30 18:10:31 +08:00
|
|
|
|
#include "http_request.h"
|
2020-01-21 19:48:05 +08:00
|
|
|
|
|
|
|
|
|
// 字符串替换
|
|
|
|
|
char *replace(char *replace_memory, int *replace_memory_len, const char *src, const int src_len, const char *dest, const int dest_len)
|
|
|
|
|
{
|
|
|
|
|
if (!replace_memory || !src || !dest)
|
|
|
|
|
return replace_memory;
|
|
|
|
|
|
|
|
|
|
char *p;
|
|
|
|
|
int diff;
|
|
|
|
|
|
|
|
|
|
if (src_len == dest_len) {
|
|
|
|
|
for (p = memmem(replace_memory, *replace_memory_len, src, src_len); p; p = memmem(p, *replace_memory_len - (p - replace_memory), src, src_len)) {
|
|
|
|
|
memcpy(p, dest, dest_len);
|
|
|
|
|
p += dest_len;
|
|
|
|
|
}
|
|
|
|
|
} else if (src_len < dest_len) {
|
|
|
|
|
int before_len;
|
|
|
|
|
char *before_end, *new_replace_memory;
|
|
|
|
|
|
|
|
|
|
diff = dest_len - src_len;
|
|
|
|
|
for (p = memmem(replace_memory, *replace_memory_len, src, src_len); p; p = memmem(p, *replace_memory_len - (p - replace_memory), src, src_len)) {
|
|
|
|
|
*replace_memory_len += diff;
|
|
|
|
|
before_len = p - replace_memory;
|
|
|
|
|
new_replace_memory = (char *)realloc(replace_memory, *replace_memory_len + 1);
|
|
|
|
|
if (new_replace_memory == NULL) {
|
|
|
|
|
free(replace_memory);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
replace_memory = new_replace_memory;
|
|
|
|
|
before_end = replace_memory + before_len;
|
|
|
|
|
p = before_end + dest_len;
|
|
|
|
|
memmove(p, p - diff, *replace_memory_len - (p - replace_memory));
|
|
|
|
|
memcpy(before_end, dest, dest_len);
|
|
|
|
|
}
|
|
|
|
|
} else if (src_len > dest_len) {
|
|
|
|
|
diff = src_len - dest_len;
|
|
|
|
|
for (p = memmem(replace_memory, *replace_memory_len, src, src_len); p; p = memmem(p, *replace_memory_len - (p - replace_memory), src, src_len)) {
|
|
|
|
|
*replace_memory_len -= diff;
|
|
|
|
|
memcpy(p, dest, dest_len);
|
|
|
|
|
p += dest_len;
|
|
|
|
|
memmove(p, p + diff, *replace_memory_len - (p - replace_memory));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
replace_memory[*replace_memory_len] = '\0';
|
|
|
|
|
return replace_memory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 正则表达式字符串替换,str为可用free释放的指针 */
|
2020-06-08 20:28:15 +08:00
|
|
|
|
static char *regrep(char *str, int *str_len, const char *src, char *dest, int dest_len)
|
2020-01-21 19:48:05 +08:00
|
|
|
|
{
|
|
|
|
|
if (!str || !src || !dest)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
regmatch_t pm[10];
|
|
|
|
|
regex_t reg;
|
|
|
|
|
char child_num[2] = { '\\', '0' }, *p, *real_dest;
|
|
|
|
|
int match_len, real_dest_len, i;
|
|
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
|
regcomp(®, src, REG_NEWLINE | REG_ICASE | REG_EXTENDED);
|
|
|
|
|
while (regexec(®, p, 10, pm, 0) == 0) {
|
|
|
|
|
real_dest = (char *)malloc(dest_len);
|
|
|
|
|
if (real_dest == NULL) {
|
|
|
|
|
regfree(®);
|
|
|
|
|
free(str);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
memcpy(real_dest, dest, dest_len);
|
|
|
|
|
real_dest_len = dest_len;
|
|
|
|
|
//不进行不必要的字符串操作
|
|
|
|
|
if (pm[1].rm_so >= 0) {
|
|
|
|
|
/* 替换目标字符串中的子表达式 */
|
|
|
|
|
for (i = 1; i < 10 && pm[i].rm_so > -1; i++) {
|
|
|
|
|
child_num[1] = i + 48;
|
2020-06-08 20:28:15 +08:00
|
|
|
|
real_dest = replace(real_dest, &real_dest_len, child_num, 2, p + pm[i].rm_so, pm[i].rm_eo - pm[i].rm_so);
|
2020-01-21 19:48:05 +08:00
|
|
|
|
if (real_dest == NULL) {
|
|
|
|
|
regfree(®);
|
|
|
|
|
free(str);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match_len = pm[0].rm_eo - pm[0].rm_so;
|
|
|
|
|
p += pm[0].rm_so;
|
|
|
|
|
//目标字符串不大于匹配字符串则不用分配新内存
|
|
|
|
|
if (match_len >= real_dest_len) {
|
|
|
|
|
memcpy(p, real_dest, real_dest_len);
|
|
|
|
|
if (match_len > real_dest_len)
|
|
|
|
|
//strcpy(p + real_dest_len, p + match_len);
|
2020-06-08 20:28:15 +08:00
|
|
|
|
memmove(p + real_dest_len, p + match_len, *str_len - (p + match_len - str));
|
2020-01-21 19:48:05 +08:00
|
|
|
|
p += real_dest_len;
|
|
|
|
|
*str_len -= match_len - real_dest_len;
|
|
|
|
|
} else {
|
|
|
|
|
int diff;
|
|
|
|
|
char *before_end, *new_str;
|
|
|
|
|
|
|
|
|
|
diff = real_dest_len - match_len;
|
|
|
|
|
*str_len += diff;
|
|
|
|
|
new_str = (char *)realloc(str, *str_len + 1);
|
|
|
|
|
if (new_str == NULL) {
|
|
|
|
|
free(str);
|
|
|
|
|
free(real_dest);
|
|
|
|
|
regfree(®);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
str = new_str;
|
|
|
|
|
before_end = str + pm[0].rm_so;
|
|
|
|
|
p = before_end + real_dest_len;
|
|
|
|
|
memmove(p, p - diff, *str_len - (p - str) + 1);
|
|
|
|
|
memcpy(before_end, real_dest, real_dest_len);
|
|
|
|
|
}
|
|
|
|
|
free(real_dest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
regfree(®);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 15:33:38 +08:00
|
|
|
|
// 删除字符串head中第一位到 character 处并拼接 string, character 为空返回原字符串.(string 字符替换第一个字符到 character 处)
|
2020-01-21 19:48:05 +08:00
|
|
|
|
char *splice_head(char *head, const char *character, char *string)
|
|
|
|
|
{
|
2020-08-16 07:58:53 +08:00
|
|
|
|
int first_len = strlen(string);
|
|
|
|
|
char *_p1 = strstr(head, character);
|
|
|
|
|
if (_p1 == NULL) {
|
2020-01-21 19:48:05 +08:00
|
|
|
|
return head;
|
|
|
|
|
}
|
2020-08-16 07:58:53 +08:00
|
|
|
|
_p1 = _p1 + 1;
|
|
|
|
|
char temporary[first_len + strlen(_p1) + 1];
|
|
|
|
|
memset(temporary, 0, (first_len + strlen(_p1) + 1));
|
|
|
|
|
strcpy(temporary, string);
|
|
|
|
|
strcat(temporary, _p1);
|
|
|
|
|
memset(head, 0, strlen(head));
|
|
|
|
|
return strcpy(head, temporary);
|
2020-01-21 19:48:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 15:33:38 +08:00
|
|
|
|
// 删除字符串 head 中 character 到 string 处, character 为空返回原字符串.
|
2020-01-21 19:48:05 +08:00
|
|
|
|
char *delete_head(char *head, const char *character, int string)
|
|
|
|
|
{
|
|
|
|
|
int head_len = strlen(head);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
char *_p1 = strstr(head, character);
|
|
|
|
|
if (_p1 == NULL) {
|
2020-01-21 19:48:05 +08:00
|
|
|
|
return head;
|
|
|
|
|
}
|
2020-08-16 07:58:53 +08:00
|
|
|
|
char *_p2 = strchr(_p1, string);
|
|
|
|
|
if (_p2 == NULL) {
|
2020-01-21 19:48:05 +08:00
|
|
|
|
return head;
|
|
|
|
|
}
|
2020-08-16 07:58:53 +08:00
|
|
|
|
char temporary[head_len];
|
|
|
|
|
memset(temporary, 0, head_len);
|
|
|
|
|
memcpy(temporary, head, (head_len - strlen(_p1) - 1));
|
|
|
|
|
strcat(temporary, _p2);
|
|
|
|
|
memset(head, 0, strlen(head));
|
|
|
|
|
return memcpy(head, temporary, head_len);
|
2020-01-21 19:48:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 11:46:32 +08:00
|
|
|
|
int extract_host(char *header, char *host, char *port)
|
2020-01-21 19:48:05 +08:00
|
|
|
|
{
|
|
|
|
|
char *_p = strstr(header, "CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号
|
|
|
|
|
if (_p) {
|
|
|
|
|
char *_p1 = strchr(_p, ' ');
|
|
|
|
|
char *_p2 = strchr(_p1 + 1, ':');
|
|
|
|
|
char *_p3 = strchr(_p1 + 1, ' ');
|
|
|
|
|
|
|
|
|
|
if (_p2) {
|
2020-08-16 07:58:53 +08:00
|
|
|
|
memcpy(host, _p1 + 1, (int)(_p2 - _p1) - 1);
|
|
|
|
|
memcpy(port, _p2 + 1, (int)(_p3 - _p2) - 1);
|
|
|
|
|
} else { // 如果_p2等于空就返回-1
|
|
|
|
|
return -1;
|
2020-01-21 19:48:05 +08:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *p = strstr(header, "Host:");
|
|
|
|
|
if (!p) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
char *p1 = strchr(p, '\n');
|
|
|
|
|
if (!p1) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *p2 = strchr(p + 5, ':'); // 5是指'Host:'的长度
|
|
|
|
|
if (p2 && p2 < p1) {
|
2020-08-16 07:58:53 +08:00
|
|
|
|
memcpy(port, p2 + 1, (int)(p1 - p2 - 1));
|
|
|
|
|
memcpy(host, p + 5 + 1, (int)(p2 - p - 5 - 1)); // Host:
|
2020-01-21 19:48:05 +08:00
|
|
|
|
} else {
|
2020-08-16 07:58:53 +08:00
|
|
|
|
memset(port, 0, strlen(port));
|
|
|
|
|
memset(host, 0, strlen(host));
|
|
|
|
|
memcpy(host, p + 5 + 1, (int)(p1 - p - 5 - 1 - 1));
|
|
|
|
|
memcpy(port, "80", 2);
|
2020-01-21 19:48:05 +08:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 15:33:38 +08:00
|
|
|
|
char *get_path(char *url, char *path)
|
|
|
|
|
{
|
2020-08-16 07:58:53 +08:00
|
|
|
|
char *_p0;
|
|
|
|
|
_p0 = NULL;
|
2020-02-13 15:33:38 +08:00
|
|
|
|
if (url) {
|
2020-08-16 07:58:53 +08:00
|
|
|
|
_p0 = strstr(url + 7, "/");
|
|
|
|
|
if (_p0)
|
|
|
|
|
return strncpy_(path, _p0, (int)strlen(_p0));
|
2020-02-13 15:33:38 +08:00
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 07:58:53 +08:00
|
|
|
|
void free_http_request(struct http_request *http_request)
|
|
|
|
|
{
|
2020-07-30 18:10:31 +08:00
|
|
|
|
if (http_request->M)
|
|
|
|
|
free(http_request->M);
|
|
|
|
|
if (http_request->U)
|
|
|
|
|
free(http_request->U);
|
|
|
|
|
if (http_request->V)
|
|
|
|
|
free(http_request->V);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
if (http_request->host)
|
|
|
|
|
free(http_request->host);
|
|
|
|
|
if (http_request->port)
|
|
|
|
|
free(http_request->port);
|
|
|
|
|
if (http_request->H)
|
|
|
|
|
free(http_request->H);
|
|
|
|
|
if (http_request->url)
|
|
|
|
|
free(http_request->url);
|
|
|
|
|
if (http_request->uri)
|
|
|
|
|
free(http_request->uri);
|
2020-07-30 18:10:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 07:58:53 +08:00
|
|
|
|
void parse_request_head(char *http_request_line, struct http_request *http_request)
|
|
|
|
|
{
|
2020-07-30 18:10:31 +08:00
|
|
|
|
char *p;
|
|
|
|
|
char *head;
|
|
|
|
|
size_t head_len;
|
|
|
|
|
char *m, *u;
|
2020-08-16 07:58:53 +08:00
|
|
|
|
|
|
|
|
|
p = strstr(http_request_line, "\r\n"); // 查找"\r\n"
|
|
|
|
|
if (p == NULL) {
|
|
|
|
|
return;
|
2020-07-30 18:10:31 +08:00
|
|
|
|
}
|
2020-08-16 07:58:53 +08:00
|
|
|
|
|
2020-07-30 18:10:31 +08:00
|
|
|
|
head_len = strlen(http_request_line) - strlen(p);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
head = (char *)malloc(sizeof(char) * head_len * 2);
|
2020-07-30 18:10:31 +08:00
|
|
|
|
if (head == NULL)
|
|
|
|
|
free(head);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
memset(head, 0, head_len * 2);
|
2020-07-30 18:10:31 +08:00
|
|
|
|
memcpy(head, http_request_line, head_len);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
|
2020-07-30 18:10:31 +08:00
|
|
|
|
http_request->M = (char *)malloc(sizeof(char) * head_len);
|
|
|
|
|
http_request->U = (char *)malloc(sizeof(char) * head_len);
|
|
|
|
|
http_request->V = (char *)malloc(10);
|
|
|
|
|
if (http_request->M == NULL) {
|
|
|
|
|
perror("malloc");
|
|
|
|
|
}
|
|
|
|
|
if (http_request->U == NULL) {
|
|
|
|
|
perror("malloc");
|
|
|
|
|
}
|
|
|
|
|
if (http_request->V == NULL) {
|
|
|
|
|
perror("malloc");
|
|
|
|
|
}
|
|
|
|
|
memset(http_request->M, 0, head_len);
|
|
|
|
|
memset(http_request->U, 0, head_len);
|
|
|
|
|
memset(http_request->V, 0, 10);
|
|
|
|
|
|
|
|
|
|
m = strstr(head, " ");
|
|
|
|
|
http_request->M_len = strlen(head) - strlen(m);
|
|
|
|
|
memcpy(http_request->M, head, http_request->M_len);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
u = strstr(m + 1, " ");
|
|
|
|
|
http_request->U_len = strlen(m + 1) - strlen(u);
|
|
|
|
|
memcpy(http_request->U, m + 1, http_request->U_len);
|
|
|
|
|
memcpy(http_request->V, u + 1, 8);
|
2020-07-30 18:10:31 +08:00
|
|
|
|
http_request->V_len = 8;
|
|
|
|
|
|
2020-08-16 07:58:53 +08:00
|
|
|
|
if (http_request->U_len < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-08-21 17:51:19 +08:00
|
|
|
|
http_request->host = (char *)malloc(sizeof(char) * head_len + CACHE_SIZE);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
http_request->port = (char *)malloc(sizeof(char) * head_len);
|
|
|
|
|
http_request->url = (char *)malloc(sizeof(char) * head_len);
|
|
|
|
|
http_request->uri = (char *)malloc(sizeof(char) * head_len);
|
|
|
|
|
http_request->H = (char *)malloc(sizeof(char) * head_len * 2);
|
2020-08-21 17:51:19 +08:00
|
|
|
|
memset(http_request->host, 0, head_len + CACHE_SIZE);
|
|
|
|
|
memset(http_request->port, 0, head_len);
|
|
|
|
|
memset(http_request->url, 0, head_len);
|
|
|
|
|
memset(http_request->uri, 0, head_len);
|
|
|
|
|
memset(http_request->H, 0, head_len * 2);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
|
|
|
|
|
if (extract_host(http_request_line, http_request->host, http_request->port) == -1)
|
|
|
|
|
return;
|
|
|
|
|
http_request->host_len = (int)strlen(http_request->host);
|
|
|
|
|
http_request->port_len = (int)strlen(http_request->port);
|
|
|
|
|
memcpy(http_request->H, http_request->host, http_request->host_len);
|
|
|
|
|
strcat(http_request->H, ":");
|
|
|
|
|
strcat(http_request->H, http_request->port);
|
|
|
|
|
memcpy(http_request->url, http_request->U, http_request->U_len);
|
|
|
|
|
get_path(http_request->url, http_request->uri);
|
|
|
|
|
|
|
|
|
|
http_request->url_len = (int)strlen(http_request->url);
|
|
|
|
|
http_request->uri_len = (int)strlen(http_request->uri);
|
|
|
|
|
http_request->H_len = (int)strlen(http_request->H);
|
2020-07-30 18:10:31 +08:00
|
|
|
|
|
2020-08-16 07:58:53 +08:00
|
|
|
|
free(head);
|
|
|
|
|
return;
|
2020-07-30 18:10:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 20:28:15 +08:00
|
|
|
|
char *request_head(conn * in, conf * configure)
|
2020-01-21 19:48:05 +08:00
|
|
|
|
{
|
2020-07-30 18:10:31 +08:00
|
|
|
|
struct http_request *http_request;
|
|
|
|
|
http_request = (struct http_request *)malloc(sizeof(struct http_request));
|
2020-08-21 17:51:19 +08:00
|
|
|
|
memset(http_request, 0, sizeof(struct http_request));
|
|
|
|
|
|
2020-07-30 18:10:31 +08:00
|
|
|
|
parse_request_head(in->header_buffer, http_request);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
|
|
|
|
|
if (strncmp(in->header_buffer, "CONNECT", 7) == 0) {
|
2020-01-21 19:48:05 +08:00
|
|
|
|
char *incomplete_head;
|
|
|
|
|
int incomplete_head_len;
|
2020-07-30 18:10:31 +08:00
|
|
|
|
char https_del_copy[configure->https_del_len * 2];
|
|
|
|
|
char *result = NULL;
|
|
|
|
|
|
2020-11-05 21:56:08 +08:00
|
|
|
|
memset(remote_host, 0, 270);
|
2020-01-21 19:48:05 +08:00
|
|
|
|
if (configure->https_port > 0)
|
|
|
|
|
remote_port = configure->https_port;
|
|
|
|
|
if (configure->https_ip != NULL)
|
|
|
|
|
strcpy(remote_host, configure->https_ip);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE));
|
2020-07-30 18:10:31 +08:00
|
|
|
|
if (incomplete_head == NULL) {
|
|
|
|
|
free(incomplete_head);
|
|
|
|
|
perror("malloc");
|
|
|
|
|
}
|
2020-08-16 07:58:53 +08:00
|
|
|
|
memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE));
|
2020-07-30 18:10:31 +08:00
|
|
|
|
memcpy(incomplete_head, in->header_buffer, strlen(in->header_buffer));
|
|
|
|
|
memcpy(https_del_copy, configure->https_del, configure->https_del_len);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
|
2020-01-21 19:48:05 +08:00
|
|
|
|
result = strtok(https_del_copy, ",");
|
|
|
|
|
while (result != NULL) {
|
|
|
|
|
delete_head(incomplete_head, result, '\n');
|
|
|
|
|
result = strtok(NULL, ",");
|
|
|
|
|
}
|
2020-08-16 07:58:53 +08:00
|
|
|
|
splice_head(incomplete_head, "\n", configure->https_first);
|
2020-01-21 19:48:05 +08:00
|
|
|
|
incomplete_head_len = strlen(incomplete_head);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
if (configure->https_strrep)
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, configure->https_strrep_aim, configure->https_strrep_aim_len, configure->https_strrep_obj, configure->https_strrep_obj_len);
|
|
|
|
|
if (configure->https_regrep)
|
|
|
|
|
incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->https_regrep_aim, configure->https_regrep_obj, configure->https_regrep_obj_len);
|
2020-01-21 19:48:05 +08:00
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\r", 2, "\r", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\n", 2, "\n", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\b", 2, "\b", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\v", 2, "\v", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\f", 2, "\f", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\a", 2, "\a", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\t", 2, "\t", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\r", 2, "\r", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\n", 2, "\n", 1);
|
2020-07-30 18:10:31 +08:00
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[M]", 3, http_request->M, http_request->M_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[method]", 8, http_request->M, http_request->M_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[U]", 3, http_request->U, http_request->U_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[V]", 3, http_request->V, http_request->V_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[version]", 9, http_request->V, http_request->V_len);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, http_request->port, http_request->port_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
|
2020-11-05 21:56:08 +08:00
|
|
|
|
printf("%s", incomplete_head); // 打印HTTP HEADER
|
2020-01-21 19:48:05 +08:00
|
|
|
|
|
|
|
|
|
memset(in->header_buffer, 0, strlen(in->header_buffer));
|
|
|
|
|
strcpy(in->header_buffer, incomplete_head);
|
|
|
|
|
in->header_buffer_len = strlen(in->header_buffer);
|
|
|
|
|
free(incomplete_head);
|
2020-07-30 18:10:31 +08:00
|
|
|
|
|
2020-11-05 21:56:08 +08:00
|
|
|
|
} else {
|
2020-01-21 19:48:05 +08:00
|
|
|
|
char *incomplete_head;
|
2020-07-30 18:10:31 +08:00
|
|
|
|
int incomplete_head_len;
|
2020-08-26 20:40:35 +08:00
|
|
|
|
char http_del_copy[configure->http_del_len];
|
2020-01-21 19:48:05 +08:00
|
|
|
|
char *result = NULL;
|
2020-06-08 20:28:15 +08:00
|
|
|
|
|
2020-11-05 21:56:08 +08:00
|
|
|
|
memset(remote_host, 0, 270);
|
2020-01-21 19:48:05 +08:00
|
|
|
|
if (configure->http_port > 0)
|
|
|
|
|
remote_port = configure->http_port;
|
2020-08-16 07:58:53 +08:00
|
|
|
|
if (configure->http_ip != NULL)
|
2020-11-05 21:56:08 +08:00
|
|
|
|
strcpy(remote_host, configure->http_ip);
|
|
|
|
|
//memmove(remote_host, configure->http_ip, strlen(configure->http_ip));
|
2020-08-16 07:58:53 +08:00
|
|
|
|
incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE));
|
2020-07-30 18:10:31 +08:00
|
|
|
|
if (incomplete_head == NULL) {
|
|
|
|
|
free(incomplete_head);
|
|
|
|
|
perror("malloc");
|
|
|
|
|
}
|
2020-08-16 07:58:53 +08:00
|
|
|
|
memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE));
|
|
|
|
|
memcpy(incomplete_head, in->header_buffer, strlen(in->header_buffer));
|
2020-08-26 20:40:35 +08:00
|
|
|
|
memcpy(http_del_copy, configure->http_del, configure->http_del_len);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
|
2020-08-26 20:40:35 +08:00
|
|
|
|
result = strtok(http_del_copy, ",");
|
2020-01-21 19:48:05 +08:00
|
|
|
|
while (result != NULL) {
|
|
|
|
|
delete_head(incomplete_head, result, '\n');
|
|
|
|
|
result = strtok(NULL, ",");
|
|
|
|
|
}
|
2020-08-16 07:58:53 +08:00
|
|
|
|
splice_head(incomplete_head, "\n", configure->http_first);
|
2020-01-21 19:48:05 +08:00
|
|
|
|
incomplete_head_len = strlen(incomplete_head);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\r", 2, "\r", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\n", 2, "\n", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\b", 2, "\b", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\v", 2, "\v", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\f", 2, "\f", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\a", 2, "\a", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\t", 2, "\t", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\r", 2, "\r", 1);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\n", 2, "\n", 1);
|
2020-07-30 18:10:31 +08:00
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[M]", 3, http_request->M, http_request->M_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[method]", 8, http_request->M, http_request->M_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[U]", 3, http_request->U, http_request->U_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[V]", 3, http_request->V, http_request->V_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[version]", 9, http_request->V, http_request->V_len);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[url]", 5, http_request->url, http_request->url_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[uri]", 5, http_request->uri, http_request->uri_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, http_request->port, http_request->port_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
|
2020-08-21 17:51:19 +08:00
|
|
|
|
if (configure->http_strrep)
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, configure->http_strrep_aim, configure->http_strrep_aim_len, configure->http_strrep_obj, configure->http_strrep_obj_len);
|
|
|
|
|
if (configure->http_regrep)
|
|
|
|
|
incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->http_regrep_aim, configure->http_regrep_obj, configure->http_regrep_obj_len);
|
|
|
|
|
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
incomplete_head_len = strlen(incomplete_head);
|
2020-08-21 17:51:19 +08:00
|
|
|
|
//printf("%s", incomplete_head);
|
2020-08-16 07:58:53 +08:00
|
|
|
|
|
2020-08-21 17:51:19 +08:00
|
|
|
|
memset(in->header_buffer, 0, in->header_buffer_len);
|
|
|
|
|
memmove(in->header_buffer, incomplete_head, incomplete_head_len + 1);
|
2020-01-21 19:48:05 +08:00
|
|
|
|
in->header_buffer_len = strlen(in->header_buffer);
|
|
|
|
|
free(incomplete_head);
|
|
|
|
|
}
|
2020-08-16 07:58:53 +08:00
|
|
|
|
|
2020-07-30 18:10:31 +08:00
|
|
|
|
free_http_request(http_request);
|
|
|
|
|
free(http_request);
|
2020-01-21 19:48:05 +08:00
|
|
|
|
return in->header_buffer;
|
|
|
|
|
}
|