php-http / message

HTTP Message related tools
http://php-http.org
MIT License
1.29k stars 41 forks source link

Add Message cloner #49

Closed joelwurtz closed 8 years ago

joelwurtz commented 8 years ago
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets mentioned in #46
Documentation Does the phpdoc is enough ?
License MIT

What's in this PR?

Add a class to copy a request including it's body

Why?

IMO cloning a request with its stream his something that is shared by some concepts (like logging, caching, ....)

Example Usage

$cloner = new MessageCloner();

$clonedResponse = $cloner->cloneMessage($response);

// If original stream is not seekable, cloned it a second time to ensure that we can read it
if (!$response->getBody()->isSeekable()) {
    $response = $cloner->cloneMessage($clonedResponse);
}

Checklist

sagikazarmark commented 8 years ago

What about memory usage in case of huge bodies? We should add a warning somewhere about that.

joelwurtz commented 8 years ago

It's in the header of the MemoryClonedStream, by default it use a php://temp stream with a 2mb memory buffer, so the memory footprint of a cloned stream will, by default, have a max memory size of 2mb, when the body is higher, all data over the limit is written into a file.

Nyholm commented 8 years ago

I like this. It could help on all the places we do some ugly hack to rewind streams and make sure they are seekable.

@joelwurtz could you address the comments?

joelwurtz commented 8 years ago

Finally i'm not a fan of this implementation, message cloner is useless and the cloned stream enforce stream reading where i'm not a fan, will do a better implementation for the wanted use case.