📄 nsapi.c
字号:
} convert_to_string_ex(uri); if (!nsapi_servact_service) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include uri '%s' - Sub-requests not supported on this platform", (*uri)->value.str.val); RETURN_FALSE; } else if (zend_ini_long("zlib.output_compression", sizeof("zlib.output_compression"), 0)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include uri '%s' - Sub-requests do not work with zlib.output_compression", (*uri)->value.str.val); RETURN_FALSE; } else { php_end_ob_buffers(1 TSRMLS_CC); php_header(); /* do the sub-request */ /* thanks to Chris Elving from Sun for this code sniplet */ if ((rq = request_restart_internal((*uri)->value.str.val, NULL)) == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include uri '%s' - Internal request creation failed", (*uri)->value.str.val); RETURN_FALSE; } /* insert host of current request to get page from same vhost */ param_free(pblock_remove("host", rq->headers)); if (value = pblock_findval("host", rc->rq->headers)) { pblock_nvinsert("host", value, rq->headers); } /* go through the normal request stages as given in obj.conf, but leave out the logging/error section */ do { rv = (*nsapi_servact_uri2path)(rc->sn, rq); if (rv != REQ_PROCEED) { continue; } rv = (*nsapi_servact_pathchecks)(rc->sn, rq); if (rv != REQ_PROCEED) { continue; } rv = (*nsapi_servact_fileinfo)(rc->sn, rq); if (rv != REQ_PROCEED) { continue; } rv = (*nsapi_servact_service)(rc->sn, rq); } while (rv == REQ_RESTART); if (rq->status_num != 200) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include uri '%s' - HTTP status code %d during subrequest", (*uri)->value.str.val, rq->status_num); request_free(rq); RETURN_FALSE; } request_free(rq); RETURN_TRUE; }}/* }}} *//* {{{ proto array nsapi_request_headers(void) Get all headers from the request */PHP_FUNCTION(nsapi_request_headers){ register int i; struct pb_entry *entry; nsapi_request_context *rc = (nsapi_request_context *)SG(server_context); array_init(return_value); for (i=0; i < rc->rq->headers->hsize; i++) { entry=rc->rq->headers->ht[i]; while (entry) { if (!PG(safe_mode) || strncasecmp(entry->param->name, "authorization", 13)) { add_assoc_string(return_value, entry->param->name, entry->param->value, 1); } entry=entry->next; } }}/* }}} *//* {{{ proto array nsapi_response_headers(void) Get all headers from the response */PHP_FUNCTION(nsapi_response_headers){ register int i; struct pb_entry *entry; nsapi_request_context *rc = (nsapi_request_context *)SG(server_context); array_init(return_value); php_header(); for (i=0; i < rc->rq->srvhdrs->hsize; i++) { entry=rc->rq->srvhdrs->ht[i]; while (entry) { add_assoc_string(return_value, entry->param->name, entry->param->value, 1); entry=entry->next; } }}/* }}} *//*************//* SAPI part *//*************/static int sapi_nsapi_ub_write(const char *str, unsigned int str_length TSRMLS_DC){ int retval; nsapi_request_context *rc; rc = (nsapi_request_context *)SG(server_context); retval = net_write(rc->sn->csd, (char *)str, str_length); if (retval == IO_ERROR /* -1 */ || retval == IO_EOF /* 0 */) { php_handle_aborted_connection(); } return retval;}static int sapi_nsapi_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC){ char *header_name, *header_content, *p; nsapi_request_context *rc = (nsapi_request_context *)SG(server_context); header_name = sapi_header->header; header_content = p = strchr(header_name, ':'); if (p == NULL) { efree(sapi_header->header); return 0; } *p = 0; do { header_content++; } while (*header_content == ' '); if (!strcasecmp(header_name, "Content-Type")) { param_free(pblock_remove("content-type", rc->rq->srvhdrs)); pblock_nvinsert("content-type", header_content, rc->rq->srvhdrs); } else { /* to lower case because NSAPI reformats the headers and wants lowercase */ for (p=header_name; *p; p++) { *p=tolower(*p); } if (sapi_header->replace) param_free(pblock_remove(header_name, rc->rq->srvhdrs)); pblock_nvinsert(header_name, header_content, rc->rq->srvhdrs); } sapi_free_header(sapi_header); return 0; /* don't use the default SAPI mechanism, NSAPI duplicates this functionality */}static int sapi_nsapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC){ int retval; nsapi_request_context *rc = (nsapi_request_context *)SG(server_context); if (SG(sapi_headers).send_default_content_type) { char *hd; param_free(pblock_remove("content-type", rc->rq->srvhdrs)); hd = sapi_get_default_content_type(TSRMLS_C); pblock_nvinsert("content-type", hd, rc->rq->srvhdrs); efree(hd); } protocol_status(rc->sn, rc->rq, SG(sapi_headers).http_response_code, NULL); retval = protocol_start_response(rc->sn, rc->rq); if (retval == REQ_PROCEED || retval == REQ_NOACTION) { return SAPI_HEADER_SENT_SUCCESSFULLY; } else { return SAPI_HEADER_SEND_FAILED; }}static int sapi_nsapi_read_post(char *buffer, uint count_bytes TSRMLS_DC){ nsapi_request_context *rc = (nsapi_request_context *)SG(server_context); char *read_ptr = buffer, *content_length_str = NULL; uint bytes_read = 0; int length, content_length = 0; netbuf *nbuf = rc->sn->inbuf; /* * Yesss! */ count_bytes = MIN(count_bytes, SG(request_info).content_length-rc->read_post_bytes); content_length = SG(request_info).content_length; if (content_length <= 0) { return 0; } /* * Gobble any pending data in the netbuf. */ length = nbuf->cursize - nbuf->pos; length = MIN(count_bytes, length); if (length > 0) { memcpy(read_ptr, nbuf->inbuf + nbuf->pos, length); bytes_read += length; read_ptr += length; content_length -= length; nbuf->pos += length; } /* * Read the remaining from the socket. */ while (content_length > 0 && bytes_read < count_bytes) { int bytes_to_read = count_bytes - bytes_read; if (content_length < bytes_to_read) { bytes_to_read = content_length; } length = net_read(rc->sn->csd, read_ptr, bytes_to_read, NSAPI_G(read_timeout)); if (length == IO_ERROR || length == IO_EOF) { break; } bytes_read += length; read_ptr += length; content_length -= length; } if ( bytes_read > 0 ) { rc->read_post_bytes += bytes_read; } return bytes_read;}static char *sapi_nsapi_read_cookies(TSRMLS_D){ char *cookie_string; nsapi_request_context *rc = (nsapi_request_context *)SG(server_context); cookie_string = pblock_findval("cookie", rc->rq->headers); return cookie_string;}static void sapi_nsapi_register_server_variables(zval *track_vars_array TSRMLS_DC){ nsapi_request_context *rc = (nsapi_request_context *)SG(server_context); register size_t i; int pos; char *value,*p; char buf[NS_BUF_SIZE + 1]; struct pb_entry *entry; for (i = 0; i < nsapi_reqpb_size; i++) { value = pblock_findval(nsapi_reqpb[i].nsapi_eq, rc->rq->reqpb); if (value) { php_register_variable((char *)nsapi_reqpb[i].env_var, value, track_vars_array TSRMLS_CC); } } for (i=0; i < rc->rq->headers->hsize; i++) { entry=rc->rq->headers->ht[i]; while (entry) { if (!PG(safe_mode) || strncasecmp(entry->param->name, "authorization", 13)) { if (strcasecmp(entry->param->name, "content-length")==0 || strcasecmp(entry->param->name, "content-type")==0) { strlcpy(buf, entry->param->name, NS_BUF_SIZE); pos = 0; } else { snprintf(buf, NS_BUF_SIZE, "HTTP_%s", entry->param->name); pos = 5; } buf[NS_BUF_SIZE]='\0'; for(p = buf + pos; *p; p++) { *p = toupper(*p); if (*p < 'A' || *p > 'Z') { *p = '_'; } } php_register_variable(buf, entry->param->value, track_vars_array TSRMLS_CC); } entry=entry->next; } } for (i = 0; i < nsapi_vars_size; i++) { value = pblock_findval(nsapi_vars[i].nsapi_eq, rc->rq->vars); if (value) { php_register_variable((char *)nsapi_vars[i].env_var, value, track_vars_array TSRMLS_CC); } } for (i = 0; i < nsapi_client_size; i++) { value = pblock_findval(nsapi_client[i].nsapi_eq, rc->sn->client); if (value) { php_register_variable((char *)nsapi_client[i].env_var, value, track_vars_array TSRMLS_CC); } } if (value = session_dns(rc->sn)) { php_register_variable("REMOTE_HOST", value, track_vars_array TSRMLS_CC); nsapi_free(value); } sprintf(buf, "%d", conf_getglobals()->Vport); php_register_variable("SERVER_PORT", buf, track_vars_array TSRMLS_CC); php_register_variable("SERVER_NAME", conf_getglobals()->Vserver_hostname, track_vars_array TSRMLS_CC); value = http_uri2url_dynamic("", "", rc->sn, rc->rq); php_register_variable("SERVER_URL", value, track_vars_array TSRMLS_CC); nsapi_free(value); php_register_variable("SERVER_SOFTWARE", system_version(), track_vars_array TSRMLS_CC); php_register_variable("HTTPS", (security_active ? "ON" : "OFF"), track_vars_array TSRMLS_CC); php_register_variable("GATEWAY_INTERFACE", "CGI/1.1", track_vars_array TSRMLS_CC); /* DOCUMENT_ROOT */ if (value = request_translate_uri("/", rc->sn)) { value[strlen(value) - 1] = '\0'; php_register_variable("DOCUMENT_ROOT", value, track_vars_array TSRMLS_CC); nsapi_free(value); } /* PATH_INFO / PATH_TRANSLATED */ if (rc->path_info) { if (value = request_translate_uri(rc->path_info, rc->sn)) { php_register_variable("PATH_TRANSLATED", value, track_vars_array TSRMLS_CC); nsapi_free(value); } php_register_variable("PATH_INFO", rc->path_info, track_vars_array TSRMLS_CC); } /* Create full Request-URI & Script-Name */ if (SG(request_info).request_uri) { strlcpy(buf, SG(request_info).request_uri, NS_BUF_SIZE); if (SG(request_info).query_string) { p = strchr(buf, 0); snprintf(p, NS_BUF_SIZE-(p-buf), "?%s", SG(request_info).query_string);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -