Example #1
0
 public function testFileUploadWithDifferentHeadersOrder()
 {
     $file = base64_decode("R0lGODlhAQABAIAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==");
     $boundary = "---------------------------12758086162038677464950549563";
     $data = "--{$boundary}\r\n";
     $data .= "Content-Type: image/gif\r\n";
     $data .= "Content-Disposition: form-data; name=\"files[]\"; filename=\"blank.gif\"\r\n";
     $data .= "\r\n";
     $data .= $file . "\r\n";
     $data .= "--{$boundary}\r\n";
     $parser = new MultipartParser($data, $boundary);
     $parser->parse();
     $this->assertEquals(1, count($parser->getFiles()));
     $this->assertEquals(1, count($parser->getFiles()['files']));
     $uploaded_image = $parser->getFiles()['files'][0];
     $this->assertEquals($file, file_get_contents($uploaded_image['tmp_name']));
     $uploaded_image['tmp_name'] = 'file';
     //override the filename as it is random
     $expected_file = ['name' => 'blank.gif', 'type' => 'image/gif', 'tmp_name' => 'file', 'error' => 0, 'size' => 43];
     $this->assertEquals($expected_file, $uploaded_image);
 }
 public function parseBody($content, $request)
 {
     $headers = $request->getHeaders();
     if (array_key_exists('Content-Type', $headers)) {
         $contentType = $headers['Content-Type'][0];
         if (strpos($contentType, 'multipart/') === 0) {
             //TODO :: parse the content while it is streaming
             preg_match("/boundary=\"?(.*)\"?\$/", $contentType, $matches);
             $boundary = $matches[1];
             $parser = new MultipartParser($content, $boundary);
             $parser->parse();
             $request = $request->withParsedBody($parser->getPost());
             $request = $request->withUploadedFiles($parser->getFiles());
         } else {
             if (strpos(strtolower($contentType), 'application/x-www-form-urlencoded') === 0) {
                 $result = [];
                 parse_str(urldecode($content), $result);
                 $request = $request->withParsedBody($result);
             }
         }
     }
     return $request;
 }
Example #3
0
 public function testFileUpload()
 {
     $file = base64_decode("R0lGODlhAQABAIAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==");
     $boundary = "---------------------------12758086162038677464950549563";
     $data = "--{$boundary}\r\n";
     $data .= "Content-Disposition: form-data; name=\"user\"\r\n";
     $data .= "\r\n";
     $data .= "single\r\n";
     $data .= "--{$boundary}\r\n";
     $data .= "Content-Disposition: form-data; name=\"user2\"\r\n";
     $data .= "\r\n";
     $data .= "second\r\n";
     $data .= "--{$boundary}\r\n";
     $data .= "Content-Disposition: form-data; name=\"users[]\"\r\n";
     $data .= "\r\n";
     $data .= "first in array\r\n";
     $data .= "--{$boundary}\r\n";
     $data .= "Content-Disposition: form-data; name=\"users[]\"\r\n";
     $data .= "\r\n";
     $data .= "second in array\r\n";
     $data .= "--{$boundary}\r\n";
     $data .= "Content-Disposition: form-data; name=\"file\"; filename=\"User.php\"\r\n";
     $data .= "Content-Type: text/php\r\n";
     $data .= "\r\n";
     $data .= "<?php echo 'User';\r\n";
     $data .= "\r\n";
     $data .= "--{$boundary}\r\n";
     $data .= "Content-Disposition: form-data; name=\"files[]\"; filename=\"blank.gif\"\r\n";
     $data .= "Content-Type: image/gif\r\n";
     $data .= "\r\n";
     $data .= $file . "\r\n";
     $data .= "--{$boundary}\r\n";
     $data .= "Content-Disposition: form-data; name=\"files[]\"; filename=\"User.php\"\r\n";
     $data .= "Content-Type: text/php\r\n";
     $data .= "\r\n";
     $data .= "<?php echo 'User';\r\n";
     $data .= "\r\n";
     $data .= "--{$boundary}--\r\n";
     $parser = new MultipartParser($data, $boundary);
     $parser->parse();
     $this->assertEquals(2, count($parser->getFiles()));
     $this->assertEquals(2, count($parser->getFiles()['files']));
     $this->assertEquals(['user' => 'single', 'user2' => 'second', 'users' => ['first in array', 'second in array']], $parser->getPost());
     $uploaded_blank = $parser->getFiles()['files'][0];
     // The original test was `file_get_contents($uploaded_blank['tmp_name'])`
     // but as we moved to resources, we can't use that anymore, this is the only
     // difference with a stock php implementation
     $this->assertEquals($file, stream_get_contents($uploaded_blank['stream']));
     $uploaded_blank['stream'] = 'file';
     //override the resource as it is random
     $expected_file = ['name' => 'blank.gif', 'type' => 'image/gif', 'stream' => 'file', 'error' => 0, 'size' => 43];
     $this->assertEquals($expected_file, $uploaded_blank);
 }
Example #4
0
 public function parseBody($content)
 {
     $headers = $this->request->getHeaders();
     if (array_key_exists('Content-Type', $headers)) {
         if (strpos($headers['Content-Type'], 'multipart/') === 0) {
             //TODO :: parse the content while it is streaming
             preg_match("/boundary=\"?(.*)\"?\$/", $headers['Content-Type'], $matches);
             $boundary = $matches[1];
             $parser = new MultipartParser($content, $boundary);
             $parser->parse();
             $this->request->setPost($parser->getPost());
             $this->request->setFiles($parser->getFiles());
             return;
         }
         if (strtolower($headers['Content-Type']) == 'application/x-www-form-urlencoded') {
             parse_str(urldecode($content), $result);
             $this->request->setPost($result);
             return;
         }
     }
     $this->request->setBody($content);
 }