Skip to content

Commit 5457238

Browse files
committed
Merge pull request #47 from slava-vishnyakov/expect-continue
Expect Continue
2 parents 0c19a24 + 37c57c2 commit 5457238

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/RequestParser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function feed($data)
3737
}
3838

3939
$this->request = $this->parseHeaders($headers . "\r\n\r\n");
40+
41+
if($this->request->expectsContinue()) {
42+
$this->emit('expects_continue');
43+
}
4044
}
4145

4246
// if there is a request (meaning the headers are parsed) and

src/Server.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public function __construct(SocketServerInterface $io)
4242
});
4343

4444
$conn->on('data', array($parser, 'feed'));
45+
46+
$parser->on('expects_continue', function() use($conn) {
47+
$conn->write("HTTP/1.1 100 Continue\r\n\r\n");
48+
});
4549
});
4650
}
4751

tests/ServerTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,45 @@ private function createGetRequest()
7575

7676
return $data;
7777
}
78+
79+
public function testServerRespondsToExpectContinue()
80+
{
81+
$io = new ServerStub();
82+
$server = new Server($io);
83+
$conn = new ConnectionStub();
84+
$io->emit('connection', array($conn));
85+
86+
$requestReceived = false;
87+
$postBody = '{"React":true}';
88+
$httpRequestText = $this->createPostRequestWithExpect($postBody);
89+
90+
$conn->emit('data', array($httpRequestText));
91+
92+
$server->on('request', function ($request, $_) use (&$requestReceived, $postBody) {
93+
$requestReceived = true;
94+
$this->assertEquals($postBody, $request->getBody());
95+
});
96+
97+
// If server received Expect: 100-continue - the client won't send the body right away
98+
$this->assertEquals(false, $requestReceived);
99+
100+
$this->assertEquals("HTTP/1.1 100 Continue\r\n\r\n", $conn->getData());
101+
102+
$conn->emit('data', array($postBody));
103+
104+
$this->assertEquals(true, $requestReceived);
105+
106+
}
107+
108+
private function createPostRequestWithExpect($postBody)
109+
{
110+
$data = "POST / HTTP/1.1\r\n";
111+
$data .= "Host: example.com:80\r\n";
112+
$data .= "Content-Type: application/json\r\n";
113+
$data .= "Content-Length: " . strlen($postBody) . "\r\n";
114+
$data .= "Expect: 100-continue\r\n";
115+
$data .= "\r\n";
116+
117+
return $data;
118+
}
78119
}

0 commit comments

Comments
 (0)