Permalink
Browse files

exposed ngx_indep_subreq_fake request and ngx_indep_subreq_init_upstr…

…eam so the proxied request can be customized
  • Loading branch information...
1 parent e1e063a commit 7c79e5f5211ce76ec0bdbf4c3b379859d3bec52b Shaun committed Nov 24, 2009
Showing with 62 additions and 37 deletions.
  1. +1 −1 config
  2. +1 −1 src/fake_request.c
  3. +29 −22 src/ngx_indep_subreq.c
  4. +31 −5 src/ngx_indep_subreq.h
  5. +0 −8 src/ngx_indep_subreq_fake_request.h
View
2 config
@@ -1,4 +1,4 @@
ngx_addon_name=ngx_indep_subreq
HTTP_MODULES="$HTTP_MODULES ngx_indep_subreq"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_indep_subreq.c $ngx_addon_dir/src/fake_request.c"
-NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ngx_indep_subreq.h $ngx_addon_dir/src/ngx_indep_subreq_fake_request.h"
+NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ngx_indep_subreq.h"
View
@@ -42,7 +42,7 @@ ngx_indep_subreq_send_chain_override(ngx_connection_t *c, ngx_chain_t *in, off_
}
ngx_http_request_t*
-make_fake_request(ngx_pool_t *pool)
+ngx_indep_subreq_fake_request(ngx_pool_t *pool)
{
ngx_connection_t *c;
ngx_http_request_t *r;
View
@@ -1,7 +1,6 @@
#include <ngx_http.h>
#include "ngx_indep_subreq.h"
-#include "ngx_indep_subreq_fake_request.h"
#define NGX_INDEP_SUBREQ_CONNECT_TIMEOUT 5000
#define NGX_INDEP_SUBREQ_SEND_TIMEOUT 5000
@@ -80,7 +79,7 @@ ngx_indep_subreq_create_request(ngx_http_request_t *r)
ngx_indep_subreq_ctx_t *ctx;
ctx = ngx_http_get_module_ctx(r, ngx_indep_subreq);
if (ctx->upstream_extensions.create_request) {
- return ctx->upstream_extensions.create_request(r);
+ return ctx->upstream_extensions.create_request(r, ctx->callback_data);
}
return NGX_OK;
@@ -92,7 +91,7 @@ ngx_indep_subreq_process_header(ngx_http_request_t *r)
ngx_indep_subreq_ctx_t *ctx;
ctx = ngx_http_get_module_ctx(r, ngx_indep_subreq);
if (ctx->upstream_extensions.process_header) {
- return ctx->upstream_extensions.process_header(r);
+ return ctx->upstream_extensions.process_header(r, ctx->callback_data);
}
return NGX_OK;
}
@@ -103,7 +102,7 @@ ngx_indep_subreq_reinit_request(ngx_http_request_t *r)
ngx_indep_subreq_ctx_t *ctx;
ctx = ngx_http_get_module_ctx(r, ngx_indep_subreq);
if (ctx->upstream_extensions.reinit_request) {
- return ctx->upstream_extensions.reinit_request(r);
+ return ctx->upstream_extensions.reinit_request(r, ctx->callback_data);
}
return NGX_OK;
}
@@ -114,7 +113,7 @@ ngx_indep_subreq_abort_request(ngx_http_request_t *r)
ngx_indep_subreq_ctx_t *ctx;
ctx = ngx_http_get_module_ctx(r, ngx_indep_subreq);
if (ctx->upstream_extensions.abort_request) {
- return ctx->upstream_extensions.abort_request(r);
+ return ctx->upstream_extensions.abort_request(r, ctx->callback_data);
}
return;
}
@@ -136,8 +135,13 @@ ngx_indep_subreq_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
return;
}
-static ngx_int_t
-initialize_upstream(ngx_http_request_t *r)
+ngx_int_t
+ngx_indep_subreq_init_upstream(
+ ngx_http_request_t *r,
+ ngx_url_t *url,
+ ngx_indep_subreq_fetch_callback_pt callback,
+ void *callback_data,
+ ngx_indep_subreq_upstream_callbacks_t *upstream_extensions)
{
ngx_http_upstream_t *u;
ngx_indep_subreq_conf_t *iscc;
@@ -187,6 +191,20 @@ initialize_upstream(ngx_http_request_t *r)
*/
u->pipe->input_filter = ngx_event_pipe_copy_input_filter;
+ ngx_indep_subreq_ctx_t *ctx;
+ ctx = ngx_pcalloc(r->pool, sizeof(ngx_indep_subreq_ctx_t));
+ if (!ctx) {
+ return NGX_ERROR;
+ }
+ ngx_http_set_ctx(r, ctx, ngx_indep_subreq);
+
+ ctx->upstream_url = *url;
+ ctx->callback = callback;
+ ctx->callback_data = callback_data;
+ if (upstream_extensions) {
+ ctx->upstream_extensions = *upstream_extensions;
+ }
+
ngx_http_upstream_init(r);
return NGX_OK;
}
@@ -202,20 +220,8 @@ ngx_indep_subreq_fetch (
{
ngx_http_request_t *subreq;
- ngx_indep_subreq_ctx_t *ctx;
-
- subreq = make_fake_request(pool);
-
- ctx = ngx_pcalloc(subreq->pool, sizeof(ngx_indep_subreq_ctx_t));
- if (!ctx) {
- return NGX_ERROR;
- }
- ngx_http_set_ctx(subreq, ctx, ngx_indep_subreq);
-
- ctx->upstream_url = *url;
- ctx->callback = callback;
- ctx->callback_data = callback_data;
+ subreq = ngx_indep_subreq_fake_request(pool);
/* this is total junk -- I need to set up the configuration somehow
* so the various modules don't segfault. However, I don't know how
@@ -224,12 +230,13 @@ ngx_indep_subreq_fetch (
*/
subreq->loc_conf = r->loc_conf;
subreq->main_conf = r->main_conf;
- ngx_list_t *h = ngx_list_create(r->pool, 20, 48);
+ ngx_list_t *h = ngx_list_create(subreq->pool, 20, 48);
subreq->headers_out.headers = *h;
subreq->variables = r->variables;
- return initialize_upstream(subreq);
+ return ngx_indep_subreq_init_upstream(subreq, url,
+ callback, callback_data, upstream_extensions);
}
static ngx_int_t ngx_indep_subreq_postconf (ngx_conf_t *cf)
View
@@ -6,10 +6,10 @@
typedef void (*ngx_indep_subreq_fetch_callback_pt) (ngx_buf_t *in, ngx_http_request_t *subreq, ngx_int_t rc, void *data);
typedef struct {
- ngx_int_t (*create_request)(ngx_http_request_t *r);
- ngx_int_t (*process_header)(ngx_http_request_t *r);
- ngx_int_t (*reinit_request)(ngx_http_request_t *r);
- void (*abort_request)(ngx_http_request_t *r);
+ ngx_int_t (*create_request)(ngx_http_request_t *r, void *data);
+ ngx_int_t (*process_header)(ngx_http_request_t *r, void *data);
+ ngx_int_t (*reinit_request)(ngx_http_request_t *r, void *data);
+ void (*abort_request)(ngx_http_request_t *r, void *data);
} ngx_indep_subreq_upstream_callbacks_t;
typedef struct {
@@ -27,8 +27,34 @@ typedef struct {
int main_req_gone:1;
} ngx_indep_subreq_ctx_t;
+
+/**
+ * fake request builds a dummy request suitable for use by
+ * ngx_indep_subreq_init_upstream.
+ */
+ngx_http_request_t *ngx_indep_subreq_fake_request(ngx_pool_t *pool);
+
+/**
+ * Force a request to proxy to the specified request. When the request
+ * completes (in the upstream->finalize request callback), your callback
+ * will be called.
+ *
+ * Currently, the upstream is set to buffer the entire response in memory,
+ * which could be a serious problem for large upstream responses.
+ */
+ngx_int_t ngx_indep_subreq_init_upstream(
+ ngx_http_request_t *r,
+ ngx_url_t *u,
+ ngx_indep_subreq_fetch_callback_pt callback,
+ void *callback_data,
+ ngx_indep_subreq_upstream_callbacks_t *upstream_extensions);
+
/**
- * upstream_extensions can be NULL if you don't need to customize anything
+ * This function does not work properly yet. The end goal is to have one
+ * function to call to kick off a proxy request without having to set up the
+ * fake request yourself. Currently, however, this requires some pieces of
+ * an existing request (configuration for some of the underlying pieces, for
+ * instance).
*/
ngx_int_t ngx_indep_subreq_fetch(
ngx_http_request_t *r,
@@ -1,8 +0,0 @@
-#ifndef _NGX_INDEP_SUBREQ_FAKE_REQUEST_H
-#define _NGX_INDEP_SUBREQ_FAKE_REQUEST_H
-
-ngx_http_request_t *make_fake_request(ngx_pool_t *pool);
-
-#endif
-
-

0 comments on commit 7c79e5f

Please sign in to comment.