初步调通HTTP请求头部内容替换业务
This commit is contained in:
@@ -242,7 +242,6 @@ struct replace_ctx
|
||||
size_t n_rule;
|
||||
struct tfe_http_half * replacing;
|
||||
struct evbuffer * http_body;
|
||||
size_t body_size;
|
||||
};
|
||||
|
||||
struct pangu_http_ctx
|
||||
@@ -270,19 +269,24 @@ static struct pangu_http_ctx * pangu_http_ctx_new(unsigned int thread_id)
|
||||
|
||||
static void pangu_http_ctx_free(struct pangu_http_ctx * ctx)
|
||||
{
|
||||
if (ctx->rep_ctx != NULL)
|
||||
if (ctx->rep_ctx == NULL)
|
||||
return;
|
||||
|
||||
for (size_t i = 0; i < ctx->rep_ctx->n_rule; i++)
|
||||
{
|
||||
FREE(&(ctx->rep_ctx->rule[i].find));
|
||||
FREE(&(ctx->rep_ctx->rule[i].replace_with));
|
||||
}
|
||||
|
||||
if (ctx->rep_ctx->http_body)
|
||||
{
|
||||
for (size_t i = 0; i < ctx->rep_ctx->n_rule; i++)
|
||||
{
|
||||
FREE(&(ctx->rep_ctx->rule[i].find));
|
||||
FREE(&(ctx->rep_ctx->rule[i].replace_with));
|
||||
}
|
||||
evbuffer_free(ctx->rep_ctx->http_body);
|
||||
ctx->rep_ctx->http_body = NULL;
|
||||
//todo destroy http_half;
|
||||
assert(ctx->rep_ctx->replacing == NULL);
|
||||
FREE(&ctx->rep_ctx);
|
||||
}
|
||||
|
||||
//todo destroy http_half;
|
||||
assert(ctx->rep_ctx->replacing == NULL);
|
||||
FREE(&ctx->rep_ctx);
|
||||
FREE(&ctx->enforce_rules);
|
||||
FREE(&ctx->enforce_para);
|
||||
Maat_clean_status(&(ctx->mid));
|
||||
@@ -470,18 +474,19 @@ static char * strtok_r_esc(char * s, const char delim, char ** save_ptr)
|
||||
|
||||
size_t format_replace_rule(const char * exec_para, struct replace_rule * replace, size_t n_replace)
|
||||
{
|
||||
|
||||
char * tmp = ALLOC(char, strlen(exec_para) + 1);
|
||||
char * token = NULL, * sub_token = NULL, * saveptr = NULL, * saveptr2 = NULL;
|
||||
size_t idx = 0;
|
||||
const char * str_zone = "replace=";
|
||||
|
||||
const char * str_zone = "zone=";
|
||||
const char * str_subs = "substitute=";
|
||||
memcpy(tmp, exec_para, strlen(exec_para));
|
||||
|
||||
for (token = tmp;; token = NULL)
|
||||
{
|
||||
sub_token = strtok_r(token, ";", &saveptr);
|
||||
if (sub_token == NULL)
|
||||
break;
|
||||
if (sub_token == NULL) break;
|
||||
|
||||
if (0 == strncasecmp(sub_token, str_zone, strlen(str_zone)))
|
||||
{
|
||||
replace[idx].zone = zone_name_to_id(sub_token + strlen(str_zone));
|
||||
@@ -490,11 +495,14 @@ size_t format_replace_rule(const char * exec_para, struct replace_rule * replace
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
sub_token = strtok_r(NULL, ";", &saveptr);
|
||||
if (0 == strncasecmp(sub_token, str_subs, strlen(str_subs)))
|
||||
{
|
||||
sub_token += strlen(str_subs);
|
||||
sub_token += strlen(str_subs) + 1;
|
||||
replace[idx].find = tfe_strdup(strtok_r_esc(sub_token, '/', &saveptr2));
|
||||
replace[idx].replace_with = tfe_strdup(strtok_r_esc(NULL, '/', &saveptr2));
|
||||
|
||||
idx++;
|
||||
if (idx == n_replace)
|
||||
{
|
||||
@@ -502,10 +510,12 @@ size_t format_replace_rule(const char * exec_para, struct replace_rule * replace
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(tmp);
|
||||
tmp = NULL;
|
||||
return idx;
|
||||
}
|
||||
|
||||
size_t select_replace_rule(enum replace_zone zone, const struct replace_rule * replace, size_t n_replace,
|
||||
const struct replace_rule ** selected, size_t n_selected)
|
||||
{
|
||||
@@ -520,6 +530,7 @@ size_t select_replace_rule(enum replace_zone zone, const struct replace_rule * r
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
static struct evbuffer * replace_string(const char * in, const struct replace_rule * zone)
|
||||
{
|
||||
//Reference to https://www.lemoda.net/c/unix-regex/
|
||||
@@ -531,6 +542,7 @@ static struct evbuffer * replace_string(const char * in, const struct replace_ru
|
||||
|
||||
size_t replace_len = strlen(zone->replace_with);
|
||||
|
||||
assert(strlen(zone->find) != 0);
|
||||
status = regcomp(®, zone->find, REG_EXTENDED | REG_NEWLINE);
|
||||
if (status != 0)
|
||||
{
|
||||
@@ -588,6 +600,7 @@ static struct evbuffer * replace_string(const char * in, const struct replace_ru
|
||||
}
|
||||
p += m[0].rm_eo;
|
||||
}
|
||||
|
||||
if (is_replaced)
|
||||
{
|
||||
evbuffer_add(out, pre_sub_expr_end, in_sz - (pre_sub_expr_end - p));
|
||||
@@ -595,8 +608,8 @@ static struct evbuffer * replace_string(const char * in, const struct replace_ru
|
||||
|
||||
regfree(®);
|
||||
return out;
|
||||
|
||||
}
|
||||
|
||||
struct evbuffer * execute_replace_rule(const char * in, size_t in_sz,
|
||||
enum replace_zone zone, const struct replace_rule * rules, size_t n_rule)
|
||||
{
|
||||
@@ -618,127 +631,176 @@ struct evbuffer * execute_replace_rule(const char * in, size_t in_sz,
|
||||
interator = in;
|
||||
for (i = 0; i < n_todo; i++)
|
||||
{
|
||||
|
||||
new_out = replace_string(interator, todo[i]);
|
||||
if (new_out != NULL)
|
||||
{
|
||||
pre_out = out;
|
||||
out = new_out;
|
||||
interator = (char *) evbuffer_pullup(out, -1);
|
||||
evbuffer_free(pre_out);
|
||||
pre_out = NULL;
|
||||
|
||||
if (pre_out != NULL)
|
||||
{
|
||||
evbuffer_free(pre_out);
|
||||
pre_out = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void http_replace(const struct tfe_stream * stream, const struct tfe_http_session * session,
|
||||
enum tfe_http_event events, const unsigned char * body_frag, size_t frag_size, struct pangu_http_ctx * ctx)
|
||||
{
|
||||
void * interator = NULL;
|
||||
struct http_field_name tmp_name;
|
||||
const char * buff_in = NULL;
|
||||
struct evbuffer * rewrite_url = NULL, * rewrite_buff = NULL;
|
||||
struct replace_ctx * rep_ctx = NULL;
|
||||
struct tfe_http_session * to_write_sess = NULL;
|
||||
|
||||
to_write_sess = tfe_http_session_allow_write(session);
|
||||
if (to_write_sess == NULL) //fail to wirte, abandon.
|
||||
{
|
||||
TFE_STREAM_LOG_INFO(stream, "tfe_http_session_allow_write() %s failed.", session->req->req_spec.uri);
|
||||
tfe_http_session_detach(session);
|
||||
return;
|
||||
tfe_http_session_detach(session); return;
|
||||
}
|
||||
|
||||
struct replace_ctx * rep_ctx = ctx->rep_ctx;
|
||||
if (ctx->rep_ctx == NULL)
|
||||
{
|
||||
ctx->rep_ctx = rep_ctx = ALLOC(struct replace_ctx, 1);
|
||||
rep_ctx->rule = ALLOC(struct replace_rule, MAX_EDIT_ZONE_NUM);
|
||||
rep_ctx->n_rule = format_replace_rule(ctx->enforce_para, rep_ctx->rule, MAX_EDIT_ZONE_NUM);
|
||||
}
|
||||
if (events & EV_HTTP_REQ_HDR)
|
||||
{
|
||||
rewrite_url = execute_replace_rule(session->req->req_spec.uri, strlen(session->req->req_spec.uri),
|
||||
kZoneRequestUri, rep_ctx->rule, rep_ctx->n_rule);
|
||||
}
|
||||
if ((events & EV_HTTP_REQ_HDR) | (events & EV_HTTP_RESP_HDR))
|
||||
{
|
||||
|
||||
if (events & EV_HTTP_REQ_HDR)
|
||||
/* we must determinate the replace action on HTTP header, otherwise,
|
||||
* the header has been forwarded, only replace the body but not modify header will raise exception */
|
||||
if ((events & EV_HTTP_REQ_HDR) || (events & EV_HTTP_RESP_HDR))
|
||||
{
|
||||
rep_ctx->replacing = tfe_http_session_request_create(to_write_sess, session->req->req_spec.method,
|
||||
rewrite_url != NULL ? (char *) evbuffer_pullup(rewrite_url, -1) : session->req->req_spec.uri);
|
||||
evbuffer_free(rewrite_url);
|
||||
rewrite_url = NULL;
|
||||
ctx->rep_ctx = rep_ctx = ALLOC(struct replace_ctx, 1);
|
||||
rep_ctx->rule = ALLOC(struct replace_rule, MAX_EDIT_ZONE_NUM);
|
||||
rep_ctx->n_rule = format_replace_rule(ctx->enforce_para, rep_ctx->rule, MAX_EDIT_ZONE_NUM);
|
||||
}
|
||||
else
|
||||
{
|
||||
rep_ctx->replacing = tfe_http_session_response_create(to_write_sess, session->resp->resp_spec.resp_code);
|
||||
}
|
||||
while (1)
|
||||
{
|
||||
buff_in = tfe_http_field_iterate(session->req, &interator, &tmp_name);
|
||||
if (tmp_name.field_id == TFE_HTTP_CONT_LENGTH)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (buff_in != NULL)
|
||||
{
|
||||
rewrite_buff = execute_replace_rule(buff_in, strlen(buff_in),
|
||||
events & EV_HTTP_REQ_HDR ? kZoneRequestHeaders : kZoneResponseHeader, rep_ctx->rule,
|
||||
rep_ctx->n_rule);
|
||||
tfe_http_field_write(rep_ctx->replacing, &tmp_name,
|
||||
rewrite_buff != NULL ? (char *) evbuffer_pullup(rewrite_buff, -1) : buff_in);
|
||||
evbuffer_free(rewrite_buff);
|
||||
rewrite_buff = NULL;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
TFE_STREAM_LOG_INFO(stream, "Can only setup replace on REQ/RESP headers, detached.");
|
||||
tfe_http_session_detach(session); return;
|
||||
}
|
||||
}
|
||||
if ((events & EV_HTTP_REQ_BODY_BEGIN) | (events & EV_HTTP_RESP_BODY_BEGIN))
|
||||
{
|
||||
assert(rep_ctx->http_body == NULL);
|
||||
assert(rep_ctx->body_size = 0);
|
||||
rep_ctx->http_body = evbuffer_new();
|
||||
}
|
||||
if (body_frag != NULL)
|
||||
{
|
||||
evbuffer_add(rep_ctx->http_body, body_frag, frag_size);
|
||||
rep_ctx->body_size++;
|
||||
}
|
||||
if ((events & EV_HTTP_REQ_BODY_END) | (events & EV_HTTP_RESP_BODY_END))
|
||||
{
|
||||
|
||||
assert(rep_ctx->body_size == evbuffer_get_length(rep_ctx->http_body));
|
||||
buff_in = (char *) evbuffer_pullup(rep_ctx->http_body, -1);
|
||||
rewrite_buff = execute_replace_rule(buff_in, rep_ctx->body_size,
|
||||
events & EV_HTTP_REQ_HDR ? kZoneRequestHeaders : kZoneResponseHeader, rep_ctx->rule, rep_ctx->n_rule);
|
||||
char cont_len_str[TFE_SYMBOL_MAX];
|
||||
snprintf(cont_len_str, sizeof(cont_len_str), "%lu", evbuffer_get_length(rewrite_buff));
|
||||
_wrap_std_field_write(rep_ctx->replacing, TFE_HTTP_CONT_LENGTH, cont_len_str);
|
||||
tfe_http_half_append_body(rep_ctx->replacing,
|
||||
(char *) evbuffer_pullup(rewrite_buff, -1), evbuffer_get_length(rewrite_buff), 0);
|
||||
evbuffer_free(rewrite_buff);
|
||||
rewrite_buff = NULL;
|
||||
struct tfe_http_half * in_req_half = session->req;
|
||||
struct tfe_http_half * in_resp_half = session->resp;
|
||||
struct tfe_http_req_spec * in_req_spec = &in_req_half->req_spec;
|
||||
struct tfe_http_resp_spec * in_resp_spec = &in_resp_half->resp_spec;
|
||||
|
||||
if ((events & EV_HTTP_REQ_HDR) || (events & EV_HTTP_RESP_HDR))
|
||||
{
|
||||
struct evbuffer * rewrite_uri = NULL;
|
||||
if (is_http_request(events))
|
||||
{
|
||||
rewrite_uri = execute_replace_rule(in_req_spec->uri, strlen(in_req_spec->uri),
|
||||
kZoneRequestUri, rep_ctx->rule, rep_ctx->n_rule);
|
||||
|
||||
rep_ctx->replacing = tfe_http_session_request_create(to_write_sess, in_req_spec->method,
|
||||
rewrite_uri != NULL ? (char *) evbuffer_pullup(rewrite_uri, -1) : in_req_spec->uri);
|
||||
|
||||
tfe_http_session_request_set(to_write_sess, rep_ctx->replacing);
|
||||
}
|
||||
else
|
||||
{
|
||||
rep_ctx->replacing = tfe_http_session_response_create(to_write_sess, in_resp_spec->resp_code);
|
||||
tfe_http_session_response_set(to_write_sess, rep_ctx->replacing);
|
||||
}
|
||||
rep_ctx->replacing = NULL;//http half's ownership has been transfered to session.
|
||||
|
||||
evbuffer_free(rep_ctx->http_body);
|
||||
rep_ctx->http_body = NULL;
|
||||
rep_ctx->body_size = 0;
|
||||
if (rewrite_uri != NULL)
|
||||
{
|
||||
evbuffer_free(rewrite_uri);
|
||||
rewrite_uri = NULL;
|
||||
}
|
||||
|
||||
enum replace_zone zone = is_http_request(events) ? kZoneRequestHeaders : kZoneResponseHeader;
|
||||
struct tfe_http_half * in_half = is_http_request(events) ? in_req_half : in_resp_half;
|
||||
|
||||
struct http_field_name in_header_field{};
|
||||
const char * in_header_value = NULL;
|
||||
void * iterator = NULL;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if ((in_header_value = tfe_http_field_iterate(in_half, &iterator, &in_header_field)) == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
struct evbuffer * rewrite_buff = execute_replace_rule(in_header_value,
|
||||
strlen(in_header_value), zone, rep_ctx->rule, rep_ctx->n_rule);
|
||||
|
||||
if (rewrite_buff != NULL)
|
||||
{
|
||||
tfe_http_field_write(rep_ctx->replacing, &in_header_field, (char *) evbuffer_pullup(rewrite_buff, -1));
|
||||
}
|
||||
else
|
||||
{
|
||||
tfe_http_field_write(rep_ctx->replacing, &in_header_field, in_header_value);
|
||||
}
|
||||
|
||||
if (rewrite_buff != NULL)
|
||||
{
|
||||
evbuffer_free(rewrite_buff);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
||||
if ((events & EV_HTTP_REQ_BODY_BEGIN) || (events & EV_HTTP_RESP_BODY_BEGIN))
|
||||
{
|
||||
assert(rep_ctx->http_body == NULL);
|
||||
rep_ctx->http_body = evbuffer_new();
|
||||
}
|
||||
|
||||
if ((events & EV_HTTP_REQ_BODY_CONT) || (events & EV_HTTP_RESP_BODY_CONT))
|
||||
{
|
||||
evbuffer_add(rep_ctx->http_body, body_frag, frag_size);
|
||||
}
|
||||
|
||||
if ((events & EV_HTTP_REQ_BODY_END) || (events & EV_HTTP_RESP_BODY_END))
|
||||
{
|
||||
char * __http_body = (char *) evbuffer_pullup(rep_ctx->http_body, -1);
|
||||
size_t __http_body_len = evbuffer_get_length(rep_ctx->http_body);
|
||||
|
||||
enum replace_zone replace_zone = is_http_request(events) ? kZoneRequestHeaders : kZoneResponseHeader;
|
||||
struct evbuffer * rewrite_buff;
|
||||
|
||||
if (is_http_request(events))
|
||||
{
|
||||
rewrite_buff = execute_replace_rule(__http_body, __http_body_len, kZoneRequestBody,
|
||||
rep_ctx->rule, rep_ctx->n_rule);
|
||||
}
|
||||
else
|
||||
{
|
||||
rewrite_buff = execute_replace_rule(__http_body, __http_body_len, kZoneResponseBody,
|
||||
rep_ctx->rule, rep_ctx->n_rule);
|
||||
}
|
||||
|
||||
if (rewrite_buff != NULL)
|
||||
{
|
||||
char * __rewrite_buff = (char *) evbuffer_pullup(rewrite_buff, -1);
|
||||
size_t __sz_rewrite_buff = evbuffer_get_length(rewrite_buff);
|
||||
tfe_http_half_append_body(rep_ctx->replacing, __rewrite_buff, __sz_rewrite_buff, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
tfe_http_half_append_body(rep_ctx->replacing, __http_body, __http_body_len, 0);
|
||||
}
|
||||
|
||||
if (rewrite_buff != NULL)
|
||||
{
|
||||
evbuffer_free(rewrite_buff);
|
||||
rewrite_buff = NULL;
|
||||
}
|
||||
|
||||
if (rep_ctx->http_body != NULL)
|
||||
{
|
||||
evbuffer_free(rep_ctx->http_body);
|
||||
rep_ctx->http_body = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if ((events & EV_HTTP_REQ_END) || (events & EV_HTTP_RESP_END))
|
||||
{
|
||||
tfe_http_half_append_body(rep_ctx->replacing, NULL, 0, 0);
|
||||
rep_ctx->replacing = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void http_reject(const struct tfe_http_session * session, enum tfe_http_event events,
|
||||
struct pangu_http_ctx * ctx)
|
||||
{
|
||||
@@ -766,6 +828,7 @@ static void http_reject(const struct tfe_http_session * session, enum tfe_http_e
|
||||
_wrap_std_field_write(response, TFE_HTTP_CONT_LENGTH, cont_len_str);
|
||||
|
||||
tfe_http_half_append_body(response, page_buff, page_size, 0);
|
||||
tfe_http_half_append_body(response, NULL, 0, 0);
|
||||
tfe_http_session_response_set(to_write_sess, response);
|
||||
tfe_http_session_detach(session);
|
||||
|
||||
@@ -800,6 +863,7 @@ static void http_redirect(const struct tfe_http_session * session, enum tfe_http
|
||||
|
||||
response = tfe_http_session_response_create(to_write, resp_code);
|
||||
_wrap_std_field_write(response, TFE_HTTP_LOCATION, url);
|
||||
tfe_http_half_append_body(response, NULL, 0, 0);
|
||||
|
||||
tfe_http_session_response_set(to_write, response);
|
||||
tfe_http_session_detach(session);
|
||||
|
||||
Reference in New Issue
Block a user