Event: sync'd with version 1.10.0

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@333508 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Ruslan Osmanov 2014-05-10 12:00:00 +00:00
parent 7f79d8835b
commit 5e5902c5d6
6 changed files with 295 additions and 4 deletions

View file

@ -12,7 +12,9 @@
</para>
<note xmlns="http://docbook.org/ns/docbook">
<para>
Note, Windows support introduced in <literal>event-1.9.0</literal>.
Note, Windows support introduced in
<literal>event-1.9.0</literal>
.
</para>
</note>
<para>

View file

@ -19,7 +19,8 @@
<para>
Removes up to
<parameter>size</parameter>
bytes from the input buffer. Returns a string of data read from the input buffer.
bytes from the input buffer. Returns a string of data read from the input
buffer.
</para>
</refsect1>
<refsect1 role="parameters">

View file

@ -31,8 +31,8 @@
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns OpenSSL error string reported on the buffer event,
or &false;, if there is no more error to return.
Returns OpenSSL error string reported on the buffer event, or &false;, if
there is no more error to return.
</para>
</refsect1>
<refsect1 role="examples">

View file

@ -14,6 +14,11 @@
<type>EventBase</type>
<parameter>base</parameter>
</methodparam>
<methodparam choice="opt">
<type>EventSslContext</type>
<parameter>ctx</parameter>
<initializer>&null;</initializer>
</methodparam>
</methodsynopsis>
<para>
Constructs the HTTP server object.
@ -32,6 +37,31 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<parameter>ctx</parameter>
</term>
<listitem>
<para>
<classname>EventSslContext</classname>
class object. Turns plain HTTP server into HTTPS server. It means that
if
<parameter>ctx</parameter>
is configured correctly, then the underlying buffer events will be based
on OpenSSL sockets. Thus, all traffic will pass through the SSL or TLS.
</para>
<note>
<para>
This parameter is available only if
<literal>Event</literal>
is compiled with OpenSSL support and only with
<literal>Libevent
2.1.0-alpha</literal>
and higher.
</para>
</note>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
@ -42,6 +72,29 @@
object.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>1.9.0</entry>
<entry>
OpenSSL support (<parameter>ctx</parameter>) added.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>

View file

@ -26,6 +26,11 @@
<type>int</type>
<parameter>port</parameter>
</methodparam>
<methodparam choice="opt">
<type>EventSslContext</type>
<parameter>ctx</parameter>
<initializer>&null;</initializer>
</methodparam>
</methodsynopsis>
<para>
Constructs EventHttpConnection object.
@ -76,6 +81,27 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<parameter>ctx</parameter>
</term>
<listitem>
<para>
<classname>EventSslContext</classname>
class object. Enables OpenSSL.
</para>
<note>
<para>
This parameter is available only if
<literal>Event</literal>
is compiled with OpenSSL support and only with
<literal>Libevent
2.1.0-alpha</literal>
and higher.
</para>
</note>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
@ -86,6 +112,29 @@
object.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>1.9.0</entry>
<entry>
OpenSSL support (<parameter>ctx</parameter>) added.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:

View file

