<commit_before>/*
    +--------------------------------------------------------------------+
    | PECL :: http                                                       |
    +--------------------------------------------------------------------+
    | Redistribution and use in source and binary forms, with or without |
    | modification, are permitted provided that the conditions mentioned |
    | in the accompanying LICENSE file are met.                          |
    +--------------------------------------------------------------------+
    | Copyright (c) 2004-2014, <NAME> <<EMAIL>>            |
    +--------------------------------------------------------------------+
*/

#include "php_http_api.h"

static void message_headers(php_http_message_t *msg, php_http_buffer_t *str);

zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info TSRMLS_DC)
{
	php_http_message_t *old = *message;

	/* advance message */
	if (!old || old->type || zend_hash_num_elements(&old->hdrs)) {
		(*message) = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
		(*message)->parent = old;
		if (headers) {
			(*headers) = &((*message)->hdrs);
		}
	}

	if (info) {
		php_http_message_set_info(*message, info);
	}

	return old != *message;
}

php_http_message_t *php_http_message_init(php_http_message_t *message, php_http_message_type_t type, php_http_message_body_t *body TSRMLS_DC)
{
	if (!message) {
		message = emalloc(sizeof(*message));
	}
	memset(message, 0, sizeof(*message));
	TSRMLS_SET_CTX(message->ts);

	php_http_message_set_type(message, type);
	message->http.version.major = 1;
	message->http.version.minor = 1;
	zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0);
	message->body = body ? body : php_http_message_body_init(NULL, NULL TSRMLS_CC);

	return message;
}

php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_http_message_type_t type TSRMLS_DC)
{
	int free_msg = !message;
	zval *sval, tval;
	php_http_message_body_t *mbody;
	
	switch (type) {
		case PHP_HTTP_REQUEST:
			mbody = php_http_env_get_request_body(TSRMLS_C);
			php_http_message_body_addref(mbody);
			message = php_http_message_init(message, type, mbody TSRMLS_CC);
			if ((sval = php_http_env_get_server_var(ZEND_STRL("SERVER_PROTOCOL"), 1 TSRMLS_CC)) && !strncmp(Z_STRVAL_P(sval), "HTTP/", lenof("HTTP/"))) {
				php_http_version_parse(&message->http.version, Z_STRVAL_P(sval) TSRMLS_CC);
			}
			if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1 TSRMLS_CC))) {
				message->http.info.request.method = estrdup(Z_STRVAL_P(sval));
			}
			if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1 TSRMLS_CC))) {
				message->http.info.request.url = estrdup(Z_STRVAL_P(sval));
			}
			
			php_http_env_get_request_headers(&message->hdrs TSRMLS_CC);
			break;
			
		case PHP_HTTP_RESPONSE:
			message = php_http_message_init(NULL, type, NULL TSRMLS_CC);
			if (!SG(sapi_headers).http_status_line || !php_http_info_parse((php_http_info_t *) &message->http, SG(sapi_headers).http_status_line TSRMLS_CC)) {
				if (!(message->http.info.response.code = SG(sapi_headers).http_response_code)) {
					message->http.info.response.code = 200;
				}
				message->http.info.response.status = estrdup(php_http_env_get_response_status_for_code(message->http.info.response.code));
			}
			
			php_http_env_get_response_headers(&message->hdrs TSRMLS_CC);
#if PHP_VERSION_ID >= 50400
			if (php_output_get_level(TSRMLS_C)) {
				if (php_output_get_status(TSRMLS_C) & PHP_OUTPUT_SENT) {
					php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(TSRMLS_C), php_output_get_start_lineno(TSRMLS_C));
					goto error;
				} else if (SUCCESS != php_output_get_contents(&tval TSRMLS_CC)) {
					php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch response body");
					goto error;
				} else {
					php_http_message_body_append(message->body, Z_STRVAL(tval), Z_STRLEN(tval));
					zval_dtor(&tval);
				}
			}
#endif
			break;
			
		default:
		error:
			if (free_msg) {
				if (message) {
					php_http_message_free(&message);
				}
			} else {
				message = NULL;
			}
			break;
	}
	
	return message;
}

