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