Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes #40

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
],
"require":{
"php": ">=5.6.0",
"ext-mbstring": "*"
"ext-mbstring": "*",
"ralouphie/getallheaders": "^3.0"
},
"require-dev":{
"phpunit/phpunit": "^5.2 || ^6.0 || ^7.0",
Expand Down
56 changes: 44 additions & 12 deletions src/Converters/Globals.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,62 @@
class Globals
{
/**
* @param bool|resource $input
* @param resource|null $input
*
* @return StreamedPart
*/
public static function convert($input = STDIN)
public static function convert($input = null)
{
$stream = fopen('php://temp', 'rw');

foreach ($_SERVER as $key => $value) {
if (0 === strpos($key, 'HTTP_')) {
$key = str_replace('_', '-', strtolower(substr($key, 5)));
fwrite($stream, "$key: $value\r\n");
} elseif (in_array($key, ['CONTENT_LENGTH', 'CONTENT_MD5', 'CONTENT_TYPE'])) {
$key = str_replace('_', '-', strtolower($key));
fwrite($stream, "$key: $value\r\n");
}
}
self::writeRequestHeadersToStream($stream);

fwrite($stream, "\r\n");

stream_copy_to_stream($input, $stream);
$inputStream = self::getInputStreamByInput($input);

stream_copy_to_stream($inputStream, $stream);
rewind($stream);

return new StreamedPart($stream);
}

static private function writeRequestHeadersToStream($stream)
{
$headers = getallheaders();

foreach ($headers as $key => $value) {
fwrite($stream, "$key: $value\r\n");
}
}

/**
* @param resource|null $input
* @return resource
*/
static private function getInputStreamByInput($input = null)
{
if (is_resource($input)) {
return $input;
}

return self::getDefaultStream();
}

static private function getDefaultStream()
{
$filename = self::getDefaultRequestStreamFilename();

return fopen($filename, 'r');
}

static private function getDefaultRequestStreamFilename()
{
return self::isCliSapi() ? 'php://stdin' : 'php://input';
}

static private function isCliSapi()
{
return strpos(PHP_SAPI, 'cli') !== false;
}
}