php_http_message_t *php_http_message_parse(php_http_message_t *msg, const char *str, size_t len, zend_bool greedy TSRMLS_DC)
{
	php_http_message_parser_t p;
	php_http_buffer_t buf;
	unsigned flags = PHP_HTTP_MESSAGE_PARSER_CLEANUP;
	int free_msg;

	php_http_buffer_from_string_ex(&buf, str, len);
	php_http_message_parser_init(&p TSRMLS_CC);

	if ((free_msg = !msg)) {
		msg = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
	}

	if (greedy) {
		flags |= PHP_HTTP_MESSAGE_PARSER_GREEDY;
	}
	if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse(&p, &buf, flags, &msg)) {
		if (free_msg) {
			php_http_message_free(&msg);
		}
		msg = NULL;
	}

	php_http_message_parser_dtor(&p);
	php_http_buffer_dtor(&buf);

	return msg;
}

zval *php_http_message_header(php_http_message_t *msg, const char *key_str, size_t key_len, int join)
{
	zval *ret = NULL, **header;
	char *key = php_http_pretty_key(estrndup(key_str, key_len), key_len, 1, 1);

	if (SUCCESS == zend_symtable_find(&msg->hdrs, key, key_len + 1, (void *) &header)) {
		if (join && Z_TYPE_PP(header) == IS_ARRAY) {
			TSRMLS_FETCH_FROM_CTX(msg->ts);

			ret = php_http_header_value_to_string(*header TSRMLS_CC);
		} else {
			Z_ADDREF_PP(header);
			ret = *header;
		}
	}

	efree(key);

	return ret;
}

zend_bool php_http_message_is_multipart(php_http_message_t *msg, char **boundary)<commit_msg>alloca<commit_after>/*
    +--------------------------------------------------------------------+
    | PECL :: http                                                       |
    +--------------------------------------------------------------------+
    | Redistribution and use in source and binary forms, with or without |
    | modification, are permitted provided that the conditions mentioned |
    | in the accompanying LICENSE file are met.                          |
    +--------------------------------------------------------------------+
    | Copyright (c) 2004-2014, <NAME> <<EMAIL>>            |
    +--------------------------------------------------------------------+
*/

#include "php_http_api.h"

static void message_headers(php_http_message_t *msg, php_http_buffer_t *str);

zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info TSRMLS_DC)
{
	php_http_message_t *old = *message;

	/* advance message */
	if (!old || old->type || zend_hash_num_elements(&old->hdrs)) {
		(*message) = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
		(*message)->parent = old;
		if (headers) {
			(*headers) = &((*message)->hdrs);
		}
	}

	if (info) {
		php_http_message_set_info(*message, info);
	}

	return old != *message;
}

php_http_message_t *php_http_message_init(php_http_message_t *message, php_http_message_type_t type, php_http_message_body_t *body TSRMLS_DC)
{
	if (!message) {
		message = emalloc(sizeof(*message));
	}
	memset(message, 0, sizeof(*message));
	TSRMLS_SET_CTX(message->ts);

	php_http_message_set_type(message, type);
	message->http.version.major = 1;
	message->http.version.minor = 1;
	zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0);
	message->body = body ? body : php_http_message_body_init(NULL, NULL TSRMLS_CC);

	return message;
}

