Skip to content

Commit

Permalink
Merge pull request #31 from rryqszq4/ngx_php7
Browse files Browse the repository at this point in the history
  • Loading branch information
rryqszq4 authored Apr 9, 2019
2 parents b495676 + e53659b commit e76d71e
Showing 24 changed files with 865 additions and 160 deletions.
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
ngx_php7 0.0.16 changes: 01 Feb 2019
* Add php impl ngx_header_set, ngx_header_get and ngx_header_gets.

* Fixed when php script was error, response return 500 code.

* Fixed core dump at php error handler.

* Fixed nginx request keepalive to close by the right way.

ngx_php7 0.0.15 changes: 12 Jan 2019
* Fixed compiled pass on freebas 11.2 and used of clang compiler.

6 changes: 4 additions & 2 deletions config
Original file line number Diff line number Diff line change
@@ -10,15 +10,16 @@ NGX_PHP_SRCS="$ngx_addon_dir/src/ngx_http_php_module.c \
$ngx_addon_dir/src/ngx_http_php_socket.c \
$ngx_addon_dir/src/ngx_http_php_util.c \
$ngx_addon_dir/src/ngx_http_php_variable.c \
$ngx_addon_dir/src/ngx_http_php_header.c \
$ngx_addon_dir/src/ngx_php_debug.c \
$ngx_addon_dir/src/php/impl/php_ngx.c \
$ngx_addon_dir/src/php/impl/php_ngx_track.c \
$ngx_addon_dir/src/php/impl/php_ngx_core.c \
$ngx_addon_dir/src/php/impl/php_ngx_log.c \
$ngx_addon_dir/src/php/impl/php_ngx_request.c \
$ngx_addon_dir/src/php/impl/php_ngx_socket.c \
$ngx_addon_dir/src/php/impl/php_ngx_var.c \
$ngx_addon_dir/src/php/impl/php_ngx_sockets.c \
$ngx_addon_dir/src/php/impl/php_ngx_header.c \
"
NGX_PHP_DEPS="$ngx_addon_dir/src/ngx_http_php_module.h \
$ngx_addon_dir/src/ngx_http_php_core.h \
@@ -30,15 +31,16 @@ NGX_PHP_DEPS="$ngx_addon_dir/src/ngx_http_php_module.h \
$ngx_addon_dir/src/ngx_http_php_socket.h \
$ngx_addon_dir/src/ngx_http_php_util.h \
$ngx_addon_dir/src/ngx_http_php_variable.h \
$ngx_addon_dir/src/ngx_http_php_header.h \
$ngx_addon_dir/src/ngx_php_debug.h \
$ngx_addon_dir/src/php/impl/php_ngx.h \
$ngx_addon_dir/src/php/impl/php_ngx_track.h \
$ngx_addon_dir/src/php/impl/php_ngx_core.h \
$ngx_addon_dir/src/php/impl/php_ngx_log.h \
$ngx_addon_dir/src/php/impl/php_ngx_request.h \
$ngx_addon_dir/src/php/impl/php_ngx_socket.h \
$ngx_addon_dir/src/php/impl/php_ngx_var.h \
$ngx_addon_dir/src/php/impl/php_ngx_sockets.h \
$ngx_addon_dir/src/php/impl/php_ngx_header.h \
"

if [ -z "$PHP_CONFIG" ]; then
24 changes: 21 additions & 3 deletions src/ngx_http_php_core.c
Original file line number Diff line number Diff line change
@@ -227,7 +227,7 @@ ngx_php_error_cb(int type,
error_type_str = "Unknown error";
break;
}
buffer_len = spprintf(&buffer, 0, "\n%s: %s in %s on line %d\n", error_type_str, buffer, error_filename, error_lineno);
buffer_len = spprintf(&buffer, 0, "%s: %s in %s on line %d", error_type_str, buffer, error_filename, error_lineno);

ngx_buf_t *b;
ngx_http_php_rputs_chain_list_t *chain;
@@ -239,6 +239,18 @@ ngx_php_error_cb(int type,
r = ngx_php_request;
ctx = ngx_http_get_module_ctx(r, ngx_http_php_module);

if ( ctx == NULL ) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s", buffer);

ngx_php_debug("ngx_php error handler, ctx is nil.");

efree(buffer);
zend_bailout();

ngx_http_php_zend_uthread_exit(r);
return ;
}

ns.data = (u_char *)buffer;
ns.len = buffer_len;

