rfc:curl_http2_push

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:curl_http2_push [2015/10/01 09:10]
davey
rfc:curl_http2_push [2017/01/12 18:43]
cmb Fix typo
Line 3: Line 3:
   * Date: 2015-10-01   * Date: 2015-10-01
   * Author: Davey Shafik, davey@php.net   * Author: Davey Shafik, davey@php.net
-  * Status: Draft+  * Status: Implemented (PHP 7.1)
   * First Published at: https://wiki.php.net/rfc/curl_http2_push   * First Published at: https://wiki.php.net/rfc/curl_http2_push
  
Line 12: Line 12:
 Server push allows the server to push additional resources relevant to the requested resource directly to the client proactively. Server push allows the server to push additional resources relevant to the requested resource directly to the client proactively.
  
-Server push is available in libcurl since 7.44.0.+Server push is available in libcurl since 7.44.0, but broken till the as-yet-unreleased 7.46.0 (See: [[https://github.com/bagder/curl/issues/529|this]] and [[https://github.com/bagder/curl/issues/530|this]] github issues for details).
  
 ===== Libcurl Implementation ===== ===== Libcurl Implementation =====
Line 28: Line 28:
 <code php> <code php>
 <?php <?php
-$callable = function ($push_ch, $parent_ch, $header_count, $headers&$user_data) +$transfers = 1; 
-+ 
-      if ($header_count > 0 && curl_pushheader_byname("Content-Type") !== 'application/json') { +$callback = function($parent_ch, $pushed_charray $headers) use (&$transfers) { 
-           return CURL_PUSH_DENY+ $transfers++; // increment to keep track of the number of concurrent requests 
-      } + return CURL_PUSH_OK
-      return CURL_PUSH_OK; +};
-}+
  
 $mh = curl_multi_init(); $mh = curl_multi_init();
-$user_data = null; 
  
-curl_multi_setopt($mh, CURLMOPT_PUSHFUNCTION$callable); +curl_multi_setopt($mh, CURLMOPT_PIPELININGCURLPIPE_MULTIPLEX); 
-curl_multi_setopt($mh, CURLMOPT_PUSHDATA&$user_data); // Not sure this is possibleI assume C-land can do this +curl_multi_setopt($mh, CURLMOPT_PUSHFUNCTION, $callback); 
-?>+ 
 +$ch = curl_init(); 
 +curl_setopt($ch, CURLOPT_URL, "https://localhost:8080/index.html"); 
 +curl_setopt($chCURLOPT_HTTP_VERSION, 3); 
 +curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 + 
 +// Debugging/Local stuff 
 +//curl_setopt($ch, CURLOPT_VERBOSE, 1); // will output curl debugging information 
 +curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // self-signed cert 
 +curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // self-signed cert 
 + 
 +curl_multi_add_handle($mh, $ch); 
 + 
 +$active = null; 
 +do { 
 +    $status = curl_multi_exec($mh, $active); 
 + 
 +    do { 
 +        $info = curl_multi_info_read($mh); 
 +        if (false !== $info && $info['msg'] == CURLMSG_DONE) { 
 +            $handle = $info['handle']; 
 +            if ($handle !== null) { 
 +                $transfers--; // decrement remaining requests 
 + $out = curl_multi_getcontent($info['handle']); // Response body 
 +                curl_multi_remove_handle($mh, $handle); 
 +                curl_close($handle); 
 +            } 
 +        } 
 +    } while ($info); 
 +} while ($transfers); 
 + 
 +curl_multi_close($mh);
 </code> </code>
  
 ===== Implementation ===== ===== Implementation =====
  
-Within [[https://github.com/php/php-src/blob/master/ext/curl/multi.c#L409|the PHP implementation of curl_multi_setopt()]] (referenced as ''php\curl_multi_setopt()'') there is a switch that handles different options. When ''CURLMOPT_PUSHFUNCTION'' is set a callback is registered that will call the callable set in ''php\curl_multi_setopt()''.+Within [[https://github.com/php/php-src/blob/master/ext/curl/multi.c#L409|the PHP implementation of curl_multi_setopt()]] (referenced as ''php\curl_multi_setopt()'') there is a switch that handles different options. When ''CURLMOPT_PUSHFUNCTION'' is set a callback is registered that will call the callable set in ''php\curl_multi_setopt()'' (and passed to the C callback using curls ''CURLMOPT_PUSHDATA'').
  
-The user can also register a referenced variable (''$user_data'') using ''CURLMOPT_PUSHDATA''InternallyIt will pass through a pointer to the zval for the ''$user_data'' argument to ''curl\curl_multi_setopt()'', which will be passed to the callback registered above, and passed on to the userland callback.+We do not support ''CURLMOPT_PUSHDATA''Instead, the user can use closures and ''use'' with references to replicate this behavior.
  
-We also have to handle the ''$headers'' argument, which I believe will have to be a resource for it to work — this will then be passed into two new libcurl functions ''curl_pushheader_bynum()'' and ''curl_pushheader_byname()''.+libcurl exposes the push headers with two functions functions ''curl_pushheader_bynum()'' and ''curl_pushheader_byname()''. We can use ''curl_pushheader_bynum()'' to create an array of header lines. These would then be parsed in user land (as with regular request headers).
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
  
-No breaks, except possibly the referenced value for ''php\curl_multi_setopt()''.+No breaks
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 77: Line 106:
  
   * ''CURLMOPT_PUSHFUNCTION''   * ''CURLMOPT_PUSHFUNCTION''
-  * ''CURLMOPT_PUSHDATA'' 
   * ''CURL_PUSH_OK''   * ''CURL_PUSH_OK''
   * ''CURL_PUSH_DENY''   * ''CURL_PUSH_DENY''
Line 89: Line 117:
 ===== Open Issues ===== ===== Open Issues =====
  
-  - Adding new resource isn't desirable but fits the current ext/curl API+Possibly memory leak in libcurl
  
 ===== Future Scope ===== ===== Future Scope =====
Line 95: Line 123:
 This change should track libcurl. This change should track libcurl.
  
-===== Proposed Voting Choices =====+===== Vote =====
  
-Two choices: Add, Deny +Simple Yes/No option. Requires 50%+1 to be accepted. 
-50% required as this is not a language change+ 
 +This vote will close on 13:00 UTC on Wed 2015-12-23 
 + 
 +<doodle title="Add HTTP/2 Server Push Support to ext/curl" auth="davey" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
  
-Currently working on a patch, a mentor would be appreciated!+  - Working patch can be found [[https://github.com/dshafik/php-src/compare/curl-http2-push|here]] 
 +  - Docker container for easy testing can be found [[http://github.com/dshafik/php-http2-push-example|here]]
  
 ===== Implementation ===== ===== Implementation =====
  
-After the project is implemented, this section should contain  +Server push support has been implemented in PHP 7.1 with [[http://git.php.net/?p=php-src.git;a=commit;h=ad15e1ccdabc678103e356535919e829ba9a0281|commit ad15e1cc]], and is documented in the [[http://php.net/manual/function.curl-multi-setopt.php|curl_multi_setopt man page]]. 
-  - the version(s) it was merged to + 
-  - a link to the git commit(s) +The language specification is not affected by this RFC.
-  - a link to the PHP manual entry for the feature+
  
 ===== References ===== ===== References =====
  
   - [[https://github.com/bagder/curl/wiki/HTTP-2-Server-Push|libcurl HTTP/2 implementation]]   - [[https://github.com/bagder/curl/wiki/HTTP-2-Server-Push|libcurl HTTP/2 implementation]]
-  - [[http://daniel.haxx.se/blog/2015/06/03/server-push-to-curl/|Authors blog post about the implementation]]+  - [[http://daniel.haxx.se/blog/2015/06/03/server-push-to-curl/|libcurl creators blog post about the implementation]]
  
 ===== Rejected Features ===== ===== Rejected Features =====
  
-Keep this updated with features that were discussed on the mail lists.+None
rfc/curl_http2_push.txt · Last modified: 2017/09/22 13:28 (external edit)