walkor 12 年之前
父節點
當前提交
69d18963e6
共有 2 個文件被更改,包括 0 次插入427 次删除
  1. 0 194
      Protocols/FastCGI.php
  2. 0 233
      Protocols/HTTP.php

+ 0 - 194
Protocols/FastCGI.php

@@ -1,194 +0,0 @@
-<?php 
-
-/**
- * fastcgi 协议解析 相关
- * 简单实现
-* @author walkor <worker-man@qq.com>
-* */
-class FastCGI{
-    
-    const VERSION_1            = 1;
-
-    const BEGIN_REQUEST        = 1;
-    const ABORT_REQUEST        = 2;
-    const END_REQUEST          = 3;
-    const PARAMS               = 4;
-    const STDIN                = 5;
-    const STDOUT               = 6;
-    const STDERR               = 7;
-    const DATA                 = 8;
-    const GET_VALUES           = 9;
-    const GET_VALUES_RESULT    = 10;
-    const UNKNOWN_TYPE         = 11;
-    const MAXTYPE              = self::UNKNOWN_TYPE;
-    
-    const HEAD_LENGTH      = 8;
-    
-    
-    private  function __construct(){}
-    
-    /**
-     * 判断数据包是否全部接收完成
-     * 
-     * @param string $data
-     * @return int 0:完成 >0:还要接收int字节
-     */
-    public static function input($data)
-    {
-        while(1)
-        {
-            $data_length = strlen($data);
-            // 长度小于包头长度,继续读
-            if($data_length < self::HEAD_LENGTH)
-            {
-                return self::HEAD_LENGTH - $data_length;
-            }
-        
-            $headers = unpack(
-                    "Cversion/".
-                    "Ctype/".
-                    "nrequestId/".
-                    "ncontentLength/".
-                    "CpaddingLength/".
-                    "Creserved/"
-                    , $data);
-        
-            $total_length = self::HEAD_LENGTH + $headers['contentLength'] + $headers['paddingLength'];
-            
-            // 全部接收完毕
-            if($data_length == $total_length)
-            {
-                return 0;
-            }
-            // 数据长度不够一个包长
-            else if($data_length < $total_length)
-            {
-                return $total_length - $data_length;
-            }
-            // 数据长度大于一个包长,还有后续包
-            else
-            {
-                $data = substr($data, $total_length);
-            }
-        }
-        return 0;
-    }    
-    
-    /**
-     * 解析全部fastcgi协议包,并设置相应环境变量
-     * 
-     * @param string $data
-     * @return array
-     */
-    public static function decode($data)
-    {
-        $params = array();
-        $_GET = $_POST = $GLOBALS['HTTP_RAW_POST_DATA'] = array();
-        
-        while(1)
-        {
-            if(!$data)
-            {
-                break;
-            }
-            $headers = unpack(
-                    "Cversion/".
-                    "Ctype/".
-                    "nrequestId/".
-                    "ncontentLength/".
-                    "CpaddingLength/".
-                    "Creserved/"
-                    , $data);
-            
-            // 获得环境变量等
-            if($headers['type'] == self::PARAMS)
-            {
-                // 解析名-值
-                $offset = self::HEAD_LENGTH;
-                while($offset + $headers['paddingLength'] < $headers['contentLength'])
-                {
-                    $namelen = ord($data[$offset++]);
-                    // 127字节或更少的长度能在一字节中编码,而更长的长度总是在四字节中编码
-                    if($namelen > 127)
-                    {
-                        $namelen = (($namelen & 0x7f) << 24) +
-                        (ord($data[$offset++]) << 16) +
-                        (ord($data[$offset++]) << 8) +
-                        ord($data[$offset++]);
-                    }
-            
-                    // 值的长度
-                    $valuelen = ord($data[$offset++]);
-                    if($valuelen > 127)
-                    {
-                        $valuelen = (($valuelen & 0x7f) << 24) +
-                        (ord($data[$offset++]) << 16) +
-                        (ord($data[$offset++]) << 8) +
-                        ord($data[$offset++]);
-                    }
-            
-                    // 名
-                    $name = substr($data, $offset, $namelen);
-                    $offset += $namelen;
-                    $value = substr($data, $offset, $valuelen);
-                    $offset += $valuelen;
-                    $params[$name] = $value;
-                }
-                
-                // 解析$_SERVER
-                foreach($params as $key=>$value)
-                {
-                    $_SERVER[$key]=$value;
-                }
-                if(array_key_exists('HTTP_COOKIE', $params))
-                {
-                    foreach(explode(';', $params['HTTP_COOKIE']) as $coo)
-                    {
-                        $nameval = explode('=', trim($coo));
-                        $_COOKIE[$nameval[0]] = urldecode($nameval[1]);
-                    }
-                }
-            }
-            elseif($headers['type'] == self::STDIN)
-            {
-                // 为啥是8,还要研究下
-                $data = substr($data, 8, $headers['contentLength']);
-                
-                // 解析$GLOBALS['HTTP_RAW_POST_DATA']
-                $GLOBALS['HTTP_RAW_POST_DATA'] = $data;
-                // 解析POST
-                parse_str($data, $_POST);
-            }
-            
-            $total_length = self::HEAD_LENGTH + $headers['contentLength'] + $headers['paddingLength'];
-            
-            $data = substr($data, $total_length);
-        }
-        
-        // 解析GET
-        parse_str(preg_replace('/^\/.*?\?/', '', $_SERVER['REQUEST_URI']), $_GET);
-        
-        return array('header' => $headers, 'data' => '');
-    }
-    
-    /**
-     * 打包fastcgi协议,用于返回数据给nginx
-     * 
-     * @param array $header
-     * @param string $data
-     * @return string
-     */
-    public static function encode($header, $data)
-    {
-        $data = "Content-type: text/html\r\n\r\n" . $data;
-        $contentLength = strlen($data);
-        $head_data = pack("CCnnxx",
-                self::VERSION_1,
-                self::STDOUT,
-                $header['requestId'],
-                $contentLength
-        );
-        
-        return $head_data.$data;
-    }
-}

