📄 nginx.xs
字号:
ngx_http_perl_set_targ(ctx->filename.data, ctx->filename.len, 1); ST(0) = TARG;voidprint(r, ...) CODE: ngx_http_request_t *r; SV *sv; int i; u_char *p; size_t size; STRLEN len; ngx_buf_t *b; ngx_http_perl_set_request(r); if (items == 2) { /* * do zero copy for prolate single read-only SV: * $r->print("some text\n"); */ sv = ST(1); if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { sv = SvRV(sv); } if (SvREADONLY(sv)) { p = (u_char *) SvPV(sv, len); if (len == 0) { XSRETURN_EMPTY; } b = ngx_calloc_buf(r->pool); if (b == NULL) { XSRETURN_EMPTY; } b->memory = 1; b->pos = p; b->last = p + len; b->start = p; b->end = b->last; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->print: read-only SV: %z", len); goto out; } } size = 0; for (i = 1; i < items; i++) { sv = ST(i); if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { sv = SvRV(sv); } (void) SvPV(sv, len); ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->print: copy SV: %z", len); size += len; } if (size == 0) { XSRETURN_EMPTY; } b = ngx_create_temp_buf(r->pool, size); if (b == NULL) { XSRETURN_EMPTY; } for (i = 1; i < items; i++) { sv = ST(i); if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { sv = SvRV(sv); } p = (u_char *) SvPV(sv, len); b->last = ngx_cpymem(b->last, p, len); } out: (void) ngx_http_perl_output(r, b); XSRETURN_EMPTY;voidsendfile(r, filename, offset = -1, bytes = 0) CODE: ngx_http_request_t *r; char *filename; int offset; size_t bytes; ngx_fd_t fd; ngx_buf_t *b; ngx_file_info_t fi; ngx_pool_cleanup_t *cln; ngx_pool_cleanup_file_t *clnf; ngx_http_perl_set_request(r); filename = SvPV_nolen(ST(1)); if (filename == NULL) { croak("sendfile(): NULL filename"); } offset = items < 3 ? -1 : SvIV(ST(2)); bytes = items < 4 ? 0 : SvIV(ST(3)); b = ngx_calloc_buf(r->pool); if (b == NULL) { XSRETURN_EMPTY; } b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); if (b->file == NULL) { XSRETURN_EMPTY; } cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t)); if (cln == NULL) { XSRETURN_EMPTY; } fd = ngx_open_file((u_char *) filename, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); if (fd == NGX_INVALID_FILE) { ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, ngx_open_file_n " \"%s\" failed", filename); XSRETURN_EMPTY; } if (offset == -1) { offset = 0; } if (bytes == 0) { if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, ngx_fd_info_n " \"%s\" failed", filename); if (ngx_close_file(fd) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, ngx_close_file_n " \"%s\" failed", filename); } XSRETURN_EMPTY; } bytes = ngx_file_size(&fi) - offset; } cln->handler = ngx_pool_cleanup_file; clnf = cln->data; clnf->fd = fd; clnf->name = (u_char *) ""; clnf->log = r->pool->log; b->in_file = 1; b->file_pos = offset; b->file_last = offset + bytes; b->file->fd = fd; b->file->log = r->connection->log; (void) ngx_http_perl_output(r, b); XSRETURN_EMPTY;voidflush(r) CODE: ngx_http_request_t *r; ngx_buf_t *b; ngx_http_perl_set_request(r); b = ngx_calloc_buf(r->pool); if (b == NULL) { XSRETURN_EMPTY; } b->flush = 1; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->flush"); (void) ngx_http_perl_output(r, b); XSRETURN_EMPTY;voidinternal_redirect(r, uri) CODE: ngx_http_request_t *r; SV *uri; ngx_uint_t i; ngx_http_perl_ctx_t *ctx; ngx_http_perl_set_request(r); uri = ST(1); ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); if (ngx_http_perl_sv2str(aTHX_ r, &ctx->redirect_uri, uri) != NGX_OK) { XSRETURN_EMPTY; } for (i = 0; i < ctx->redirect_uri.len; i++) { if (ctx->redirect_uri.data[i] == '?') { ctx->redirect_args.len = ctx->redirect_uri.len - (i + 1); ctx->redirect_args.data = &ctx->redirect_uri.data[i + 1]; ctx->redirect_uri.len = i; XSRETURN_EMPTY; } }voidallow_ranges(r) CODE: ngx_http_request_t *r; ngx_http_perl_set_request(r); r->allow_ranges = 1; XSRETURN_EMPTY;voidunescape(r, text, type = 0) CODE: dXSTARG; ngx_http_request_t *r; SV *text; int type; u_char *p, *dst, *src; STRLEN len; ngx_http_perl_set_request(r); text = ST(1); src = (u_char *) SvPV(text, len); p = ngx_palloc(r->pool, len + 1); if (p == NULL) { XSRETURN_UNDEF; } dst = p; type = items < 3 ? 0 : SvIV(ST(2)); ngx_unescape_uri(&dst, &src, len, (ngx_uint_t) type); *dst = '\0'; ngx_http_perl_set_targ(p, dst - p, 1); ST(0) = TARG;voidvariable(r, name, value = NULL) CODE: dXSTARG; ngx_http_request_t *r; SV *name, *value; u_char *p, *lowcase; STRLEN len; ngx_str_t var, val; ngx_uint_t i, hash; ngx_http_perl_var_t *v; ngx_http_perl_ctx_t *ctx; ngx_http_variable_value_t *vv; ngx_http_perl_set_request(r); name = ST(1); if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) { name = SvRV(name); } if (items == 2) { value = NULL; } else { value = ST(2); if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) { value = SvRV(value); } if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) { XSRETURN_UNDEF; } } p = (u_char *) SvPV(name, len); lowcase = ngx_palloc(r->pool, len); if (lowcase == NULL) { XSRETURN_UNDEF; } hash = 0; for (i = 0; i < len; i++) { lowcase[i] = ngx_tolower(p[i]); hash = ngx_hash(hash, lowcase[i]); } var.len = len; var.data = lowcase; #if (NGX_LOG_DEBUG) if (value) { ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl variable: \"%V\"=\"%V\"", &var, &val); } else { ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl variable: \"%V\"", &var); } #endif vv = ngx_http_get_variable(r, &var, hash, 1); if (vv == NULL) { XSRETURN_UNDEF; } if (vv->not_found) { ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); if (ctx->variables) { v = ctx->variables->elts; for (i = 0; i < ctx->variables->nelts; i++) { if (hash != v[i].hash || len != v[i].name.len || ngx_strncmp(lowcase, v[i].name.data, len) != 0) { continue; } if (value) { v[i].value = val; XSRETURN_UNDEF; } ngx_http_perl_set_targ(v[i].value.data, v[i].value.len, 0); goto done; } } if (value) { if (ctx->variables == NULL) { ctx->variables = ngx_array_create(r->pool, 1, sizeof(ngx_http_perl_var_t)); if (ctx->variables == NULL) { XSRETURN_UNDEF; } } v = ngx_array_push(ctx->variables); if (v == NULL) { XSRETURN_UNDEF; } v->hash = hash; v->name.len = len; v->name.data = lowcase; v->value = val; XSRETURN_UNDEF; } ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "variable \"%V\" not found", &var); XSRETURN_UNDEF; } if (value) { vv->len = val.len; vv->valid = 1; vv->no_cachable = 0; vv->not_found = 0; vv->data = val.data; XSRETURN_UNDEF; } ngx_http_perl_set_targ(vv->data, vv->len, 0); done: ST(0) = TARG;voidsleep(r, sleep, next) CODE: dXSTARG; ngx_http_request_t *r; ngx_http_perl_ctx_t *ctx; ngx_http_perl_set_request(r); ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); ctx->sleep = SvIV(ST(1)); ctx->next = SvRV(ST(2)); ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl sleep: %d", ctx->sleep); XSRETURN_EMPTY;voidlog_error(r, err, msg) CODE: ngx_http_request_t *r; SV *err, *msg; u_char *p; STRLEN len; ngx_err_t e; ngx_http_perl_set_request(r); err = ST(1); if (SvROK(err) && SvTYPE(SvRV(err)) == SVt_PV) { err = SvRV(err); } e = SvIV(err); msg = ST(2); if (SvROK(msg) && SvTYPE(SvRV(msg)) == SVt_PV) { msg = SvRV(msg); } p = (u_char *) SvPV(msg, len); ngx_log_error(NGX_LOG_ERR, r->connection->log, e, "perl: %s", p); XSRETURN_EMPTY;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -