<commit_before>/**
 *    Copyright(c) 2016 rryqszq4
 *
 *
 */

#include "ngx_http_php_module.h"
#include "ngx_http_php_request.h"
#include "ngx_http_php_core.h"

ngx_http_php_code_t *
ngx_http_php_code_from_file(ngx_pool_t *pool, ngx_str_t *code_file_path)
{
	ngx_http_php_code_t *code;
	size_t len;
	u_char *p;

	code = ngx_pcalloc(pool, sizeof(*code));
	if (code == NULL){
		return NGX_CONF_UNSET_PTR;
	}

	len = ngx_strlen((char *)code_file_path->data);
	if (len == 0){
		return NGX_CONF_UNSET_PTR;
	}else if (code_file_path->data[0] == '/'){
		code->code.file = ngx_pcalloc(pool, len + 1);
		if (code->code.file == NULL){
			return NGX_CONF_UNSET_PTR;
		}
		ngx_cpystrn((u_char *)code->code.file, (u_char *)code_file_path->data, code_file_path->len + 1);	
	}else {
		code->code.file = ngx_pcalloc(pool, ngx_cycle->conf_prefix.len + len + 1);
		if (code->code.file == NULL){
			return NGX_CONF_UNSET_PTR;
		}
		p = ngx_cpystrn((u_char *)code->code.file, (u_char *)ngx_cycle->conf_prefix.data, ngx_cycle->conf_prefix.len + 1);
		ngx_cpystrn(p, (u_char *)code_file_path->data, code_file_path->len + 1);
	}
	code->code_type = NGX_HTTP_PHP_CODE_TYPE_FILE;
	return code;
}

ngx_http_php_code_t *
ngx_http_php_code_from_string(ngx_pool_t *pool, ngx_str_t *code_str)
{
	ngx_http_php_code_t *code;
	size_t len;

	code = ngx_pcalloc(pool, sizeof(*code));
	if (code == NULL){
		return NGX_CONF_UNSET_PTR;
	}

	len = ngx_strlen(code_str->data);
	code->code.string = ngx_pcalloc(pool, len + 1);
	if (code->code.string == NULL){
		return NGX_CONF_UNSET_PTR;
	}
	ngx_cpystrn((u_char *)code->code.string, code_str->data, len + 1);
	code->code_type = NGX_HTTP_PHP_CODE_TYPE_STRING;
	return code;
}


int ngx_http_php_code_ub_write(const char *str, unsigned int str_length TSRMLS_DC)
{
	ngx_buf_t *b;
	ngx_http_php_rputs_chain_list_t *chain;
	ngx_http_php_ctx_t *ctx;
	ngx_http_request_t *r;
	u_char *u_str;
	ngx_str_t ns;

	r = ngx_php_request;
	ctx = ngx_http_get_module_ctx(r, ngx_http_php_module);

	ns.data = (u_char *)str;
	ns.len = str_length;

	if (ctx->rputs_chain == NULL){
		chain = ngx_pcalloc(r->pool, sizeof(ngx_http_php_rputs_chain_list_t));
		chain->out = ngx_alloc_chain_link(r->pool);
		chain->last = &chain->out;
	}else {
		chain = ctx->rputs_chain;
		(*chain->last)->next = ngx_alloc_chain_link(r->pool);
		chain->last = &(*chain->last)->next;
	}

	b = ngx_calloc_buf(r->pool);
	(*chain->last)->buf = b;
	(*chain->last)->next = NULL;

	u_str = ngx_pstrdup(r->pool, &ns);
	u_str[ns.len] = '\0';
	(*chain->last)->buf->pos = u_str;
	(*chain->last)->buf->last = u_str + ns.len;
	(*chain->last)->buf->memory = 1;
	ctx->rputs_chain = chain;
	ngx_http_set_ctx(r, ctx, ngx_http_php_module);

	if (r->headers_out.content_length_n == -1){
		r->headers_out.content_length_n += ns.len + 1;
	}else {
		r->headers_out.content_length_n += ns.len;
	}

	return r->headers_out.content_length_n;
}

void 
ngx_http_php_code_flush(void *server_context)
{
	
}

void ngx_http_php_code_log_message(char *message)
{
	

}

