1: <?php
2: namespace Ratchet\Http;
3: use Ratchet\MessageComponentInterface;
4: use Ratchet\ConnectionInterface;
5:
6: class HttpServer implements MessageComponentInterface {
7: use CloseResponseTrait;
8:
9: /**
10: * Buffers incoming HTTP requests returning a Guzzle Request when coalesced
11: * @var HttpRequestParser
12: * @note May not expose this in the future, may do through facade methods
13: */
14: protected $_reqParser;
15:
16: /**
17: * @var \Ratchet\Http\HttpServerInterface
18: */
19: protected $_httpServer;
20:
21: /**
22: * @param HttpServerInterface
23: */
24: public function __construct(HttpServerInterface $component) {
25: $this->_httpServer = $component;
26: $this->_reqParser = new HttpRequestParser;
27: }
28:
29: /**
30: * {@inheritdoc}
31: */
32: public function onOpen(ConnectionInterface $conn) {
33: $conn->httpHeadersReceived = false;
34: }
35:
36: /**
37: * {@inheritdoc}
38: */
39: public function onMessage(ConnectionInterface $from, $msg) {
40: if (true !== $from->httpHeadersReceived) {
41: try {
42: if (null === ($request = $this->_reqParser->onMessage($from, $msg))) {
43: return;
44: }
45: } catch (\OverflowException $oe) {
46: return $this->close($from, 413);
47: }
48:
49: $from->httpHeadersReceived = true;
50:
51: return $this->_httpServer->onOpen($from, $request);
52: }
53:
54: $this->_httpServer->onMessage($from, $msg);
55: }
56:
57: /**
58: * {@inheritdoc}
59: */
60: public function onClose(ConnectionInterface $conn) {
61: if ($conn->httpHeadersReceived) {
62: $this->_httpServer->onClose($conn);
63: }
64: }
65:
66: /**
67: * {@inheritdoc}
68: */
69: public function onError(ConnectionInterface $conn, \Exception $e) {
70: if ($conn->httpHeadersReceived) {
71: $this->_httpServer->onError($conn, $e);
72: } else {
73: $this->close($conn, 500);
74: }
75: }
76: }
77: