<commit_before><?php

class Proxy {
    public function __construct($serv, $port = 80, $root = '/', $localRoot = '') {
        // Functions
        function str_replace_once($search, $replace, $subject) { // TODO Credit
            $pos = strpos($subject, $search);
            if ($pos === false) {
                return $subject;
            }

            return substr($subject, 0, $pos) . $replace . substr($subject, $pos + strlen($search));
        }

        if (!function_exists('getallheaders')) { // TODO Credit
            function getallheaders() {
                if (!is_array($_SERVER)) {
                    return array();
                }

                $headers = array();
                foreach ($_SERVER as $name => $value) {
                    if (substr($name, 0, 5) == 'HTTP_') {
                        $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
                    }
                }
                return $headers;
            }
        }

        // Target determination
        $metd = $_SERVER['REQUEST_METHOD'];
        $reqp = $_SERVER['REQUEST_URI'];
        if ($localRoot != '') {
            $reqp = str_replace_once($localRoot, '', $reqp); // TODO Reliable method
        }

        // Preparing request headers
        $reqHeds = "$metd $root$reqp HTTP/1.1\r\n";
        $reqHeds .= "Host: $serv:$port\r\n";

        // Converting client request headers to server request headers
        $reqsHedsC = getallheaders();
        foreach ($reqsHedsC as $name => $content) {
            if ($name != 'Host' && $name != 'Connection') { // TODO More analysis
                $reqHeds .= "$name: $content\r\n";
            }
        }

        if ($metd == 'POST') { // TODO Other with data methods
            if (isset($_POST['payload'])) {
                $postData = stripslashes($_POST['payload']);
                $reqHeds .= "Content-Length: ".strlen($postData)."\r\n";
                $reqHeds .= "Connection: Close\r\n";
                $reqHeds .= "\r\n" . $postData;
            } else {
                $postinfo = '';
                foreach ($_POST as $key => $value) {
                    $postinfo .= $key . '=' . urlencode($value) . '&';
                }
                $postinfo = rtrim($postinfo, '&');
                $reqHeds .= "\r\n" . $postinfo;
            }
        } else {
            $reqHeds .= "Connection: Close\r\n\r\n";
        }


        $fp = fsockopen($serv, $port, $errno, $errstr, 30);
        if (!$fp) { // TODO ErrorCode, ErrorDocument
            echo "Couldn't connect to server\n<br/>$errstr ($errno)<br />\n";
        } else {
            // Sending request
            fwrite($fp, $reqHeds);<commit_msg>Fixed long POST<commit_after><?php

class Proxy {
    public function __construct($serv, $port = 80, $root = '/', $localRoot = '') {
        // Functions
        function str_replace_once($search, $replace, $subject) { // TODO Credit
            $pos = strpos($subject, $search);
            if ($pos === false) {
                return $subject;
            }

            return substr($subject, 0, $pos) . $replace . substr($subject, $pos + strlen($search));
        }

        if (!function_exists('getallheaders')) { // TODO Credit
            function getallheaders() {
                if (!is_array($_SERVER)) {
                    return array();
                }

                $headers = array();
                foreach ($_SERVER as $name => $value) {
                    if (substr($name, 0, 5) == 'HTTP_') {
                        $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
                    }
                }
                return $headers;
            }
        }

        // Target determination
        $metd = $_SERVER['REQUEST_METHOD'];
        $reqp = $_SERVER['REQUEST_URI'];
        if ($localRoot != '') {
            $reqp = str_replace_once($localRoot, '', $reqp); // TODO Reliable method
        }

        // Preparing request headers
        $reqHeds = "$metd $root$reqp HTTP/1.1\r\n";
        $reqHeds .= "Host: $serv:$port\r\n";

        // Converting client request headers to server request headers
        $reqsHedsC = getallheaders();
        foreach ($reqsHedsC as $name => $content) {
            if ($name != 'Host' && $name != 'Connection') { // TODO More analysis
                $reqHeds .= "$name: $content\r\n";
            }
        }

        if ($metd == 'POST') { // TODO Other with data methods
            if (isset($_POST['payload'])) {
                $postData = stripslashes($_POST['payload']);
            } else {
                $postData = '';
                foreach ($_POST as $key => $value) {
                    $postData .= $key . '=' . urlencode($value) . '&';
                }
                $postData = rtrim($postData, '&');
            }
            $reqHeds .= "Content-Length: ".strlen($postData)."\r\n";
            $reqHeds .= "Connection: Close\r\n";
            $reqHeds .= "\r\n" . $postData;
        } else {
            $reqHeds .= "Connection: Close\r\n\r\n";
        }

        $fp = fsockopen($serv, $port, $errno, $errstr, 30);
        if (!$fp) { // TODO ErrorCode, ErrorDocument
            echo "Couldn't connect to server\n<br/>$errstr ($errno)<br />\n";
        } else {
            // Sending request
            fwrite($fp, $reqHeds);
{'fraction_non_alphanumeric': 0.16418732782369147, 'fraction_numerical': 0.0036730945821854912, 'mean_word_length': 1.5039080459770116, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '587400', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1523, 'n_words': 348}