+ 0 - 233
Protocols/HTTP.php

@@ -1,233 +0,0 @@
-<?php 
-
-/**
- * http 协议解析 相关
- * 简单的实现,可能会有bug,不要用于生产环境
-* @author walkor <worker-man@qq.com>
-* */
-class HTTP{
-    
-    /**
-     * 构造函数
-     */
-    private  function __construct(){}
-    
-    /**
-     * http头
-     * @var array
-     */
-    public static $header = array();
-    
-    /**
-     * cookie 
-     * @var array
-     */
-    protected static $cookie = array();
-    
-    /**
-     * 判断数据包是否全部接收完成
-     * 
-     * @param string $data
-     * @return int 0:完成 1:还要接收数据
-     */
-    public static function input($data)
-    {
-        // 查找\r\n\r\n
-        $data_length = strlen($data);
-        
-        if(!strpos($data, "\r\n\r\n"))
-        {
-            return 1;
-        }
-        
-        // POST请求还要读包体
-        if(strpos($data, "POST"))
-        {
-            // 找Content-Length
-            $match = array();
-            if(preg_match("/\r\nContent-Length: ?(\d?)\r\n/", $data, $match))
-            {
-                $content_lenght = $match[1];
-            }
-            else
-            {
-                return 0;
-            }
-            
-            // 看包体长度是否符合
-            $tmp = explode("\r\n\r\n", $data);
-            if(strlen($tmp[1]) >= $content_lenght)
-            {
-                return 0;
-            }
-            return 1;
-        }
-        else 
-        {
-            return 0;
-        }
-        
-        // var_export($header_data);
-        return 0;
-    }    
-    
-    /**
-     * 解析http协议包,并设置相应环境变量
-     * 
-     * @param string $data
-     * @return array
-     */
-    public static function decode($data)
-    {
-        $_SERVER = array(
-                'REQUEST_URI'    => '/',
-                'HTTP_HOST'      => '127.0.0.1',
-                'HTTP_COOKIE'    => '',
-                );
-        
-        $_POST = array();
-        $_GET = array();
-        $GLOBALS['HTTP_RAW_POST_DATA'] = array();
-        
-        // 将header分割成数组
-        $header_data = explode("\r\n", $data);
-        
-        // 需要解析$_POST
-        if(strpos($data, "POST") === 0)
-        {
-            $tmp = explode("\r\n\r\n", $data);
-            parse_str($tmp[1], $_POST);
-            
-            // $GLOBALS['HTTP_RAW_POST_DATA']
-            $GLOBALS['HTTP_RAW_POST_DATA'] = $tmp[1];
-        }
-        
-        // REQUEST_URI
-        $tmp = explode(' ', $header_data[0]);
-        $_SERVER['REQUEST_URI'] = isset($tmp[1]) ? $tmp[1] : '/';
-        
-        // PHP_SELF
-        $base_name = basename($_SERVER['REQUEST_URI']);
-        $_SERVER['PHP_SELF'] = empty($base_name) ? 'index.php' : $base_name;
-        
-        unset($header_data[0]);
-        foreach($header_data as $content)
-        {
-            // 解析HTTP_HOST
-            if(strpos($content, 'Host') === 0)
-            {
-                $tmp = explode(':', $content);
-                if(isset($tmp[1]))
-                {
-                    $_SERVER['HTTP_HOST'] = $tmp[1];
-                }
-                if(isset($tmp[2]))
-                {
-                    $_SERVER['SERVER_PORT'] = $tmp[2];
-                }
-            }
-            // 解析Cookie
-            elseif(strpos($content, 'Cookie') === 0)
-            {
-                $tmp = explode(' ', $content);
-                if(isset($tmp[1]))
-                {
-                    $_SERVER['HTTP_COOKIE'] = $tmp[1];
-                }
-            }
-        }
-        
-        // 'REQUEST_TIME_FLOAT' => 1375774613.237,
-        $_SERVER['REQUEST_TIME_FLOAT'] = microtime(true);
-        $_SERVER['REQUEST_TIME'] = intval($_SERVER['REQUEST_TIME_FLOAT']);
-        
-        // GET
-        parse_str(preg_replace('/^\/.*?\?/', '', $_SERVER['REQUEST_URI']), $_GET);
-        unset($_GET['/']);
-        
-    }
-    
-    /**
-     * 设置http头
-     * @return bool
-     */
-    public static function header($content)
-    {
-        if(strpos($content, 'HTTP') === 0)
-        {
-            $key = 'Http-Code';
-        }
-        else
-        {
-            $key = strstr($content, ":", true);
-            if(empty($key))
-            {
-                return false;
-            }
-        }
-        self::$header[$key] = $content;
-        return true;
-    }
-    
-    /**
-     * 
-     * @param string $name
-     * @param string/int $value
-     * @param int $expire
-     */
-    public static function setcookie($name, $value='', $expire=0)
-    {
-        // 待完善
-    }
-    
-    /**
-     * 清除header
-     * @return void
-     */
-    public static function clear()
-    {
-        self::$header = array();
-    }
-    
-    /**
-     * 打包http协议,用于返回数据给nginx
-     * 
-     * @param string $data
-     * @return string
-     */
-    public static function encode($data)
-    {
-        // header
-        $header = "Server: PHPServer/1.0\r\nContent-Length: ".strlen($data)."\r\n";
-        
-        // 没有Content-Type默认给个
-        if(!isset(self::$header['Content-Type']))
-        {
-            $header = "Content-Type: text/html;charset=utf-8\r\n".$header;
-        }
-        
-        // 没有http-code默认给个
-        if(!isset(self::$header['Http-Code']))
-        {
-            $header = "HTTP/1.1 200 OK\r\n".$header;
-        }
-        else
-        {
-            $header = self::$header['Http-Code']."\r\n".$header;
-            unset(self::$header['Http-Code']);
-        }
-        
-        // 其它header
-        foreach(self::$header as $content)
-        {
-            $header .= $content."\r\n";
-        }
-        
-        $header .= "\r\n";
-        
-        self::clear();
-        
-        // 整个http包
-        return $header.$data;
-    }
-}