Skip to content

Commit

Permalink
update subrequest : support http any method
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Dec 29, 2018
1 parent 72822da commit 5406112
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 210 deletions.
2 changes: 1 addition & 1 deletion ngx_http_hi_module/module_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern "C" {
#include <ngx_http_variables.h>
}

#define HI_NGINX_SERVER_VERSION "1.8.2.1"
#define HI_NGINX_SERVER_VERSION "1.8.2.2"
#define HI_NGINX_SERVER_NAME "hi-nginx"
#define SESSION_ID_NAME "SESSIONID"
#define form_multipart_type "multipart/form-data"
Expand Down
3 changes: 1 addition & 2 deletions ngx_http_hi_module/ngx_http_hi_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,8 @@ static ngx_int_t ngx_http_hi_subrequest_handler(ngx_http_request_t *r) {
psr->handler = ngx_http_hi_subrequest_post_handler;
psr->data = conf;


ngx_http_request_t *sr;
ngx_int_t rc = ngx_http_subrequest_by_method(r, &conf->subrequest, &r->args, &sr, psr, NGX_HTTP_SUBREQUEST_IN_MEMORY, r->method);
ngx_int_t rc = ngx_http_subrequest(r, &conf->subrequest, &r->args, &sr, psr, NGX_HTTP_SUBREQUEST_IN_MEMORY);

if (rc != NGX_OK) {
return NGX_ERROR;
Expand Down
21 changes: 1 addition & 20 deletions ngx_http_hi_module/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,7 @@ namespace hi {
static void deserialize(const std::string& str, std::unordered_map<std::string, std::string>& m) {
msgpack::unpack(str.c_str(), str.size()).get().convert(m);
}

static void split(const std::string& s, const std::string& delim, std::vector<std::string>& v) {
size_t last = 0;
size_t index = s.find_first_of(delim, last);
std::string tmp;
while (index != std::string::npos) {
tmp = std::move(s.substr(last, index - last));
if (!tmp.empty()) {
v.push_back(std::move(tmp));
}
last = index + 1;
index = s.find_first_of(delim, last);
}
if (index - last > 0) {
tmp = std::move(s.substr(last, index - last));
if (!tmp.empty()) {
v.push_back(std::move(tmp));
}
}
}

}

#endif /* UTILS_HPP */
Expand Down
186 changes: 2 additions & 184 deletions src/http/ngx_http_core_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,7 @@ ngx_http_subrequest(ngx_http_request_t *r,
sr->stream = r->stream;
#endif

sr->method = NGX_HTTP_GET;
sr->method = r->method;
sr->http_version = r->http_version;

sr->request_line = r->request_line;
Expand All @@ -2316,189 +2316,7 @@ ngx_http_subrequest(ngx_http_request_t *r,
sr->background = (flags & NGX_HTTP_SUBREQUEST_BACKGROUND) != 0;

sr->unparsed_uri = r->unparsed_uri;
sr->method_name = ngx_http_core_get_method;
sr->http_protocol = r->http_protocol;
sr->schema = r->schema;

ngx_http_set_exten(sr);

sr->main = r->main;
sr->parent = r;
sr->post_subrequest = ps;
sr->read_event_handler = ngx_http_request_empty_handler;
sr->write_event_handler = ngx_http_handler;

sr->variables = r->variables;

sr->log_handler = r->log_handler;

if (sr->subrequest_in_memory) {
sr->filter_need_in_memory = 1;
}

if (!sr->background) {
if (c->data == r && r->postponed == NULL) {
c->data = sr;
}

pr = ngx_palloc(r->pool, sizeof(ngx_http_postponed_request_t));
if (pr == NULL) {
return NGX_ERROR;
}

pr->request = sr;
pr->out = NULL;
pr->next = NULL;

if (r->postponed) {
for (p = r->postponed; p->next; p = p->next) { /* void */ }
p->next = pr;

} else {
r->postponed = pr;
}
}

sr->internal = 1;

sr->discard_body = r->discard_body;
sr->expect_tested = 1;
sr->main_filter_need_in_memory = r->main_filter_need_in_memory;

sr->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
sr->subrequests = r->subrequests - 1;

tp = ngx_timeofday();
sr->start_sec = tp->sec;
sr->start_msec = tp->msec;

r->main->count++;

*psr = sr;

if (flags & NGX_HTTP_SUBREQUEST_CLONE) {
sr->method = r->method;
sr->method_name = r->method_name;
sr->loc_conf = r->loc_conf;
sr->valid_location = r->valid_location;
sr->valid_unparsed_uri = r->valid_unparsed_uri;
sr->content_handler = r->content_handler;
sr->phase_handler = r->phase_handler;
sr->write_event_handler = ngx_http_core_run_phases;

#if (NGX_PCRE)
sr->ncaptures = r->ncaptures;
sr->captures = r->captures;
sr->captures_data = r->captures_data;
sr->realloc_captures = 1;
r->realloc_captures = 1;
#endif

ngx_http_update_location_config(sr);
}

return ngx_http_post_request(sr, NULL);
}

ngx_int_t ngx_http_subrequest_by_method(ngx_http_request_t *r,
ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **psr,
ngx_http_post_subrequest_t *ps, ngx_uint_t flags,ngx_uint_t method)
{
ngx_time_t *tp;
ngx_connection_t *c;
ngx_http_request_t *sr;
ngx_http_core_srv_conf_t *cscf;
ngx_http_postponed_request_t *pr, *p;

if (r->subrequests == 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"subrequests cycle while processing \"%V\"", uri);
return NGX_ERROR;
}

/*
* 1000 is reserved for other purposes.
*/
if (r->main->count >= 65535 - 1000) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
"request reference counter overflow "
"while processing \"%V\"", uri);
return NGX_ERROR;
}

if (r->subrequest_in_memory) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"nested in-memory subrequest \"%V\"", uri);
return NGX_ERROR;
}

