Mercurial > hg > nginx-quic
annotate src/http/modules/perl/nginx.xs @ 7527:02cd116ebe2a
Perl: protection against duplicate $r->sleep() calls.
Duplicate $r->sleep() and/or $r->has_request_body() calls result
in undefined behaviour (in practice, connection leaks were observed).
To prevent this, croak() added in appropriate places.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 12 Jul 2019 15:34:37 +0300 |
parents | 8125552a10ca |
children | 0cb693b4cbbb |
rev | line source |
---|---|
599 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
599 | 5 */ |
6 | |
7 | |
882
26c3e48b9996
the PERL_NO_GET_CONTEXT is actually required, see perlguts
Igor Sysoev <igor@sysoev.ru>
parents:
869
diff
changeset
|
8 #define PERL_NO_GET_CONTEXT |
26c3e48b9996
the PERL_NO_GET_CONTEXT is actually required, see perlguts
Igor Sysoev <igor@sysoev.ru>
parents:
869
diff
changeset
|
9 |
599 | 10 #include <ngx_config.h> |
11 #include <ngx_core.h> | |
12 #include <ngx_http.h> | |
13 #include <ngx_http_perl_module.h> | |
14 | |
603 | 15 #include "XSUB.h" |
16 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
17 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
18 #define ngx_http_perl_set_request(r, ctx) \ |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
19 \ |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
20 ctx = INT2PTR(ngx_http_perl_ctx_t *, SvIV((SV *) SvRV(ST(0)))); \ |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
21 r = ctx->request |
633 | 22 |
23 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
24 #define ngx_http_perl_set_targ(p, len) \ |
633 | 25 \ |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
26 SvUPGRADE(TARG, SVt_PV); \ |
633 | 27 SvPOK_on(TARG); \ |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
28 sv_setpvn(TARG, (char *) p, len) |
633 | 29 |
599 | 30 |
31 static ngx_int_t | |
32 ngx_http_perl_sv2str(pTHX_ ngx_http_request_t *r, ngx_str_t *s, SV *sv) | |
33 { | |
34 u_char *p; | |
35 STRLEN len; | |
36 | |
37 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
38 sv = SvRV(sv); | |
39 } | |
40 | |
41 p = (u_char *) SvPV(sv, len); | |
42 | |
43 s->len = len; | |
44 | |
1703
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
45 if (SvREADONLY(sv) && SvPOK(sv)) { |
599 | 46 s->data = p; |
1703
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
47 |
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
48 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
49 "perl sv2str: %08XD \"%V\"", sv->sv_flags, s); |
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
50 |
599 | 51 return NGX_OK; |
52 } | |
53 | |
2049 | 54 s->data = ngx_pnalloc(r->pool, len); |
599 | 55 if (s->data == NULL) { |
56 return NGX_ERROR; | |
57 } | |
58 | |
59 ngx_memcpy(s->data, p, len); | |
60 | |
1703
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
61 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
62 "perl sv2str: %08XD \"%V\"", sv->sv_flags, s); |
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
63 |
599 | 64 return NGX_OK; |
65 } | |
66 | |
67 | |
68 static ngx_int_t | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
69 ngx_http_perl_output(ngx_http_request_t *r, ngx_http_perl_ctx_t *ctx, |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
70 ngx_buf_t *b) |
599 | 71 { |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
72 ngx_chain_t out; |
617 | 73 #if (NGX_HTTP_SSI) |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
74 ngx_chain_t *cl; |
599 | 75 |
76 if (ctx->ssi) { | |
77 cl = ngx_alloc_chain_link(r->pool); | |
78 if (cl == NULL) { | |
79 return NGX_ERROR; | |
80 } | |
81 | |
82 cl->buf = b; | |
83 cl->next = NULL; | |
84 *ctx->ssi->last_out = cl; | |
85 ctx->ssi->last_out = &cl->next; | |
86 | |
87 return NGX_OK; | |
88 } | |
617 | 89 #endif |
599 | 90 |
91 out.buf = b; | |
92 out.next = NULL; | |
93 | |
94 return ngx_http_output_filter(r, &out); | |
95 } | |
96 | |
97 | |
98 MODULE = nginx PACKAGE = nginx | |
99 | |
100 | |
6233
c6cc0b79a43d
Perl: prototyping behavior explicitly specified.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6232
diff
changeset
|
101 PROTOTYPES: DISABLE |
c6cc0b79a43d
Perl: prototyping behavior explicitly specified.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6232
diff
changeset
|
102 |
c6cc0b79a43d
Perl: prototyping behavior explicitly specified.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6232
diff
changeset
|
103 |
633 | 104 void |
915 | 105 status(r, code) |
106 CODE: | |
107 | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
108 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
109 ngx_http_perl_ctx_t *ctx; |
915 | 110 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
111 ngx_http_perl_set_request(r, ctx); |
915 | 112 |
113 r->headers_out.status = SvIV(ST(1)); | |
114 | |
115 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
116 "perl status: %d", r->headers_out.status); | |
117 | |
118 XSRETURN_UNDEF; | |
119 | |
120 | |
121 void | |
599 | 122 send_http_header(r, ...) |
633 | 123 CODE: |
599 | 124 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
125 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
126 ngx_http_perl_ctx_t *ctx; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
127 SV *sv; |
7525 | 128 ngx_int_t rc; |
599 | 129 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
130 ngx_http_perl_set_request(r, ctx); |
599 | 131 |
7525 | 132 if (ctx->error) { |
133 croak("send_http_header(): called after error"); | |
134 } | |
135 | |
599 | 136 if (r->headers_out.status == 0) { |
137 r->headers_out.status = NGX_HTTP_OK; | |
138 } | |
139 | |
140 if (items != 1) { | |
141 sv = ST(1); | |
142 | |
143 if (ngx_http_perl_sv2str(aTHX_ r, &r->headers_out.content_type, sv) | |
144 != NGX_OK) | |
145 { | |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
146 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
147 croak("ngx_http_perl_sv2str() failed"); |
599 | 148 } |
149 | |
1444
37938e68910b
allow to append charset to the "Content-Type" header
Igor Sysoev <igor@sysoev.ru>
parents:
1372
diff
changeset
|
150 r->headers_out.content_type_len = r->headers_out.content_type.len; |
37938e68910b
allow to append charset to the "Content-Type" header
Igor Sysoev <igor@sysoev.ru>
parents:
1372
diff
changeset
|
151 |
599 | 152 } else { |
673 | 153 if (ngx_http_set_content_type(r) != NGX_OK) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
154 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
155 croak("ngx_http_set_content_type() failed"); |
599 | 156 } |
157 } | |
158 | |
7516
36c52a0f6ded
Perl: disabled not_modified filter (ticket #1786).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6986
diff
changeset
|
159 r->disable_not_modified = 1; |
36c52a0f6ded
Perl: disabled not_modified filter (ticket #1786).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6986
diff
changeset
|
160 |
7525 | 161 rc = ngx_http_send_header(r); |
162 | |
163 if (rc == NGX_ERROR || rc > NGX_OK) { | |
164 ctx->error = 1; | |
165 ctx->status = rc; | |
166 croak("ngx_http_send_header() failed"); | |
167 } | |
599 | 168 |
169 | |
633 | 170 void |
171 header_only(r) | |
599 | 172 CODE: |
173 | |
633 | 174 dXSTARG; |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
175 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
176 ngx_http_perl_ctx_t *ctx; |
633 | 177 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
178 ngx_http_perl_set_request(r, ctx); |
599 | 179 |
633 | 180 sv_upgrade(TARG, SVt_IV); |
181 sv_setiv(TARG, r->header_only); | |
599 | 182 |
633 | 183 ST(0) = TARG; |
599 | 184 |
185 | |
633 | 186 void |
187 uri(r) | |
188 CODE: | |
189 | |
190 dXSTARG; | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
191 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
192 ngx_http_perl_ctx_t *ctx; |
599 | 193 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
194 ngx_http_perl_set_request(r, ctx); |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
195 ngx_http_perl_set_targ(r->uri.data, r->uri.len); |
633 | 196 |
197 ST(0) = TARG; | |
198 | |
199 | |
200 void | |
201 args(r) | |
599 | 202 CODE: |
203 | |
633 | 204 dXSTARG; |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
205 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
206 ngx_http_perl_ctx_t *ctx; |
599 | 207 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
208 ngx_http_perl_set_request(r, ctx); |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
209 ngx_http_perl_set_targ(r->args.data, r->args.len); |
599 | 210 |
633 | 211 ST(0) = TARG; |
599 | 212 |
213 | |
633 | 214 void |
629 | 215 request_method(r) |
633 | 216 CODE: |
217 | |
218 dXSTARG; | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
219 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
220 ngx_http_perl_ctx_t *ctx; |
629 | 221 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
222 ngx_http_perl_set_request(r, ctx); |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
223 ngx_http_perl_set_targ(r->method_name.data, r->method_name.len); |
633 | 224 |
225 ST(0) = TARG; | |
226 | |
227 | |
228 void | |
229 remote_addr(r) | |
629 | 230 CODE: |
231 | |
633 | 232 dXSTARG; |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
233 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
234 ngx_http_perl_ctx_t *ctx; |
629 | 235 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
236 ngx_http_perl_set_request(r, ctx); |
633 | 237 ngx_http_perl_set_targ(r->connection->addr_text.data, |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
238 r->connection->addr_text.len); |
629 | 239 |
633 | 240 ST(0) = TARG; |
629 | 241 |
242 | |
633 | 243 void |
244 header_in(r, key) | |
629 | 245 CODE: |
246 | |
633 | 247 dXSTARG; |
667 | 248 ngx_http_request_t *r; |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
249 ngx_http_perl_ctx_t *ctx; |
667 | 250 SV *key; |
5248
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
251 u_char *p, *lowcase_key, *value, sep; |
667 | 252 STRLEN len; |
253 ssize_t size; | |
254 ngx_uint_t i, n, hash; | |
5248
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
255 ngx_array_t *a; |
667 | 256 ngx_list_part_t *part; |
257 ngx_table_elt_t *h, **ph; | |
258 ngx_http_header_t *hh; | |
259 ngx_http_core_main_conf_t *cmcf; | |
629 | 260 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
261 ngx_http_perl_set_request(r, ctx); |
599 | 262 |
633 | 263 key = ST(1); |
599 | 264 |
265 if (SvROK(key) && SvTYPE(SvRV(key)) == SVt_PV) { | |
266 key = SvRV(key); | |
267 } | |
268 | |
269 p = (u_char *) SvPV(key, len); | |
270 | |
667 | 271 /* look up hashed headers */ |
272 | |
2049 | 273 lowcase_key = ngx_pnalloc(r->pool, len); |
667 | 274 if (lowcase_key == NULL) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
275 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
276 croak("ngx_pnalloc() failed"); |
667 | 277 } |
278 | |
2136 | 279 hash = ngx_hash_strlow(lowcase_key, p, len); |
667 | 280 |
281 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
282 | |
283 hh = ngx_hash_find(&cmcf->headers_in_hash, hash, lowcase_key, len); | |
284 | |
285 if (hh) { | |
5248
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
286 |
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
287 if (hh->offset == offsetof(ngx_http_headers_in_t, cookies)) { |
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
288 sep = ';'; |
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
289 goto multi; |
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
290 } |
5306
43900b822890
Perl: fixed syntax usage for C preprocessor directives.
Sergey Kandaurov <pluknet@nginx.com>
parents:
5248
diff
changeset
|
291 #if (NGX_HTTP_X_FORWARDED_FOR) |
5248
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
292 if (hh->offset == offsetof(ngx_http_headers_in_t, x_forwarded_for)) { |
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
293 sep = ','; |
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
294 goto multi; |
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
295 } |
5306
43900b822890
Perl: fixed syntax usage for C preprocessor directives.
Sergey Kandaurov <pluknet@nginx.com>
parents:
5248
diff
changeset
|
296 #endif |
5248
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
297 |
6232
5f2a0739da19
Perl: fixed warning about "sep" may be used uninitialized.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5306
diff
changeset
|
298 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + hh->offset); |
667 | 299 |
6232
5f2a0739da19
Perl: fixed warning about "sep" may be used uninitialized.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5306
diff
changeset
|
300 if (*ph) { |
5f2a0739da19
Perl: fixed warning about "sep" may be used uninitialized.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5306
diff
changeset
|
301 ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len); |
667 | 302 |
6232
5f2a0739da19
Perl: fixed warning about "sep" may be used uninitialized.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5306
diff
changeset
|
303 goto done; |
5f2a0739da19
Perl: fixed warning about "sep" may be used uninitialized.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5306
diff
changeset
|
304 } |
667 | 305 |
6232
5f2a0739da19
Perl: fixed warning about "sep" may be used uninitialized.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5306
diff
changeset
|
306 XSRETURN_UNDEF; |
667 | 307 |
5248
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
308 multi: |
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
309 |
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
310 /* Cookie, X-Forwarded-For */ |
667 | 311 |
5248
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
312 a = (ngx_array_t *) ((char *) &r->headers_in + hh->offset); |
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
313 |
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
314 n = a->nelts; |
667 | 315 |
316 if (n == 0) { | |
317 XSRETURN_UNDEF; | |
318 } | |
319 | |
5248
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
320 ph = a->elts; |
667 | 321 |
322 if (n == 1) { | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
323 ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len); |
667 | 324 |
325 goto done; | |
326 } | |
327 | |
328 size = - (ssize_t) (sizeof("; ") - 1); | |
329 | |
330 for (i = 0; i < n; i++) { | |
331 size += ph[i]->value.len + sizeof("; ") - 1; | |
332 } | |
333 | |
5248
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
334 value = ngx_pnalloc(r->pool, size); |
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
335 if (value == NULL) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
336 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
337 croak("ngx_pnalloc() failed"); |
667 | 338 } |
339 | |
5248
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
340 p = value; |
667 | 341 |
342 for (i = 0; /* void */ ; i++) { | |
343 p = ngx_copy(p, ph[i]->value.data, ph[i]->value.len); | |
344 | |
345 if (i == n - 1) { | |
346 break; | |
347 } | |
348 | |
5248
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
349 *p++ = sep; *p++ = ' '; |
667 | 350 } |
351 | |
5248
f5626ab8cb87
Perl: fixed r->header_in("Cookie") (ticket #351).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5198
diff
changeset
|
352 ngx_http_perl_set_targ(value, size); |
667 | 353 |
354 goto done; | |
355 } | |
356 | |
357 /* iterate over all headers */ | |
358 | |
599 | 359 part = &r->headers_in.headers.part; |
667 | 360 h = part->elts; |
599 | 361 |
362 for (i = 0; /* void */ ; i++) { | |
363 | |
364 if (i >= part->nelts) { | |
365 if (part->next == NULL) { | |
366 break; | |
367 } | |
368 | |
369 part = part->next; | |
667 | 370 h = part->elts; |
599 | 371 i = 0; |
372 } | |
373 | |
667 | 374 if (len != h[i].key.len |
375 || ngx_strcasecmp(p, h[i].key.data) != 0) | |
599 | 376 { |
377 continue; | |
378 } | |
379 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
380 ngx_http_perl_set_targ(h[i].value.data, h[i].value.len); |
599 | 381 |
382 goto done; | |
383 } | |
384 | |
385 XSRETURN_UNDEF; | |
386 | |
387 done: | |
388 | |
633 | 389 ST(0) = TARG; |
599 | 390 |
391 | |
633 | 392 void |
681 | 393 has_request_body(r, next) |
394 CODE: | |
395 | |
396 dXSTARG; | |
397 ngx_http_request_t *r; | |
398 ngx_http_perl_ctx_t *ctx; | |
7525 | 399 ngx_int_t rc; |
681 | 400 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
401 ngx_http_perl_set_request(r, ctx); |
681 | 402 |
7527
02cd116ebe2a
Perl: protection against duplicate $r->sleep() calls.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7526
diff
changeset
|
403 if (ctx->next) { |
02cd116ebe2a
Perl: protection against duplicate $r->sleep() calls.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7526
diff
changeset
|
404 croak("has_request_body(): another handler active"); |
02cd116ebe2a
Perl: protection against duplicate $r->sleep() calls.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7526
diff
changeset
|
405 } |
02cd116ebe2a
Perl: protection against duplicate $r->sleep() calls.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7526
diff
changeset
|
406 |
5181
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
407 if (r->headers_in.content_length_n <= 0 && !r->headers_in.chunked) { |
681 | 408 XSRETURN_UNDEF; |
409 } | |
410 | |
909 | 411 ctx->next = SvRV(ST(1)); |
681 | 412 |
413 r->request_body_in_single_buf = 1; | |
414 r->request_body_in_persistent_file = 1; | |
1075
4d203f76b757
undo "client_body_in_file_only any"
Igor Sysoev <igor@sysoev.ru>
parents:
1056
diff
changeset
|
415 r->request_body_in_clean_file = 1; |
681 | 416 |
417 if (r->request_body_in_file_only) { | |
418 r->request_body_file_log_level = 0; | |
419 } | |
420 | |
7525 | 421 rc = ngx_http_read_client_request_body(r, ngx_http_perl_handle_request); |
422 | |
423 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | |
424 ctx->error = 1; | |
425 ctx->status = rc; | |
426 ctx->next = NULL; | |
427 croak("ngx_http_read_client_request_body() failed"); | |
428 } | |
681 | 429 |
430 sv_upgrade(TARG, SVt_IV); | |
431 sv_setiv(TARG, 1); | |
432 | |
433 ST(0) = TARG; | |
434 | |
435 | |
436 void | |
631 | 437 request_body(r) |
438 CODE: | |
439 | |
633 | 440 dXSTARG; |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
441 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
442 ngx_http_perl_ctx_t *ctx; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
443 u_char *p, *data; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
444 size_t len; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
445 ngx_buf_t *buf; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
446 ngx_chain_t *cl; |
633 | 447 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
448 ngx_http_perl_set_request(r, ctx); |
631 | 449 |
941 | 450 if (r->request_body == NULL |
451 || r->request_body->temp_file | |
452 || r->request_body->bufs == NULL) | |
453 { | |
633 | 454 XSRETURN_UNDEF; |
455 } | |
631 | 456 |
5181
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
457 cl = r->request_body->bufs; |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
458 buf = cl->buf; |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
459 |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
460 if (cl->next == NULL) { |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
461 len = buf->last - buf->pos; |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
462 data = buf->pos; |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
463 goto done; |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
464 } |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
465 |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
466 len = buf->last - buf->pos; |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
467 cl = cl->next; |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
468 |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
469 for ( /* void */ ; cl; cl = cl->next) { |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
470 buf = cl->buf; |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
471 len += buf->last - buf->pos; |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
472 } |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
473 |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
474 p = ngx_pnalloc(r->pool, len); |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
475 if (p == NULL) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
476 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
477 croak("ngx_pnalloc() failed"); |
5181
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
478 } |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
479 |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
480 data = p; |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
481 cl = r->request_body->bufs; |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
482 |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
483 for ( /* void */ ; cl; cl = cl->next) { |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
484 buf = cl->buf; |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
485 p = ngx_cpymem(p, buf->pos, buf->last - buf->pos); |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
486 } |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
487 |
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
488 done: |
631 | 489 |
490 if (len == 0) { | |
491 XSRETURN_UNDEF; | |
492 } | |
493 | |
5181
4d0ac175f6e4
Perl: request body handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4644
diff
changeset
|
494 ngx_http_perl_set_targ(data, len); |
631 | 495 |
633 | 496 ST(0) = TARG; |
631 | 497 |
498 | |
633 | 499 void |
500 request_body_file(r) | |
501 CODE: | |
502 | |
503 dXSTARG; | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
504 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
505 ngx_http_perl_ctx_t *ctx; |
633 | 506 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
507 ngx_http_perl_set_request(r, ctx); |
633 | 508 |
941 | 509 if (r->request_body == NULL || r->request_body->temp_file == NULL) { |
633 | 510 XSRETURN_UNDEF; |
511 } | |
512 | |
513 ngx_http_perl_set_targ(r->request_body->temp_file->file.name.data, | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
514 r->request_body->temp_file->file.name.len); |
599 | 515 |
633 | 516 ST(0) = TARG; |
517 | |
518 | |
519 void | |
1371 | 520 discard_request_body(r) |
521 CODE: | |
522 | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
523 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
524 ngx_http_perl_ctx_t *ctx; |
7525 | 525 ngx_int_t rc; |
1371 | 526 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
527 ngx_http_perl_set_request(r, ctx); |
1371 | 528 |
7525 | 529 rc = ngx_http_discard_request_body(r); |
530 | |
531 if (rc != NGX_OK) { | |
532 ctx->error = 1; | |
533 ctx->status = rc; | |
534 croak("ngx_http_discard_request_body() failed"); | |
535 } | |
1371 | 536 |
537 | |
538 void | |
633 | 539 header_out(r, key, value) |
540 CODE: | |
599 | 541 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
542 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
543 ngx_http_perl_ctx_t *ctx; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
544 SV *key; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
545 SV *value; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
546 ngx_table_elt_t *header; |
599 | 547 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
548 ngx_http_perl_set_request(r, ctx); |
633 | 549 |
7525 | 550 if (ctx->error) { |
551 croak("header_out(): called after error"); | |
552 } | |
553 | |
633 | 554 key = ST(1); |
555 value = ST(2); | |
599 | 556 |
557 header = ngx_list_push(&r->headers_out.headers); | |
558 if (header == NULL) { | |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
559 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
560 croak("ngx_list_push() failed"); |
599 | 561 } |
562 | |
563 header->hash = 1; | |
564 | |
565 if (ngx_http_perl_sv2str(aTHX_ r, &header->key, key) != NGX_OK) { | |
6986
0cdee26605f3
Cleaned up r->headers_out.headers allocation error handling.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6960
diff
changeset
|
566 header->hash = 0; |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
567 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
568 croak("ngx_http_perl_sv2str() failed"); |
599 | 569 } |
570 | |
571 if (ngx_http_perl_sv2str(aTHX_ r, &header->value, value) != NGX_OK) { | |
6986
0cdee26605f3
Cleaned up r->headers_out.headers allocation error handling.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6960
diff
changeset
|
572 header->hash = 0; |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
573 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
574 croak("ngx_http_perl_sv2str() failed"); |
599 | 575 } |
576 | |
577 if (header->key.len == sizeof("Content-Length") - 1 | |
3870 | 578 && ngx_strncasecmp(header->key.data, (u_char *) "Content-Length", |
741
63a08390a8a2
$r->headers_out("Content-Length", "NNN") did not work
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
579 sizeof("Content-Length") - 1) == 0) |
599 | 580 { |
741
63a08390a8a2
$r->headers_out("Content-Length", "NNN") did not work
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
581 r->headers_out.content_length_n = (off_t) SvIV(value); |
599 | 582 r->headers_out.content_length = header; |
583 } | |
584 | |
4196
190ae1a7f917
Handling of Content-Encoding set from perl.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4188
diff
changeset
|
585 if (header->key.len == sizeof("Content-Encoding") - 1 |
4644
95763fce86a8
Fixed warning during nginx.xs compilation.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4494
diff
changeset
|
586 && ngx_strncasecmp(header->key.data, (u_char *) "Content-Encoding", |
4196
190ae1a7f917
Handling of Content-Encoding set from perl.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4188
diff
changeset
|
587 sizeof("Content-Encoding") - 1) == 0) |
190ae1a7f917
Handling of Content-Encoding set from perl.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4188
diff
changeset
|
588 { |
190ae1a7f917
Handling of Content-Encoding set from perl.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4188
diff
changeset
|
589 r->headers_out.content_encoding = header; |
190ae1a7f917
Handling of Content-Encoding set from perl.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4188
diff
changeset
|
590 } |
190ae1a7f917
Handling of Content-Encoding set from perl.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4188
diff
changeset
|
591 |
599 | 592 |
633 | 593 void |
599 | 594 filename(r) |
633 | 595 CODE: |
599 | 596 |
633 | 597 dXSTARG; |
598 ngx_http_request_t *r; | |
599 | 599 ngx_http_perl_ctx_t *ctx; |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
600 size_t root; |
599 | 601 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
602 ngx_http_perl_set_request(r, ctx); |
599 | 603 |
633 | 604 if (ctx->filename.data) { |
599 | 605 goto done; |
606 } | |
607 | |
774
589841f06b87
previous commit broke two modules
Igor Sysoev <igor@sysoev.ru>
parents:
741
diff
changeset
|
608 if (ngx_http_map_uri_to_path(r, &ctx->filename, &root, 0) == NULL) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
609 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
610 croak("ngx_http_map_uri_to_path() failed"); |
599 | 611 } |
612 | |
633 | 613 ctx->filename.len--; |
614 sv_setpv(PL_statname, (char *) ctx->filename.data); | |
599 | 615 |
616 done: | |
617 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
618 ngx_http_perl_set_targ(ctx->filename.data, ctx->filename.len); |
599 | 619 |
633 | 620 ST(0) = TARG; |
599 | 621 |
622 | |
633 | 623 void |
599 | 624 print(r, ...) |
625 CODE: | |
626 | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
627 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
628 ngx_http_perl_ctx_t *ctx; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
629 SV *sv; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
630 int i; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
631 u_char *p; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
632 size_t size; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
633 STRLEN len; |
7525 | 634 ngx_int_t rc; |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
635 ngx_buf_t *b; |
633 | 636 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
637 ngx_http_perl_set_request(r, ctx); |
599 | 638 |
7525 | 639 if (ctx->error) { |
640 croak("print(): called after error"); | |
641 } | |
642 | |
599 | 643 if (items == 2) { |
644 | |
645 /* | |
646 * do zero copy for prolate single read-only SV: | |
647 * $r->print("some text\n"); | |
648 */ | |
649 | |
650 sv = ST(1); | |
651 | |
652 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
653 sv = SvRV(sv); | |
654 } | |
655 | |
1703
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
656 if (SvREADONLY(sv) && SvPOK(sv)) { |
599 | 657 |
658 p = (u_char *) SvPV(sv, len); | |
659 | |
660 if (len == 0) { | |
633 | 661 XSRETURN_EMPTY; |
599 | 662 } |
663 | |
664 b = ngx_calloc_buf(r->pool); | |
665 if (b == NULL) { | |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
666 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
667 croak("ngx_calloc_buf() failed"); |
599 | 668 } |
669 | |
670 b->memory = 1; | |
671 b->pos = p; | |
672 b->last = p + len; | |
673 b->start = p; | |
674 b->end = b->last; | |
675 | |
601 | 676 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
677 "$r->print: read-only SV: %z", len); | |
678 | |
599 | 679 goto out; |
680 } | |
681 } | |
682 | |
683 size = 0; | |
684 | |
685 for (i = 1; i < items; i++) { | |
686 | |
687 sv = ST(i); | |
688 | |
689 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
690 sv = SvRV(sv); | |
691 } | |
692 | |
601 | 693 (void) SvPV(sv, len); |
599 | 694 |
601 | 695 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
696 "$r->print: copy SV: %z", len); | |
599 | 697 |
698 size += len; | |
699 } | |
700 | |
701 if (size == 0) { | |
633 | 702 XSRETURN_EMPTY; |
599 | 703 } |
704 | |
705 b = ngx_create_temp_buf(r->pool, size); | |
706 if (b == NULL) { | |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
707 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
708 croak("ngx_create_temp_buf() failed"); |
599 | 709 } |
710 | |
711 for (i = 1; i < items; i++) { | |
712 sv = ST(i); | |
713 | |
714 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
715 sv = SvRV(sv); | |
716 } | |
717 | |
718 p = (u_char *) SvPV(sv, len); | |
719 b->last = ngx_cpymem(b->last, p, len); | |
720 } | |
721 | |
722 out: | |
723 | |
7525 | 724 rc = ngx_http_perl_output(r, ctx, b); |
725 | |
726 if (rc == NGX_ERROR) { | |
727 ctx->error = 1; | |
728 croak("ngx_http_perl_output() failed"); | |
729 } | |
599 | 730 |
731 | |
633 | 732 void |
613 | 733 sendfile(r, filename, offset = -1, bytes = 0) |
633 | 734 CODE: |
735 | |
1454 | 736 ngx_http_request_t *r; |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
737 ngx_http_perl_ctx_t *ctx; |
1454 | 738 char *filename; |
2794
92bd6afe8d9c
use off_t in $r->sendfile(), this allows to use 64-bit off_t on platforms
Igor Sysoev <igor@sysoev.ru>
parents:
2756
diff
changeset
|
739 off_t offset; |
1454 | 740 size_t bytes; |
7525 | 741 ngx_int_t rc; |
1454 | 742 ngx_str_t path; |
743 ngx_buf_t *b; | |
744 ngx_open_file_info_t of; | |
745 ngx_http_core_loc_conf_t *clcf; | |
599 | 746 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
747 ngx_http_perl_set_request(r, ctx); |
633 | 748 |
7525 | 749 if (ctx->error) { |
750 croak("sendfile(): called after error"); | |
751 } | |
752 | |
633 | 753 filename = SvPV_nolen(ST(1)); |
599 | 754 |
755 if (filename == NULL) { | |
756 croak("sendfile(): NULL filename"); | |
757 } | |
758 | |
633 | 759 offset = items < 3 ? -1 : SvIV(ST(2)); |
760 bytes = items < 4 ? 0 : SvIV(ST(3)); | |
761 | |
599 | 762 b = ngx_calloc_buf(r->pool); |
763 if (b == NULL) { | |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
764 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
765 croak("ngx_calloc_buf() failed"); |
599 | 766 } |
767 | |
768 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); | |
769 if (b->file == NULL) { | |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
770 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
771 croak("ngx_pcalloc() failed"); |
599 | 772 } |
773 | |
1454 | 774 path.len = ngx_strlen(filename); |
775 | |
2061
b0a1c84725cf
change useless ngx_pcalloc() to ngx_pnalloc()
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
776 path.data = ngx_pnalloc(r->pool, path.len + 1); |
1454 | 777 if (path.data == NULL) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
778 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
779 croak("ngx_pnalloc() failed"); |
599 | 780 } |
781 | |
3870 | 782 (void) ngx_cpystrn(path.data, (u_char *) filename, path.len + 1); |
1560
25ee6eee7573
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1457
diff
changeset
|
783 |
2063
67a29af877ed
initialize of.uniq in ngx_open_cached_file()
Igor Sysoev <igor@sysoev.ru>
parents:
2061
diff
changeset
|
784 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
67a29af877ed
initialize of.uniq in ngx_open_cached_file()
Igor Sysoev <igor@sysoev.ru>
parents:
2061
diff
changeset
|
785 |
2068
75a8d34459c5
ngx_memzero() ngx_open_file_info_t
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
786 ngx_memzero(&of, sizeof(ngx_open_file_info_t)); |
75a8d34459c5
ngx_memzero() ngx_open_file_info_t
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
787 |
3178 | 788 of.read_ahead = clcf->read_ahead; |
2129 | 789 of.directio = clcf->directio; |
2063
67a29af877ed
initialize of.uniq in ngx_open_cached_file()
Igor Sysoev <igor@sysoev.ru>
parents:
2061
diff
changeset
|
790 of.valid = clcf->open_file_cache_valid; |
67a29af877ed
initialize of.uniq in ngx_open_cached_file()
Igor Sysoev <igor@sysoev.ru>
parents:
2061
diff
changeset
|
791 of.min_uses = clcf->open_file_cache_min_uses; |
67a29af877ed
initialize of.uniq in ngx_open_cached_file()
Igor Sysoev <igor@sysoev.ru>
parents:
2061
diff
changeset
|
792 of.errors = clcf->open_file_cache_errors; |
67a29af877ed
initialize of.uniq in ngx_open_cached_file()
Igor Sysoev <igor@sysoev.ru>
parents:
2061
diff
changeset
|
793 of.events = clcf->open_file_cache_events; |
4494
13e09cf11d4e
Disable symlinks: initialization of the "disable_symlinks" field in
Valentin Bartenev <vbart@nginx.com>
parents:
4478
diff
changeset
|
794 |
13e09cf11d4e
Disable symlinks: initialization of the "disable_symlinks" field in
Valentin Bartenev <vbart@nginx.com>
parents:
4478
diff
changeset
|
795 if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
796 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
797 croak("ngx_http_set_disable_symlinks() failed"); |
4494
13e09cf11d4e
Disable symlinks: initialization of the "disable_symlinks" field in
Valentin Bartenev <vbart@nginx.com>
parents:
4478
diff
changeset
|
798 } |
2063
67a29af877ed
initialize of.uniq in ngx_open_cached_file()
Igor Sysoev <igor@sysoev.ru>
parents:
2061
diff
changeset
|
799 |
1799 | 800 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool) |
801 != NGX_OK) | |
802 { | |
1454 | 803 if (of.err == 0) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
804 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
805 croak("ngx_open_cached_file() failed"); |
1454 | 806 } |
807 | |
599 | 808 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, |
2756
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2231
diff
changeset
|
809 "%s \"%s\" failed", of.failed, filename); |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
810 |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
811 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
812 croak("ngx_open_cached_file() failed"); |
599 | 813 } |
814 | |
613 | 815 if (offset == -1) { |
816 offset = 0; | |
817 } | |
818 | |
819 if (bytes == 0) { | |
1454 | 820 bytes = of.size - offset; |
599 | 821 } |
822 | |
823 b->in_file = 1; | |
613 | 824 |
825 b->file_pos = offset; | |
826 b->file_last = offset + bytes; | |
599 | 827 |
1454 | 828 b->file->fd = of.fd; |
599 | 829 b->file->log = r->connection->log; |
2231
8564129d49b6
*) handle unaligned file part for directio
Igor Sysoev <igor@sysoev.ru>
parents:
2136
diff
changeset
|
830 b->file->directio = of.is_directio; |
599 | 831 |
7525 | 832 rc = ngx_http_perl_output(r, ctx, b); |
833 | |
834 if (rc == NGX_ERROR) { | |
835 ctx->error = 1; | |
836 croak("ngx_http_perl_output() failed"); | |
837 } | |
599 | 838 |
839 | |
633 | 840 void |
1178
a77f6980de50
rename $r->rflush to $r->flush
Igor Sysoev <igor@sysoev.ru>
parents:
1075
diff
changeset
|
841 flush(r) |
633 | 842 CODE: |
599 | 843 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
844 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
845 ngx_http_perl_ctx_t *ctx; |
7525 | 846 ngx_int_t rc; |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
847 ngx_buf_t *b; |
599 | 848 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
849 ngx_http_perl_set_request(r, ctx); |
599 | 850 |
7525 | 851 if (ctx->error) { |
852 croak("flush(): called after error"); | |
853 } | |
854 | |
599 | 855 b = ngx_calloc_buf(r->pool); |
856 if (b == NULL) { | |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
857 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
858 croak("ngx_calloc_buf() failed"); |
599 | 859 } |
860 | |
861 b->flush = 1; | |
862 | |
1178
a77f6980de50
rename $r->rflush to $r->flush
Igor Sysoev <igor@sysoev.ru>
parents:
1075
diff
changeset
|
863 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->flush"); |
601 | 864 |
7525 | 865 rc = ngx_http_perl_output(r, ctx, b); |
866 | |
867 if (rc == NGX_ERROR) { | |
868 ctx->error = 1; | |
869 croak("ngx_http_perl_output() failed"); | |
870 } | |
599 | 871 |
633 | 872 XSRETURN_EMPTY; |
599 | 873 |
874 | |
875 void | |
876 internal_redirect(r, uri) | |
633 | 877 CODE: |
599 | 878 |
633 | 879 ngx_http_request_t *r; |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
880 ngx_http_perl_ctx_t *ctx; |
633 | 881 SV *uri; |
599 | 882 ngx_uint_t i; |
883 | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
884 ngx_http_perl_set_request(r, ctx); |
633 | 885 |
886 uri = ST(1); | |
599 | 887 |
888 if (ngx_http_perl_sv2str(aTHX_ r, &ctx->redirect_uri, uri) != NGX_OK) { | |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
889 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
890 croak("ngx_http_perl_sv2str() failed"); |
599 | 891 } |
892 | |
893 for (i = 0; i < ctx->redirect_uri.len; i++) { | |
894 if (ctx->redirect_uri.data[i] == '?') { | |
895 | |
896 ctx->redirect_args.len = ctx->redirect_uri.len - (i + 1); | |
897 ctx->redirect_args.data = &ctx->redirect_uri.data[i + 1]; | |
898 ctx->redirect_uri.len = i; | |
899 | |
900 XSRETURN_EMPTY; | |
901 } | |
902 } | |
601 | 903 |
904 | |
633 | 905 void |
811 | 906 allow_ranges(r) |
907 CODE: | |
908 | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
909 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
910 ngx_http_perl_ctx_t *ctx; |
811 | 911 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
912 ngx_http_perl_set_request(r, ctx); |
811 | 913 |
914 r->allow_ranges = 1; | |
915 | |
916 | |
917 void | |
601 | 918 unescape(r, text, type = 0) |
919 CODE: | |
920 | |
633 | 921 dXSTARG; |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
922 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
923 ngx_http_perl_ctx_t *ctx; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
924 SV *text; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
925 int type; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
926 u_char *p, *dst, *src; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
927 STRLEN len; |
601 | 928 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
929 ngx_http_perl_set_request(r, ctx); |
633 | 930 |
931 text = ST(1); | |
932 | |
933 src = (u_char *) SvPV(text, len); | |
934 | |
2049 | 935 p = ngx_pnalloc(r->pool, len + 1); |
601 | 936 if (p == NULL) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
937 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
938 croak("ngx_pnalloc() failed"); |
601 | 939 } |
940 | |
941 dst = p; | |
942 | |
633 | 943 type = items < 3 ? 0 : SvIV(ST(2)); |
944 | |
945 ngx_unescape_uri(&dst, &src, len, (ngx_uint_t) type); | |
601 | 946 *dst = '\0'; |
947 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
948 ngx_http_perl_set_targ(p, dst - p); |
601 | 949 |
633 | 950 ST(0) = TARG; |
833 | 951 |
952 | |
953 void | |
954 variable(r, name, value = NULL) | |
955 CODE: | |
956 | |
957 dXSTARG; | |
958 ngx_http_request_t *r; | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
959 ngx_http_perl_ctx_t *ctx; |
833 | 960 SV *name, *value; |
961 u_char *p, *lowcase; | |
962 STRLEN len; | |
963 ngx_str_t var, val; | |
964 ngx_uint_t i, hash; | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
965 ngx_http_perl_var_t *v; |
833 | 966 ngx_http_variable_value_t *vv; |
967 | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
968 ngx_http_perl_set_request(r, ctx); |
833 | 969 |
970 name = ST(1); | |
971 | |
972 if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) { | |
973 name = SvRV(name); | |
974 } | |
975 | |
976 if (items == 2) { | |
977 value = NULL; | |
978 | |
979 } else { | |
980 value = ST(2); | |
981 | |
982 if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) { | |
983 value = SvRV(value); | |
984 } | |
985 | |
986 if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) { | |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
987 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
988 croak("ngx_http_perl_sv2str() failed"); |
833 | 989 } |
990 } | |
991 | |
992 p = (u_char *) SvPV(name, len); | |
993 | |
2049 | 994 lowcase = ngx_pnalloc(r->pool, len); |
833 | 995 if (lowcase == NULL) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
996 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
997 croak("ngx_pnalloc() failed"); |
833 | 998 } |
999 | |
2136 | 1000 hash = ngx_hash_strlow(lowcase, p, len); |
833 | 1001 |
1002 var.len = len; | |
1003 var.data = lowcase; | |
5306
43900b822890
Perl: fixed syntax usage for C preprocessor directives.
Sergey Kandaurov <pluknet@nginx.com>
parents:
5248
diff
changeset
|
1004 #if (NGX_DEBUG) |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1005 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1006 if (value) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1007 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1008 "perl variable: \"%V\"=\"%V\"", &var, &val); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1009 } else { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1010 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1011 "perl variable: \"%V\"", &var); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1012 } |
5306
43900b822890
Perl: fixed syntax usage for C preprocessor directives.
Sergey Kandaurov <pluknet@nginx.com>
parents:
5248
diff
changeset
|
1013 #endif |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1014 |
3500
0eb46e3c5c02
change processing variables accessed by SSI and perl module:
Igor Sysoev <igor@sysoev.ru>
parents:
3447
diff
changeset
|
1015 vv = ngx_http_get_variable(r, &var, hash); |
833 | 1016 if (vv == NULL) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
1017 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
1018 croak("ngx_http_get_variable() failed"); |
833 | 1019 } |
1020 | |
1021 if (vv->not_found) { | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1022 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1023 if (ctx->variables) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1024 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1025 v = ctx->variables->elts; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1026 for (i = 0; i < ctx->variables->nelts; i++) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1027 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1028 if (hash != v[i].hash |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1029 || len != v[i].name.len |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1030 || ngx_strncmp(lowcase, v[i].name.data, len) != 0) |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1031 { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1032 continue; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1033 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1034 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1035 if (value) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1036 v[i].value = val; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1037 XSRETURN_UNDEF; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1038 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1039 |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
1040 ngx_http_perl_set_targ(v[i].value.data, v[i].value.len); |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1041 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1042 goto done; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1043 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1044 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1045 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1046 if (value) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1047 if (ctx->variables == NULL) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1048 ctx->variables = ngx_array_create(r->pool, 1, |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1049 sizeof(ngx_http_perl_var_t)); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1050 if (ctx->variables == NULL) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
1051 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
1052 croak("ngx_array_create() failed"); |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1053 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1054 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1055 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1056 v = ngx_array_push(ctx->variables); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1057 if (v == NULL) { |
7526
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
1058 ctx->error = 1; |
8125552a10ca
Perl: handling of allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7525
diff
changeset
|
1059 croak("ngx_array_push() failed"); |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1060 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1061 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1062 v->hash = hash; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1063 v->name.len = len; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1064 v->name.data = lowcase; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1065 v->value = val; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1066 |
833 | 1067 XSRETURN_UNDEF; |
1068 } | |
1069 | |
1070 XSRETURN_UNDEF; | |
1071 } | |
1072 | |
1073 if (value) { | |
1074 vv->len = val.len; | |
1075 vv->valid = 1; | |
1565 | 1076 vv->no_cacheable = 0; |
833 | 1077 vv->not_found = 0; |
1078 vv->data = val.data; | |
1079 | |
1080 XSRETURN_UNDEF; | |
1081 } | |
1082 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
1083 ngx_http_perl_set_targ(vv->data, vv->len); |
833 | 1084 |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1085 done: |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
1086 |
833 | 1087 ST(0) = TARG; |
907 | 1088 |
1089 | |
1090 void | |
911 | 1091 sleep(r, sleep, next) |
1092 CODE: | |
1093 | |
1094 ngx_http_request_t *r; | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
1095 ngx_http_perl_ctx_t *ctx; |
1897 | 1096 ngx_msec_t sleep; |
911 | 1097 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
1098 ngx_http_perl_set_request(r, ctx); |
911 | 1099 |
7527
02cd116ebe2a
Perl: protection against duplicate $r->sleep() calls.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7526
diff
changeset
|
1100 if (ctx->next) { |
02cd116ebe2a
Perl: protection against duplicate $r->sleep() calls.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7526
diff
changeset
|
1101 croak("sleep(): another handler active"); |
02cd116ebe2a
Perl: protection against duplicate $r->sleep() calls.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7526
diff
changeset
|
1102 } |
02cd116ebe2a
Perl: protection against duplicate $r->sleep() calls.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7526
diff
changeset
|
1103 |
1898 | 1104 sleep = (ngx_msec_t) SvIV(ST(1)); |
1897 | 1105 |
1106 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1107 "perl sleep: %M", sleep); | |
1108 | |
911 | 1109 ctx->next = SvRV(ST(2)); |
1110 | |
6960
1c5e5e5b008d
Perl: fixed delaying subrequests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6233
diff
changeset
|
1111 r->connection->write->delayed = 1; |
1897 | 1112 ngx_add_timer(r->connection->write, sleep); |
1113 | |
1114 r->write_event_handler = ngx_http_perl_sleep_handler; | |
3447
de70f912ad58
fix request counter for $r->sleep(), the bug was introduced in r3050
Igor Sysoev <igor@sysoev.ru>
parents:
3317
diff
changeset
|
1115 r->main->count++; |
911 | 1116 |
1117 | |
1118 void | |
907 | 1119 log_error(r, err, msg) |
1120 CODE: | |
1121 | |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
1122 ngx_http_request_t *r; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
1123 ngx_http_perl_ctx_t *ctx; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
1124 SV *err, *msg; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
1125 u_char *p; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
1126 STRLEN len; |
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
1127 ngx_err_t e; |
907 | 1128 |
7524
deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7516
diff
changeset
|
1129 ngx_http_perl_set_request(r, ctx); |
907 | 1130 |
1131 err = ST(1); | |
1132 | |
1133 if (SvROK(err) && SvTYPE(SvRV(err)) == SVt_PV) { | |
1134 err = SvRV(err); | |
1135 } | |
1136 | |
1137 e = SvIV(err); | |
1138 | |
1139 msg = ST(2); | |
1140 | |
1141 if (SvROK(msg) && SvTYPE(SvRV(msg)) == SVt_PV) { | |
1142 msg = SvRV(msg); | |
1143 } | |
1144 | |
1145 p = (u_char *) SvPV(msg, len); | |
1146 | |
910 | 1147 ngx_log_error(NGX_LOG_ERR, r->connection->log, e, "perl: %s", p); |