PHPAPI int php_header(TSRMLS_D) { if (sapi_send_headers(TSRMLS_C)==FAILURE || SG(request_info).headers_only) { return 0; /* don't allow output */ } else { return 1; /* allow output */ } }
static int sapi_nsapi_ub_write(const char *str, unsigned int str_length TSRMLS_DC) { int retval; nsapi_request_context *rc = (nsapi_request_context *)SG(server_context); if (!SG(headers_sent)) { sapi_send_headers(TSRMLS_C); } 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; }
/* modified version of apache2 */ static void sapi_nsapi_flush(void *server_context) { nsapi_request_context *rc = (nsapi_request_context *)server_context; TSRMLS_FETCH(); if (!rc) { /* we have no context, so no flushing needed. This fixes a SIGSEGV on shutdown */ return; } if (!SG(headers_sent)) { sapi_send_headers(TSRMLS_C); } /* flushing is only supported in iPlanet servers from version 6.1 on, make it conditional */ #if NSAPI_VERSION >= 302 if (net_flush(rc->sn->csd) < 0) { php_handle_aborted_connection(); } #endif }
static void php_apache_sapi_flush(void *server_context) { php_struct *ctx; apr_bucket_brigade *bb; apr_bucket_alloc_t *ba; apr_bucket *b; ap_filter_t *f; /* output filters */ ctx = server_context; /* If we haven't registered a server_context yet, * then don't bother flushing. */ if (!server_context) return; sapi_send_headers(); ctx->r->status = SG(sapi_headers).http_response_code; SG(headers_sent) = 1; f = ctx->f; /* Send a flush bucket down the filter chain. The current default * handler seems to act on the first flush bucket, but ignores * all further flush buckets. */ ba = ctx->r->connection->bucket_alloc; bb = apr_brigade_create(ctx->r->pool, ba); b = apr_bucket_flush_create(ba); APR_BRIGADE_INSERT_TAIL(bb, b); if (ap_pass_brigade(f->next, bb) != APR_SUCCESS || ctx->r->connection->aborted) { php_handle_aborted_connection(); } }
static void runPhp(MaQueue *q) { MaConn *conn; MaRequest *req; MaResponse *resp; MaPhp *php; FILE *fp; char shebang[MPR_MAX_STRING]; zend_file_handle file_handle; TSRMLS_FETCH(); conn = q->conn; req = conn->request; resp = conn->response; php = q->queueData; maPutForService(q, maCreateHeaderPacket(q), 0); /* * Set the request context */ zend_first_try { php->var_array = 0; SG(server_context) = conn; if (req->user) { SG(request_info).auth_user = estrdup(req->user); } if (req->password) { SG(request_info).auth_password = estrdup(req->password); } if (req->authType && req->authDetails) { SG(request_info).auth_digest = estrdup(mprAsprintf(req, -1, "%s %s", req->authType, req->authDetails)); } SG(request_info).auth_password = req->password; SG(request_info).content_type = req->mimeType; SG(request_info).content_length = req->length; SG(sapi_headers).http_response_code = MPR_HTTP_CODE_OK; SG(request_info).path_translated = resp->filename; SG(request_info).query_string = req->parsedUri->query; SG(request_info).request_method = req->methodName; SG(request_info).request_uri = req->url; /* * Workaround on MAC OS X where the SIGPROF is given to the wrong thread */ PG(max_input_time) = -1; EG(timeout_seconds) = 0; /* The readPostData callback may be invoked during startup */ php_request_startup(TSRMLS_C); CG(zend_lineno) = 0; } zend_catch { mprError(q, "Can't start PHP request"); zend_try { php_request_shutdown(0); } zend_end_try(); maFailRequest(conn, MPR_HTTP_CODE_INTERNAL_SERVER_ERROR, "PHP initialization failed"); return; } zend_end_try(); /* * Execute the script file */ file_handle.filename = resp->filename; file_handle.free_filename = 0; file_handle.opened_path = 0; #if LOAD_FROM_FILE file_handle.type = ZEND_HANDLE_FILENAME; #else file_handle.type = ZEND_HANDLE_FP; if ((fp = fopen(resp->filename, "r")) == NULL) { maFailRequest(conn, MPR_HTTP_CODE_INTERNAL_SERVER_ERROR, "PHP can't open script"); return; } /* Check for shebang and skip */ file_handle.handle.fp = fp; shebang[0] = '\0'; if (fgets(shebang, sizeof(shebang), file_handle.handle.fp) != 0) { if (shebang[0] != '#' || shebang[1] != '!') { fseek(fp, 0L, SEEK_SET); } } #endif zend_try { php_execute_script(&file_handle TSRMLS_CC); if (!SG(headers_sent)) { sapi_send_headers(TSRMLS_C); } } zend_catch { php_request_shutdown(0); maFailRequest(conn, MPR_HTTP_CODE_INTERNAL_SERVER_ERROR, "PHP script execution failed"); return; } zend_end_try(); zend_try { php_request_shutdown(0); } zend_end_try(); maPutForService(q, maCreateEndPacket(q), 1); }
/* Run the request. This is invoked when all the input data has been received and buffered. This routine completely services the request. */ static void readyPhp(HttpQueue *q) { HttpConn *conn; HttpRx *rx; HttpTx *tx; MaPhp *php; FILE *fp; cchar *value; char shebang[MPR_MAX_STRING]; zend_file_handle file_handle; TSRMLS_FETCH(); conn = q->conn; rx = conn->rx; tx = conn->tx; if ((php = q->queueData) == 0) { return; } /* Set the request context */ zend_first_try { php->var_array = 0; SG(server_context) = conn; if (conn->username) { SG(request_info).auth_user = estrdup(conn->username); } if (conn->password) { SG(request_info).auth_password = estrdup(conn->password); } if ((value = httpGetHeader(conn, "Authorization")) != 0) { SG(request_info).auth_digest = estrdup(value); } SG(request_info).content_type = rx->mimeType; SG(request_info).path_translated = tx->filename; SG(request_info).content_length = (long) (ssize) rx->length; SG(sapi_headers).http_response_code = HTTP_CODE_OK; SG(request_info).query_string = rx->parsedUri->query; SG(request_info).request_method = rx->method; SG(request_info).request_uri = rx->uri; /* Workaround on MAC OS X where the SIGPROF is given to the wrong thread */ PG(max_input_time) = -1; EG(timeout_seconds) = 0; /* The readBodyData callback may be invoked during startup */ php_request_startup(TSRMLS_C); CG(zend_lineno) = 0; } zend_catch { mprError("Cannot start PHP request"); zend_try { php_request_shutdown(0); } zend_end_try(); httpError(conn, HTTP_CODE_INTERNAL_SERVER_ERROR, "PHP initialization failed"); return; } zend_end_try(); /* Execute the script file */ file_handle.filename = tx->filename; file_handle.free_filename = 0; file_handle.opened_path = 0; #if LOAD_FROM_FILE file_handle.type = ZEND_HANDLE_FILENAME; #else file_handle.type = ZEND_HANDLE_FP; if ((fp = fopen(tx->filename, "r")) == NULL) { if (rx->referrer) { httpError(conn, HTTP_CODE_NOT_FOUND, "Cannot open document: %s from %s", tx->filename, rx->referrer); } else { httpError(conn, HTTP_CODE_NOT_FOUND, "Cannot open document: %s", tx->filename); } return; } /* Check for shebang and skip */ file_handle.handle.fp = fp; shebang[0] = '\0'; if (fgets(shebang, sizeof(shebang), file_handle.handle.fp)) {} if (shebang[0] != '#' || shebang[1] != '!') { fseek(fp, 0L, SEEK_SET); } #endif zend_try { php_execute_script(&file_handle TSRMLS_CC); if (!SG(headers_sent)) { sapi_send_headers(TSRMLS_C); } } zend_catch { php_request_shutdown(NULL); httpError(conn, HTTP_CODE_INTERNAL_SERVER_ERROR, "PHP script execution failed"); return; } zend_end_try(); zend_try { php_request_shutdown(NULL); SG(server_context) = NULL; } zend_catch { httpError(conn, HTTP_CODE_INTERNAL_SERVER_ERROR, "PHP script shutdown failed"); } zend_end_try(); httpFinalize(conn); }