/** * Writes the $record with the writer $writerType or depending on the get * parameter format or of the mime type of the Accept header * * @param \PSX\Data\RecordInterface $record * @param string $writerType * @return void */ private function setResponse(RecordInterface $record, $writerType = null) { // find best writer type $writer = $this->getResponseWriter($writerType); // set writer specific settings $this->configureWriter($writer); // write the response $response = $writer->write($record); // the response may have multiple presentations based on the Accept // header field if (!$this->response->hasHeader('Vary')) { $this->response->setHeader('Vary', 'Accept'); } // set content type header if not available if (!$this->response->hasHeader('Content-Type')) { $contentType = $writer->getContentType(); if ($contentType !== null) { $this->response->setHeader('Content-Type', $contentType); } } // for iframe file uploads we need an text/html content type header even // if we want serve json content. If all browsers support the FormData // api we can send file uploads per ajax but for now we use this hack. // Note do not rely on this param it will be removed as soon as possible if (isset($_GET['htmlMime'])) { $this->response->setHeader('Content-Type', 'text/html'); } $this->response->getBody()->write($response); }
public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain) { $accept = $request->getHeader('Accept'); if (stripos($accept, 'text/html') !== false && is_file($this->file)) { $response->setHeader('Content-Type', 'text/html'); $response->getBody()->write(file_get_contents($this->file)); } else { $filterChain->handle($request, $response); } }
public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain) { $key = $this->getCacheKey($request); if (!empty($key)) { $item = $this->cache->getItem($key); if ($item->isHit()) { // serve cache response $resp = $item->get(); $response->setHeaders($resp['headers']); $response->getBody()->write($resp['body']); } else { $filterChain->handle($request, $response); // save response $resp = array('headers' => $this->getCacheHeaders($response), 'body' => Util::toString($response->getBody())); $item->set($resp, $this->ttl); $this->cache->save($item); } } else { // if we have no key we can not use a cache $filterChain->handle($request, $response); } }
protected function sendContentChunked(ResponseInterface $response) { $body = $response->getBody(); $body->seek(0); while (!$body->eof()) { $chunk = $body->read($this->chunkSize); $len = strlen($chunk); if ($len > 0) { echo dechex($len) . "\r\n" . $chunk . "\r\n"; flush(); } } echo '0' . "\r\n" . "\r\n"; flush(); $body->close(); }
/** * Method which checks whether this is an valid response from an google * server */ protected function assertGoogleResponse(ResponseInterface $response) { $this->assertTrue($response->getStatusCode() >= 200 && $response->getStatusCode() < 400); // google server always response with an Server header $this->assertTrue($response->hasHeader('Server')); // we assume that the response should be more the 128 bytes $this->assertTrue(strlen((string) $response->getBody()) > 128); }
public static function createResponse(ResponseInterface $response) { return new PsrResponse($response->getBody(), $response->getStatusCode(), $response->getHeaders()); }
public function send(ResponseInterface $response) { $this->response = (string) $response->getBody(); }