48
48
#include "ext/standard/php_smart_str.h"
49
49
#include "ext/standard/info.h"
50
50
#include "ext/standard/file.h"
51
+ #include "ext/standard/php_string.h"
51
52
#include "php_curl.h"
52
53
53
54
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,
263
264
php_stream * stream ;
264
265
php_curl_stream * curlstream ;
265
266
zval * tmp , * * ctx_opt = NULL ;
267
+ struct curl_slist * slist = NULL ;
266
268
267
269
curlstream = emalloc (sizeof (php_curl_stream ));
268
270
memset (curlstream , 0 , sizeof (php_curl_stream ));
@@ -311,6 +313,15 @@ php_stream *php_curl_stream_opener(php_stream_wrapper *wrapper, char *filename,
311
313
312
314
/* TODO: read cookies and options from context */
313
315
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
+
314
325
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 ) {
315
326
curl_easy_setopt (curlstream -> curl , CURLOPT_SSL_VERIFYHOST , 1 );
316
327
} else {
@@ -326,20 +337,34 @@ php_stream *php_curl_stream_opener(php_stream_wrapper *wrapper, char *filename,
326
337
if (SUCCESS == php_stream_context_get_option (context , "http" , "user_agent" , & ctx_opt ) && Z_TYPE_PP (ctx_opt ) == IS_STRING ) {
327
338
curl_easy_setopt (curlstream -> curl , CURLOPT_USERAGENT , Z_STRVAL_PP (ctx_opt ));
328
339
}
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 ;
333
344
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 );
339
363
}
364
+ efree (copy_ctx_opt );
340
365
}
341
- if (hl ) {
342
- curl_easy_setopt (curlstream -> curl , CURLOPT_HTTPHEADER , hl );
366
+ if (slist ) {
367
+ curl_easy_setopt (curlstream -> curl , CURLOPT_HTTPHEADER , slist );
343
368
}
344
369
}
345
370
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,
472
497
return NULL ;
473
498
}
474
499
}
475
-
500
+ if (slist ) {
501
+ curl_slist_free_all (slist );
502
+ }
476
503
return stream ;
477
504
}
478
505
0 commit comments