148
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
644
|
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
|
638
|
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;
|
|
665
|
362
|
666 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
|
|
667 != NGX_OK)
|
|
668 {
|
332
|
669 if (of.err == 0) {
|
|
670 XSRETURN_EMPTY;
|
|
671 }
|
|
672
|
148
|
673 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
482
|
674 "%s \"%s\" failed", of.failed, filename);
|
182
|
675 XSRETURN_EMPTY;
|
148
|
676 }
|
|
677
|
162
|
678 if (offset == -1) {
|
|
679 offset = 0;
|
|
680 }
|
|
681
|
|
682 if (bytes == 0) {
|
332
|
683 bytes = of.size - offset;
|
148
|
684 }
|
|
685
|
|
686 b->in_file = 1;
|
162
|
687
|
|
688 b->file_pos = offset;
|
|
689 b->file_last = offset + bytes;
|
148
|
690
|
332
|
691 b->file->fd = of.fd;
|
148
|
692 b->file->log = r->connection->log;
|
406
|
693 b->file->directio = of.is_directio;
|
148
|
694
|
182
|
695 (void) ngx_http_perl_output(r, b);
|
148
|
696
|
|
697
|
182
|
698 void
|
298
|
699 flush(r)
|
182
|
700 CODE:
|
148
|
701
|
182
|
702 ngx_http_request_t *r;
|
|
703 ngx_buf_t *b;
|
148
|
704
|
182
|
705 ngx_http_perl_set_request(r);
|
148
|
706
|
|
707 b = ngx_calloc_buf(r->pool);
|
|
708 if (b == NULL) {
|
182
|
709 XSRETURN_EMPTY;
|
148
|
710 }
|
|
711
|
|
712 b->flush = 1;
|
|
713
|
298
|
714 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->flush");
|
150
|
715
|
182
|
716 (void) ngx_http_perl_output(r, b);
|
148
|
717
|
182
|
718 XSRETURN_EMPTY;
|
148
|
719
|
|
720
|
|
721 void
|
|
722 internal_redirect(r, uri)
|
182
|
723 CODE:
|
148
|
724
|
182
|
725 ngx_http_request_t *r;
|
|
726 SV *uri;
|
148
|
727 ngx_uint_t i;
|
|
728 ngx_http_perl_ctx_t *ctx;
|
|
729
|
182
|
730 ngx_http_perl_set_request(r);
|
|
731
|
|
732 uri = ST(1);
|
148
|
733
|
|
734 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
|
|
735
|
|
736 if (ngx_http_perl_sv2str(aTHX_ r, &ctx->redirect_uri, uri) != NGX_OK) {
|
|
737 XSRETURN_EMPTY;
|
|
738 }
|
|
739
|
|
740 for (i = 0; i < ctx->redirect_uri.len; i++) {
|
|
741 if (ctx->redirect_uri.data[i] == '?') {
|
|
742
|
|
743 ctx->redirect_args.len = ctx->redirect_uri.len - (i + 1);
|
|
744 ctx->redirect_args.data = &ctx->redirect_uri.data[i + 1];
|
|
745 ctx->redirect_uri.len = i;
|
|
746
|
|
747 XSRETURN_EMPTY;
|
|
748 }
|
|
749 }
|
150
|
750
|
|
751
|
182
|
752 void
|
252
|
753 allow_ranges(r)
|
|
754 CODE:
|
|
755
|
|
756 ngx_http_request_t *r;
|
|
757
|
|
758 ngx_http_perl_set_request(r);
|
|
759
|
|
760 r->allow_ranges = 1;
|
|
761
|
|
762
|
|
763 void
|
150
|
764 unescape(r, text, type = 0)
|
|
765 CODE:
|
|
766
|
182
|
767 dXSTARG;
|
|
768 ngx_http_request_t *r;
|
|
769 SV *text;
|
|
770 int type;
|
|
771 u_char *p, *dst, *src;
|
|
772 STRLEN len;
|
150
|
773
|
182
|
774 ngx_http_perl_set_request(r);
|
|
775
|
|
776 text = ST(1);
|
|
777
|
|
778 src = (u_char *) SvPV(text, len);
|
|
779
|
382
|
780 p = ngx_pnalloc(r->pool, len + 1);
|
150
|
781 if (p == NULL) {
|
|
782 XSRETURN_UNDEF;
|
|
783 }
|
|
784
|
|
785 dst = p;
|
|
786
|
182
|
787 type = items < 3 ? 0 : SvIV(ST(2));
|
|
788
|
|
789 ngx_unescape_uri(&dst, &src, len, (ngx_uint_t) type);
|
150
|
790 *dst = '\0';
|
|
791
|
356
|
792 ngx_http_perl_set_targ(p, dst - p);
|
150
|
793
|
182
|
794 ST(0) = TARG;
|
254
|
795
|
|
796
|
|
797 void
|
|
798 variable(r, name, value = NULL)
|
|
799 CODE:
|
|
800
|
|
801 dXSTARG;
|
|
802 ngx_http_request_t *r;
|
|
803 SV *name, *value;
|
|
804 u_char *p, *lowcase;
|
|
805 STRLEN len;
|
|
806 ngx_str_t var, val;
|
|
807 ngx_uint_t i, hash;
|
266
|
808 ngx_http_perl_var_t *v;
|
|
809 ngx_http_perl_ctx_t *ctx;
|
254
|
810 ngx_http_variable_value_t *vv;
|
|
811
|
|
812 ngx_http_perl_set_request(r);
|
|
813
|
|
814 name = ST(1);
|
|
815
|
|
816 if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) {
|
|
817 name = SvRV(name);
|
|
818 }
|
|
819
|
|
820 if (items == 2) {
|
|
821 value = NULL;
|
|
822
|
|
823 } else {
|
|
824 value = ST(2);
|
|
825
|
|
826 if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) {
|
|
827 value = SvRV(value);
|
|
828 }
|
|
829
|
|
830 if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) {
|
|
831 XSRETURN_UNDEF;
|
|
832 }
|
|
833 }
|
|
834
|
|
835 p = (u_char *) SvPV(name, len);
|
|
836
|
382
|
837 lowcase = ngx_pnalloc(r->pool, len);
|
254
|
838 if (lowcase == NULL) {
|
|
839 XSRETURN_UNDEF;
|
|
840 }
|
|
841
|
392
|
842 hash = ngx_hash_strlow(lowcase, p, len);
|
254
|
843
|
|
844 var.len = len;
|
|
845 var.data = lowcase;
|
|
846
|
638
|
847 #if (NGX_DEBUG)
|
266
|
848
|
|
849 if (value) {
|
|
850 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
851 "perl variable: \"%V\"=\"%V\"", &var, &val);
|
|
852 } else {
|
|
853 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
854 "perl variable: \"%V\"", &var);
|
|
855 }
|
|
856
|
|
857 #endif
|
|
858
|
568
|
859 vv = ngx_http_get_variable(r, &var, hash);
|
254
|
860 if (vv == NULL) {
|
|
861 XSRETURN_UNDEF;
|
|
862 }
|
|
863
|
|
864 if (vv->not_found) {
|
266
|
865
|
|
866 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
|
|
867
|
|
868 if (ctx->variables) {
|
|
869
|
|
870 v = ctx->variables->elts;
|
|
871 for (i = 0; i < ctx->variables->nelts; i++) {
|
|
872
|
|
873 if (hash != v[i].hash
|
|
874 || len != v[i].name.len
|
|
875 || ngx_strncmp(lowcase, v[i].name.data, len) != 0)
|
|
876 {
|
|
877 continue;
|
|
878 }
|
|
879
|
|
880 if (value) {
|
|
881 v[i].value = val;
|
|
882 XSRETURN_UNDEF;
|
|
883 }
|
|
884
|
356
|
885 ngx_http_perl_set_targ(v[i].value.data, v[i].value.len);
|
266
|
886
|
|
887 goto done;
|
|
888 }
|
|
889 }
|
|
890
|
|
891 if (value) {
|
|
892 if (ctx->variables == NULL) {
|
|
893 ctx->variables = ngx_array_create(r->pool, 1,
|
|
894 sizeof(ngx_http_perl_var_t));
|
|
895 if (ctx->variables == NULL) {
|
|
896 XSRETURN_UNDEF;
|
|
897 }
|
|
898 }
|
|
899
|
|
900 v = ngx_array_push(ctx->variables);
|
|
901 if (v == NULL) {
|
|
902 XSRETURN_UNDEF;
|
|
903 }
|
|
904
|
|
905 v->hash = hash;
|
|
906 v->name.len = len;
|
|
907 v->name.data = lowcase;
|
|
908 v->value = val;
|
|
909
|
254
|
910 XSRETURN_UNDEF;
|
|
911 }
|
|
912
|
|
913 XSRETURN_UNDEF;
|
|
914 }
|
|
915
|
|
916 if (value) {
|
|
917 vv->len = val.len;
|
|
918 vv->valid = 1;
|
340
|
919 vv->no_cacheable = 0;
|
254
|
920 vv->not_found = 0;
|
|
921 vv->data = val.data;
|
|
922
|
|
923 XSRETURN_UNDEF;
|
|
924 }
|
|
925
|
356
|
926 ngx_http_perl_set_targ(vv->data, vv->len);
|
254
|
927
|
266
|
928 done:
|
|
929
|
254
|
930 ST(0) = TARG;
|
266
|
931
|
|
932
|
|
933 void
|
|
934 sleep(r, sleep, next)
|
|
935 CODE:
|
|
936
|
|
937 ngx_http_request_t *r;
|
366
|
938 ngx_msec_t sleep;
|
266
|
939 ngx_http_perl_ctx_t *ctx;
|
|
940
|
|
941 ngx_http_perl_set_request(r);
|
|
942
|
366
|
943 sleep = (ngx_msec_t) SvIV(ST(1));
|
|
944
|
|
945 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
946 "perl sleep: %M", sleep);
|
|
947
|
266
|
948 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
|
|
949
|
|
950 ctx->next = SvRV(ST(2));
|
|
951
|
366
|
952 ngx_add_timer(r->connection->write, sleep);
|
|
953
|
|
954 r->write_event_handler = ngx_http_perl_sleep_handler;
|
564
|
955 r->main->count++;
|
266
|
956
|
|
957
|
|
958 void
|
|
959 log_error(r, err, msg)
|
|
960 CODE:
|
|
961
|
|
962 ngx_http_request_t *r;
|
|
963 SV *err, *msg;
|
|
964 u_char *p;
|
|
965 STRLEN len;
|
|
966 ngx_err_t e;
|
|
967
|
|
968 ngx_http_perl_set_request(r);
|
|
969
|
|
970 err = ST(1);
|
|
971
|
|
972 if (SvROK(err) && SvTYPE(SvRV(err)) == SVt_PV) {
|
|
973 err = SvRV(err);
|
|
974 }
|
|
975
|
|
976 e = SvIV(err);
|
|
977
|
|
978 msg = ST(2);
|
|
979
|
|
980 if (SvROK(msg) && SvTYPE(SvRV(msg)) == SVt_PV) {
|
|
981 msg = SvRV(msg);
|
|
982 }
|
|
983
|
|
984 p = (u_char *) SvPV(msg, len);
|
|
985
|
|
986 ngx_log_error(NGX_LOG_ERR, r->connection->log, e, "perl: %s", p);
|