-
-
Notifications
You must be signed in to change notification settings - Fork 145
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
Multipart handling #13
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a2bad36
Rename RequestHeaderParser to RequestParser
onigoetz 2445430
Added POST and Multipart handling
onigoetz f696c9c
Merge branch 'master' of github.com:reactphp/http
onigoetz 8213ff5
Merge remote-tracking branch 'source/master'
onigoetz 5bea2e7
Do not write file uploads to files, keep in memory
onigoetz a3cf90a
Added Todo for the future
onigoetz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
<?php | ||
|
||
namespace React\Http; | ||
|
||
/** | ||
* Parse a multipart body | ||
* | ||
* Original source is from https://gist.github.com/jas-/5c3fdc26fedd11cb9fb5 | ||
* | ||
* @author [email protected] | ||
* @author [email protected] | ||
* @license http://www.gnu.org/licenses/gpl.html GPL License 3 | ||
*/ | ||
class MultipartParser | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $input; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $boundary; | ||
|
||
/** | ||
* Contains the resolved posts | ||
* | ||
* @var array | ||
*/ | ||
protected $post = []; | ||
|
||
/** | ||
* Contains the resolved files | ||
* | ||
* @var array | ||
*/ | ||
protected $files = []; | ||
|
||
/** | ||
* @param $input | ||
* @param $boundary | ||
*/ | ||
public function __construct($input, $boundary) | ||
{ | ||
$this->input = $input; | ||
$this->boundary = $boundary; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getPost() | ||
{ | ||
return $this->post; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getFiles() | ||
{ | ||
return $this->files; | ||
} | ||
|
||
/** | ||
* Do the actual parsing | ||
*/ | ||
public function parse() | ||
{ | ||
$blocks = $this->split($this->boundary); | ||
|
||
foreach ($blocks as $value) { | ||
if (empty($value)) { | ||
continue; | ||
} | ||
|
||
$this->parseBlock($value); | ||
} | ||
} | ||
|
||
/** | ||
* @param $boundary string | ||
* @returns Array | ||
*/ | ||
protected function split($boundary) | ||
{ | ||
$boundary = preg_quote($boundary); | ||
$result = preg_split("/\\-+$boundary/", $this->input); | ||
array_pop($result); | ||
return $result; | ||
} | ||
|
||
/** | ||
* Decide if we handle a file, post value or octet stream | ||
* | ||
* @param $string string | ||
* @returns void | ||
*/ | ||
protected function parseBlock($string) | ||
{ | ||
if (strpos($string, 'filename') !== false) { | ||
$this->file($string); | ||
return; | ||
} | ||
|
||
// This may never be called, if an octet stream | ||
// has a filename it is catched by the previous | ||
// condition already. | ||
if (strpos($string, 'application/octet-stream') !== false) { | ||
$this->octetStream($string); | ||
return; | ||
} | ||
|
||
$this->post($string); | ||
} | ||
|
||
/** | ||
* Parse a raw octet stream | ||
* | ||
* @param $string | ||
* @return array | ||
*/ | ||
protected function octetStream($string) | ||
{ | ||
preg_match('/name=\"([^\"]*)\".*stream[\n|\r]+([^\n\r].*)?$/s', $string, $match); | ||
|
||
$this->addResolved('post', $match[1], $match[2]); | ||
} | ||
|
||
/** | ||
* Parse a file | ||
* | ||
* @param $string | ||
* @return array | ||
*/ | ||
protected function file($string) | ||
{ | ||
preg_match('/name=\"([^\"]*)\"; filename=\"([^\"]*)\"[\n|\r]+([^\n\r].*)?\r$/s', $string, $match); | ||
preg_match('/Content-Type: (.*)?/', $match[3], $mime); | ||
|
||
$content = preg_replace('/Content-Type: (.*)[^\n\r]/', '', $match[3]); | ||
$content = ltrim($content, "\r\n"); | ||
|
||
$path = tempnam(sys_get_temp_dir(), "php"); | ||
$err = file_put_contents($path, $content); | ||
|
||
$data = [ | ||
'name' => $match[2], | ||
'type' => trim($mime[1]), | ||
'tmp_name' => $path, | ||
'error' => ($err === false) ? UPLOAD_ERR_NO_FILE : UPLOAD_ERR_OK, | ||
'size' => filesize($path), | ||
]; | ||
|
||
$this->addResolved('files', $match[1], $data); | ||
} | ||
|
||
/** | ||
* Parse POST values | ||
* | ||
* @param $string | ||
* @return array | ||
*/ | ||
protected function post($string) | ||
{ | ||
preg_match('/name=\"([^\"]*)\"[\n|\r]+([^\n\r].*)?\r$/s', $string, $match); | ||
|
||
$this->addResolved('post', $match[1], $match[2]); | ||
} | ||
|
||
/** | ||
* Put the file or post where it belongs, | ||
* The key names can be simple, or containing [] | ||
* it can also be a named key | ||
* | ||
* @param $type | ||
* @param $key | ||
* @param $content | ||
*/ | ||
protected function addResolved($type, $key, $content) | ||
{ | ||
if (preg_match('/^(.*)\[(.*)\]$/i', $key, $tmp)) { | ||
if (!empty($tmp[2])) { | ||
$this->{$type}[$tmp[1]][$tmp[2]] = $content; | ||
} else { | ||
$this->{$type}[$tmp[1]][] = $content; | ||
} | ||
} else { | ||
$this->{$type}[$key] = $content; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filesystem interactions block. We can't have these in the library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for your input. but then how could we handle file uploads ?
Keeping them in memory is too dangerous because of the size these uploads could be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following the Single Responsibility Principle the library should be all things handling and serving HTTP. How to transfer a file is defined in the HTTP specification but not how to save it. I think at this point instead of writing to a file we should provide indirection for the user to handle the file in chunks.
For example if I were implementing this I think I would want a callback to receive a file in chunks and as I received each chunk I would pipe it to a file using react/filesystem or perhaps pipe it to a worker script that writes to disk (but blocks).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the idea, I'll figure something out and update the pull request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imho it would be better when you use streams and pass those along. That way the developer can decide what to do with the contents of the stream.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You think one stream per fileupload or the raw stream ?
I'll look how I can do that, I'm not a stream expert
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A stream per fileupload. Ping me on IRC or on twitter if you have questions about that. Happy to help :).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have suggestion about how to do that:
What you think about such approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your ideas, I will try to make it this week.