~ubuntu-branches/ubuntu/oneiric/nginx/oneiric-updates

« back to all changes in this revision

Viewing changes to modules/nginx-echo/src/ngx_http_echo_util.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2010-11-28 08:38:27 UTC
  • mfrom: (4.2.24 sid)
  • Revision ID: james.westby@ubuntu.com-20101128083827-yxc30ucpllbu3h08
Tags: 0.8.53-1ubuntu1
* Merge from debian unstable. Remaining changes:
  - Don't copy the default site directly into /var/www/ in the package,
    but instead keep it in /usr/share/nginx/nginx-default and move it to
   /var/www/nginx-default/ just after installation. (LP: #547267)
* debian/patches/nginx-html5-codecs.diff
  - Add support for html5 codecs (LP: #674224)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define DDEBUG 0
 
2
#include "ddebug.h"
 
3
 
 
4
#include "ngx_http_echo_util.h"
 
5
#include "ngx_http_echo_sleep.h"
 
6
 
 
7
 
 
8
ngx_int_t
 
9
ngx_http_echo_init_ctx(ngx_http_request_t *r, ngx_http_echo_ctx_t **ctx_ptr)
 
10
{
 
11
    ngx_http_echo_ctx_t         *ctx;
 
12
 
 
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;
 
16
    }
 
17
 
 
18
    ctx = *ctx_ptr;
 
19
 
 
20
    ctx->sleep.handler   = ngx_http_echo_sleep_event_handler;
 
21
    ctx->sleep.data      = r;
 
22
    ctx->sleep.log       = r->connection->log;
 
23
 
 
24
    return NGX_OK;
 
25
}
 
26
 
 
27
ngx_int_t
 
28
ngx_http_echo_eval_cmd_args(ngx_http_request_t *r,
 
29
        ngx_http_echo_cmd_t *cmd, ngx_array_t *computed_args,
 
30
        ngx_array_t *opts)
 
31
{
 
32
    ngx_uint_t                       i;
 
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;
 
37
 
 
38
 
 
39
    value = args->elts;
 
40
 
 
41
    for (i = 0; i < args->nelts; i++) {
 
42
        raw = &value[i].raw_value;
 
43
 
 
44
        if (value[i].lengths == NULL && raw->len > 0) {
 
45
            if (expecting_opts) {
 
46
                if (raw->len == 1 || raw->data[0] != '-') {
 
47
                    expecting_opts = 0;
 
48
 
 
49
                } else if (raw->data[1] == '-') {
 
50
                    expecting_opts = 0;
 
51
                    continue;
 
52
 
 
53
                } else {
 
54
                    opt = ngx_array_push(opts);
 
55
                    if (opt == NULL) {
 
56
                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
 
57
                    }
 
58
 
 
59
                    opt->len = raw->len - 1;
 
60
                    opt->data = raw->data + 1;
 
61
 
 
62
                    continue;
 
63
                }
 
64
            }
 
65
        }
 
66
 
 
67
        arg = ngx_array_push(computed_args);
 
68
        if (arg == NULL) {
 
69
            return NGX_HTTP_INTERNAL_SERVER_ERROR;
 
70
        }
 
71
 
 
72
        if (value[i].lengths == NULL) { /* does not contain vars */
 
73
            dd("Using raw value \"%.*s\"", (int) raw->len, raw->data);
 
74
            *arg = *raw;
 
75
 
 
76
        } else {
 
77
            if (ngx_http_script_run(r, arg, value[i].lengths->elts,
 
78
                        0, value[i].values->elts) == NULL)
 
79
            {
 
80
                return NGX_HTTP_INTERNAL_SERVER_ERROR;
 
81
            }
 
82
        }
 
83
    }
 
84
 
 
85
    return NGX_OK;
 
86
}
 
87
 
 
88
 
 
89
ngx_int_t
 
90
ngx_http_echo_send_chain_link(ngx_http_request_t* r,
 
91
        ngx_http_echo_ctx_t *ctx, ngx_chain_t *cl)
 
92
{
 
93
    ngx_int_t       rc;
 
94
    size_t          size;
 
95
    ngx_chain_t     *p;
 
96
 
 
97
    rc = ngx_http_echo_send_header_if_needed(r, ctx);
 
98
 
 
99
    if (r->header_only || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
 
100
        return rc;
 
101
    }
 
102
 
 
103
    if (r->http_version < NGX_HTTP_VERSION_11 && !ctx->headers_sent) {
 
104
        ctx->headers_sent = 1;
 
105
 
 
106
        size = 0;
 
107
 
 
108
        for (p = cl; p; p = p->next) {
 
109
            if (p->buf->memory) {
 
110
                size += p->buf->last - p->buf->pos;
 
111
            }
 
112
        }
 
113
 
 
114
        r->headers_out.content_length_n = (off_t) size;
 
115
 
 
116
        if (r->headers_out.content_length) {
 
117
            r->headers_out.content_length->hash = 0;
 
118
        }
 
119
 
 
120
        r->headers_out.content_length = NULL;
 
121
 
 
122
        rc = ngx_http_send_header(r);
 
123
 
 
124
        if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
 
125
            return rc;
 
126
        }
 
127
    }
 
128
 
 
129
    if (cl == NULL) {
 
130
 
 
131
#if defined(nginx_version) && nginx_version <= 8004
 
132
 
 
133
        /* earlier versions of nginx does not allow subrequests
 
134
            to send last_buf themselves */
 
135
        if (r != r->main) {
 
136
            return NGX_OK;
 
137
        }
 
138
 
 
139
#endif
 
140
 
 
141
        rc = ngx_http_send_special(r, NGX_HTTP_LAST);
 
142
        if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
 
143
            return rc;
 
144
        }
 
145
 
 
146
        return NGX_OK;
 
147
    }
 
