Commits

Ruslan Osmanov committed 13fba30

HTTPS examples added

  • Participants
  • Parent commits 877635c

Comments (0)

Files changed (5)

File classes/http_connection.c

 		SSL_set_ex_data(ssl, php_event_ssl_data_index, ectx);
 
 #ifdef HAVE_EVENT_PTHREADS_LIB
-		options = BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE;
+		options = BEV_OPT_DEFER_CALLBACKS | BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE;
 #else
-		options = BEV_OPT_CLOSE_ON_FREE;
+		options = BEV_OPT_DEFER_CALLBACKS | BEV_OPT_CLOSE_ON_FREE;
 #endif
 
 		bevent = bufferevent_openssl_socket_new(b->base, -1, ssl, BUFFEREVENT_SSL_CONNECTING, options);

File classes/http_request.c

 
 	/* Tell Libevent that we will free the request ourselves(evhttp_request_free in the free-storage handler)
 	 * XXX Not sure if it's really needed here though. */
-	evhttp_request_own(req);
+	/*evhttp_request_own(req);*/
 	http_req->ptr = req;
 
 	if (zarg) {

File examples/https.php

 <?php
 /*
- * Simple HTTP server.
+ * Simple HTTPS server.
  *
- * To test it:
- * 1) Run it on a port of your choice, e.g.:
- * $ php examples/http.php 8010
- * 2) In another terminal connect to some address on this port
- * and make GET or POST request(others are turned off here), e.g.:
- * $ nc -t 127.0.0.1 8010
- * POST /about HTTP/1.0
- * Content-Type: text/plain
- * Content-Length: 4
- * Connection: close
- * (press Enter)
- *
- * It will output
- * a=12
- * HTTP/1.0 200 OK
- * Content-Type: text/html; charset=ISO-8859-1
- * Connection: close
- *
- * $ nc -t 127.0.0.1 8010
- * GET /dump HTTP/1.0
- * Content-Type: text/plain
- * Content-Encoding: UTF-8
- * Connection: close
- * (press Enter)
- *
- * It will output:
- * HTTP/1.0 200 OK
- * Content-Type: text/html; charset=ISO-8859-1
- * Connection: close
- * (press Enter)
- *
- * $ nc -t 127.0.0.1 8010
- * GET /unknown HTTP/1.0
- * Connection: close
- *
- * It will output:
- * HTTP/1.0 200 OK
- * Content-Type: text/html; charset=ISO-8859-1
- * Connection: close
- *
- * 3) See what the server outputs on the previous terminal window.
+ * 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 = 2;
+	static $max_requests = 200;
 
 	if (++$counter >= $max_requests)  {
 		echo "Counter reached max requests $max_requests. Exiting\n";
 	$req->sendReply(200, "OK");
 	echo "OK\n";
 
-	echo "\n >> Reading input buffer ...\n";
 	$buf = $req->getInputBuffer();
-	while ($s = $buf->readLine(EventBuffer::EOL_ANY)) {
-		echo $s, PHP_EOL;
+	echo "\n >> Reading input buffer (", $buf->length, ") ...\n";
+	while ($s = $buf->read(1024)) {
+		echo $s;
 	}
-	echo "No more data in the buffer\n";
+	echo "\nNo more data in the buffer\n";
 }
 
 function _http_about($req) {
 	$local_cert = __DIR__."/ssl-echo-server/cert.pem";
 	$local_pk   = __DIR__."/ssl-echo-server/privkey.pem";
 
-	$ctx = new EventSslContext(EventSslContext::SSLv3_CLIENT_METHOD, array (
+	$ctx = new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, array (
 		EventSslContext::OPT_LOCAL_CERT  => $local_cert,
 		EventSslContext::OPT_LOCAL_PK    => $local_pk,
 		//EventSslContext::OPT_PASSPHRASE  => "test",
 $http->setCallback("/err400", "_http_400");
 $http->setDefaultCallback("_http_default", "custom data value");
 
-echo "bind($ip, $port)\n";
 $http->bind($ip, $port);
 $base->dispatch();

File examples/ssl-connection.php

 }
 
 function _init_ssl() {
-	$local_cert = __DIR__."/ssl-echo-server/cert.pem";
-	$local_pk   = __DIR__."/ssl-echo-server/privkey.pem";
-
-	$ctx = new EventSslContext(EventSslContext::SSLv3_CLIENT_METHOD, array (
-		EventSslContext::OPT_LOCAL_CERT  => $local_cert,
-		EventSslContext::OPT_LOCAL_PK    => $local_pk,
-		//EventSslContext::OPT_PASSPHRASE  => "test",
-		EventSslContext::OPT_ALLOW_SELF_SIGNED => true,
-	));
+	$ctx = new EventSslContext(EventSslContext::SSLv3_CLIENT_METHOD, array ());
 
 	return $ctx;
 }
 if ($port <= 0 || $port > 65535) {
 	exit("Invalid port\n");
 }
-$host = 'localhost';
+$host = '127.0.0.1';
 
 $ctx = _init_ssl();
 if (!$ctx) {
 	trigger_error("Failed to initialize event base", E_USER_ERROR);
 }
 
-echo "new EventHttpConnection(base, null, $host, $port, ctx)\n";
 $conn = new EventHttpConnection($base, NULL, $host, $port, $ctx);
 $conn->setTimeout(50);
 
 $req = new EventHttpRequest("_request_handler", $base);
 $req->addHeader("Host", $host, EventHttpRequest::OUTPUT_HEADER);
-$conn->makeRequest($req, EventHttpRequest::CMD_GET, "/about");
+$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";
     <email>remi@php.net</email>
     <active>yes</active>
   </contributor>
-  <date>2014-04-20</date>
+  <date>2014-04-21</date>
   <!--{{{ Current version -->
   <version>
     <release>1.10.0</release>
         <file role="doc" name="http_request.php"/>
         <file role="doc" name="httpv0client.php"/>
         <file role="doc" name="httpv0client2.php"/>
+        <file role="doc" name="https.php"/>
         <file role="doc" name="listener.php"/>
         <file role="doc" name="misc.php"/>
         <file role="doc" name="signal.php"/>
         <file role="doc" name="sslfilter.php"/>
+        <file role="doc" name="ssl-connection.php"/>
         <file role="doc" name="timer.php"/>
         <file role="doc" name="uppercase_proxy.php"/>
       </dir>