void ngx_http_php_code_register_server_variables(zval *track_vars_array TSRMLS_DC)
{
	php_import_environment_variables(track_vars_array TSRMLS_CC);

	ngx_http_request_t *r;
	r = ngx_php_request;

	if (r->method == NGX_HTTP_GET){
		php_register_variable("REQUEST_METHOD", (char *)"GET", track_vars_array TSRMLS_CC);
	} else if (r->method == NGX_HTTP_POST){
		php_register_variable("REQUEST_METHOD", (char *)"POST", track_vars_array TSRMLS_CC);
	}

	php_register_variable_safe("REQUEST_LINE", (char *)r->request_line.data, r->request_line.len, track_vars_array TSRMLS_CC);

	php_register_variable_safe("DOCUMENT_URI", (char *)r->uri.data, r->uri.len, track_vars_array TSRMLS_CC);

	php_register_variable_safe("QUERY_STRING", (char *)r->args.data, r->args.len, track_vars_array TSRMLS_CC);

	php_register_variable_safe("REQUEST_URI", (char *)r->uri_start, strlen((char *)r->uri_start)-strlen((char *)r->uri_end),track_vars_array TSRMLS_CC);
	
	php_register_variable_safe("SERVER_PROTOCOL", (char *)r->http_protocol.data, r->http_protocol.len, track_vars_array TSRMLS_CC);

	/*if (SG(request_info).request_method) {
		php_register_variable("REQUEST_METHOD", (char *)SG(request_info).request_method, track_vars_array TSRMLS_CC);<commit_msg>[src] regist php $_SERVER<commit_after>/**
 *    Copyright(c) 2016 rryqszq4
 *
 *
 */

#include "ngx_http_php_module.h"
#include "ngx_http_php_request.h"
#include "ngx_http_php_core.h"

ngx_http_php_code_t *
ngx_http_php_code_from_file(ngx_pool_t *pool, ngx_str_t *code_file_path)
{
	ngx_http_php_code_t *code;
	size_t len;
	u_char *p;

	code = ngx_pcalloc(pool, sizeof(*code));
	if (code == NULL){
		return NGX_CONF_UNSET_PTR;
	}

	len = ngx_strlen((char *)code_file_path->data);
	if (len == 0){
		return NGX_CONF_UNSET_PTR;
	}else if (code_file_path->data[0] == '/'){
		code->code.file = ngx_pcalloc(pool, len + 1);
		if (code->code.file == NULL){
			return NGX_CONF_UNSET_PTR;
		}
		ngx_cpystrn((u_char *)code->code.file, (u_char *)code_file_path->data, code_file_path->len + 1);	
	}else {
		code->code.file = ngx_pcalloc(pool, ngx_cycle->conf_prefix.len + len + 1);
		if (code->code.file == NULL){
			return NGX_CONF_UNSET_PTR;
		}
		p = ngx_cpystrn((u_char *)code->code.file, (u_char *)ngx_cycle->conf_prefix.data, ngx_cycle->conf_prefix.len + 1);
		ngx_cpystrn(p, (u_char *)code_file_path->data, code_file_path->len + 1);
	}
	code->code_type = NGX_HTTP_PHP_CODE_TYPE_FILE;
	return code;
}

ngx_http_php_code_t *
ngx_http_php_code_from_string(ngx_pool_t *pool, ngx_str_t *code_str)
{
	ngx_http_php_code_t *code;
	size_t len;

	code = ngx_pcalloc(pool, sizeof(*code));
	if (code == NULL){
		return NGX_CONF_UNSET_PTR;
	}

	len = ngx_strlen(code_str->data);
	code->code.string = ngx_pcalloc(pool, len + 1);
	if (code->code.string == NULL){
		return NGX_CONF_UNSET_PTR;
	}
	ngx_cpystrn((u_char *)code->code.string, code_str->data, len + 1);
	code->code_type = NGX_HTTP_PHP_CODE_TYPE_STRING;
	return code;
}


int ngx_http_php_code_ub_write(const char *str, unsigned int str_length TSRMLS_DC)
{
	ngx_buf_t *b;
	ngx_http_php_rputs_chain_list_t *chain;
	ngx_http_php_ctx_t *ctx;
	ngx_http_request_t *r;
	u_char *u_str;
	ngx_str_t ns;

	r = ngx_php_request;
	ctx = ngx_http_get_module_ctx(r, ngx_http_php_module);

	ns.data = (u_char *)str;
	ns.len = str_length;

	if (ctx->rputs_chain == NULL){
		chain = ngx_pcalloc(r->pool, sizeof(ngx_http_php_rputs_chain_list_t));
		chain->out = ngx_alloc_chain_link(r->pool);
		chain->last = &chain->out;
	}else {
		chain = ctx->rputs_chain;
		(*chain->last)->next = ngx_alloc_chain_link(r->pool);
		chain->last = &(*chain->last)->next;
	}

	b = ngx_calloc_buf(r->pool);
	(*chain->last)->buf = b;
	(*chain->last)->next = NULL;

	u_str = ngx_pstrdup(r->pool, &ns);
	u_str[ns.len] = '\0';
	(*chain->last)->buf->pos = u_str;
	(*chain->last)->buf->last = u_str + ns.len;
	(*chain->last)->buf->memory = 1;
	ctx->rputs_chain = chain;
	ngx_http_set_ctx(r, ctx, ngx_http_php_module);

	if (r->headers_out.content_length_n == -1){
		r->headers_out.content_length_n += ns.len + 1;
	}else {
		r->headers_out.content_length_n += ns.len;
	}

	return r->headers_out.content_length_n;
}

void 
ngx_http_php_code_flush(void *server_context)
{
	
}

void ngx_http_php_code_log_message(char *message)
{
	

}

void ngx_http_php_code_register_server_variables(zval *track_vars_array TSRMLS_DC)
{
	php_import_environment_variables(track_vars_array TSRMLS_CC);

	ngx_http_request_t *r;
	r = ngx_php_request;

	ngx_http_headers_in_t *headers_in;
	headers_in = &r->headers_in;

	ngx_table_elt_t *host = headers_in->host;
	ngx_table_elt_t *connection = headers_in->connection;
	ngx_table_elt_t *user_agent = headers_in->user_agent;
	ngx_table_elt_t *accept_encoding = headers_in->accept_encoding;
	ngx_table_elt_t *content_length = headers_in->content_length;
	ngx_table_elt_t *content_type = headers_in->content_type;

	if (r->method == NGX_HTTP_GET){
		php_register_variable("REQUEST_METHOD", (char *)"GET", track_vars_array TSRMLS_CC);
	} else if (r->method == NGX_HTTP_POST){
		php_register_variable("REQUEST_METHOD", (char *)"POST", track_vars_array TSRMLS_CC);
	}

	//php_register_variable_safe("REQUEST_LINE", (char *)r->request_line.data, r->request_line.len, track_vars_array TSRMLS_CC);

	php_register_variable_safe("DOCUMENT_URI", (char *)r->uri.data, r->uri.len, track_vars_array TSRMLS_CC);

	php_register_variable_safe("QUERY_STRING", (char *)r->args.data, r->args.len, track_vars_array TSRMLS_CC);

	php_register_variable_safe("REQUEST_URI", (char *)r->uri_start, strlen((char *)r->uri_start)-strlen((char *)r->uri_end),track_vars_array TSRMLS_CC);
	
	php_register_variable_safe("SERVER_PROTOCOL", (char *)r->http_protocol.data, r->http_protocol.len, track_vars_array TSRMLS_CC);

	if (host){
		php_register_variable_safe("HTTP_HOST", (char *)host->value.data, host->value.len, track_vars_array TSRMLS_CC);
	}

	if (connection){
		php_register_variable_safe("HTTP_CONNECTION", (char *)connection->value.data, connection->value.len, track_vars_array TSRMLS_CC);
	}

	if (user_agent){
		php_register_variable_safe("HTTP_USER_AGENT", (char *)user_agent->value.data, user_agent->value.len, track_vars_array TSRMLS_CC);
	}

	if (accept_encoding){
		php_register_variable_safe("HTTP_ACCEPT_ENCODING", (char *)accept_encoding->value.data, accept_encoding->value.len, track_vars_array TSRMLS_CC);
	}

	if (content_length){
		php_register_variable_safe("CONTENT_LENGTH", (char *)content_length->value.data, content_length->value.len, track_vars_array TSRMLS_CC);
	}

	if (content_type){
		php_register_variable_safe("CONTENT_TYPE", (char *)content_type->value.data, content_type->value.len, track_vars_array TSRMLS_CC);
	}

	/*if (SG(request_info).request_method) {
		php_register_variable("REQUEST_METHOD", (char *)SG(request_info).request_method, track_vars_array TSRMLS_CC);
{'fraction_non_alphanumeric': 0.14808787632221318, 'fraction_numerical': 0.003661513425549227, 'mean_word_length': 5.973758865248227, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 158, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7624484', 'n_tokens_mistral': 4312, 'n_tokens_neox': 4146, 'n_words': 614}