Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #27814. Construct HTTP request headers into smart_str #2040

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use a bit more meaningful variable name.
  • Loading branch information
Ville Hukkamäki committed Aug 3, 2016
commit f60083c30ac2c7afa057066d132bc4d56f93fc7a
38 changes: 19 additions & 19 deletions ext/standard/http_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
php_stream_filter *transfer_encoding = NULL;
int response_code;
zend_array *symbol_table;
smart_str shdr = {0};
smart_str header_buf = {0};

ZVAL_UNDEF(&response_header);
tmp_line[0] = '\0';
Expand Down Expand Up @@ -424,7 +424,7 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
strlcat(scratch, " HTTP/1.0\r\n", scratch_len);
}

smart_str_appends(&shdr,scratch);
smart_str_appends(&header_buf,scratch);

if (context && (tmpzval = php_stream_context_get_option(context, "http", "header")) != NULL) {
tmp = NULL;
Expand Down Expand Up @@ -554,7 +554,7 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
stmp = php_base64_encode((unsigned char*)scratch, strlen(scratch));

if (snprintf(scratch, scratch_len, "Authorization: Basic %s\r\n", ZSTR_VAL(stmp)) > 0) {
smart_str_appends(&shdr, scratch);
smart_str_appends(&header_buf, scratch);
php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_REQUIRED, NULL, 0);
}

Expand All @@ -564,18 +564,18 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
/* if the user has configured who they are, send a From: line */
if (((have_header & HTTP_HEADER_FROM) == 0) && FG(from_address)) {
if (snprintf(scratch, scratch_len, "From: %s\r\n", FG(from_address)) > 0)
smart_str_appends(&shdr, scratch);
smart_str_appends(&header_buf, scratch);
}

/* Send Host: header so name-based virtual hosts work */
if ((have_header & HTTP_HEADER_HOST) == 0) {
if ((use_ssl && resource->port != 443 && resource->port != 0) ||
(!use_ssl && resource->port != 80 && resource->port != 0)) {
if (snprintf(scratch, scratch_len, "Host: %s:%i\r\n", resource->host, resource->port) > 0)
smart_str_appends(&shdr, scratch);
smart_str_appends(&header_buf, scratch);
} else {
if (snprintf(scratch, scratch_len, "Host: %s\r\n", resource->host) > 0) {
smart_str_appends(&shdr, scratch);
smart_str_appends(&header_buf, scratch);
}
}
}
Expand All @@ -587,7 +587,7 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
* HTTP/1.0 to avoid issues when the server respond with a HTTP/1.1
* keep-alive response, which is the preferred response type. */
if ((have_header & HTTP_HEADER_CONNECTION) == 0) {
smart_str_appends(&shdr,"Connection: close\r\n");
smart_str_appends(&header_buf,"Connection: close\r\n");
}

if (context &&
Expand All @@ -610,7 +610,7 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
ua = emalloc(ua_len + 1);
if ((ua_len = slprintf(ua, ua_len, _UA_HEADER, ua_str)) > 0) {
ua[ua_len] = 0;
smart_str_appendl(&shdr, ua, ua_len);
smart_str_appendl(&header_buf, ua, ua_len);
} else {
php_error_docref(NULL, E_WARNING, "Cannot construct User-agent header");
}
Expand All @@ -630,12 +630,12 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval) > 0
) {
scratch_len = slprintf(scratch, scratch_len, "Content-Length: %zd\r\n", Z_STRLEN_P(tmpzval));
smart_str_appendl(&shdr, scratch, scratch_len);
smart_str_appendl(&header_buf, scratch, scratch_len);
have_header |= HTTP_HEADER_CONTENT_LENGTH;
}

smart_str_appends(&shdr, user_headers);
smart_str_appends(&shdr, "\r\n");
smart_str_appends(&header_buf, user_headers);
smart_str_appends(&header_buf, "\r\n");
efree(user_headers);
}

Expand All @@ -645,21 +645,21 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval) > 0) {
if (!(have_header & HTTP_HEADER_CONTENT_LENGTH)) {
scratch_len = slprintf(scratch, scratch_len, "Content-Length: %zd\r\n", Z_STRLEN_P(tmpzval));
smart_str_appendl(&shdr, scratch, scratch_len);
smart_str_appendl(&header_buf, scratch, scratch_len);
}
if (!(have_header & HTTP_HEADER_TYPE)) {
smart_str_appends(&shdr, "Content-Type: application/x-www-form-urlencoded\r\n");
smart_str_appends(&header_buf, "Content-Type: application/x-www-form-urlencoded\r\n");
php_error_docref(NULL, E_NOTICE, "Content-type not specified assuming application/x-www-form-urlencoded");
}
smart_str_appends(&shdr, "\r\n");
smart_str_appendl(&shdr, Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval));
smart_str_appends(&header_buf, "\r\n");
smart_str_appendl(&header_buf, Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval));
} else {
smart_str_appends(&shdr, "\r\n");
smart_str_appends(&header_buf, "\r\n");
}

// Send request
/* send it */

php_stream_write(stream, ZSTR_VAL(shdr.s), ZSTR_LEN(shdr.s));
php_stream_write(stream, ZSTR_VAL(header_buf.s), ZSTR_LEN(header_buf.s));

location[0] = '\0';

Expand Down Expand Up @@ -897,7 +897,7 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
}
out:

smart_str_free(&shdr);
smart_str_free(&header_buf);

if (protocol_version) {
efree(protocol_version);
Expand Down