HTTP_Server
[ class tree: HTTP_Server ] [ index: HTTP_Server ] [ all elements ]

Source for file Request.php

Documentation is available at Request.php

  1. <?PHP
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stephan Schmidt <schst@php-tools.net>                       |
  17. // +----------------------------------------------------------------------+
  18. //
  19. //    $Id: Request.php 176200 2005-01-01 11:28:29Z schst $
  20.  
  21. /**
  22.  * HTTP_Server_Request
  23.  *
  24.  * Interface that parses the request
  25.  *
  26.  * @author  Stephan Schmidt <schst@php-tools.de>
  27.  */
  28.  
  29.    /**
  30.     * method
  31.     *
  32.     * @access   public
  33.     * @var      string 
  34.     */
  35.     var $method;
  36.  
  37.    /**
  38.     * protocol
  39.     *
  40.     * @access   public
  41.     * @var      string 
  42.     */
  43.     var $protocol;
  44.  
  45.    /**
  46.     * uri
  47.     *
  48.     * @access   public
  49.     * @var      string 
  50.     */
  51.     var $uri;
  52.  
  53.    /**
  54.     * path info
  55.     *
  56.     * @access   public
  57.     * @var      string 
  58.     */
  59.     var $path_info;
  60.  
  61.    /**
  62.     * query string
  63.     *
  64.     * @access   public
  65.     * @var      string 
  66.     */
  67.     var $query_string;
  68.  
  69.    /**
  70.     * headers
  71.     *
  72.     * @access   public
  73.     * @var      array 
  74.     */
  75.     var $headers    =   array();
  76.     
  77.     /**
  78.     *   the data (like POST) sent after the headers
  79.     *   @access public
  80.     *   @var    string 
  81.     */
  82.     var $content    = '';
  83.  
  84.    /**
  85.     * parse a http request
  86.     *
  87.     * @access    public
  88.     * @static
  89.     * @param     string    $request    raw request data
  90.     * @return    array     $request    parsed request
  91.     */
  92.     function &parse($request)
  93.     {
  94.         //    split lines
  95.         $lines explode ("\r\n"$request);
  96.  
  97.         //    check for method, uri and protocol in line 1
  98.         $regs = array();
  99.         if (!preg_match("'([^ ]+) ([^ ]+) (HTTP/[^ ]+)'"$lines[0]$regs)) {
  100.             return false;
  101.         }
  102.  
  103.         $request &new HTTP_Server_Request();
  104.         
  105.         $request->method   = $regs[1];
  106.         $request->uri      = $regs[2];
  107.         $request->protocol = $regs[3];
  108.             
  109.         //    parse the uri    
  110.         if ($tmp HTTP_Server_Request::_parsePath($regs[2])) {
  111.             $request->path_info    = $tmp['path_info'];
  112.             $request->query_string = $tmp['query_string'];
  113.         }
  114.     
  115.         //    parse and store additional headers (not needed, but nice to have)
  116.         /**
  117.         * FIXME: 
  118.         *   HTTP1.1 allows headers be broken on several lines if the next line begins with
  119.         *   a space or a tab (rfc2616#2.2)
  120.         */
  121.         for ($i = 1; $i count($lines)$i++{
  122.             if (trim($lines[$i]== ''{
  123.                 //empty line, after this the content should follow
  124.                 $i++;
  125.                 break;
  126.             }
  127.             $regs    =    array();
  128.             if (preg_match("'([^: ]+): (.+)'"$lines[$i]$regs)) {
  129.                 $request->headers[(strtolower($regs[1]))]    =    $regs[2];
  130.             }
  131.         }
  132.         //aggregate the content (POST data or so)
  133.         $request->content = '';
  134.         for ($i $i$i count($lines)$i++{
  135.             $request->content .= $lines[$i"\r\n";
  136.         }
  137.         
  138.         return $request;
  139.     }
  140.  
  141.    /**
  142.     * get the request method
  143.     *
  144.     * @access   public
  145.     * @return   string 
  146.     */
  147.     function getMethod()
  148.     {
  149.         return $this->method;
  150.     }
  151.     
  152.    /**
  153.     * get path info
  154.     *
  155.     * @access   public
  156.     * @return   string 
  157.     */
  158.     function getPathInfo()
  159.     {
  160.         return $this->path_info;
  161.     }
  162.     
  163.    /**
  164.     * parse a request uri
  165.     *
  166.     * @access    public
  167.     * @param    string    $path    uri to parse
  168.     * @return    array    $path    path data
  169.     */
  170.     function _parsePath($path)
  171.     {
  172.         $regs = array();
  173.         if (!preg_match("'([^?]*)(?:\?([^#]*))?(?:#.*)? *'"$path$regs)) {
  174.             return false;
  175.         }
  176.  
  177.         return array(
  178.                       'path_info'    => $regs[1],
  179.                       'query_string' => isset($regs[2]$regs[2: null
  180.                   );
  181.     }
  182.     
  183.    /**
  184.     *   Exports server variables based on request data
  185.     *   like _GET, _SERVER[HTTP_*] and so
  186.     *   The function can be used to make your own
  187.     *   HTTP server act more than a "real" one (like apache)
  188.     *
  189.     *   @access public
  190.     */
  191.     function export(
  192.     {
  193.         //_SERVER[HTTP_*] from headers
  194.         foreach ($this->headers as $strId => $strValue{
  195.             $_SERVER['HTTP_' str_replace('-''_'strtoupper($strId))$strValue;
  196.         }
  197.         
  198.         $nPos strpos($this->headers['host']':');
  199.         if ($nPos !== false{
  200.             $_SERVER['HTTP_HOST']   substr$this->headers['host']0$nPos);
  201.             $_SERVER['SERVER_PORT'substr$this->headers['host']$nPos);
  202.         else {
  203.             $_SERVER['SERVER_PORT'= 80;
  204.         }
  205.         
  206.         $_SERVER['QUERY_STRING']    $this->query_string;
  207.         $_SERVER['SERVER_PROTOCOL'$this->protocol;
  208.         $_SERVER['REQUEST_METHOD']  $this->method;
  209.         $_SERVER['REQUEST_URI']     $this->uri;
  210.         
  211.         //GET variables
  212.         parse_str($this->query_string$_GET);
  213.         if (count($_GET== 0{
  214.             $_SERVER['argc'= 0;
  215.             $_SERVER['argv'= array();
  216.         else {
  217.             $_SERVER['argc'= 1;
  218.             $_SERVER['argv'= array($this->query_string);
  219.         }
  220.         
  221.         //@todo: POST, COOKIE, FILES, SESSION,....
  222.     }
  223. }
  224. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:25 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.