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