<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.eventhttp.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'eventhttp.construct.php',
    1 => 'EventHttp::__construct',
    2 => 'Constructs EventHttp object (the HTTP server)',
  ),
  'up' => 
  array (
    0 => 'class.eventhttp.php',
    1 => 'EventHttp',
  ),
  'prev' => 
  array (
    0 => 'eventhttp.bind.php',
    1 => 'EventHttp::bind',
  ),
  'next' => 
  array (
    0 => 'eventhttp.removeserveralias.php',
    1 => 'EventHttp::removeServerAlias',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/event/eventhttp/construct.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="eventhttp.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">EventHttp::__construct</h1>
  <p class="verinfo">(PECL event &gt;= 1.2.6-beta)</p><p class="refpurpose"><span class="refname">EventHttp::__construct</span> &mdash; <span class="dc-title">Constructs EventHttp object (the HTTP server)</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-eventhttp.construct-description">
  <h3 class="title">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="methodname"><strong>EventHttp::__construct</strong></span>(<span class="methodparam">
    
    <span class="type"><a href="class.eventbase.php" class="type EventBase">EventBase</a></span> <code class="parameter">$base</code>
   </span>, <span class="methodparam">
    
    <span class="type"><a href="class.eventsslcontext.php" class="type EventSslContext">EventSslContext</a></span> <code class="parameter">$ctx</code>
    <span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span>
   </span>)</div>

  <p class="para rdfs-comment">
   Constructs the HTTP server object.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-eventhttp.construct-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <dl>
   
    <dt>
     <code class="parameter">base</code>
    </dt>
    <dd>
     <p class="para">
      Associated event base.
     </p>
    </dd>
   
   
    <dt>
     <code class="parameter">ctx</code>
    </dt>
    <dd>
     <p class="para">
      <span class="classname"><a href="class.eventsslcontext.php" class="classname">EventSslContext</a></span>
      class object. Turns plain HTTP server into HTTPS server. It means that
      if
      <code class="parameter">ctx</code>
      is configured correctly, then the underlying buffer events will be based
      on OpenSSL sockets. Thus, all traffic will pass through the SSL or TLS.
     </p>
     <blockquote class="note"><p><strong class="note">Nota</strong>: 
      <p class="para">
       This parameter is available only if
       <code class="literal">Event</code>
       is compiled with OpenSSL support and only with
       <code class="literal">Libevent
        2.1.0-alpha</code>
       and higher.
      </p>
     </p></blockquote>
    </dd>
   
  </dl>
 </div>

 <div class="refsect1 changelog" id="refsect1-eventhttp.construct-changelog">
  <h3 class="title">Log delle modifiche</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Versione</th>
      <th>Descrizione</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>PECL event 1.9.0</td>
      <td>
       OpenSSL support (<code class="parameter">ctx</code>) added.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>

 <div class="refsect1 examples" id="refsect1-eventhttp.construct-examples">
  <h3 class="title">Esempi</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 Simple HTTP server</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*<br /> * Simple HTTP server.<br /> *<br /> * To test it:<br /> * 1) Run it on a port of your choice, e.g.:<br /> * $ php examples/http.php 8010<br /> * 2) In another terminal connect to some address on this port<br /> * and make GET or POST request(others are turned off here), e.g.:<br /> * $ nc -t 127.0.0.1 8010<br /> * POST /about HTTP/1.0<br /> * Content-Type: text/plain<br /> * Content-Length: 4<br /> * Connection: close<br /> * (press Enter)<br /> *<br /> * It will output<br /> * a=12<br /> * HTTP/1.0 200 OK<br /> * Content-Type: text/html; charset=ISO-8859-1<br /> * Connection: close<br /> *<br /> * $ nc -t 127.0.0.1 8010<br /> * GET /dump HTTP/1.0<br /> * Content-Type: text/plain<br /> * Content-Encoding: UTF-8<br /> * Connection: close<br /> * (press Enter)<br /> *<br /> * It will output:<br /> * HTTP/1.0 200 OK<br /> * Content-Type: text/html; charset=ISO-8859-1<br /> * Connection: close<br /> * (press Enter)<br /> *<br /> * $ nc -t 127.0.0.1 8010<br /> * GET /unknown HTTP/1.0<br /> * Connection: close<br /> *<br /> * It will output:<br /> * HTTP/1.0 200 OK<br /> * Content-Type: text/html; charset=ISO-8859-1<br /> * Connection: close<br /> *<br /> * 3) See what the server outputs on the previous terminal window.<br /> */<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">_http_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">, </span><span style="color: #0000BB">$data</span><span style="color: #007700">) {<br />    static </span><span style="color: #0000BB">$counter      </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />    static </span><span style="color: #0000BB">$max_requests </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />    if (++</span><span style="color: #0000BB">$counter </span><span style="color: #007700">&gt;= </span><span style="color: #0000BB">$max_requests</span><span style="color: #007700">)  {<br />        echo </span><span style="color: #DD0000">"Counter reached max requests </span><span style="color: #0000BB">$max_requests</span><span style="color: #DD0000">. Exiting\n"</span><span style="color: #007700">;<br />        exit();<br />    }<br /><br />    echo </span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">, </span><span style="color: #DD0000">" called\n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"request:"</span><span style="color: #007700">; </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">);<br />    echo </span><span style="color: #DD0000">"data:"</span><span style="color: #007700">; </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /><br />    echo </span><span style="color: #DD0000">"\n===== DUMP =====\n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"Command:"</span><span style="color: #007700">, </span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getCommand</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"URI:"</span><span style="color: #007700">, </span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getUri</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"Input headers:"</span><span style="color: #007700">; </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getInputHeaders</span><span style="color: #007700">());<br />    echo </span><span style="color: #DD0000">"Output headers:"</span><span style="color: #007700">; </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getOutputHeaders</span><span style="color: #007700">());<br /><br />    echo </span><span style="color: #DD0000">"\n &gt;&gt; Sending reply ..."</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sendReply</span><span style="color: #007700">(</span><span style="color: #0000BB">200</span><span style="color: #007700">, </span><span style="color: #DD0000">"OK"</span><span style="color: #007700">);<br />    echo </span><span style="color: #DD0000">"OK\n"</span><span style="color: #007700">;<br /><br />    echo </span><span style="color: #DD0000">"\n &gt;&gt; Reading input buffer ...\n"</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">$buf </span><span style="color: #007700">= </span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getInputBuffer</span><span style="color: #007700">();<br />    while (</span><span style="color: #0000BB">$s </span><span style="color: #007700">= </span><span style="color: #0000BB">$buf</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">readLine</span><span style="color: #007700">(</span><span style="color: #0000BB">EventBuffer</span><span style="color: #007700">::</span><span style="color: #0000BB">EOL_ANY</span><span style="color: #007700">)) {<br />        echo </span><span style="color: #0000BB">$s</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    }<br />    echo </span><span style="color: #DD0000">"No more data in the buffer\n"</span><span style="color: #007700">;<br />}<br /><br />function </span><span style="color: #0000BB">_http_about</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"URI: "</span><span style="color: #007700">, </span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getUri</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"\n &gt;&gt; Sending reply ..."</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sendReply</span><span style="color: #007700">(</span><span style="color: #0000BB">200</span><span style="color: #007700">, </span><span style="color: #DD0000">"OK"</span><span style="color: #007700">);<br />    echo </span><span style="color: #DD0000">"OK\n"</span><span style="color: #007700">;<br />}<br /><br />function </span><span style="color: #0000BB">_http_default</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">, </span><span style="color: #0000BB">$data</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"URI: "</span><span style="color: #007700">, </span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getUri</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"\n &gt;&gt; Sending reply ..."</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sendReply</span><span style="color: #007700">(</span><span style="color: #0000BB">200</span><span style="color: #007700">, </span><span style="color: #DD0000">"OK"</span><span style="color: #007700">);<br />    echo </span><span style="color: #DD0000">"OK\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$port </span><span style="color: #007700">= </span><span style="color: #0000BB">8010</span><span style="color: #007700">;<br />if (</span><span style="color: #0000BB">$argc </span><span style="color: #007700">&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$port </span><span style="color: #007700">= (int) </span><span style="color: #0000BB">$argv</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br />}<br />if (</span><span style="color: #0000BB">$port </span><span style="color: #007700">&lt;= </span><span style="color: #0000BB">0 </span><span style="color: #007700">|| </span><span style="color: #0000BB">$port </span><span style="color: #007700">&gt; </span><span style="color: #0000BB">65535</span><span style="color: #007700">) {<br />    exit(</span><span style="color: #DD0000">"Invalid port"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$base </span><span style="color: #007700">= new </span><span style="color: #0000BB">EventBase</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$http </span><span style="color: #007700">= new </span><span style="color: #0000BB">EventHttp</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setAllowedMethods</span><span style="color: #007700">(</span><span style="color: #0000BB">EventHttpRequest</span><span style="color: #007700">::</span><span style="color: #0000BB">CMD_GET </span><span style="color: #007700">| </span><span style="color: #0000BB">EventHttpRequest</span><span style="color: #007700">::</span><span style="color: #0000BB">CMD_POST</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setCallback</span><span style="color: #007700">(</span><span style="color: #DD0000">"/dump"</span><span style="color: #007700">, </span><span style="color: #DD0000">"_http_dump"</span><span style="color: #007700">, array(</span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">8</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setCallback</span><span style="color: #007700">(</span><span style="color: #DD0000">"/about"</span><span style="color: #007700">, </span><span style="color: #DD0000">"_http_about"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setDefaultCallback</span><span style="color: #007700">(</span><span style="color: #DD0000">"_http_default"</span><span style="color: #007700">, </span><span style="color: #DD0000">"custom data value"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bind</span><span style="color: #007700">(</span><span style="color: #DD0000">"0.0.0.0"</span><span style="color: #007700">, </span><span style="color: #0000BB">8010</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$base</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Il precedente esempio visualizzerà
qualcosa simile a:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">a=12
HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close

HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close
(press Enter)

HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close</pre>
</div>
   </div>
  </div>
 </div>

</div><?php manual_footer($setup); ?>