Mercurial > hg > nginx
annotate src/http/modules/perl/nginx.xs @ 2472:d7d3a72c07d8 stable-0.6
r2121, r2126 merge:
*) fix utf-8 names in autoindex
*) rename ngx_utf_...() to ngx_utf8_...()
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 26 Jan 2009 15:09:55 +0000 |
parents | 9a73235ce453 |
children | 2a92804f4109 |
rev | line source |
---|---|
599 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
882
26c3e48b9996
the PERL_NO_GET_CONTEXT is actually required, see perlguts
Igor Sysoev <igor@sysoev.ru>
parents:
869
diff
changeset
|
7 #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
|
8 |
599 | 9 #include <ngx_config.h> |
10 #include <ngx_core.h> | |
11 #include <ngx_http.h> | |
12 #include <ngx_http_perl_module.h> | |
13 | |
603 | 14 #include "XSUB.h" |
15 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
16 |
633 | 17 #define ngx_http_perl_set_request(r) \ |
18 r = INT2PTR(ngx_http_request_t *, SvIV((SV *) SvRV(ST(0)))) | |
19 | |
20 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
21 #define ngx_http_perl_set_targ(p, len) \ |
633 | 22 \ |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
23 SvUPGRADE(TARG, SVt_PV); \ |
633 | 24 SvPOK_on(TARG); \ |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
25 sv_setpvn(TARG, (char *) p, len) |
633 | 26 |
599 | 27 |
28 static ngx_int_t | |
29 ngx_http_perl_sv2str(pTHX_ ngx_http_request_t *r, ngx_str_t *s, SV *sv) | |
30 { | |
31 u_char *p; | |
32 STRLEN len; | |
33 | |
34 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
35 sv = SvRV(sv); | |
36 } | |
37 | |
38 p = (u_char *) SvPV(sv, len); | |
39 | |
40 s->len = len; | |
41 | |
1703
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
42 if (SvREADONLY(sv) && SvPOK(sv)) { |
599 | 43 s->data = p; |
1703
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
44 |
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
45 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
|
46 "perl sv2str: %08XD \"%V\"", sv->sv_flags, s); |
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
47 |
599 | 48 return NGX_OK; |
49 } | |
50 | |
51 s->data = ngx_palloc(r->pool, len); | |
52 if (s->data == NULL) { | |
53 return NGX_ERROR; | |
54 } | |
55 | |
56 ngx_memcpy(s->data, p, len); | |
57 | |
1703
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
58 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
|
59 "perl sv2str: %08XD \"%V\"", sv->sv_flags, s); |
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
60 |
599 | 61 return NGX_OK; |
62 } | |
63 | |
64 | |
65 static ngx_int_t | |
66 ngx_http_perl_output(ngx_http_request_t *r, ngx_buf_t *b) | |
67 { | |
617 | 68 ngx_chain_t out; |
69 #if (NGX_HTTP_SSI) | |
70 ngx_chain_t *cl; | |
599 | 71 ngx_http_perl_ctx_t *ctx; |
72 | |
73 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
74 | |
75 if (ctx->ssi) { | |
76 cl = ngx_alloc_chain_link(r->pool); | |
77 if (cl == NULL) { | |
78 return NGX_ERROR; | |
79 } | |
80 | |
81 cl->buf = b; | |
82 cl->next = NULL; | |
83 *ctx->ssi->last_out = cl; | |
84 ctx->ssi->last_out = &cl->next; | |
85 | |
86 return NGX_OK; | |
87 } | |
617 | 88 #endif |
599 | 89 |
90 out.buf = b; | |
91 out.next = NULL; | |
92 | |
93 return ngx_http_output_filter(r, &out); | |
94 } | |
95 | |
96 | |
97 MODULE = nginx PACKAGE = nginx | |
98 | |
99 | |
633 | 100 void |
915 | 101 status(r, code) |
102 CODE: | |
103 | |
104 ngx_http_request_t *r; | |
105 | |
106 ngx_http_perl_set_request(r); | |
107 | |
108 r->headers_out.status = SvIV(ST(1)); | |
109 | |
110 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
111 "perl status: %d", r->headers_out.status); | |
112 | |
113 XSRETURN_UNDEF; | |
114 | |
115 | |
116 void | |
599 | 117 send_http_header(r, ...) |
633 | 118 CODE: |
599 | 119 |
633 | 120 ngx_http_request_t *r; |
121 SV *sv; | |
599 | 122 |
633 | 123 ngx_http_perl_set_request(r); |
599 | 124 |
125 if (r->headers_out.status == 0) { | |
126 r->headers_out.status = NGX_HTTP_OK; | |
127 } | |
128 | |
129 if (items != 1) { | |
130 sv = ST(1); | |
131 | |
132 if (ngx_http_perl_sv2str(aTHX_ r, &r->headers_out.content_type, sv) | |
133 != NGX_OK) | |
134 { | |
633 | 135 XSRETURN_EMPTY; |
599 | 136 } |
137 | |
1444
37938e68910b
allow to append charset to the "Content-Type" header
Igor Sysoev <igor@sysoev.ru>
parents:
1372
diff
changeset
|
138 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
|
139 |
599 | 140 } else { |
673 | 141 if (ngx_http_set_content_type(r) != NGX_OK) { |
142 XSRETURN_EMPTY; | |
599 | 143 } |
144 } | |
145 | |
633 | 146 (void) ngx_http_send_header(r); |
599 | 147 |
148 | |
633 | 149 void |
150 header_only(r) | |
599 | 151 CODE: |
152 | |
633 | 153 dXSTARG; |
154 ngx_http_request_t *r; | |
155 | |
156 ngx_http_perl_set_request(r); | |
599 | 157 |
633 | 158 sv_upgrade(TARG, SVt_IV); |
159 sv_setiv(TARG, r->header_only); | |
599 | 160 |
633 | 161 ST(0) = TARG; |
599 | 162 |
163 | |
633 | 164 void |
165 uri(r) | |
166 CODE: | |
167 | |
168 dXSTARG; | |
169 ngx_http_request_t *r; | |
599 | 170 |
633 | 171 ngx_http_perl_set_request(r); |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
172 ngx_http_perl_set_targ(r->uri.data, r->uri.len); |
633 | 173 |
174 ST(0) = TARG; | |
175 | |
176 | |
177 void | |
178 args(r) | |
599 | 179 CODE: |
180 | |
633 | 181 dXSTARG; |
182 ngx_http_request_t *r; | |
599 | 183 |
633 | 184 ngx_http_perl_set_request(r); |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
185 ngx_http_perl_set_targ(r->args.data, r->args.len); |
599 | 186 |
633 | 187 ST(0) = TARG; |
599 | 188 |
189 | |
633 | 190 void |
629 | 191 request_method(r) |
633 | 192 CODE: |
193 | |
194 dXSTARG; | |
195 ngx_http_request_t *r; | |
629 | 196 |
633 | 197 ngx_http_perl_set_request(r); |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
198 ngx_http_perl_set_targ(r->method_name.data, r->method_name.len); |
633 | 199 |
200 ST(0) = TARG; | |
201 | |
202 | |
203 void | |
204 remote_addr(r) | |
629 | 205 CODE: |
206 | |
633 | 207 dXSTARG; |
208 ngx_http_request_t *r; | |
629 | 209 |
633 | 210 ngx_http_perl_set_request(r); |
211 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
|
212 r->connection->addr_text.len); |
629 | 213 |
633 | 214 ST(0) = TARG; |
629 | 215 |
216 | |
633 | 217 void |
218 header_in(r, key) | |
629 | 219 CODE: |
220 | |
633 | 221 dXSTARG; |
667 | 222 ngx_http_request_t *r; |
223 SV *key; | |
224 u_char *p, *lowcase_key, *cookie; | |
225 STRLEN len; | |
226 ssize_t size; | |
227 ngx_uint_t i, n, hash; | |
228 ngx_list_part_t *part; | |
229 ngx_table_elt_t *h, **ph; | |
230 ngx_http_header_t *hh; | |
231 ngx_http_core_main_conf_t *cmcf; | |
629 | 232 |
633 | 233 ngx_http_perl_set_request(r); |
599 | 234 |
633 | 235 key = ST(1); |
599 | 236 |
237 if (SvROK(key) && SvTYPE(SvRV(key)) == SVt_PV) { | |
238 key = SvRV(key); | |
239 } | |
240 | |
241 p = (u_char *) SvPV(key, len); | |
242 | |
667 | 243 /* look up hashed headers */ |
244 | |
245 lowcase_key = ngx_palloc(r->pool, len); | |
246 if (lowcase_key == NULL) { | |
247 XSRETURN_UNDEF; | |
248 } | |
249 | |
250 hash = 0; | |
251 for (i = 0; i < len; i++) { | |
252 lowcase_key[i] = ngx_tolower(p[i]); | |
253 hash = ngx_hash(hash, lowcase_key[i]); | |
254 } | |
255 | |
256 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
257 | |
258 hh = ngx_hash_find(&cmcf->headers_in_hash, hash, lowcase_key, len); | |
259 | |
260 if (hh) { | |
261 if (hh->offset) { | |
262 | |
263 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + hh->offset); | |
264 | |
265 if (*ph) { | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
266 ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len); |
667 | 267 |
268 goto done; | |
269 } | |
270 | |
271 XSRETURN_UNDEF; | |
272 } | |
273 | |
274 /* Cookie */ | |
275 | |
276 n = r->headers_in.cookies.nelts; | |
277 | |
278 if (n == 0) { | |
279 XSRETURN_UNDEF; | |
280 } | |
281 | |
282 ph = r->headers_in.cookies.elts; | |
283 | |
284 if (n == 1) { | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
285 ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len); |
667 | 286 |
287 goto done; | |
288 } | |
289 | |
290 size = - (ssize_t) (sizeof("; ") - 1); | |
291 | |
292 for (i = 0; i < n; i++) { | |
293 size += ph[i]->value.len + sizeof("; ") - 1; | |
294 } | |
295 | |
296 cookie = ngx_palloc(r->pool, size); | |
297 if (cookie == NULL) { | |
298 XSRETURN_UNDEF; | |
299 } | |
300 | |
301 p = cookie; | |
302 | |
303 for (i = 0; /* void */ ; i++) { | |
304 p = ngx_copy(p, ph[i]->value.data, ph[i]->value.len); | |
305 | |
306 if (i == n - 1) { | |
307 break; | |
308 } | |
309 | |
310 *p++ = ';'; *p++ = ' '; | |
311 } | |
312 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
313 ngx_http_perl_set_targ(cookie, size); |
667 | 314 |
315 goto done; | |
316 } | |
317 | |
318 /* iterate over all headers */ | |
319 | |
599 | 320 part = &r->headers_in.headers.part; |
667 | 321 h = part->elts; |
599 | 322 |
323 for (i = 0; /* void */ ; i++) { | |
324 | |
325 if (i >= part->nelts) { | |
326 if (part->next == NULL) { | |
327 break; | |
328 } | |
329 | |
330 part = part->next; | |
667 | 331 h = part->elts; |
599 | 332 i = 0; |
333 } | |
334 | |
667 | 335 if (len != h[i].key.len |
336 || ngx_strcasecmp(p, h[i].key.data) != 0) | |
599 | 337 { |
338 continue; | |
339 } | |
340 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
341 ngx_http_perl_set_targ(h[i].value.data, h[i].value.len); |
599 | 342 |
343 goto done; | |
344 } | |
345 | |
346 XSRETURN_UNDEF; | |
347 | |
348 done: | |
349 | |
633 | 350 ST(0) = TARG; |
599 | 351 |
352 | |
633 | 353 void |
681 | 354 has_request_body(r, next) |
355 CODE: | |
356 | |
357 dXSTARG; | |
358 ngx_http_request_t *r; | |
359 ngx_http_perl_ctx_t *ctx; | |
360 | |
361 ngx_http_perl_set_request(r); | |
362 | |
363 if (r->headers_in.content_length_n <= 0) { | |
364 XSRETURN_UNDEF; | |
365 } | |
366 | |
367 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
909 | 368 ctx->next = SvRV(ST(1)); |
681 | 369 |
370 r->request_body_in_single_buf = 1; | |
371 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
|
372 r->request_body_in_clean_file = 1; |
681 | 373 |
374 if (r->request_body_in_file_only) { | |
375 r->request_body_file_log_level = 0; | |
376 } | |
377 | |
378 ngx_http_read_client_request_body(r, ngx_http_perl_handle_request); | |
379 | |
380 sv_upgrade(TARG, SVt_IV); | |
381 sv_setiv(TARG, 1); | |
382 | |
383 ST(0) = TARG; | |
384 | |
385 | |
386 void | |
631 | 387 request_body(r) |
388 CODE: | |
389 | |
633 | 390 dXSTARG; |
391 ngx_http_request_t *r; | |
392 size_t len; | |
393 | |
394 ngx_http_perl_set_request(r); | |
631 | 395 |
941 | 396 if (r->request_body == NULL |
397 || r->request_body->temp_file | |
398 || r->request_body->bufs == NULL) | |
399 { | |
633 | 400 XSRETURN_UNDEF; |
401 } | |
631 | 402 |
633 | 403 len = r->request_body->bufs->buf->last - r->request_body->bufs->buf->pos; |
631 | 404 |
405 if (len == 0) { | |
406 XSRETURN_UNDEF; | |
407 } | |
408 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
409 ngx_http_perl_set_targ(r->request_body->bufs->buf->pos, len); |
631 | 410 |
633 | 411 ST(0) = TARG; |
631 | 412 |
413 | |
633 | 414 void |
415 request_body_file(r) | |
416 CODE: | |
417 | |
418 dXSTARG; | |
419 ngx_http_request_t *r; | |
420 | |
421 ngx_http_perl_set_request(r); | |
422 | |
941 | 423 if (r->request_body == NULL || r->request_body->temp_file == NULL) { |
633 | 424 XSRETURN_UNDEF; |
425 } | |
426 | |
427 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
|
428 r->request_body->temp_file->file.name.len); |
599 | 429 |
633 | 430 ST(0) = TARG; |
431 | |
432 | |
433 void | |
1371 | 434 discard_request_body(r) |
435 CODE: | |
436 | |
437 ngx_http_request_t *r; | |
438 | |
439 ngx_http_perl_set_request(r); | |
440 | |
441 ngx_http_discard_request_body(r); | |
442 | |
443 | |
444 void | |
633 | 445 header_out(r, key, value) |
446 CODE: | |
599 | 447 |
633 | 448 ngx_http_request_t *r; |
449 SV *key; | |
450 SV *value; | |
451 ngx_table_elt_t *header; | |
599 | 452 |
633 | 453 ngx_http_perl_set_request(r); |
454 | |
455 key = ST(1); | |
456 value = ST(2); | |
599 | 457 |
458 header = ngx_list_push(&r->headers_out.headers); | |
459 if (header == NULL) { | |
633 | 460 XSRETURN_EMPTY; |
599 | 461 } |
462 | |
463 header->hash = 1; | |
464 | |
465 if (ngx_http_perl_sv2str(aTHX_ r, &header->key, key) != NGX_OK) { | |
633 | 466 XSRETURN_EMPTY; |
599 | 467 } |
468 | |
469 if (ngx_http_perl_sv2str(aTHX_ r, &header->value, value) != NGX_OK) { | |
633 | 470 XSRETURN_EMPTY; |
599 | 471 } |
472 | |
473 if (header->key.len == sizeof("Content-Length") - 1 | |
474 && ngx_strncasecmp(header->key.data, "Content-Length", | |
741
63a08390a8a2
$r->headers_out("Content-Length", "NNN") did not work
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
475 sizeof("Content-Length") - 1) == 0) |
599 | 476 { |
741
63a08390a8a2
$r->headers_out("Content-Length", "NNN") did not work
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
477 r->headers_out.content_length_n = (off_t) SvIV(value); |
599 | 478 r->headers_out.content_length = header; |
479 } | |
480 | |
481 | |
633 | 482 void |
599 | 483 filename(r) |
633 | 484 CODE: |
599 | 485 |
633 | 486 dXSTARG; |
774
589841f06b87
previous commit broke two modules
Igor Sysoev <igor@sysoev.ru>
parents:
741
diff
changeset
|
487 size_t root; |
633 | 488 ngx_http_request_t *r; |
599 | 489 ngx_http_perl_ctx_t *ctx; |
490 | |
633 | 491 ngx_http_perl_set_request(r); |
599 | 492 |
493 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
633 | 494 if (ctx->filename.data) { |
599 | 495 goto done; |
496 } | |
497 | |
774
589841f06b87
previous commit broke two modules
Igor Sysoev <igor@sysoev.ru>
parents:
741
diff
changeset
|
498 if (ngx_http_map_uri_to_path(r, &ctx->filename, &root, 0) == NULL) { |
599 | 499 XSRETURN_UNDEF; |
500 } | |
501 | |
633 | 502 ctx->filename.len--; |
503 sv_setpv(PL_statname, (char *) ctx->filename.data); | |
599 | 504 |
505 done: | |
506 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
507 ngx_http_perl_set_targ(ctx->filename.data, ctx->filename.len); |
599 | 508 |
633 | 509 ST(0) = TARG; |
599 | 510 |
511 | |
633 | 512 void |
599 | 513 print(r, ...) |
514 CODE: | |
515 | |
633 | 516 ngx_http_request_t *r; |
517 SV *sv; | |
518 int i; | |
519 u_char *p; | |
520 size_t size; | |
521 STRLEN len; | |
522 ngx_buf_t *b; | |
523 | |
524 ngx_http_perl_set_request(r); | |
599 | 525 |
526 if (items == 2) { | |
527 | |
528 /* | |
529 * do zero copy for prolate single read-only SV: | |
530 * $r->print("some text\n"); | |
531 */ | |
532 | |
533 sv = ST(1); | |
534 | |
535 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
536 sv = SvRV(sv); | |
537 } | |
538 | |
1703
43747661804d
*) copy regex captures $1, $2, etc.
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
539 if (SvREADONLY(sv) && SvPOK(sv)) { |
599 | 540 |
541 p = (u_char *) SvPV(sv, len); | |
542 | |
543 if (len == 0) { | |
633 | 544 XSRETURN_EMPTY; |
599 | 545 } |
546 | |
547 b = ngx_calloc_buf(r->pool); | |
548 if (b == NULL) { | |
633 | 549 XSRETURN_EMPTY; |
599 | 550 } |
551 | |
552 b->memory = 1; | |
553 b->pos = p; | |
554 b->last = p + len; | |
555 b->start = p; | |
556 b->end = b->last; | |
557 | |
601 | 558 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
559 "$r->print: read-only SV: %z", len); | |
560 | |
599 | 561 goto out; |
562 } | |
563 } | |
564 | |
565 size = 0; | |
566 | |
567 for (i = 1; i < items; i++) { | |
568 | |
569 sv = ST(i); | |
570 | |
571 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
572 sv = SvRV(sv); | |
573 } | |
574 | |
601 | 575 (void) SvPV(sv, len); |
599 | 576 |
601 | 577 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
578 "$r->print: copy SV: %z", len); | |
599 | 579 |
580 size += len; | |
581 } | |
582 | |
583 if (size == 0) { | |
633 | 584 XSRETURN_EMPTY; |
599 | 585 } |
586 | |
587 b = ngx_create_temp_buf(r->pool, size); | |
588 if (b == NULL) { | |
633 | 589 XSRETURN_EMPTY; |
599 | 590 } |
591 | |
592 for (i = 1; i < items; i++) { | |
593 sv = ST(i); | |
594 | |
595 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
596 sv = SvRV(sv); | |
597 } | |
598 | |
599 p = (u_char *) SvPV(sv, len); | |
600 b->last = ngx_cpymem(b->last, p, len); | |
601 } | |
602 | |
603 out: | |
604 | |
633 | 605 (void) ngx_http_perl_output(r, b); |
599 | 606 |
607 | |
633 | 608 void |
613 | 609 sendfile(r, filename, offset = -1, bytes = 0) |
633 | 610 CODE: |
611 | |
1454 | 612 ngx_http_request_t *r; |
613 char *filename; | |
614 int offset; | |
615 size_t bytes; | |
616 ngx_str_t path; | |
617 ngx_buf_t *b; | |
618 ngx_open_file_info_t of; | |
619 ngx_http_core_loc_conf_t *clcf; | |
599 | 620 |
633 | 621 ngx_http_perl_set_request(r); |
622 | |
623 filename = SvPV_nolen(ST(1)); | |
599 | 624 |
625 if (filename == NULL) { | |
626 croak("sendfile(): NULL filename"); | |
627 } | |
628 | |
633 | 629 offset = items < 3 ? -1 : SvIV(ST(2)); |
630 bytes = items < 4 ? 0 : SvIV(ST(3)); | |
631 | |
599 | 632 b = ngx_calloc_buf(r->pool); |
633 if (b == NULL) { | |
633 | 634 XSRETURN_EMPTY; |
599 | 635 } |
636 | |
637 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); | |
638 if (b->file == NULL) { | |
633 | 639 XSRETURN_EMPTY; |
599 | 640 } |
641 | |
1454 | 642 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
1560
25ee6eee7573
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1457
diff
changeset
|
643 |
1454 | 644 of.test_dir = 0; |
1768 | 645 of.valid = clcf->open_file_cache_valid; |
1772 | 646 of.min_uses = clcf->open_file_cache_min_uses; |
1560
25ee6eee7573
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1457
diff
changeset
|
647 of.errors = clcf->open_file_cache_errors; |
25ee6eee7573
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1457
diff
changeset
|
648 of.events = clcf->open_file_cache_events; |
1454 | 649 |
650 path.len = ngx_strlen(filename); | |
651 | |
652 path.data = ngx_pcalloc(r->pool, path.len + 1); | |
653 if (path.data == NULL) { | |
633 | 654 XSRETURN_EMPTY; |
599 | 655 } |
656 | |
1454 | 657 (void) ngx_cpystrn(path.data, filename, path.len + 1); |
1560
25ee6eee7573
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1457
diff
changeset
|
658 |
1799 | 659 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool) |
660 != NGX_OK) | |
661 { | |
1454 | 662 if (of.err == 0) { |
663 XSRETURN_EMPTY; | |
664 } | |
665 | |
599 | 666 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, |
667 ngx_open_file_n " \"%s\" failed", filename); | |
633 | 668 XSRETURN_EMPTY; |
599 | 669 } |
670 | |
613 | 671 if (offset == -1) { |
672 offset = 0; | |
673 } | |
674 | |
675 if (bytes == 0) { | |
1454 | 676 bytes = of.size - offset; |
599 | 677 } |
678 | |
679 b->in_file = 1; | |
613 | 680 |
681 b->file_pos = offset; | |
682 b->file_last = offset + bytes; | |
599 | 683 |
1454 | 684 b->file->fd = of.fd; |
599 | 685 b->file->log = r->connection->log; |
686 | |
633 | 687 (void) ngx_http_perl_output(r, b); |
599 | 688 |
689 | |
633 | 690 void |
1178
a77f6980de50
rename $r->rflush to $r->flush
Igor Sysoev <igor@sysoev.ru>
parents:
1075
diff
changeset
|
691 flush(r) |
633 | 692 CODE: |
599 | 693 |
633 | 694 ngx_http_request_t *r; |
695 ngx_buf_t *b; | |
599 | 696 |
633 | 697 ngx_http_perl_set_request(r); |
599 | 698 |
699 b = ngx_calloc_buf(r->pool); | |
700 if (b == NULL) { | |
633 | 701 XSRETURN_EMPTY; |
599 | 702 } |
703 | |
704 b->flush = 1; | |
705 | |
1178
a77f6980de50
rename $r->rflush to $r->flush
Igor Sysoev <igor@sysoev.ru>
parents:
1075
diff
changeset
|
706 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->flush"); |
601 | 707 |
633 | 708 (void) ngx_http_perl_output(r, b); |
599 | 709 |
633 | 710 XSRETURN_EMPTY; |
599 | 711 |
712 | |
713 void | |
714 internal_redirect(r, uri) | |
633 | 715 CODE: |
599 | 716 |
633 | 717 ngx_http_request_t *r; |
718 SV *uri; | |
599 | 719 ngx_uint_t i; |
720 ngx_http_perl_ctx_t *ctx; | |
721 | |
633 | 722 ngx_http_perl_set_request(r); |
723 | |
724 uri = ST(1); | |
599 | 725 |
726 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
727 | |
728 if (ngx_http_perl_sv2str(aTHX_ r, &ctx->redirect_uri, uri) != NGX_OK) { | |
729 XSRETURN_EMPTY; | |
730 } | |
731 | |
732 for (i = 0; i < ctx->redirect_uri.len; i++) { | |
733 if (ctx->redirect_uri.data[i] == '?') { | |
734 | |
735 ctx->redirect_args.len = ctx->redirect_uri.len - (i + 1); | |
736 ctx->redirect_args.data = &ctx->redirect_uri.data[i + 1]; | |
737 ctx->redirect_uri.len = i; | |
738 | |
739 XSRETURN_EMPTY; | |
740 } | |
741 } | |
601 | 742 |
743 | |
633 | 744 void |
811 | 745 allow_ranges(r) |
746 CODE: | |
747 | |
748 ngx_http_request_t *r; | |
749 | |
750 ngx_http_perl_set_request(r); | |
751 | |
752 r->allow_ranges = 1; | |
753 | |
754 | |
755 void | |
601 | 756 unescape(r, text, type = 0) |
757 CODE: | |
758 | |
633 | 759 dXSTARG; |
760 ngx_http_request_t *r; | |
761 SV *text; | |
762 int type; | |
763 u_char *p, *dst, *src; | |
764 STRLEN len; | |
601 | 765 |
633 | 766 ngx_http_perl_set_request(r); |
767 | |
768 text = ST(1); | |
769 | |
770 src = (u_char *) SvPV(text, len); | |
771 | |
772 p = ngx_palloc(r->pool, len + 1); | |
601 | 773 if (p == NULL) { |
774 XSRETURN_UNDEF; | |
775 } | |
776 | |
777 dst = p; | |
778 | |
633 | 779 type = items < 3 ? 0 : SvIV(ST(2)); |
780 | |
781 ngx_unescape_uri(&dst, &src, len, (ngx_uint_t) type); | |
601 | 782 *dst = '\0'; |
783 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
784 ngx_http_perl_set_targ(p, dst - p); |
601 | 785 |
633 | 786 ST(0) = TARG; |
833 | 787 |
788 | |
789 void | |
790 variable(r, name, value = NULL) | |
791 CODE: | |
792 | |
793 dXSTARG; | |
794 ngx_http_request_t *r; | |
795 SV *name, *value; | |
796 u_char *p, *lowcase; | |
797 STRLEN len; | |
798 ngx_str_t var, val; | |
799 ngx_uint_t i, hash; | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
800 ngx_http_perl_var_t *v; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
801 ngx_http_perl_ctx_t *ctx; |
833 | 802 ngx_http_variable_value_t *vv; |
803 | |
804 ngx_http_perl_set_request(r); | |
805 | |
806 name = ST(1); | |
807 | |
808 if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) { | |
809 name = SvRV(name); | |
810 } | |
811 | |
812 if (items == 2) { | |
813 value = NULL; | |
814 | |
815 } else { | |
816 value = ST(2); | |
817 | |
818 if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) { | |
819 value = SvRV(value); | |
820 } | |
821 | |
822 if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) { | |
823 XSRETURN_UNDEF; | |
824 } | |
825 } | |
826 | |
827 p = (u_char *) SvPV(name, len); | |
828 | |
829 lowcase = ngx_palloc(r->pool, len); | |
830 if (lowcase == NULL) { | |
831 XSRETURN_UNDEF; | |
832 } | |
833 | |
834 hash = 0; | |
835 for (i = 0; i < len; i++) { | |
836 lowcase[i] = ngx_tolower(p[i]); | |
837 hash = ngx_hash(hash, lowcase[i]); | |
838 } | |
839 | |
840 var.len = len; | |
841 var.data = lowcase; | |
842 | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
843 #if (NGX_LOG_DEBUG) |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
844 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
845 if (value) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
846 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
|
847 "perl variable: \"%V\"=\"%V\"", &var, &val); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
848 } else { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
849 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
|
850 "perl variable: \"%V\"", &var); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
851 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
852 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
853 #endif |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
854 |
833 | 855 vv = ngx_http_get_variable(r, &var, hash, 1); |
856 if (vv == NULL) { | |
857 XSRETURN_UNDEF; | |
858 } | |
859 | |
860 if (vv->not_found) { | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
861 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
862 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
863 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
864 if (ctx->variables) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
865 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
866 v = ctx->variables->elts; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
867 for (i = 0; i < ctx->variables->nelts; i++) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
868 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
869 if (hash != v[i].hash |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
870 || len != v[i].name.len |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
871 || 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
|
872 { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
873 continue; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
874 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
875 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
876 if (value) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
877 v[i].value = val; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
878 XSRETURN_UNDEF; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
879 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
880 |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
881 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
|
882 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
883 goto done; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
884 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
885 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
886 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
887 if (value) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
888 if (ctx->variables == NULL) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
889 ctx->variables = ngx_array_create(r->pool, 1, |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
890 sizeof(ngx_http_perl_var_t)); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
891 if (ctx->variables == NULL) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
892 XSRETURN_UNDEF; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
893 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
894 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
895 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
896 v = ngx_array_push(ctx->variables); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
897 if (v == NULL) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
898 XSRETURN_UNDEF; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
899 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
900 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
901 v->hash = hash; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
902 v->name.len = len; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
903 v->name.data = lowcase; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
904 v->value = val; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
905 |
833 | 906 XSRETURN_UNDEF; |
907 } | |
908 | |
909 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
910 "variable \"%V\" not found", &var); | |
911 | |
912 XSRETURN_UNDEF; | |
913 } | |
914 | |
915 if (value) { | |
916 vv->len = val.len; | |
917 vv->valid = 1; | |
1565 | 918 vv->no_cacheable = 0; |
833 | 919 vv->not_found = 0; |
920 vv->data = val.data; | |
921 | |
922 XSRETURN_UNDEF; | |
923 } | |
924 | |
1739
5b7baef2e11e
copy return values to perl's allocated memory
Igor Sysoev <igor@sysoev.ru>
parents:
1703
diff
changeset
|
925 ngx_http_perl_set_targ(vv->data, vv->len); |
833 | 926 |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
927 done: |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
928 |
833 | 929 ST(0) = TARG; |
907 | 930 |
931 | |
932 void | |
911 | 933 sleep(r, sleep, next) |
934 CODE: | |
935 | |
936 ngx_http_request_t *r; | |
1897 | 937 ngx_msec_t sleep; |
911 | 938 ngx_http_perl_ctx_t *ctx; |
939 | |
940 ngx_http_perl_set_request(r); | |
941 | |
1898 | 942 sleep = (ngx_msec_t) SvIV(ST(1)); |
1897 | 943 |
944 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
945 "perl sleep: %M", sleep); | |
946 | |
911 | 947 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); |
948 | |
949 ctx->next = SvRV(ST(2)); | |
950 | |
1897 | 951 ngx_add_timer(r->connection->write, sleep); |
952 | |
953 r->write_event_handler = ngx_http_perl_sleep_handler; | |
911 | 954 |
955 | |
956 void | |
907 | 957 log_error(r, err, msg) |
958 CODE: | |
959 | |
960 ngx_http_request_t *r; | |
961 SV *err, *msg; | |
962 u_char *p; | |
963 STRLEN len; | |
964 ngx_err_t e; | |
965 | |
966 ngx_http_perl_set_request(r); | |
967 | |
968 err = ST(1); | |
969 | |
970 if (SvROK(err) && SvTYPE(SvRV(err)) == SVt_PV) { | |
971 err = SvRV(err); | |
972 } | |
973 | |
974 e = SvIV(err); | |
975 | |
976 msg = ST(2); | |
977 | |
978 if (SvROK(msg) && SvTYPE(SvRV(msg)) == SVt_PV) { | |
979 msg = SvRV(msg); | |
980 } | |
981 | |
982 p = (u_char *) SvPV(msg, len); | |
983 | |
910 | 984 ngx_log_error(NGX_LOG_ERR, r->connection->log, e, "perl: %s", p); |