Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php namespace Swoole\Http; use Swoole; /** * Class ExtParser * pecl_http * @package..

Decoded Output download

<?php
namespace Swoole\Http;

use Swoole;

/**
 * Class ExtParser
 * pecl_http
 * @package Swoole\Http
 */
class Parser
{
    const HTTP_EOF = "

";

    protected $buffer;

    /**
     * 
     * @param $data
     * @return array
     */
    static function parseHeader($data)
    {
        $header = array();
        $header[0] = array();
        $meta = &$header[0];
        $parts = explode("

", $data, 2);

        // parts[0] = HTTP;
        // parts[1] = HTTPGETbody
        $headerLines = explode("
", $parts[0]);

        // HTTP,[RFC-2616 5.1]
        list($meta['method'], $meta['uri'], $meta['protocol']) = explode(' ', $headerLines[0], 3);

        //HTTP
        if (empty($meta['method']) or empty($meta['uri']) or empty($meta['protocol']))
        {
            return false;
        }
        unset($headerLines[0]);
        //Header
        $header = array_merge($header, self::parseHeaderLine($headerLines));
        return $header;
    }

    /**
     * 
     * @param $headerLines string/array
     * @return array
     */
    static function parseHeaderLine($headerLines)
    {
        if (is_string($headerLines))
        {
            $headerLines = explode("
", $headerLines);
        }
        $header = array();
        foreach ($headerLines as $_h)
        {
            $_h = trim($_h);
            if (empty($_h)) continue;
            $_r = explode(':', $_h, 2);
            // 
            $keys = explode('-', $_r[0]);
            $keys = array_map("ucfirst", $keys);
            $key = implode('-', $keys);
            $value = isset($_r[1])?$_r[1]:'';
            $header[trim($key)] = trim($value);
        }
        return $header;
    }

    static function parseParams($str)
    {
        $params = array();
        $blocks = explode(";", $str);
        foreach ($blocks as $b)
        {
            $_r = explode("=", $b, 2);
            if(count($_r)==2)
            {
                list ($key, $value) = $_r;
                $params[trim($key)] = trim($value, "
 	\"");
            }
            else
            {
                $params[$_r[0]] = '';
            }
        }
        return $params;
    }

    /**
     * Body
     * @param $request Swoole\Request
     */
    function parseBody(Swoole\Request $request)
    {
        $cd = strstr($request->header['Content-Type'], 'boundary');
        if (isset($request->header['Content-Type']) and $cd !== false)
        {
            $this->parseFormData($request, $cd);
        }
        else
        {
            if (substr($request->header['Content-Type'], 0, 33) == 'application/x-www-form-urlencoded')
            {
                parse_str($request->body, $request->post);
            }
        }
    }
    /**
     * Cookies
     * @param $request Swoole\Request
     */
    function parseCookie(Swoole\Request $request)
    {
        $request->cookie = self::parseParams($request->header['Cookie']);
    }

    /**
     * form_data
     * @param $part
     * @param $request Swoole\Request
     * @param $cd
     * @return unknown_type
     */
    static function parseFormData(Swoole\Request $request, $cd)
    {
        $cd = '--' . str_replace('boundary=', '', $cd);
        $form = explode($cd, rtrim($request->body, "-")); //--
        foreach ($form as $f)
        {
            if ($f === '') continue;
            $parts = explode("

", trim($f));
            $header = self::parseHeaderLine($parts[0]);
            if (!isset($header['Content-Disposition'])) continue;
            $meta = self::parseParams($header['Content-Disposition']);
            //filename
            if (!isset($meta['filename']))
            {
                if(count($parts) < 2) $parts[1] = "";
                //checkbox
                if (substr($meta['name'], -2) === '[]') $request->post[substr($meta['name'], 0, -2)][] = trim($parts[1]);
                else $request->post[$meta['name']] = trim($parts[1], "
");
            }
            else
            {
                $file = trim($parts[1]);
                $tmp_file = tempnam('/tmp', 'sw');
                file_put_contents($tmp_file, $file);
                if (!isset($meta['name'])) $meta['name'] = 'file';
                $request->files[$meta['name']] = array('name' => $meta['filename'],
                    'type' => $header['Content-Type'],
                    'size' => strlen($file),
                    'error' => UPLOAD_ERR_OK,
                    'tmp_name' => $tmp_file);
            }
        }
    }

    /**
     * http
     * @param $data
     * @return array
     */
    function parse($data)
    {
        $_header = strstr($data, self::HTTP_EOF, true);
        if ($_header === false)
        {
            $this->buffer = $data;
        }
        $header = self::parseHeader($_header);
        if ($header === false)
        {
            $this->isError = true;
        }
        $this->header = $header;
        return $header;
    }

}
 ?>

Did this file decode correctly?

Original Code

<?php
namespace Swoole\Http;

use Swoole;

/**
 * Class ExtParser
 * pecl_http
 * @package Swoole\Http
 */
class Parser
{
    const HTTP_EOF = "\r\n\r\n";

    protected $buffer;

    /**
     * 
     * @param $data
     * @return array
     */
    static function parseHeader($data)
    {
        $header = array();
        $header[0] = array();
        $meta = &$header[0];
        $parts = explode("\r\n\r\n", $data, 2);

        // parts[0] = HTTP;
        // parts[1] = HTTPGETbody
        $headerLines = explode("\r\n", $parts[0]);

        // HTTP,[RFC-2616 5.1]
        list($meta['method'], $meta['uri'], $meta['protocol']) = explode(' ', $headerLines[0], 3);

        //HTTP
        if (empty($meta['method']) or empty($meta['uri']) or empty($meta['protocol']))
        {
            return false;
        }
        unset($headerLines[0]);
        //Header
        $header = array_merge($header, self::parseHeaderLine($headerLines));
        return $header;
    }

    /**
     * 
     * @param $headerLines string/array
     * @return array
     */
    static function parseHeaderLine($headerLines)
    {
        if (is_string($headerLines))
        {
            $headerLines = explode("\r\n", $headerLines);
        }
        $header = array();
        foreach ($headerLines as $_h)
        {
            $_h = trim($_h);
            if (empty($_h)) continue;
            $_r = explode(':', $_h, 2);
            // 
            $keys = explode('-', $_r[0]);
            $keys = array_map("ucfirst", $keys);
            $key = implode('-', $keys);
            $value = isset($_r[1])?$_r[1]:'';
            $header[trim($key)] = trim($value);
        }
        return $header;
    }

    static function parseParams($str)
    {
        $params = array();
        $blocks = explode(";", $str);
        foreach ($blocks as $b)
        {
            $_r = explode("=", $b, 2);
            if(count($_r)==2)
            {
                list ($key, $value) = $_r;
                $params[trim($key)] = trim($value, "\r\n \t\"");
            }
            else
            {
                $params[$_r[0]] = '';
            }
        }
        return $params;
    }

    /**
     * Body
     * @param $request Swoole\Request
     */
    function parseBody(Swoole\Request $request)
    {
        $cd = strstr($request->header['Content-Type'], 'boundary');
        if (isset($request->header['Content-Type']) and $cd !== false)
        {
            $this->parseFormData($request, $cd);
        }
        else
        {
            if (substr($request->header['Content-Type'], 0, 33) == 'application/x-www-form-urlencoded')
            {
                parse_str($request->body, $request->post);
            }
        }
    }
    /**
     * Cookies
     * @param $request Swoole\Request
     */
    function parseCookie(Swoole\Request $request)
    {
        $request->cookie = self::parseParams($request->header['Cookie']);
    }

    /**
     * form_data
     * @param $part
     * @param $request Swoole\Request
     * @param $cd
     * @return unknown_type
     */
    static function parseFormData(Swoole\Request $request, $cd)
    {
        $cd = '--' . str_replace('boundary=', '', $cd);
        $form = explode($cd, rtrim($request->body, "-")); //--
        foreach ($form as $f)
        {
            if ($f === '') continue;
            $parts = explode("\r\n\r\n", trim($f));
            $header = self::parseHeaderLine($parts[0]);
            if (!isset($header['Content-Disposition'])) continue;
            $meta = self::parseParams($header['Content-Disposition']);
            //filename
            if (!isset($meta['filename']))
            {
                if(count($parts) < 2) $parts[1] = "";
                //checkbox
                if (substr($meta['name'], -2) === '[]') $request->post[substr($meta['name'], 0, -2)][] = trim($parts[1]);
                else $request->post[$meta['name']] = trim($parts[1], "\r\n");
            }
            else
            {
                $file = trim($parts[1]);
                $tmp_file = tempnam('/tmp', 'sw');
                file_put_contents($tmp_file, $file);
                if (!isset($meta['name'])) $meta['name'] = 'file';
                $request->files[$meta['name']] = array('name' => $meta['filename'],
                    'type' => $header['Content-Type'],
                    'size' => strlen($file),
                    'error' => UPLOAD_ERR_OK,
                    'tmp_name' => $tmp_file);
            }
        }
    }

    /**
     * http
     * @param $data
     * @return array
     */
    function parse($data)
    {
        $_header = strstr($data, self::HTTP_EOF, true);
        if ($_header === false)
        {
            $this->buffer = $data;
        }
        $header = self::parseHeader($_header);
        if ($header === false)
        {
            $this->isError = true;
        }
        $this->header = $header;
        return $header;
    }

}

Function Calls

None

Variables

None

Stats

MD5 d6c2ae75d172902833a20b616c1e4c01
Eval Count 0
Decode Time 103 ms