4
#include "ngx_http_echo_util.h"
5
#include "ngx_http_echo_sleep.h"
9
ngx_http_echo_init_ctx(ngx_http_request_t *r, ngx_http_echo_ctx_t **ctx_ptr)
11
ngx_http_echo_ctx_t *ctx;
13
*ctx_ptr = ngx_pcalloc(r->pool, sizeof(ngx_http_echo_ctx_t));
14
if (*ctx_ptr == NULL) {
15
return NGX_HTTP_INTERNAL_SERVER_ERROR;
20
ctx->sleep.handler = ngx_http_echo_sleep_event_handler;
22
ctx->sleep.log = r->connection->log;
28
ngx_http_echo_eval_cmd_args(ngx_http_request_t *r,
29
ngx_http_echo_cmd_t *cmd, ngx_array_t *computed_args,
33
ngx_array_t *args = cmd->args;
34
ngx_str_t *arg, *raw, *opt;
35
ngx_http_echo_arg_template_t *value;
36
ngx_flag_t expecting_opts = 1;
41
for (i = 0; i < args->nelts; i++) {
42
raw = &value[i].raw_value;
44
if (value[i].lengths == NULL && raw->len > 0) {
46
if (raw->len == 1 || raw->data[0] != '-') {
49
} else if (raw->data[1] == '-') {
54
opt = ngx_array_push(opts);
56
return NGX_HTTP_INTERNAL_SERVER_ERROR;
59
opt->len = raw->len - 1;
60
opt->data = raw->data + 1;
67
arg = ngx_array_push(computed_args);
69
return NGX_HTTP_INTERNAL_SERVER_ERROR;
72
if (value[i].lengths == NULL) { /* does not contain vars */
73
dd("Using raw value \"%.*s\"", (int) raw->len, raw->data);
77
if (ngx_http_script_run(r, arg, value[i].lengths->elts,
78
0, value[i].values->elts) == NULL)
80
return NGX_HTTP_INTERNAL_SERVER_ERROR;
90
ngx_http_echo_send_chain_link(ngx_http_request_t* r,
91
ngx_http_echo_ctx_t *ctx, ngx_chain_t *cl)
97
rc = ngx_http_echo_send_header_if_needed(r, ctx);
99
if (r->header_only || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
103
if (r->http_version < NGX_HTTP_VERSION_11 && !ctx->headers_sent) {
104
ctx->headers_sent = 1;
108
for (p = cl; p; p = p->next) {
109
if (p->buf->memory) {
110
size += p->buf->last - p->buf->pos;
114
r->headers_out.content_length_n = (off_t) size;
116
if (r->headers_out.content_length) {
117
r->headers_out.content_length->hash = 0;
120
r->headers_out.content_length = NULL;
122
rc = ngx_http_send_header(r);
124
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
131
#if defined(nginx_version) && nginx_version <= 8004
133
/* earlier versions of nginx does not allow subrequests
134
to send last_buf themselves */
141
rc = ngx_http_send_special(r, NGX_HTTP_LAST);
142
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
149
return ngx_http_output_filter(r, cl);
153
ngx_http_echo_send_header_if_needed(ngx_http_request_t* r,
154
ngx_http_echo_ctx_t *ctx) {
157
if ( ! ctx->headers_sent ) {
158
r->headers_out.status = NGX_HTTP_OK;
160
if (ngx_http_set_content_type(r) != NGX_OK) {
161
return NGX_HTTP_INTERNAL_SERVER_ERROR;
164
ngx_http_clear_content_length(r);
165
ngx_http_clear_accept_ranges(r);
167
if (r->http_version >= NGX_HTTP_VERSION_11) {
168
ctx->headers_sent = 1;
169
return ngx_http_send_header(r);
177
ngx_http_echo_atosz(u_char *line, size_t n) {
184
for (value = 0; n--; line++) {
185
if (*line == '_') { /* we ignore undercores */
189
if (*line < '0' || *line > '9') {
193
value = value * 10 + (*line - '0');
203
/* Modified from the ngx_strlcasestrn function in ngx_string.h
204
* Copyright (C) by Igor Sysoev */
206
ngx_http_echo_strlstrn(u_char *s1, u_char *last, u_char *s2, size_t n)
210
c2 = (ngx_uint_t) *s2++;
219
c1 = (ngx_uint_t) *s1++;
223
} while (ngx_strncmp(s1, s2, n) != 0);
230
ngx_http_echo_post_request_at_head(ngx_http_request_t *r,
231
ngx_http_posted_request_t *pr)
236
pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t));
243
pr->next = r->main->posted_requests;
244
r->main->posted_requests = pr;