sr = ngx_pcalloc(r->pool, sizeof(ngx_http_request_t));
if (sr == NULL) {
return NGX_ERROR;
}

sr->signature = NGX_HTTP_MODULE;

c = r->connection;
sr->connection = c;

sr->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
if (sr->ctx == NULL) {
return NGX_ERROR;
}

if (ngx_list_init(&sr->headers_out.headers, r->pool, 20,
sizeof(ngx_table_elt_t))
!= NGX_OK)
{
return NGX_ERROR;
}

if (ngx_list_init(&sr->headers_out.trailers, r->pool, 4,
sizeof(ngx_table_elt_t))
!= NGX_OK)
{
return NGX_ERROR;
}

cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
sr->main_conf = cscf->ctx->main_conf;
sr->srv_conf = cscf->ctx->srv_conf;
sr->loc_conf = cscf->ctx->loc_conf;

sr->pool = r->pool;

sr->headers_in = r->headers_in;

ngx_http_clear_content_length(sr);
ngx_http_clear_accept_ranges(sr);
ngx_http_clear_last_modified(sr);

sr->request_body = r->request_body;

#if (NGX_HTTP_V2)
sr->stream = r->stream;
#endif

sr->method = method;
sr->http_version = r->http_version;

sr->request_line = r->request_line;
sr->uri = *uri;

if (args) {
sr->args = *args;
}

ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http subrequest \"%V?%V\"", uri, &sr->args);

sr->subrequest_in_memory = (flags & NGX_HTTP_SUBREQUEST_IN_MEMORY) != 0;
sr->waited = (flags & NGX_HTTP_SUBREQUEST_WAITED) != 0;
sr->background = (flags & NGX_HTTP_SUBREQUEST_BACKGROUND) != 0;

sr->unparsed_uri = r->unparsed_uri;
sr->method_name = ngx_http_core_get_method;
sr->method_name = r->method_name;
sr->http_protocol = r->http_protocol;
sr->schema = r->schema;

Expand Down
3 changes: 0 additions & 3 deletions src/http/ngx_http_core_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,6 @@ ngx_int_t ngx_http_gzip_ok(ngx_http_request_t *r);
ngx_int_t ngx_http_subrequest(ngx_http_request_t *r,
ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **sr,
ngx_http_post_subrequest_t *psr, ngx_uint_t flags);
ngx_int_t ngx_http_subrequest_by_method(ngx_http_request_t *r,
ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **sr,
ngx_http_post_subrequest_t *psr, ngx_uint_t flags,ngx_uint_t method);
ngx_int_t ngx_http_internal_redirect(ngx_http_request_t *r,
ngx_str_t *uri, ngx_str_t *args);
ngx_int_t ngx_http_named_location(ngx_http_request_t *r, ngx_str_t *name);
Expand Down

0 comments on commit 5406112

Please sign in to comment.