summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2008-12-11 15:30:52 +0000
committerIgor Sysoev <igor@sysoev.ru>2008-12-11 15:30:52 +0000
commit371766c0a5593e1d413d05cec3f43384521bf2fc (patch)
tree99a250377db3b77c60ef10cafec0751c27ced578
parent2d83ed000f3890f2e1c4344b8c3c5e8dfb0fe3b1 (diff)
downloadnginx-371766c0a5593e1d413d05cec3f43384521bf2fc.tar.gz
nginx-371766c0a5593e1d413d05cec3f43384521bf2fc.tar.bz2
$upstream_response_length
-rw-r--r--src/http/ngx_http_upstream.c86
-rw-r--r--src/http/ngx_http_upstream.h1
2 files changed, 83 insertions, 4 deletions
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index ae56c121c..fac45fe35 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -96,6 +96,8 @@ static ngx_int_t ngx_http_upstream_status_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_upstream_response_time_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_upstream_response_length_variable(
+ ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
static char *ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy);
static char *ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd,
@@ -296,6 +298,10 @@ static ngx_http_variable_t ngx_http_upstream_vars[] = {
ngx_http_upstream_response_time_variable, 0,
NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 },
+ { ngx_string("upstream_response_length"), NULL,
+ ngx_http_upstream_response_length_variable, 0,
+ NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
@@ -1379,6 +1385,8 @@ ngx_http_upstream_process_header(ngx_http_request_t *r, ngx_http_upstream_t *u)
if (n) {
u->buffer.last -= n;
+ u->state->response_length += n;
+
if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
return;
@@ -1581,6 +1589,8 @@ ngx_http_upstream_process_body_in_memory(ngx_http_request_t *r,
return;
}
+ u->state->response_length += n;
+
if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
return;
@@ -1609,7 +1619,7 @@ static void
ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u)
{
int tcp_nodelay;
- ssize_t size;
+ ssize_t n;
ngx_int_t rc;
ngx_event_pipe_t *p;
ngx_connection_t *c;
@@ -1681,12 +1691,14 @@ ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u)
c->tcp_nodelay = NGX_TCP_NODELAY_SET;
}
- size = u->buffer.last - u->buffer.pos;
+ n = u->buffer.last - u->buffer.pos;
- if (size) {
+ if (n) {
u->buffer.last = u->buffer.pos;
- if (u->input_filter(u->input_filter_ctx, size) == NGX_ERROR) {
+ u->state->response_length += n;
+
+ if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
ngx_http_upstream_finalize_request(r, u, 0);
return;
}
@@ -1960,6 +1972,8 @@ ngx_http_upstream_process_non_buffered_request(ngx_http_request_t *r,
}
if (n > 0) {
+ u->state->response_length += n;
+
if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
ngx_http_upstream_finalize_request(r, u, 0);
return;
@@ -2480,6 +2494,10 @@ ngx_http_upstream_finalize_request(ngx_http_request_t *r,
tp = ngx_timeofday();
u->state->response_sec = tp->sec - u->state->response_sec;
u->state->response_msec = tp->msec - u->state->response_msec;
+
+ if (u->pipe) {
+ u->state->response_length = u->pipe->read_length;
+ }
}
u->finalize_request(r, rc);
@@ -3123,6 +3141,66 @@ ngx_http_upstream_response_time_variable(ngx_http_request_t *r,
}
+static ngx_int_t
+ngx_http_upstream_response_length_variable(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ u_char *p;
+ size_t len;
+ ngx_uint_t i;
+ ngx_http_upstream_state_t *state;
+
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+
+ if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
+ v->not_found = 1;
+ return NGX_OK;
+ }
+
+ len = r->upstream_states->nelts * (NGX_OFF_T_LEN + 2);
+
+ p = ngx_pnalloc(r->pool, len);
+ if (p == NULL) {
+ return NGX_ERROR;
+ }
+
+ v->data = p;
+
+ i = 0;
+ state = r->upstream_states->elts;
+
+ for ( ;; ) {
+ p = ngx_sprintf(p, "%O", state[i].response_length);
+
+ if (++i == r->upstream_states->nelts) {
+ break;
+ }
+
+ if (state[i].peer) {
+ *p++ = ',';
+ *p++ = ' ';
+
+ } else {
+ *p++ = ' ';
+ *p++ = ':';
+ *p++ = ' ';
+
+ if (++i == r->upstream_states->nelts) {
+ break;
+ }
+
+ continue;
+ }
+ }
+
+ v->len = p - v->data;
+
+ return NGX_OK;
+}
+
+
ngx_int_t
ngx_http_upstream_header_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h
index d38d6c025..b6f2161d4 100644
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -45,6 +45,7 @@ typedef struct {
ngx_uint_t status;
time_t response_sec;
ngx_uint_t response_msec;
+ off_t response_length;
ngx_str_t *peer;
} ngx_http_upstream_state_t;