@ -772,6 +772,192 @@ Connection: close
]]>
</screen>
</example>
<example>
<title>Simple HTTPS server</title>
<programlisting role="php">
<![CDATA[
<?php
/*
* Simple HTTPS server.
*
* 1) Run the server: `php examples/https.php 9999`
* 2) Test it: `php examples/ssl-connection.php 9999`
*/
function _http_dump($req, $data) {
static $counter = 0;
static $max_requests = 200;
if (++$counter >= $max_requests) {
echo "Counter reached max requests $max_requests. Exiting\n";
exit();
}
echo __METHOD__, " called\n";
echo "request:"; var_dump($req);
echo "data:"; var_dump($data);
echo "\n===== DUMP =====\n";
echo "Command:", $req->getCommand(), PHP_EOL;
echo "URI:", $req->getUri(), PHP_EOL;
echo "Input headers:"; var_dump($req->getInputHeaders());
echo "Output headers:"; var_dump($req->getOutputHeaders());
echo "\n >> Sending reply ...";
$req->sendReply(200, "OK");
echo "OK\n";
$buf = $req->getInputBuffer();
echo "\n >> Reading input buffer (", $buf->length, ") ...\n";
while ($s = $buf->read(1024)) {
echo $s;
}
echo "\nNo more data in the buffer\n";
}
function _http_about($req) {
echo __METHOD__, PHP_EOL;
echo "URI: ", $req->getUri(), PHP_EOL;
echo "\n >> Sending reply ...";
$req->sendReply(200, "OK");
echo "OK\n";
}
function _http_default($req, $data) {
echo __METHOD__, PHP_EOL;
echo "URI: ", $req->getUri(), PHP_EOL;
echo "\n >> Sending reply ...";
$req->sendReply(200, "OK");
echo "OK\n";
}
function _http_400($req) {
$req->sendError(400);
}
function _init_ssl() {
$local_cert = __DIR__."/ssl-echo-server/cert.pem";
$local_pk = __DIR__."/ssl-echo-server/privkey.pem";
$ctx = new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, array (
EventSslContext::OPT_LOCAL_CERT => $local_cert,
EventSslContext::OPT_LOCAL_PK => $local_pk,
//EventSslContext::OPT_PASSPHRASE => "test",
EventSslContext::OPT_ALLOW_SELF_SIGNED => true,
));
return $ctx;
}
$port = 9999;
if ($argc > 1) {
$port = (int) $argv[1];
}
if ($port <= 0 || $port > 65535) {
exit("Invalid port");
}
$ip = '0.0.0.0';
$base = new EventBase();
$ctx = _init_ssl();
$http = new EventHttp($base, $ctx);
$http->setAllowedMethods(EventHttpRequest::CMD_GET | EventHttpRequest::CMD_POST);
$http->setCallback("/dump", "_http_dump", array(4, 8));
$http->setCallback("/about", "_http_about");
$http->setCallback("/err400", "_http_400");
$http->setDefaultCallback("_http_default", "custom data value");
$http->bind($ip, $port);
$base->dispatch();
]]>
</programlisting>
</example>
<example>
<title>OpenSSL connection</title>
<programlisting role="php">
<![CDATA[
<?php
/*
* Sample OpenSSL client.
*
* Usage:
* 1) Launch a server, e.g.:
* $ php examples/https.php 9999
*
* 2) Launch the client in another terminal:
* $ php examples/ssl-connection.php 9999
*/
function _request_handler($req, $base) {
echo __FUNCTION__, PHP_EOL;
if (is_null($req)) {
echo "Timed out\n";
} else {
$response_code = $req->getResponseCode();
if ($response_code == 0) {
echo "Connection refused\n";
} elseif ($response_code != 200) {
echo "Unexpected response: $response_code\n";
} else {
echo "Success: $response_code\n";
$buf = $req->getInputBuffer();
echo "Body:\n";
while ($s = $buf->readLine(EventBuffer::EOL_ANY)) {
echo $s, PHP_EOL;
}
}
}
$base->exit(NULL);
}
function _init_ssl() {
$ctx = new EventSslContext(EventSslContext::SSLv3_CLIENT_METHOD, array ());
return $ctx;
}
// Allow to override the port
$port = 9999;
if ($argc > 1) {
$port = (int) $argv[1];
}
if ($port <= 0 || $port > 65535) {
exit("Invalid port\n");
}
$host = '127.0.0.1';
$ctx = _init_ssl();
if (!$ctx) {
trigger_error("Failed creating SSL context", E_USER_ERROR);
}
$base = new EventBase();
if (!$base) {
trigger_error("Failed to initialize event base", E_USER_ERROR);
}
$conn = new EventHttpConnection($base, NULL, $host, $port, $ctx);
$conn->setTimeout(50);
$req = new EventHttpRequest("_request_handler", $base);
$req->addHeader("Host", $host, EventHttpRequest::OUTPUT_HEADER);
$buf = $req->getOutputBuffer();
$buf->add("<html>HTML TEST</html>");
//$req->addHeader("Content-Length", $buf->length, EventHttpRequest::OUTPUT_HEADER);
//$req->addHeader("Connection", "close", EventHttpRequest::OUTPUT_HEADER);
$conn->makeRequest($req, EventHttpRequest::CMD_POST, "/dump");
$base->dispatch();
echo "END\n";
?>
]]>
</programlisting>
</example>
<example>
<title>
<function>EventHttpConnection::makeRequest</function> example</title>