comparison src/stream/ngx_stream_return_module.c @ 6692:56fc55e32f23

Stream: filters.
author Roman Arutyunyan <arut@nginx.com>
date Thu, 15 Sep 2016 14:55:46 +0300
parents 38143d1abdec
children
comparison
equal deleted inserted replaced
6691:4bce3edfac2c 6692:56fc55e32f23
9 #include <ngx_core.h> 9 #include <ngx_core.h>
10 #include <ngx_stream.h> 10 #include <ngx_stream.h>
11 11
12 12
13 typedef struct { 13 typedef struct {
14 ngx_stream_complex_value_t text; 14 ngx_stream_complex_value_t text;
15 } ngx_stream_return_srv_conf_t; 15 } ngx_stream_return_srv_conf_t;
16 16
17 17
18 typedef struct { 18 typedef struct {
19 ngx_buf_t buf; 19 ngx_chain_t *out;
20 } ngx_stream_return_ctx_t; 20 } ngx_stream_return_ctx_t;
21 21
22 22
23 static void ngx_stream_return_handler(ngx_stream_session_t *s); 23 static void ngx_stream_return_handler(ngx_stream_session_t *s);
24 static void ngx_stream_return_write_handler(ngx_event_t *ev); 24 static void ngx_stream_return_write_handler(ngx_event_t *ev);
70 70
71 static void 71 static void
72 ngx_stream_return_handler(ngx_stream_session_t *s) 72 ngx_stream_return_handler(ngx_stream_session_t *s)
73 { 73 {
74 ngx_str_t text; 74 ngx_str_t text;
75 ngx_buf_t *b;
75 ngx_connection_t *c; 76 ngx_connection_t *c;
76 ngx_stream_return_ctx_t *ctx; 77 ngx_stream_return_ctx_t *ctx;
77 ngx_stream_return_srv_conf_t *rscf; 78 ngx_stream_return_srv_conf_t *rscf;
78 79
79 c = s->connection; 80 c = s->connection;
101 return; 102 return;
102 } 103 }
103 104
104 ngx_stream_set_ctx(s, ctx, ngx_stream_return_module); 105 ngx_stream_set_ctx(s, ctx, ngx_stream_return_module);
105 106
106 ctx->buf.pos = text.data; 107 b = ngx_calloc_buf(c->pool);
107 ctx->buf.last = text.data + text.len; 108 if (b == NULL) {
109 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
110 return;
111 }
112
113 b->memory = 1;
114 b->pos = text.data;
115 b->last = text.data + text.len;
116 b->last_buf = 1;
117
118 ctx->out = ngx_alloc_chain_link(c->pool);
119 if (ctx->out == NULL) {
120 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
121 return;
122 }
123
124 ctx->out->buf = b;
125 ctx->out->next = NULL;
108 126
109 c->write->handler = ngx_stream_return_write_handler; 127 c->write->handler = ngx_stream_return_write_handler;
110 128
111 ngx_stream_return_write_handler(c->write); 129 ngx_stream_return_write_handler(c->write);
112 } 130 }
113 131
114 132
115 static void 133 static void
116 ngx_stream_return_write_handler(ngx_event_t *ev) 134 ngx_stream_return_write_handler(ngx_event_t *ev)
117 { 135 {
118 ssize_t n;
119 ngx_buf_t *b;
120 ngx_connection_t *c; 136 ngx_connection_t *c;
121 ngx_stream_session_t *s; 137 ngx_stream_session_t *s;
122 ngx_stream_return_ctx_t *ctx; 138 ngx_stream_return_ctx_t *ctx;
123 139
124 c = ev->data; 140 c = ev->data;
128 ngx_connection_error(c, NGX_ETIMEDOUT, "connection timed out"); 144 ngx_connection_error(c, NGX_ETIMEDOUT, "connection timed out");
129 ngx_stream_finalize_session(s, NGX_STREAM_OK); 145 ngx_stream_finalize_session(s, NGX_STREAM_OK);
130 return; 146 return;
131 } 147 }
132 148
133 if (ev->ready) { 149 ctx = ngx_stream_get_module_ctx(s, ngx_stream_return_module);
134 ctx = ngx_stream_get_module_ctx(s, ngx_stream_return_module); 150
135 151 if (ngx_stream_top_filter(s, ctx->out, 1) == NGX_ERROR) {
136 b = &ctx->buf; 152 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
137 153 return;
138 n = c->send(c, b->pos, b->last - b->pos); 154 }
139 if (n == NGX_ERROR) { 155
140 ngx_stream_finalize_session(s, NGX_STREAM_OK); 156 ctx->out = NULL;
141 return; 157
142 } 158 if (!c->buffered) {
143 159 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0,
144 if (n > 0) { 160 "stream return done sending");
145 b->pos += n; 161 ngx_stream_finalize_session(s, NGX_STREAM_OK);
146 162 return;
147 if (b->pos == b->last) {
148 ngx_stream_finalize_session(s, NGX_STREAM_OK);
149 return;
150 }
151 }
152 } 163 }
153 164
154 if (ngx_handle_write_event(ev, 0) != NGX_OK) { 165 if (ngx_handle_write_event(ev, 0) != NGX_OK) {
155 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR); 166 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
156 return; 167 return;