@@ -270,12 +282,18 @@ ngx_php_error_cb(int type,
r->headers_out.content_length_n += ns.len;
}

ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, " %s: %s in %s on line %d \n", error_type_str, buffer, error_filename, error_lineno);
if (!r->headers_out.status) {
r->headers_out.status = NGX_HTTP_INTERNAL_SERVER_ERROR;
}

ngx_http_php_zend_uthread_exit(r);
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s", buffer);

ngx_php_debug("ngx_php error handler.");

efree(buffer);
zend_bailout();

ngx_http_php_zend_uthread_exit(r);
return ;
}

2 changes: 2 additions & 0 deletions src/ngx_http_php_core.h
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ngx_http.h>
#include <php_embed.h>
#include "php/impl/php_ngx.h"
#include "php/impl/php_ngx_sockets.h"

#include "ngx_http_php_socket.h"

@@ -123,6 +124,7 @@ typedef struct ngx_http_php_ctx_s {
ngx_int_t delay_time;
ngx_event_t sleep;

php_ngx_socket_t *php_socket;
ngx_http_php_socket_upstream_t *upstream;
ngx_str_t host;
in_port_t port;
38 changes: 34 additions & 4 deletions src/ngx_http_php_handler.c
Original file line number Diff line number Diff line change
@@ -303,9 +303,17 @@ ngx_http_php_rewrite_inline_handler(ngx_http_request_t *r)
set_output:
rc = ngx_php_get_request_status();

/*if ( r->keepalive == 0 ) {
return NGX_OK;
}*/

ctx = ngx_http_get_module_ctx(r, ngx_http_php_module);

if ( ctx->generator_closure ) {
if ( ctx == NULL ) {
return NGX_ERROR;
}

if ( ctx && ctx->generator_closure ) {
zval_ptr_dtor(ctx->generator_closure);
}

@@ -578,9 +586,17 @@ ngx_http_php_access_inline_handler(ngx_http_request_t *r)
set_output:
rc = ngx_php_get_request_status();

/*if ( r->keepalive == 0 ) {
return NGX_OK;
}*/

ctx = ngx_http_get_module_ctx(r, ngx_http_php_module);

if (ctx->generator_closure) {
if ( ctx == NULL ) {
return NGX_ERROR;
}

if ( ctx && ctx->generator_closure ) {
zval_ptr_dtor(ctx->generator_closure);
}

@@ -927,6 +943,9 @@ ngx_http_php_content_inline_handler(ngx_http_request_t *r)
return rc;
}

ngx_php_debug("ctx->phase_status: %d", (int)ctx->phase_status);
ngx_php_debug("r->keepalive: %d", r->keepalive);

if (ctx->phase_status == NGX_DECLINED) {

//ngx_http_php_content_inline_uthread_routine(r);
@@ -961,9 +980,20 @@ ngx_http_php_content_inline_handler(ngx_http_request_t *r)
set_output:
rc = ngx_php_get_request_status();

/*if ( r->keepalive == 0 ) {
return NGX_OK;
}*/

ctx = ngx_http_get_module_ctx(r, ngx_http_php_module);

if (ctx->generator_closure) {
ngx_php_debug("r: %p, ctx: %p", r, ctx);

if ( ctx == NULL ) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_php ctx is nil at content inline handler.");
return NGX_ERROR;
}

if ( ctx && ctx->generator_closure ) {
zval_ptr_dtor(ctx->generator_closure);
}

@@ -1024,7 +1054,7 @@ ngx_http_php_content_inline_handler(ngx_http_request_t *r)

ngx_http_output_filter(r, chain->out);

//ngx_http_set_ctx(r, NULL, ngx_http_php_module);
ngx_http_set_ctx(r, NULL, ngx_http_php_module);

return NGX_OK;

98 changes: 98 additions & 0 deletions src/ngx_http_php_header.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
==============================================================================
Copyright (c) 2016-2019, rryqszq4 <rryqszq@gmail.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
==============================================================================
*/

#include "ngx_http_php_header.h"

ngx_str_t *
ngx_http_php_output_header_get(ngx_http_request_t *r, const u_char *key_data, size_t key_len)
{
ngx_str_t *value;
ngx_list_part_t *part;
ngx_table_elt_t *header;
ngx_uint_t i;

value = NULL;

if (ngx_strncasecmp((u_char *)key_data, (u_char *)"content-type", 12) == 0){
value = &r->headers_out.content_type;
}else {
part = &r->headers_out.headers.part;
header = part->elts;

for ( i = 0; /* void */; i++) {
if ( i >= part->nelts ) {
if ( part->next == NULL ) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}

if ( ngx_strncasecmp((u_char *)key_data, header[i].key.data, header[i].key.len) == 0 ) {
value = &header[i].value;
break;
}
}
}

return value;
}

ngx_int_t
ngx_http_php_output_header_set(ngx_http_request_t *r,
const u_char *key_data, size_t key_len,
const u_char *value_data, size_t value_len)
{
ngx_table_elt_t *h;

if ( ngx_strncasecmp((u_char *)key_data, (u_char *)"content-type", 12) == 0 ){
r->headers_out.content_type.data = (u_char *)value_data;
r->headers_out.content_type.len = value_len;
r->headers_out.content_type_len = value_len;
}else if ( ngx_strncasecmp((u_char *)key_data, (u_char *)"content-length", 14) == 0 ) {
r->headers_out.content_length_n = value_len;
}else {
h = ngx_list_push(&r->headers_out.headers);

if ( h == NULL ) {
return 0;
}

h->hash = 1;
h->key.len = key_len;
h->key.data = (u_char *)key_data;
h->value.len = value_len;
h->value.data = (u_char *)value_data;
}

return 1;
}



38 changes: 38 additions & 0 deletions src/ngx_http_php_header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
==============================================================================
Copyright (c) 2016-2019, rryqszq4 <rryqszq@gmail.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
==============================================================================
*/

#ifndef __NGX_HTTP_PHP_HEADER_H_
#define __NGX_HTTP_PHP_HEADER_H_

#include "ngx_http_php_module.h"

ngx_str_t *ngx_http_php_output_header_get(ngx_http_request_t *r, const u_char *key_data, size_t key_len);

ngx_int_t ngx_http_php_output_header_set(ngx_http_request_t *r, const u_char *key_data, size_t key_len, const u_char *value_data, size_t value_len);

#endif
16 changes: 9 additions & 7 deletions src/ngx_http_php_module.c
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "php/impl/php_ngx_socket.h"
#include "php/impl/php_ngx_var.h"
#include "php/impl/php_ngx_sockets.h"
#include "php/impl/php_ngx_header.h"

#include <ngx_core.h>
#include <ngx_http.h>
@@ -534,23 +535,24 @@ ngx_http_php_init_worker(ngx_cycle_t *cycle)
old_zend_error_cb = zend_error_cb;
zend_error_cb = ngx_php_error_cb;

ori_compile_file = zend_compile_file;
zend_compile_file = ngx_compile_file;
//ori_compile_file = zend_compile_file;
//zend_compile_file = ngx_compile_file;

ori_compile_string = zend_compile_string;
zend_compile_string = ngx_compile_string;
//ori_compile_string = zend_compile_string;
//zend_compile_string = ngx_compile_string;

ori_execute_ex = zend_execute_ex;
zend_execute_ex = ngx_execute_ex;
//ori_execute_ex = zend_execute_ex;
//zend_execute_ex = ngx_execute_ex;

zend_execute_internal = ngx_execute_internal;
//zend_execute_internal = ngx_execute_internal;

php_impl_ngx_core_init(0 TSRMLS_CC);
php_impl_ngx_log_init(0 TSRMLS_CC);
php_impl_ngx_request_init(0 TSRMLS_CC);
php_impl_ngx_socket_init(0 TSRMLS_CC);
php_impl_ngx_var_init(0 TSRMLS_CC);
php_impl_ngx_sockets_init(0 TSRMLS_CC);
php_impl_ngx_header_init(0 TSRMLS_CC);

return NGX_OK;
}
2 changes: 1 addition & 1 deletion src/ngx_http_php_module.h
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#define NGX_HTTP_PHP_MODULE_NAME "ngx_php"
#define NGX_HTTP_PHP_MODULE_VERSION "0.0.15"
#define NGX_HTTP_PHP_MODULE_VERSION "0.0.16"

extern ngx_module_t ngx_http_php_module;
ngx_http_request_t *ngx_php_request;
2 changes: 1 addition & 1 deletion src/ngx_http_php_sleep.c
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ ngx_http_php_sleep(ngx_http_request_t *r)
cln->handler = ngx_http_php_sleep_cleanup;
cln->data = r;

r->keepalive = 0;
//r->keepalive = 0;

return NGX_OK;
}
Loading

0 comments on commit e76d71e

Please sign in to comment.