Example #1
0
 function test_get_redirection()
 {
     $url = $this->get_test_server_url() . '?redirect=true';
     $response_headers = array();
     $response_body = '';
     p2mixi_http_get($url, array(), $response_headers, $response_body);
     Assert::equals(strlen($response_body), 5221, '');
 }
Example #2
0
function p2mixi_http_get($url, $request_headers, &$response_headers, &$response_body, $retries = 0)
{
    global $p2mixi_debug;
    $url_comps = parse_url($url);
    if (!isset($url_comps['port'])) {
        $url_comps['port'] = 80;
    }
    $sock = new p2mixi_TinyHttpSocket($url_comps['host'], $url_comps['port']);
    $sock->setDebugMode($p2mixi_debug);
    if (!$sock->connect()) {
        error_log("p2mixi_http_get: fsockopen failed: {$errstr} ( {$errno} )");
        return false;
    } else {
        $request_headers["Host"] = $url_comps['host'];
        $sock->send("GET", $url, $request_headers);
        $sock->recv($response_headers, $response_body);
        if ($p2mixi_debug) {
            error_log("p2mixi_TinyHttpClient.get:  socket recv end ");
        }
        if ($response_headers["Status-Code"] == 403 && $p2mixi_debug) {
            error_log("p2mixi_http_get:  Body: {$response_body} ");
        }
        // Redirection support
        if (isset($response_headers["Status-Code"])) {
            $code = $response_headers["Status-Code"];
            switch ($code) {
                case 300 <= $code && $code <= 303 || $code == 307:
                    if (isset($response_headers["Location"])) {
                        $location = $response_headers["Location"];
                        if ($p2mixi_debug) {
                            error_log("p2mixi_http_get: Redirecting({$retries} retries so far): {$location}");
                        }
                        //					if ( isset( $response_headers['cookies'] ) ) {
                        //						if ( $p2mixi_debug ) error_log( "p2mixi_http_get: Setting cookies :". p2mixi_constrcut_cookies_string( $response_headers['cookies'] ) );
                        //						$request_headers['Cookie'] = p2mixi_construct_cookies_string( $response_headers['cookies'] );
                        //					}
                        p2mixi_http_get($location, $request_headers, $response_headers, $response_body, $retries + 1);
                    }
                    break;
            }
        }
        return true;
    }
}