Mercurial > hg > nginx-vendor-0-8
comparison src/http/modules/perl/nginx.xs @ 266:251bcd11a5b8 NGINX_0_5_3
nginx 0.5.3
*) Feature: the ngx_http_perl_module supports the $r->status,
$r->log_error, and $r->sleep methods.
*) Feature: the $r->variable method supports variables that do not
exist in nginx configuration.
*) Bugfix: the $r->has_request_body method did not work.
author | Igor Sysoev <http://sysoev.ru> |
---|---|
date | Wed, 13 Dec 2006 00:00:00 +0300 |
parents | 0effe91f6083 |
children | 6eb1e38f0f1f |
comparison
equal
deleted
inserted
replaced
265:3d4634b3b321 | 266:251bcd11a5b8 |
---|---|
89 return ngx_http_output_filter(r, &out); | 89 return ngx_http_output_filter(r, &out); |
90 } | 90 } |
91 | 91 |
92 | 92 |
93 MODULE = nginx PACKAGE = nginx | 93 MODULE = nginx PACKAGE = nginx |
94 | |
95 | |
96 void | |
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; | |
94 | 110 |
95 | 111 |
96 void | 112 void |
97 send_http_header(r, ...) | 113 send_http_header(r, ...) |
98 CODE: | 114 CODE: |
332 has_request_body(r, next) | 348 has_request_body(r, next) |
333 CODE: | 349 CODE: |
334 | 350 |
335 dXSTARG; | 351 dXSTARG; |
336 ngx_http_request_t *r; | 352 ngx_http_request_t *r; |
337 SV *next; | |
338 ngx_http_perl_ctx_t *ctx; | 353 ngx_http_perl_ctx_t *ctx; |
339 | 354 |
340 ngx_http_perl_set_request(r); | 355 ngx_http_perl_set_request(r); |
341 | 356 |
342 if (r->headers_in.content_length_n <= 0) { | 357 if (r->headers_in.content_length_n <= 0) { |
343 XSRETURN_UNDEF; | 358 XSRETURN_UNDEF; |
344 } | 359 } |
345 | 360 |
346 next = ST(1); | |
347 | |
348 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | 361 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); |
349 ctx->next = next; | 362 ctx->next = SvRV(ST(1)); |
350 | 363 |
351 r->request_body_in_single_buf = 1; | 364 r->request_body_in_single_buf = 1; |
352 r->request_body_in_persistent_file = 1; | 365 r->request_body_in_persistent_file = 1; |
353 r->request_body_delete_incomplete_file = 1; | 366 r->request_body_delete_incomplete_file = 1; |
354 | 367 |
774 SV *name, *value; | 787 SV *name, *value; |
775 u_char *p, *lowcase; | 788 u_char *p, *lowcase; |
776 STRLEN len; | 789 STRLEN len; |
777 ngx_str_t var, val; | 790 ngx_str_t var, val; |
778 ngx_uint_t i, hash; | 791 ngx_uint_t i, hash; |
792 ngx_http_perl_var_t *v; | |
793 ngx_http_perl_ctx_t *ctx; | |
779 ngx_http_variable_value_t *vv; | 794 ngx_http_variable_value_t *vv; |
780 | 795 |
781 ngx_http_perl_set_request(r); | 796 ngx_http_perl_set_request(r); |
782 | 797 |
783 name = ST(1); | 798 name = ST(1); |
815 } | 830 } |
816 | 831 |
817 var.len = len; | 832 var.len = len; |
818 var.data = lowcase; | 833 var.data = lowcase; |
819 | 834 |
835 #if (NGX_LOG_DEBUG) | |
836 | |
837 if (value) { | |
838 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
839 "perl variable: \"%V\"=\"%V\"", &var, &val); | |
840 } else { | |
841 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
842 "perl variable: \"%V\"", &var); | |
843 } | |
844 | |
845 #endif | |
846 | |
820 vv = ngx_http_get_variable(r, &var, hash, 1); | 847 vv = ngx_http_get_variable(r, &var, hash, 1); |
821 if (vv == NULL) { | 848 if (vv == NULL) { |
822 XSRETURN_UNDEF; | 849 XSRETURN_UNDEF; |
823 } | 850 } |
824 | 851 |
825 if (vv->not_found) { | 852 if (vv->not_found) { |
826 if (value == NULL) { | 853 |
854 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
855 | |
856 if (ctx->variables) { | |
857 | |
858 v = ctx->variables->elts; | |
859 for (i = 0; i < ctx->variables->nelts; i++) { | |
860 | |
861 if (hash != v[i].hash | |
862 || len != v[i].name.len | |
863 || ngx_strncmp(lowcase, v[i].name.data, len) != 0) | |
864 { | |
865 continue; | |
866 } | |
867 | |
868 if (value) { | |
869 v[i].value = val; | |
870 XSRETURN_UNDEF; | |
871 } | |
872 | |
873 ngx_http_perl_set_targ(v[i].value.data, v[i].value.len, 0); | |
874 | |
875 goto done; | |
876 } | |
877 } | |
878 | |
879 if (value) { | |
880 if (ctx->variables == NULL) { | |
881 ctx->variables = ngx_array_create(r->pool, 1, | |
882 sizeof(ngx_http_perl_var_t)); | |
883 if (ctx->variables == NULL) { | |
884 XSRETURN_UNDEF; | |
885 } | |
886 } | |
887 | |
888 v = ngx_array_push(ctx->variables); | |
889 if (v == NULL) { | |
890 XSRETURN_UNDEF; | |
891 } | |
892 | |
893 v->hash = hash; | |
894 v->name.len = len; | |
895 v->name.data = lowcase; | |
896 v->value = val; | |
897 | |
827 XSRETURN_UNDEF; | 898 XSRETURN_UNDEF; |
828 } | 899 } |
829 | 900 |
830 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | 901 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
831 "variable \"%V\" not found", &var); | 902 "variable \"%V\" not found", &var); |
843 XSRETURN_UNDEF; | 914 XSRETURN_UNDEF; |
844 } | 915 } |
845 | 916 |
846 ngx_http_perl_set_targ(vv->data, vv->len, 0); | 917 ngx_http_perl_set_targ(vv->data, vv->len, 0); |
847 | 918 |
848 ST(0) = TARG; | 919 done: |
920 | |
921 ST(0) = TARG; | |
922 | |
923 | |
924 void | |
925 sleep(r, sleep, next) | |
926 CODE: | |
927 | |
928 dXSTARG; | |
929 ngx_http_request_t *r; | |
930 ngx_http_perl_ctx_t *ctx; | |
931 | |
932 ngx_http_perl_set_request(r); | |
933 | |
934 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
935 | |
936 ctx->sleep = SvIV(ST(1)); | |
937 ctx->next = SvRV(ST(2)); | |
938 | |
939 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
940 "perl sleep: %d", ctx->sleep); | |
941 | |
942 XSRETURN_EMPTY; | |
943 | |
944 | |
945 void | |
946 log_error(r, err, msg) | |
947 CODE: | |
948 | |
949 ngx_http_request_t *r; | |
950 SV *err, *msg; | |
951 u_char *p; | |
952 STRLEN len; | |
953 ngx_err_t e; | |
954 | |
955 ngx_http_perl_set_request(r); | |
956 | |
957 err = ST(1); | |
958 | |
959 if (SvROK(err) && SvTYPE(SvRV(err)) == SVt_PV) { | |
960 err = SvRV(err); | |
961 } | |
962 | |
963 e = SvIV(err); | |
964 | |
965 msg = ST(2); | |
966 | |
967 if (SvROK(msg) && SvTYPE(SvRV(msg)) == SVt_PV) { | |
968 msg = SvRV(msg); | |
969 } | |
970 | |
971 p = (u_char *) SvPV(msg, len); | |
972 | |
973 ngx_log_error(NGX_LOG_ERR, r->connection->log, e, "perl: %s", p); | |
974 | |
975 XSRETURN_EMPTY; |