|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-03-24 17:43 UTC] kontakt at beberlei dot de
Description: ------------ Currently HttpRequest send the message themselves. This way its very hard to use pecl_http in code where we want to mock http for testing. I would really like to see HttpRequest#send() removed, and instead having a class $response = HttpClient#send(HttpRequest $request) That way also all the response related methods (getResponse*) could be removed from the Request. These methods make not much sense on a Request class. Also the API is very inconsistent through them, since it makes a difference if you called Request#send() before calling them. If HttpClient could then also extend a HttpClientInterface, pecl_http could be the first PHP extension that you don't need to wrap in your code anymore :-) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 13 23:00:01 2025 UTC |
> First things first, very cool on this huge refactoring in such a short > time. Thank you for trying this out. No problem on not using the stubs :-) > > I reflection2php'ed this again. It gets sorted weird, so the diff is not > so nice, but its still interesting: That's because I introduced a usort() call on line 60. > The simple sending use-case looks much more intuitive than previous API. > Here are my remarks to your points: > > 1. I missed that, of course its necessary to attach a client to the pool. > I guess you would then call $client->setRequest($request) before attaching > it to the pool? Just working with the php stubs obviously made me miss some > facts about the requirements on the underlying resources (curl). Yes, a client (which is a curl_easy handle) needs a request, one-to-one. > 3. Speaking "low" level the client is now the one holding the curl > (multi-)resource, and the request is a data container for all the > configuration of how to use the curl resource? The client has a curl_easy handle and the pool has a curl_multi handle and the request is just an HTTP message, i.e. protocol, URL, method, headers and body. Proxy, auth, persistent cookie, etc. configuration is still done on the client. > If yes, then for a pool to successfully send multiple requests, you need > to have one client per request? (one to one relationship)? Exactly. > This obviously is a problem for the API. While the API for single requests > now very nicely decouples sending from request build, the API with Pools > gets a bit confusing, especially the setRequest+"attaching clients instead > of requests" doesnt feel obvious: Yeah, I know, it's still quite clunky. > Personally, if something like the following would be possible that would > be awesome: > > <?php > > $factory = new http\Client\Factory; > $client = $factory->createClient(); > $request1 = new http\Client\Request("GET", "http://php.net"); > $request2 = new http\Client\Request("GET", "http://php.net"); > $pool = $client->createPool($request1, $request2); // Its a pool for this > particular type of $client not on $factory > $pool->send(); > > // wait for finish, now request has last response again. > $response1 = $request1->getResponseMessage(); > $response2 = $request2->getResponseMessage(); Wait… you told me off because of that the last time :) > At the moment the client has information that doesnt allow this, as you > cant just "clone" the Client for each new request to get a new curl handle. I concur, you can. Clone should work on the client as well as on the request. > For this above API to work, cookies and observers would need to be moved to > the Request. Then whenever a new request is attached to $pool, it would > clone a new client for it and keep internally. Does this make sense? The > history would have to be kept on the one $client object though. > > Is this even possible? Looking at the C code it seems whenever Client > accesses Observers or Cookies it could also do that using the request. Hardly, looks like lots of very hard refactoring. > To have multiple curl resources then the client probably has to be "dumb" > as well and create new curl resources on "send" being called, like this > following PHP pseudo code: > > <?php > class CurlClient > { > public function send(Request $request); > { > $curlResource = new InternalCurlObject(); > $curlResource->setOptions($this->options); > > $curlResource->sendStartEvent($request->observers); > $response = $curlResource->send($request); > $curlResource->sendEndEvent($request->observers); > > return $reponse; > } > } But some piece in the puzzle has to be intelligent, previously it was the Request itself, which was some kind of a wunderwuzi, now the request is just the relevant HTTP information and payload and the client is the source of power. I'm quite happy with the outcome of the refactoring, though I suppose some configuration might still be better put into the request. Also keep in mind that there are a lot more "events" than start and finish there are lots of progress notifications in between, which are forwarded from curl. > Some remarks i found while playing with the code: > > * $client1 = $factory->createClient(); segfaults $client1 = > $factory->createClient(array()); does not. Fixed. > * Extending the Abstractclient (AbstractPool) should be forbidden for > userland code, "own" implementations segfaults on usage. Yeah, still have to look into this one. I'm not sure about the naming yet and how to prevent user interception. Thank you.