Klaus Demo nginx / eccb108
$r->variable Igor Sysoev 15 years ago
1 changed file(s) with 83 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
762762 ngx_http_perl_set_targ(p, dst - p, 1);
763763
764764 ST(0) = TARG;
765
766
767 void
768 variable(r, name, value = NULL)
769 CODE:
770
771 dXSTARG;
772 ngx_http_request_t *r;
773 SV *name, *value;
774 u_char *p, *lowcase;
775 STRLEN len;
776 ngx_str_t var, val;
777 ngx_uint_t i, hash;
778 ngx_http_variable_value_t *vv;
779
780 ngx_http_perl_set_request(r);
781
782 name = ST(1);
783
784 if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) {
785 name = SvRV(name);
786 }
787
788 if (items == 2) {
789 value = NULL;
790
791 } else {
792 value = ST(2);
793
794 if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) {
795 value = SvRV(value);
796 }
797
798 if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) {
799 XSRETURN_UNDEF;
800 }
801 }
802
803 p = (u_char *) SvPV(name, len);
804
805 lowcase = ngx_palloc(r->pool, len);
806 if (lowcase == NULL) {
807 XSRETURN_UNDEF;
808 }
809
810 hash = 0;
811 for (i = 0; i < len; i++) {
812 lowcase[i] = ngx_tolower(p[i]);
813 hash = ngx_hash(hash, lowcase[i]);
814 }
815
816 var.len = len;
817 var.data = lowcase;
818
819 vv = ngx_http_get_variable(r, &var, hash, 1);
820 if (vv == NULL) {
821 XSRETURN_UNDEF;
822 }
823
824 if (vv->not_found) {
825 if (value == NULL) {
826 XSRETURN_UNDEF;
827 }
828
829 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
830 "variable \"%V\" not found", &var);
831
832 XSRETURN_UNDEF;
833 }
834
835 if (value) {
836 vv->len = val.len;
837 vv->valid = 1;
838 vv->no_cachable = 0;
839 vv->not_found = 0;
840 vv->data = val.data;
841
842 XSRETURN_UNDEF;
843 }
844
845 ngx_http_perl_set_targ(vv->data, vv->len, 0);
846
847 ST(0) = TARG;