webserver.php
changeset 43 2634d550a97b
parent 40 bd3372a2afc1
child 48 d643bfb862d8
equal deleted inserted replaced
42:774751e7faed 43:2634d550a97b
    16 /**
    16 /**
    17  * Version of the server
    17  * Version of the server
    18  * @const string
    18  * @const string
    19  */
    19  */
    20 
    20 
    21 define('HTTPD_VERSION', '0.1b4');
    21 define('HTTPD_VERSION', '0.1b5');
    22 
    22 
    23 /**
    23 /**
    24  * Length of keep-alive connections
    24  * Length of keep-alive connections
    25  * @const int
    25  * @const int
    26  */
    26  */
   465       }
   465       }
   466       $method =& $match[1];
   466       $method =& $match[1];
   467       $uri =& $match[2];
   467       $uri =& $match[2];
   468       
   468       
   469       // set client headers
   469       // set client headers
       
   470       foreach ( $_SERVER as $key => $_ )
       
   471       {
       
   472         if ( preg_match('/^HTTP_/', $key) )
       
   473           unset($_SERVER[$key]);
       
   474       }
   470       unset($client_headers[0]);
   475       unset($client_headers[0]);
   471       foreach ( $client_headers as $line )
   476       foreach ( $client_headers as $line )
   472       {
   477       {
   473         if ( !preg_match('/^([A-z0-9-]+): (.+)$/is', $line, $match) )
   478         if ( !preg_match('/^([A-z0-9-]+): (.+)$/is', $line, $match) )
   474           continue;
   479           continue;
   480       if ( isset($_SERVER['HTTP_CONNECTION']) && defined('HTTPD_WS_CHILD') )
   485       if ( isset($_SERVER['HTTP_CONNECTION']) && defined('HTTPD_WS_CHILD') )
   481       {
   486       {
   482         $this->in_keepalive = ( strtolower($_SERVER['HTTP_CONNECTION']) === 'keep-alive' );
   487         $this->in_keepalive = ( strtolower($_SERVER['HTTP_CONNECTION']) === 'keep-alive' );
   483       }
   488       }
   484       
   489       
       
   490       // process cookies
       
   491       $_COOKIE = array();
       
   492       if ( isset($_SERVER['HTTP_COOKIE']) )
       
   493       {
       
   494         preg_match_all('/([a-z0-9_-]+)=([^;]*)(?:;|$)/', trim($_SERVER['HTTP_COOKIE']), $matches);
       
   495         foreach ( $matches[0] as $i => $match )
       
   496         {
       
   497           $_COOKIE[$matches[1][$i]] = str_replace('\\r', "\r", str_replace('\\n', "\n", str_replace(rawurlencode(';'), ';', $matches[2][$i])));
       
   498         }
       
   499       }
       
   500       
   485       // parse authorization, if any
   501       // parse authorization, if any
   486       if ( isset($_SERVER['PHP_AUTH_USER']) )
   502       unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
   487       {
       
   488         unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
       
   489       }
       
   490       if ( isset($_SERVER['HTTP_AUTHORIZATION']) )
   503       if ( isset($_SERVER['HTTP_AUTHORIZATION']) )
   491       {
   504       {
   492         $data = $_SERVER['HTTP_AUTHORIZATION'];
   505         $data = $_SERVER['HTTP_AUTHORIZATION'];
   493         $data = substr(strstr($data, ' '), 1);
   506         $data = substr(strstr($data, ' '), 1);
   494         $data = base64_decode($data);
   507         $data = base64_decode($data);
  1194     $this->response_headers[] = $str;
  1207     $this->response_headers[] = $str;
  1195     return true;
  1208     return true;
  1196   }
  1209   }
  1197   
  1210   
  1198   /**
  1211   /**
       
  1212    * Sets a cookie. Identical to PHP's setcookie() function.
       
  1213    * @param string Cookie name
       
  1214    * @param string Cookie value
       
  1215    * @param int Expiration time of cookie as a UNIX timestamp; if omitted or set to zero, cookie will expire at the end of the user's browser session
       
  1216    * @param string Path of the cookie
       
  1217    * @param string Domain the cookie is available under
       
  1218    * @param bool If true, browser will only send the cookie through an HTTPS connection.
       
  1219    * @param bool If true, cookie will not be accessible to client-side code
       
  1220    */
       
  1221   
       
  1222   function setcookie($cookiename, $cookievalue, $expiry = false, $path = false, $domain = false, $secure = false, $httponly = false)
       
  1223   {
       
  1224     $header = "Set-Cookie: $cookiename=$cookievalue";
       
  1225     if ( !empty($expiry) )
       
  1226       $header .= "; expires=" . date('D, d-M-Y H:i:s T', $expiry);
       
  1227     if ( !empty($path) )
       
  1228       $header .= "; path=$path";
       
  1229     if ( !empty($domain) )
       
  1230       $header .= "; domain=$domain";
       
  1231     if ( $secure )
       
  1232       $header .= "; secure";
       
  1233     if ( $httponly )
       
  1234       $header .= "; httponly";
       
  1235     
       
  1236     if ( is_int($expiry) && $expiry < time() )
       
  1237     {
       
  1238       unset($_COOKIE[$cookiename]);
       
  1239     }
       
  1240     else
       
  1241     {
       
  1242       $_COOKIE[$cookiename] = $cookievalue;
       
  1243     }
       
  1244     
       
  1245     $this->header($header);
       
  1246   }
       
  1247   
       
  1248   /**
  1199    * Sends the client an HTTP error page
  1249    * Sends the client an HTTP error page
  1200    * @param resource Socket connection to client
  1250    * @param resource Socket connection to client
  1201    * @param int HTTP status code
  1251    * @param int HTTP status code
  1202    * @param string Detailed error string
  1252    * @param string Detailed error string
  1203    */
  1253    */