Skip to content

Commit 0b48753

Browse files
author
Jani Taskinen
committed
MFH: - Fixed Bug #45092header HTTP context option not being used (--with-curlwrappers)
1 parent 6611eb0 commit 0b48753

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed

ext/curl/streams.c

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "ext/standard/php_smart_str.h"
4949
#include "ext/standard/info.h"
5050
#include "ext/standard/file.h"
51+
#include "ext/standard/php_string.h"
5152
#include "php_curl.h"
5253

5354
static size_t on_data_available(char *data, size_t size, size_t nmemb, void *ctx)
@@ -263,6 +264,7 @@ php_stream *php_curl_stream_opener(php_stream_wrapper *wrapper, char *filename,
263264
php_stream *stream;
264265
php_curl_stream *curlstream;
265266
zval *tmp, **ctx_opt = NULL;
267+
struct curl_slist *slist = NULL;
266268

267269
curlstream = emalloc(sizeof(php_curl_stream));
268270
memset(curlstream, 0, sizeof(php_curl_stream));
@@ -311,6 +313,15 @@ php_stream *php_curl_stream_opener(php_stream_wrapper *wrapper, char *filename,
311313

312314
/* TODO: read cookies and options from context */
313315
if (context && !strncasecmp(filename, "http", sizeof("http")-1)) {
316+
/* Protocol version */
317+
if (SUCCESS == php_stream_context_get_option(context, "http", "protocol_version", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_DOUBLE) {
318+
if (Z_DVAL_PP(ctx_opt) == 1.1) {
319+
curl_easy_setopt(curlstream->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
320+
} else {
321+
curl_easy_setopt(curlstream->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
322+
}
323+
}
324+
314325
if (SUCCESS == php_stream_context_get_option(context, "http", "curl_verify_ssl_host", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_BOOL && Z_LVAL_PP(ctx_opt) == 1) {
315326
curl_easy_setopt(curlstream->curl, CURLOPT_SSL_VERIFYHOST, 1);
316327
} else {
@@ -326,20 +337,34 @@ php_stream *php_curl_stream_opener(php_stream_wrapper *wrapper, char *filename,
326337
if (SUCCESS == php_stream_context_get_option(context, "http", "user_agent", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_STRING) {
327338
curl_easy_setopt(curlstream->curl, CURLOPT_USERAGENT, Z_STRVAL_PP(ctx_opt));
328339
}
329-
if (SUCCESS == php_stream_context_get_option(context, "http", "header", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_ARRAY) {
330-
HashPosition pos;
331-
zval **header = NULL;
332-
struct curl_slist *hl = NULL;
340+
if (SUCCESS == php_stream_context_get_option(context, "http", "header", &ctx_opt)) {
341+
if (Z_TYPE_PP(ctx_opt) == IS_ARRAY) {
342+
HashPosition pos;
343+
zval **header = NULL;
333344

334-
for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(ctx_opt), &pos);
335-
SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(ctx_opt), (void *)&header, &pos);
336-
zend_hash_move_forward_ex(Z_ARRVAL_PP(ctx_opt), &pos)) {
337-
if (Z_TYPE_PP(header) == IS_STRING) {
338-
hl = curl_slist_append(hl, Z_STRVAL_PP(header));
345+
for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(ctx_opt), &pos);
346+
SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(ctx_opt), (void *)&header, &pos);
347+
zend_hash_move_forward_ex(Z_ARRVAL_PP(ctx_opt), &pos)
348+
) {
349+
if (Z_TYPE_PP(header) == IS_STRING) {
350+
slist = curl_slist_append(slist, Z_STRVAL_PP(header));
351+
}
352+
}
353+
} else if (Z_TYPE_PP(ctx_opt) == IS_STRING && Z_STRLEN_PP(ctx_opt)) {
354+
char *p, *token, *trimmed, *copy_ctx_opt;
355+
356+
copy_ctx_opt = php_trim(Z_STRVAL_PP(ctx_opt), Z_STRLEN_PP(ctx_opt), NULL, 0, NULL, 3 TSRMLS_CC);
357+
p = php_strtok_r(copy_ctx_opt, "\r\n", &token);
358+
while (p) {
359+
trimmed = php_trim(p, strlen(p), NULL, 0, NULL, 3 TSRMLS_CC);
360+
slist = curl_slist_append(slist, trimmed);
361+
efree(trimmed);
362+
p = php_strtok_r(NULL, "\r\n", &token);
339363
}
364+
efree(copy_ctx_opt);
340365
}
341-
if (hl) {
342-
curl_easy_setopt(curlstream->curl, CURLOPT_HTTPHEADER, hl);
366+
if (slist) {
367+
curl_easy_setopt(curlstream->curl, CURLOPT_HTTPHEADER, slist);
343368
}
344369
}
345370
if (SUCCESS == php_stream_context_get_option(context, "http", "method", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_STRING) {
@@ -472,7 +497,9 @@ php_stream *php_curl_stream_opener(php_stream_wrapper *wrapper, char *filename,
472497
return NULL;
473498
}
474499
}
475-
500+
if (slist) {
501+
curl_slist_free_all(slist);
502+
}
476503
return stream;
477504
}
478505

ext/standard/http_fopen_wrapper.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,31 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, char *path,
330330
/* send it */
331331
php_stream_write(stream, scratch, strlen(scratch));
332332

333-
if (context &&
334-
php_stream_context_get_option(context, "http", "header", &tmpzval) == SUCCESS &&
335-
Z_TYPE_PP(tmpzval) == IS_STRING && Z_STRLEN_PP(tmpzval)) {
336-
/* Remove newlines and spaces from start and end,
337-
php_trim will estrndup() */
338-
tmp = php_trim(Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval), NULL, 0, NULL, 3 TSRMLS_CC);
339-
if (strlen(tmp) > 0) {
333+
if (context && php_stream_context_get_option(context, "http", "header", &tmpzval) == SUCCESS) {
334+
tmp = NULL;
335+
336+
if (Z_TYPE_PP(tmpzval) == IS_ARRAY) {
337+
HashPosition pos;
338+
zval **tmpheader = NULL;
339+
smart_str tmpstr = {0};
340+
341+
for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(tmpzval), &pos);
342+
SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(tmpzval), (void *)&tmpheader, &pos);
343+
zend_hash_move_forward_ex(Z_ARRVAL_PP(tmpzval), &pos)
344+
) {
345+
if (Z_TYPE_PP(tmpheader) == IS_STRING) {
346+
smart_str_appendl(&tmpstr, Z_STRVAL_PP(tmpheader), Z_STRLEN_PP(tmpheader));
347+
smart_str_appendl(&tmpstr, "\r\n", sizeof("\r\n") - 1);
348+
}
349+
}
350+
smart_str_0(&tmpstr);
351+
tmp = tmpstr.c;
352+
}
353+
if (Z_TYPE_PP(tmpzval) == IS_STRING && Z_STRLEN_PP(tmpzval)) {
354+
/* Remove newlines and spaces from start and end php_trim will estrndup() */
355+
tmp = php_trim(Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval), NULL, 0, NULL, 3 TSRMLS_CC);
356+
}
357+
if (tmp && strlen(tmp) > 0) {
340358
if (!header_init) { /* Remove post headers for redirects */
341359
int l = strlen(tmp);
342360
char *s, *s2, *tmp_c = estrdup(tmp);

0 commit comments

Comments
 (0)