Commit f84f307d authored by Alejandro Garza's avatar Alejandro Garza

Fixes bugs from PR 484; fix Http adapter for extraction requests

parent aa463671
...@@ -111,13 +111,30 @@ class Http extends Configurable implements AdapterInterface ...@@ -111,13 +111,30 @@ class Http extends Configurable implements AdapterInterface
if ($method == Request::METHOD_POST) { if ($method == Request::METHOD_POST) {
if ($request->getFileUpload()) { if ($request->getFileUpload()) {
$boundary = '----------' . md5(time());
$CRLF = "\r\n";
$file = $request->getFileUpload();
// Add the proper boundary to the Content-Type header
$headers = $request->getHeaders();
// Remove the Content-Type header, because we will replace it with something else.
if (($key = array_search("Content-Type: multipart/form-data", $headers)) !== false) {
unset($headers[$key]);
}
$request->setHeaders($headers);
$request->addHeader("Content-Type: multipart/form-data; boundary={$boundary}");
$data = "--{$boundary}" . $CRLF;
$data .= 'Content-Disposition: form-data; name="upload"; filename=' . $file . $CRLF;
$data .= 'Content-Type: application/octet-stream' . $CRLF . $CRLF;
$data .= file_get_contents($file) . $CRLF;
$data .= '--' . $boundary . '--';
$content_length = strlen($data);
$request->addHeader("Content-Length: $content_length\r\n");
stream_context_set_option( stream_context_set_option(
$context, $context,
'http', 'http',
'content', 'content',
file_get_contents($request->getFileUpload()) $data
); );
$request->addHeader('Content-Type: multipart/form-data');
} else { } else {
$data = $request->getRawData(); $data = $request->getRawData();
if (null !== $data) { if (null !== $data) {
......
...@@ -184,16 +184,20 @@ class HttpTest extends \PHPUnit_Framework_TestCase ...@@ -184,16 +184,20 @@ class HttpTest extends \PHPUnit_Framework_TestCase
$context = $this->adapter->createContext($request, $endpoint); $context = $this->adapter->createContext($request, $endpoint);
// Remove content from comparison, since we can't determine the
// random boundary string.
$stream_context_get_options = stream_context_get_options($context);
unset($stream_context_get_options['http']['content']);
unset($stream_context_get_options['http']['header']);
$this->assertEquals( $this->assertEquals(
array( array(
'http' => array( 'http' => array(
'method' => $method, 'method' => $method,
'timeout' => $timeout, 'timeout' => $timeout,
'content' => file_get_contents(__FILE__),
'header' => 'Content-Type: multipart/form-data',
) )
), ),
stream_context_get_options($context) $stream_context_get_options
); );
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment