Mercurial > hg > nginx
annotate src/http/modules/perl/nginx.xs @ 1372:2cc9b6651f75
omit unnecessary XSRETURN_EMPTY
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 06 Aug 2007 15:40:26 +0000 |
parents | 7dbdf47cdcba |
children | 37938e68910b |
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 | |
1371 | 428 discard_request_body(r) |
429 CODE: | |
430 | |
431 ngx_http_request_t *r; | |
432 | |
433 ngx_http_perl_set_request(r); | |
434 | |
435 ngx_http_discard_request_body(r); | |
436 | |
437 | |
438 void | |
633 | 439 header_out(r, key, value) |
440 CODE: | |
599 | 441 |
633 | 442 ngx_http_request_t *r; |
443 SV *key; | |
444 SV *value; | |
445 ngx_table_elt_t *header; | |
599 | 446 |
633 | 447 ngx_http_perl_set_request(r); |
448 | |
449 key = ST(1); | |
450 value = ST(2); | |
599 | 451 |
452 header = ngx_list_push(&r->headers_out.headers); | |
453 if (header == NULL) { | |
633 | 454 XSRETURN_EMPTY; |
599 | 455 } |
456 | |
457 header->hash = 1; | |
458 | |
459 if (ngx_http_perl_sv2str(aTHX_ r, &header->key, key) != NGX_OK) { | |
633 | 460 XSRETURN_EMPTY; |
599 | 461 } |
462 | |
463 if (ngx_http_perl_sv2str(aTHX_ r, &header->value, value) != NGX_OK) { | |
633 | 464 XSRETURN_EMPTY; |
599 | 465 } |
466 | |
467 if (header->key.len == sizeof("Content-Length") - 1 | |
468 && 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
|
469 sizeof("Content-Length") - 1) == 0) |
599 | 470 { |
741
63a08390a8a2
$r->headers_out("Content-Length", "NNN") did not work
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
471 r->headers_out.content_length_n = (off_t) SvIV(value); |
599 | 472 r->headers_out.content_length = header; |
473 } | |
474 | |
475 | |
633 | 476 void |
599 | 477 filename(r) |
633 | 478 CODE: |
599 | 479 |
633 | 480 dXSTARG; |
774
589841f06b87
previous commit broke two modules
Igor Sysoev <igor@sysoev.ru>
parents:
741
diff
changeset
|
481 size_t root; |
633 | 482 ngx_http_request_t *r; |
599 | 483 ngx_http_perl_ctx_t *ctx; |
484 | |
633 | 485 ngx_http_perl_set_request(r); |
599 | 486 |
487 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
633 | 488 if (ctx->filename.data) { |
599 | 489 goto done; |
490 } | |
491 | |
774
589841f06b87
previous commit broke two modules
Igor Sysoev <igor@sysoev.ru>
parents:
741
diff
changeset
|
492 if (ngx_http_map_uri_to_path(r, &ctx->filename, &root, 0) == NULL) { |
599 | 493 XSRETURN_UNDEF; |
494 } | |
495 | |
633 | 496 ctx->filename.len--; |
497 sv_setpv(PL_statname, (char *) ctx->filename.data); | |
599 | 498 |
499 done: | |
500 | |
633 | 501 ngx_http_perl_set_targ(ctx->filename.data, ctx->filename.len, 1); |
599 | 502 |
633 | 503 ST(0) = TARG; |
599 | 504 |
505 | |
633 | 506 void |
599 | 507 print(r, ...) |
508 CODE: | |
509 | |
633 | 510 ngx_http_request_t *r; |
511 SV *sv; | |
512 int i; | |
513 u_char *p; | |
514 size_t size; | |
515 STRLEN len; | |
516 ngx_buf_t *b; | |
517 | |
518 ngx_http_perl_set_request(r); | |
599 | 519 |
520 if (items == 2) { | |
521 | |
522 /* | |
523 * do zero copy for prolate single read-only SV: | |
524 * $r->print("some text\n"); | |
525 */ | |
526 | |
527 sv = ST(1); | |
528 | |
529 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
530 sv = SvRV(sv); | |
531 } | |
532 | |
533 if (SvREADONLY(sv)) { | |
534 | |
535 p = (u_char *) SvPV(sv, len); | |
536 | |
537 if (len == 0) { | |
633 | 538 XSRETURN_EMPTY; |
599 | 539 } |
540 | |
541 b = ngx_calloc_buf(r->pool); | |
542 if (b == NULL) { | |
633 | 543 XSRETURN_EMPTY; |
599 | 544 } |
545 | |
546 b->memory = 1; | |
547 b->pos = p; | |
548 b->last = p + len; | |
549 b->start = p; | |
550 b->end = b->last; | |
551 | |
601 | 552 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
553 "$r->print: read-only SV: %z", len); | |
554 | |
599 | 555 goto out; |
556 } | |
557 } | |
558 | |
559 size = 0; | |
560 | |
561 for (i = 1; i < items; i++) { | |
562 | |
563 sv = ST(i); | |
564 | |
565 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
566 sv = SvRV(sv); | |
567 } | |
568 | |
601 | 569 (void) SvPV(sv, len); |
599 | 570 |
601 | 571 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
572 "$r->print: copy SV: %z", len); | |
599 | 573 |
574 size += len; | |
575 } | |
576 | |
577 if (size == 0) { | |
633 | 578 XSRETURN_EMPTY; |
599 | 579 } |
580 | |
581 b = ngx_create_temp_buf(r->pool, size); | |
582 if (b == NULL) { | |
633 | 583 XSRETURN_EMPTY; |
599 | 584 } |
585 | |
586 for (i = 1; i < items; i++) { | |
587 sv = ST(i); | |
588 | |
589 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
590 sv = SvRV(sv); | |
591 } | |
592 | |
593 p = (u_char *) SvPV(sv, len); | |
594 b->last = ngx_cpymem(b->last, p, len); | |
595 } | |
596 | |
597 out: | |
598 | |
633 | 599 (void) ngx_http_perl_output(r, b); |
599 | 600 |
601 | |
633 | 602 void |
613 | 603 sendfile(r, filename, offset = -1, bytes = 0) |
633 | 604 CODE: |
605 | |
606 ngx_http_request_t *r; | |
607 char *filename; | |
613 | 608 int offset; |
609 size_t bytes; | |
599 | 610 ngx_fd_t fd; |
611 ngx_buf_t *b; | |
612 ngx_file_info_t fi; | |
613 ngx_pool_cleanup_t *cln; | |
614 ngx_pool_cleanup_file_t *clnf; | |
615 | |
633 | 616 ngx_http_perl_set_request(r); |
617 | |
618 filename = SvPV_nolen(ST(1)); | |
599 | 619 |
620 if (filename == NULL) { | |
621 croak("sendfile(): NULL filename"); | |
622 } | |
623 | |
633 | 624 offset = items < 3 ? -1 : SvIV(ST(2)); |
625 bytes = items < 4 ? 0 : SvIV(ST(3)); | |
626 | |
599 | 627 b = ngx_calloc_buf(r->pool); |
628 if (b == NULL) { | |
633 | 629 XSRETURN_EMPTY; |
599 | 630 } |
631 | |
632 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); | |
633 if (b->file == NULL) { | |
633 | 634 XSRETURN_EMPTY; |
599 | 635 } |
636 | |
637 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t)); | |
638 if (cln == NULL) { | |
633 | 639 XSRETURN_EMPTY; |
599 | 640 } |
641 | |
1056
82b9e57993ec
update nginx.xs to ngx_open_file(name, mode, create, access)
Igor Sysoev <igor@sysoev.ru>
parents:
941
diff
changeset
|
642 fd = ngx_open_file((u_char *) filename, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); |
599 | 643 |
644 if (fd == NGX_INVALID_FILE) { | |
645 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | |
646 ngx_open_file_n " \"%s\" failed", filename); | |
633 | 647 XSRETURN_EMPTY; |
599 | 648 } |
649 | |
613 | 650 if (offset == -1) { |
651 offset = 0; | |
652 } | |
653 | |
654 if (bytes == 0) { | |
655 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { | |
656 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | |
657 ngx_fd_info_n " \"%s\" failed", filename); | |
599 | 658 |
613 | 659 if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
660 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
661 ngx_close_file_n " \"%s\" failed", filename); | |
662 } | |
663 | |
633 | 664 XSRETURN_EMPTY; |
599 | 665 } |
666 | |
613 | 667 bytes = ngx_file_size(&fi) - offset; |
599 | 668 } |
669 | |
670 cln->handler = ngx_pool_cleanup_file; | |
671 clnf = cln->data; | |
672 | |
673 clnf->fd = fd; | |
674 clnf->name = (u_char *) ""; | |
675 clnf->log = r->pool->log; | |
676 | |
677 b->in_file = 1; | |
613 | 678 |
679 b->file_pos = offset; | |
680 b->file_last = offset + bytes; | |
599 | 681 |
682 b->file->fd = fd; | |
683 b->file->log = r->connection->log; | |
684 | |
633 | 685 (void) ngx_http_perl_output(r, b); |
599 | 686 |
687 | |
633 | 688 void |
1178
a77f6980de50
rename $r->rflush to $r->flush
Igor Sysoev <igor@sysoev.ru>
parents:
1075
diff
changeset
|
689 flush(r) |
633 | 690 CODE: |
599 | 691 |
633 | 692 ngx_http_request_t *r; |
693 ngx_buf_t *b; | |
599 | 694 |
633 | 695 ngx_http_perl_set_request(r); |
599 | 696 |
697 b = ngx_calloc_buf(r->pool); | |
698 if (b == NULL) { | |
633 | 699 XSRETURN_EMPTY; |
599 | 700 } |
701 | |
702 b->flush = 1; | |
703 | |
1178
a77f6980de50
rename $r->rflush to $r->flush
Igor Sysoev <igor@sysoev.ru>
parents:
1075
diff
changeset
|
704 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->flush"); |
601 | 705 |
633 | 706 (void) ngx_http_perl_output(r, b); |
599 | 707 |
633 | 708 XSRETURN_EMPTY; |
599 | 709 |
710 | |
711 void | |
712 internal_redirect(r, uri) | |
633 | 713 CODE: |
599 | 714 |
633 | 715 ngx_http_request_t *r; |
716 SV *uri; | |
599 | 717 ngx_uint_t i; |
718 ngx_http_perl_ctx_t *ctx; | |
719 | |
633 | 720 ngx_http_perl_set_request(r); |
721 | |
722 uri = ST(1); | |
599 | 723 |
724 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
725 | |
726 if (ngx_http_perl_sv2str(aTHX_ r, &ctx->redirect_uri, uri) != NGX_OK) { | |
727 XSRETURN_EMPTY; | |
728 } | |
729 | |
730 for (i = 0; i < ctx->redirect_uri.len; i++) { | |
731 if (ctx->redirect_uri.data[i] == '?') { | |
732 | |
733 ctx->redirect_args.len = ctx->redirect_uri.len - (i + 1); | |
734 ctx->redirect_args.data = &ctx->redirect_uri.data[i + 1]; | |
735 ctx->redirect_uri.len = i; | |
736 | |
737 XSRETURN_EMPTY; | |
738 } | |
739 } | |
601 | 740 |
741 | |
633 | 742 void |
811 | 743 allow_ranges(r) |
744 CODE: | |
745 | |
746 ngx_http_request_t *r; | |
747 | |
748 ngx_http_perl_set_request(r); | |
749 | |
750 r->allow_ranges = 1; | |
751 | |
752 | |
753 void | |
601 | 754 unescape(r, text, type = 0) |
755 CODE: | |
756 | |
633 | 757 dXSTARG; |
758 ngx_http_request_t *r; | |
759 SV *text; | |
760 int type; | |
761 u_char *p, *dst, *src; | |
762 STRLEN len; | |
601 | 763 |
633 | 764 ngx_http_perl_set_request(r); |
765 | |
766 text = ST(1); | |
767 | |
768 src = (u_char *) SvPV(text, len); | |
769 | |
770 p = ngx_palloc(r->pool, len + 1); | |
601 | 771 if (p == NULL) { |
772 XSRETURN_UNDEF; | |
773 } | |
774 | |
775 dst = p; | |
776 | |
633 | 777 type = items < 3 ? 0 : SvIV(ST(2)); |
778 | |
779 ngx_unescape_uri(&dst, &src, len, (ngx_uint_t) type); | |
601 | 780 *dst = '\0'; |
781 | |
633 | 782 ngx_http_perl_set_targ(p, dst - p, 1); |
601 | 783 |
633 | 784 ST(0) = TARG; |
833 | 785 |
786 | |
787 void | |
788 variable(r, name, value = NULL) | |
789 CODE: | |
790 | |
791 dXSTARG; | |
792 ngx_http_request_t *r; | |
793 SV *name, *value; | |
794 u_char *p, *lowcase; | |
795 STRLEN len; | |
796 ngx_str_t var, val; | |
797 ngx_uint_t i, hash; | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
798 ngx_http_perl_var_t *v; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
799 ngx_http_perl_ctx_t *ctx; |
833 | 800 ngx_http_variable_value_t *vv; |
801 | |
802 ngx_http_perl_set_request(r); | |
803 | |
804 name = ST(1); | |
805 | |
806 if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) { | |
807 name = SvRV(name); | |
808 } | |
809 | |
810 if (items == 2) { | |
811 value = NULL; | |
812 | |
813 } else { | |
814 value = ST(2); | |
815 | |
816 if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) { | |
817 value = SvRV(value); | |
818 } | |
819 | |
820 if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) { | |
821 XSRETURN_UNDEF; | |
822 } | |
823 } | |
824 | |
825 p = (u_char *) SvPV(name, len); | |
826 | |
827 lowcase = ngx_palloc(r->pool, len); | |
828 if (lowcase == NULL) { | |
829 XSRETURN_UNDEF; | |
830 } | |
831 | |
832 hash = 0; | |
833 for (i = 0; i < len; i++) { | |
834 lowcase[i] = ngx_tolower(p[i]); | |
835 hash = ngx_hash(hash, lowcase[i]); | |
836 } | |
837 | |
838 var.len = len; | |
839 var.data = lowcase; | |
840 | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
841 #if (NGX_LOG_DEBUG) |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
842 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
843 if (value) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
844 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
|
845 "perl variable: \"%V\"=\"%V\"", &var, &val); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
846 } else { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
847 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
|
848 "perl variable: \"%V\"", &var); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
849 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
850 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
851 #endif |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
852 |
833 | 853 vv = ngx_http_get_variable(r, &var, hash, 1); |
854 if (vv == NULL) { | |
855 XSRETURN_UNDEF; | |
856 } | |
857 | |
858 if (vv->not_found) { | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
859 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
860 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
|
861 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
862 if (ctx->variables) { |
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 v = ctx->variables->elts; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
865 for (i = 0; i < ctx->variables->nelts; i++) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
866 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
867 if (hash != v[i].hash |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
868 || len != v[i].name.len |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
869 || 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
|
870 { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
871 continue; |
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 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
874 if (value) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
875 v[i].value = val; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
876 XSRETURN_UNDEF; |
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 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
879 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
|
880 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
881 goto done; |
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 } |
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 if (value) { |
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 ctx->variables = ngx_array_create(r->pool, 1, |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
888 sizeof(ngx_http_perl_var_t)); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
889 if (ctx->variables == NULL) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
890 XSRETURN_UNDEF; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
891 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
892 } |
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 v = ngx_array_push(ctx->variables); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
895 if (v == NULL) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
896 XSRETURN_UNDEF; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
897 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
898 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
899 v->hash = hash; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
900 v->name.len = len; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
901 v->name.data = lowcase; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
902 v->value = val; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
903 |
833 | 904 XSRETURN_UNDEF; |
905 } | |
906 | |
907 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
908 "variable \"%V\" not found", &var); | |
909 | |
910 XSRETURN_UNDEF; | |
911 } | |
912 | |
913 if (value) { | |
914 vv->len = val.len; | |
915 vv->valid = 1; | |
916 vv->no_cachable = 0; | |
917 vv->not_found = 0; | |
918 vv->data = val.data; | |
919 | |
920 XSRETURN_UNDEF; | |
921 } | |
922 | |
923 ngx_http_perl_set_targ(vv->data, vv->len, 0); | |
924 | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
925 done: |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
926 |
833 | 927 ST(0) = TARG; |
907 | 928 |
929 | |
930 void | |
911 | 931 sleep(r, sleep, next) |
932 CODE: | |
933 | |
934 dXSTARG; | |
935 ngx_http_request_t *r; | |
936 ngx_http_perl_ctx_t *ctx; | |
937 | |
938 ngx_http_perl_set_request(r); | |
939 | |
940 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
941 | |
942 ctx->sleep = SvIV(ST(1)); | |
943 ctx->next = SvRV(ST(2)); | |
944 | |
945 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
946 "perl sleep: %d", ctx->sleep); | |
947 | |
948 | |
949 void | |
907 | 950 log_error(r, err, msg) |
951 CODE: | |
952 | |
953 ngx_http_request_t *r; | |
954 SV *err, *msg; | |
955 u_char *p; | |
956 STRLEN len; | |
957 ngx_err_t e; | |
958 | |
959 ngx_http_perl_set_request(r); | |
960 | |
961 err = ST(1); | |
962 | |
963 if (SvROK(err) && SvTYPE(SvRV(err)) == SVt_PV) { | |
964 err = SvRV(err); | |
965 } | |
966 | |
967 e = SvIV(err); | |
968 | |
969 msg = ST(2); | |
970 | |
971 if (SvROK(msg) && SvTYPE(SvRV(msg)) == SVt_PV) { | |
972 msg = SvRV(msg); | |
973 } | |
974 | |
975 p = (u_char *) SvPV(msg, len); | |
976 | |
910 | 977 ngx_log_error(NGX_LOG_ERR, r->connection->log, e, "perl: %s", p); |