end() public method

User applications are NOT required to call Response::end() after streaming or sending response data (though it's not incorrect to do so) -- the server will automatically call end() as needed. Passing the optional $finalBodyChunk parameter is a shortcut equivalent to the following: $response->stream($finalBodyChunk); $response->end(); Note: Invoking Response::end() with a non-empty $finalBodyChunk parameter without having previously invoked Response::stream() is equivalent to calling Response::send($finalBodyChunk).
public end ( string $finalBodyChunk = null )
$finalBodyChunk string Optional final body data to send
コード例 #1
0
ファイル: Chat.php プロジェクト: kelunik/demo-chat
 public function onHandshake(Request $request, Response $response)
 {
     // During handshakes, you should always check the origin header, otherwise any site will
     // be able to connect to your endpoint. Websockets are not restricted by the same-origin-policy!
     $origin = $request->getHeader("origin");
     if ($origin !== "http://localhost:1337") {
         $response->setStatus(403);
         $response->end("<h1>origin not allowed</h1>");
         return null;
     }
     // returned values will be passed to onOpen, that way you can pass cookie values or the whole request object.
     return $request->getConnectionInfo()["client_addr"];
 }
コード例 #2
0
ファイル: Root.php プロジェクト: hunslater/aerys
 private function respond($fileInfo, Request $request, Response $response)
 {
     // If the file doesn't exist don't bother to do anything else so the
     // HTTP server can send a 404 and/or allow handlers further down the chain
     // a chance to respond.
     if (empty($fileInfo->exists)) {
         return;
     }
     switch ($request->getMethod()) {
         case "GET":
         case "HEAD":
             break;
         case "OPTIONS":
             $response->setStatus(HTTP_STATUS["OK"]);
             $response->setHeader("Allow", "GET, HEAD, OPTIONS");
             $response->setHeader("Accept-Ranges", "bytes");
             $response->setHeader("Aerys-Generic-Response", "enable");
             return;
         default:
             $response->setStatus(HTTP_STATUS["METHOD_NOT_ALLOWED"]);
             $response->setHeader("Allow", "GET, HEAD, OPTIONS");
             $response->setHeader("Aerys-Generic-Response", "enable");
             return;
     }
     $precondition = $this->checkPreconditions($request, $fileInfo->mtime, $fileInfo->etag);
     switch ($precondition) {
         case self::PRECOND_NOT_MODIFIED:
             $response->setStatus(HTTP_STATUS["NOT_MODIFIED"]);
             $lastModifiedHttpDate = \gmdate('D, d M Y H:i:s', $fileInfo->mtime) . " GMT";
             $response->setHeader("Last-Modified", $lastModifiedHttpDate);
             if ($fileInfo->etag) {
                 $response->setHeader("Etag", $fileInfo->etag);
             }
             $response->end();
             return;
         case self::PRECOND_FAILED:
             $response->setStatus(HTTP_STATUS["PRECONDITION_FAILED"]);
             $response->end();
             return;
         case self::PRECOND_IF_RANGE_FAILED:
             // Return this so the resulting generator will be auto-resolved
             return $this->doNonRangeResponse($fileInfo, $response);
     }
     if (!($rangeHeader = $request->getHeader("Range"))) {
         // Return this so the resulting generator will be auto-resolved
         return $this->doNonRangeResponse($fileInfo, $response);
     }
     if ($range = $this->normalizeByteRanges($fileInfo->size, $rangeHeader)) {
         // Return this so the resulting generator will be auto-resolved
         return $this->doRangeResponse($range, $fileInfo, $response);
     }
     // If we're still here this is the only remaining response we can send
     $response->setStatus(HTTP_STATUS["REQUESTED_RANGE_NOT_SATISFIABLE"]);
     $response->setHeader("Content-Range", "*/{$fileInfo->size}");
     $response->end();
 }
コード例 #3
0
ファイル: Server.php プロジェクト: Room-11/Jeeves
 public function getAllRooms(AerysRequest $request, AerysResponse $response)
 {
     $result = [];
     /** @var ChatRoom $room */
     foreach ($this->chatRooms as $room) {
         $result[] = ['host' => $room->getIdentifier()->getHost(), 'room_id' => $room->getIdentifier()->getId()];
     }
     $response->setHeader('Content-Type', 'application/json');
     $response->end(json_encode($result));
 }
コード例 #4
0
ファイル: Server.php プロジェクト: amphp/aerys
 private function tryErrorResponse(\Throwable $error, InternalRequest $ireq, Response $response, array $filters)
 {
     try {
         $status = HTTP_STATUS["INTERNAL_SERVER_ERROR"];
         $msg = $this->options->debug ? "<pre>" . htmlspecialchars($error) . "</pre>" : "<p>Something went wrong ...</p>";
         $body = makeGenericBody($status, ["sub_heading" => "Requested: {$ireq->uri}", "msg" => $msg]);
         $response->setStatus(HTTP_STATUS["INTERNAL_SERVER_ERROR"]);
         $response->setHeader("Connection", "close");
         $response->end($body);
     } catch (ClientException $error) {
         return;
     } catch (\Throwable $error) {
         if ($ireq->filterErrorFlag) {
             $this->tryFilterErrorResponse($error, $ireq, $filters);
         } else {
             $this->logger->error($error);
             $this->close($ireq->client);
         }
     }
 }
コード例 #5
0
ファイル: ReverseProxy.php プロジェクト: amphp/aerys-reverse
 public function __invoke(Request $req, Response $res)
 {
     $headers = $req->getAllHeaders();
     unset($headers["accept-encoding"]);
     $connection = $headers["connection"];
     unset($headers["connection"]);
     foreach ($connection as $value) {
         foreach (explode(",", strtolower($value)) as $type) {
             $type = trim($type);
             if ($type == "upgrade") {
                 $headers["connection"][0] = "upgrade";
             } else {
                 unset($headers[$type]);
             }
         }
     }
     if ($this->headers) {
         if (is_callable($this->headers)) {
             $headers = ($this->headers)($headers);
         } else {
             $headers = $this->headers + $headers;
         }
     }
     $promise = $this->client->request((new \Amp\Artax\Request())->setMethod($req->getMethod())->setUri($this->target . $req->getUri())->setAllHeaders($headers)->setBody((yield $req->getBody())));
     // no async sending possible :-( [because of redirects]
     $promise->watch(function ($update) use($req, $res, &$hasBody, &$status, &$zlib) {
         list($type, $data) = $update;
         if ($type == Notify::RESPONSE_HEADERS) {
             $headers = array_change_key_case($data["headers"], CASE_LOWER);
             foreach ($data["headers"] as $header => $values) {
                 foreach ($values as $value) {
                     $res->addHeader($header, $value);
                 }
             }
             $res->setStatus($status = $data["status"]);
             $res->setReason($data["reason"]);
             if (isset($headers["content-encoding"]) && strcasecmp(trim(current($headers["content-encoding"])), 'gzip') === 0) {
                 $zlib = inflate_init(ZLIB_ENCODING_GZIP);
             }
             $hasBody = true;
         }
         if ($type == Notify::RESPONSE_BODY_DATA) {
             if ($zlib) {
                 $data = inflate_add($zlib, $data);
             }
             $res->stream($data);
         }
         if ($type == Notify::RESPONSE) {
             if (!$hasBody) {
                 foreach ($data->getAllHeaders() as $header => $values) {
                     foreach ($values as $value) {
                         $res->addHeader($header, $value);
                     }
                 }
                 $res->setStatus($status = $data->getStatus());
                 $res->setReason($data->getReason());
             }
             if ($status == 101) {
                 $req->setLocalVar("aerys.reverse.socket", $update["export_socket"]());
             }
             $res->end($zlib ? inflate_add("", ZLIB_FINISH) : null);
         }
     });
     (yield $promise);
 }