148
 
 
149
    return ngx_http_output_filter(r, cl);
 
150
}
 
151
 
 
152
ngx_int_t
 
153
ngx_http_echo_send_header_if_needed(ngx_http_request_t* r,
 
154
        ngx_http_echo_ctx_t *ctx) {
 
155
    /* ngx_int_t   rc; */
 
156
 
 
157
    if ( ! ctx->headers_sent ) {
 
158
        r->headers_out.status = NGX_HTTP_OK;
 
159
 
 
160
        if (ngx_http_set_content_type(r) != NGX_OK) {
 
161
            return NGX_HTTP_INTERNAL_SERVER_ERROR;
 
162
        }
 
163
 
 
164
        ngx_http_clear_content_length(r);
 
165
        ngx_http_clear_accept_ranges(r);
 
166
 
 
167
        if (r->http_version >= NGX_HTTP_VERSION_11) {
 
168
            ctx->headers_sent = 1;
 
169
            return ngx_http_send_header(r);
 
170
        }
 
171
    }
 
172
 
 
173
    return NGX_OK;
 
174
}
 
175
 
 
176
ssize_t
 
177
ngx_http_echo_atosz(u_char *line, size_t n) {
 
178
    ssize_t  value;
 
179
 
 
180
    if (n == 0) {
 
181
        return NGX_ERROR;
 
182
    }
 
183
 
 
184
    for (value = 0; n--; line++) {
 
185
        if (*line == '_') { /* we ignore undercores */
 
186
            continue;
 
187
        }
 
188
 
 
189
        if (*line < '0' || *line > '9') {
 
190
            return NGX_ERROR;
 
191
        }
 
192
 
 
193
        value = value * 10 + (*line - '0');
 
194
    }
 
195
 
 
196
    if (value < 0) {
 
197
        return NGX_ERROR;
 
198
    } else {
 
199
        return value;
 
200
    }
 
201
}
 
202
 
 
203
/* Modified from the ngx_strlcasestrn function in ngx_string.h
 
204
 * Copyright (C) by Igor Sysoev */
 
205
u_char *
 
206
ngx_http_echo_strlstrn(u_char *s1, u_char *last, u_char *s2, size_t n)
 
207
{
 
208
    ngx_uint_t  c1, c2;
 
209
 
 
210
    c2 = (ngx_uint_t) *s2++;
 
211
    last -= n;
 
212
 
 
213
    do {
 
214
        do {
 
215
            if (s1 >= last) {
 
216
                return NULL;
 
217
            }
 
218
 
 
219
            c1 = (ngx_uint_t) *s1++;
 
220
 
 
221
        } while (c1 != c2);
 
222
 
 
223
    } while (ngx_strncmp(s1, s2, n) != 0);
 
224
 
 
225
    return --s1;
 
226
}
 
227
 
 
228
 
 
229
ngx_int_t
 
230
ngx_http_echo_post_request_at_head(ngx_http_request_t *r,
 
231
        ngx_http_posted_request_t *pr)
 
232
{
 
233
    dd_enter();
 
234
 
 
235
    if (pr == NULL) {
 
236
        pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t));
 
237
        if (pr == NULL) {
 
238
            return NGX_ERROR;
 
239
        }
 
240
    }
 
241
 
 
242
    pr->request = r;
 
243
    pr->next = r->main->posted_requests;
 
244
    r->main->posted_requests = pr;
 
245
 
 
246
    return NGX_OK;
 
247
}
 
248