/** * Send the file to the client (Download) * * @param string|array $options Options for the file(s) to send * @throws \UnexpectedValueException * @throws \InvalidArgumentException * @return void */ public function send($options = null) { if (is_string($options)) { $filepath = $options; } elseif (is_array($options) && isset($options['filepath'])) { $filepath = $options['filepath']; } else { throw new \InvalidArgumentException("Filename is not set."); } if (!is_file($filepath) || !is_readable($filepath)) { throw new \InvalidArgumentException("File '{$filepath}' does not exists."); } $mimeType = $this->mime->getMimeType($filepath); $this->response->setHeader('Content-length', filesize($filepath)); $this->response->setHeader('Content-Type', $mimeType); $this->response->sendHeaders(); $handle = fopen($filepath, 'r'); if ($handle) { while (($buffer = fgets($handle, 4096)) !== false) { echo $buffer; } if (!feof($handle)) { throw new \UnexpectedValueException("Unexpected end of file"); } fclose($handle); } }
public function testSend() { $file = __DIR__ . '/../../_files/javascript.js'; $contentType = 'content/type'; $this->response->expects($this->at(0))->method('setHeader')->with('Content-length', filesize($file)); $this->response->expects($this->at(1))->method('setHeader')->with('Content-Type', $contentType); $this->response->expects($this->once())->method('sendHeaders'); $this->mime->expects($this->once())->method('getMimeType')->with($file)->will($this->returnValue($contentType)); $this->expectOutputString(file_get_contents($file)); $this->object->send($file); }
/** * Create response string for problem during request and set HTTP error code * * @param \Exception $exception * @param \Magento\Framework\HTTP\PhpEnvironment\Response $response OPTIONAL If NULL - will use internal getter * @return array */ public function prepareErrorResponse(\Exception $exception, \Magento\Framework\HTTP\PhpEnvironment\Response $response = null) { $errorMsg = $exception->getMessage(); if ($exception instanceof \Magento\Framework\Oauth\Exception) { $responseCode = self::HTTP_UNAUTHORIZED; } elseif ($exception instanceof \Magento\Framework\Oauth\OauthInputException) { $responseCode = self::HTTP_BAD_REQUEST; if ($errorMsg == \Magento\Framework\Oauth\OauthInputException::DEFAULT_MESSAGE) { $errorMsg = $exception->getAggregatedErrorMessage(); } } else { $errorMsg = 'internal_error&message=' . ($errorMsg ? $errorMsg : 'empty_message'); $responseCode = self::HTTP_INTERNAL_ERROR; } $response->setHttpResponseCode($responseCode); return ['oauth_problem' => $errorMsg]; }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessageRegExp Invalid HTTP response code */ public function testHttpResponseCodeWithException() { $this->response->setHttpResponseCode(1); }
/** * Send the response, including all headers, rendering exceptions if so * requested. * * @return void */ public function sendResponse() { $this->sendVary(); parent::sendResponse(); }