PHP’s HttpRequest object – delayed and fragmented requests

Having trouble receiving HTTP requests made by PHP’s HttpRequest object? Well I did… A HTTP request made by a proxy service written in PHP was being send to a Web-Interface-Server written in QT, and it mostly worked fine, but every now and then, and almost always with large requests (with lots of POST parameter data) it became laggy, and lasted for 2 seconds without any good reason.

The problem appeared to be that the request came in two parts, and the second part was send about 2 seconds after the first one. After a colleague of mine point out a solution to this same problem when using curl, I fixed the problem with the HttpRequest in the same manner.

Curl solution:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

HttpRequest equivalent:

$r->setOptions
(
    array
    (
        'headers' => array('Expect:' => ''),    // send the request as a whole
    )
);

What does this actually mean? Well, when making a request, either by curl or by HttpRequest object, by default, header contains “Expect: 100″ parameter, meaning that the server should return HTTP code 100 before receiving the other part of the request. As this wasn’t the case on our server, we had slow and delayed HTTP requests (and I wonder why there was any in the first place). By removing or at least modifying the Expect parameter to have no value rather than 100, we fixed the issue. I hope this helps you out…

Whole request code:

$r = new HttpRequest($requestUrl, HttpRequest::METH_POST);

// configure the request options
$r->setOptions
(
    array
    (
        'headers' => array('Expect:' => ''),    // remove the expect parameter
    )
);

$r->addPostFields(array('proxy' => 'proxy'));    // add some post parameters

try
{
    // make the request
    $r->send();

    // check the return code and render contents or an error message
    if ($r->getResponseCode() == 200)
    {
        $responseBody = $r->getResponseBody();

        echo $responseBody;
        die();
    }
    else
    {
        // handle this yourself
    }
}
catch (HttpException $ex)
{
    // handle this yourself
}

For loop with a delay in JavaScript

I recently had a need to run a simple for loop in JavaScript, but with a delay (or a pause, or sleep…) of few milliseconds in between iterations. It’s not as simple as I thought, but it can be done, with plane old JavaScript. This is that simple for loop I was talking about:

var words = ['Hello', ', ', 'I ', 'am ', 'in ', 'a ', 'loop', '...'];
for(var i in words)
{
    document.getElementById('output').innerHTML += words[i];
    sleep(1000);
}

Simple, yes, but it doesn’t work, however you can make something similar to have this functionality, but it won’t be a loop, and it won’t look this simple, but at least it’s possible.

var words = ['Hello', ', ', 'I ', 'am ', 'in ', 'a ', 'loop', '...'];

// (1) define the variable for the array index
var i = 0;

// (2) define the delayed loop function
function delayedLoop()
{
    // (3) do action
    document.getElementById('output').innerHTML += words[i];

    // (4) if the end of the array has been reached, stop
    if(++i == words.length)
    {
        return;
    }

    // (5) recursively call the delayed loop function with a delay
    window.setTimeout(delayedLoop, 1000);
}
delayedLoop(); // (6) start the loop

What is this example doing:

  1. define the variable for the array index
  2. define the function which will pose for the loop
  3. that function does the same action as in the loop in the first example
  4. make a check if the loop should end
  5. uses the window.setTimeout(function, delay) to recursively start that function again, but with a 1000 ms delay
  6. everything above only defined the delayedLoop funcion, so you need to call it to do anything

Working example below:

Start…

Output area…