php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_http_message_type_t type TSRMLS_DC)
{
	int free_msg = !message;
	zval *sval, tval;
	php_http_message_body_t *mbody;
	
	switch (type) {
		case PHP_HTTP_REQUEST:
			mbody = php_http_env_get_request_body(TSRMLS_C);
			php_http_message_body_addref(mbody);
			message = php_http_message_init(message, type, mbody TSRMLS_CC);
			if ((sval = php_http_env_get_server_var(ZEND_STRL("SERVER_PROTOCOL"), 1 TSRMLS_CC)) && !strncmp(Z_STRVAL_P(sval), "HTTP/", lenof("HTTP/"))) {
				php_http_version_parse(&message->http.version, Z_STRVAL_P(sval) TSRMLS_CC);
			}
			if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1 TSRMLS_CC))) {
				message->http.info.request.method = estrdup(Z_STRVAL_P(sval));
			}
			if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1 TSRMLS_CC))) {
				message->http.info.request.url = estrdup(Z_STRVAL_P(sval));
			}
			
			php_http_env_get_request_headers(&message->hdrs TSRMLS_CC);
			break;
			
		case PHP_HTTP_RESPONSE:
			message = php_http_message_init(NULL, type, NULL TSRMLS_CC);
			if (!SG(sapi_headers).http_status_line || !php_http_info_parse((php_http_info_t *) &message->http, SG(sapi_headers).http_status_line TSRMLS_CC)) {
				if (!(message->http.info.response.code = SG(sapi_headers).http_response_code)) {
					message->http.info.response.code = 200;
				}
				message->http.info.response.status = estrdup(php_http_env_get_response_status_for_code(message->http.info.response.code));
			}
			
			php_http_env_get_response_headers(&message->hdrs TSRMLS_CC);
#if PHP_VERSION_ID >= 50400
			if (php_output_get_level(TSRMLS_C)) {
				if (php_output_get_status(TSRMLS_C) & PHP_OUTPUT_SENT) {
					php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(TSRMLS_C), php_output_get_start_lineno(TSRMLS_C));
					goto error;
				} else if (SUCCESS != php_output_get_contents(&tval TSRMLS_CC)) {
					php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch response body");
					goto error;
				} else {
					php_http_message_body_append(message->body, Z_STRVAL(tval), Z_STRLEN(tval));
					zval_dtor(&tval);
				}
			}
#endif
			break;
			
		default:
		error:
			if (free_msg) {
				if (message) {
					php_http_message_free(&message);
				}
			} else {
				message = NULL;
			}
			break;
	}
	
	return message;
}

php_http_message_t *php_http_message_parse(php_http_message_t *msg, const char *str, size_t len, zend_bool greedy TSRMLS_DC)
{
	php_http_message_parser_t p;
	php_http_buffer_t buf;
	unsigned flags = PHP_HTTP_MESSAGE_PARSER_CLEANUP;
	int free_msg;

	php_http_buffer_from_string_ex(&buf, str, len);
	php_http_message_parser_init(&p TSRMLS_CC);

	if ((free_msg = !msg)) {
		msg = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
	}

	if (greedy) {
		flags |= PHP_HTTP_MESSAGE_PARSER_GREEDY;
	}
	if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse(&p, &buf, flags, &msg)) {
		if (free_msg) {
			php_http_message_free(&msg);
		}
		msg = NULL;
	}

	php_http_message_parser_dtor(&p);
	php_http_buffer_dtor(&buf);

	return msg;
}

zval *php_http_message_header(php_http_message_t *msg, const char *key_str, size_t key_len, int join)
{
	zval *ret = NULL, **header;
	char *key;
	ALLOCA_FLAG(free_key);

	key = do_alloca(key_len + 1, free_key);
	memcpy(key, key_str, key_len);
	key[key_len] = '\0';
	php_http_pretty_key(key, key_len, 1, 1);

	if (SUCCESS == zend_symtable_find(&msg->hdrs, key, key_len + 1, (void *) &header)) {
		if (join && Z_TYPE_PP(header) == IS_ARRAY) {
			TSRMLS_FETCH_FROM_CTX(msg->ts);

			ret = php_http_header_value_to_string(*header TSRMLS_CC);
		} else {
			Z_ADDREF_PP(header);
			ret = *header;
		}
	}

	free_alloca(key, free_key);

	return ret;
}

zend_bool php_http_message_is_multipart(php_http_message_t *msg, char **boundary)
{'fraction_non_alphanumeric': 0.16523341523341523, 'fraction_numerical': 0.005265005265005265, 'mean_word_length': 3.6104368932038833, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2596550', 'n_tokens_mistral': 4706, 'n_tokens_neox': 4241, 'n_words': 711}