вторник, август 24, 2010

Response delay problem with PHP OAuth PECL extension

In our situation we have configuration of Apache (httpd.conf) that takes advantage of Keep-Alive extension to HTTP (HTTP/1.1)

When our OAuth consumer application is calling $consumer->getRequestToken(), $consumer->getAccessToken() or $consumer->fetch(), the request goes to the OAuthProvider and the work done by the Provider is finished for little time (under a second), however response to the consumer is coming back after 5 seconds. This is exactly the value set for Keep-Alive in the Apache configuration.

We read again the OAuth documentation and saw that when calling $consumer->fetch(), the fourth parameter (optional) that can be passed to the function is array of HTTP headers. So we passed in "Connection:close" header.
$consumer->fetch('http://example.com/api/', null, OAUTH_HTTP_METHOD_POST, array('Connection'=>'close'));
That solved the problem but only for the fetch function, because the other functions do not provide a way to pass in HTTP headers.

We looked in the oauth.c code (the extension is open-source) and saw this for getRequestToken function:
/* {{{ proto array OAuth::getRequestToken(string request_token_url [, string callback_url ])
   Get request token */
SO_METHOD(getRequestToken)
{
 ...
 ...

 retcode = oauth_fetch(soo, url, oauth_get_http_method(soo, OAUTH_HTTP_METHOD_GET TSRMLS_CC), NULL, NULL, args, 0 TSRMLS_CC);

 ...
 ...
}
Obviously the fourth parameter which is for HTTP headers is always NULL when getRequestToken is called and it's the same for getAccessToken.

The solution that worked for us was to send this header always from the Provider side when returning result to the consumer:
header('HTTP/1.1 200 OK');
header('Content-Type: text/plain');
header('Connection:close');
echo $result;

Няма коментари: