ZF-3288: Custom User-Agent support in Zend_XmlRpc_Client requests

Description

IT IS IMPOSSIBLE TO SET A CUSTOM User-Agent HTTP header with Zend_XmlRpc_Client !!!

According to the HTTP specification, the User-Agent field may contain mulitple product tokens and comments identifying the client. By convention, the product tokens are listed in the order of their significance for identifying the application. The token must be organized as follows "User-Agent: Agent-name/Protocol version/Client version".

It is a very important feature to be incorporated into Zend_XmlRpc_Client class in order to reach proper client identification for XML-RPC services.

I propose a VERY simple change in Zend_XmlRpc_Client class. The only method concerned is doRequest(). Just an extra optional $userAgent param is added. Everything else remains the same.

Below is the changed source code:


    /**
     * Perform an XML-RPC request and return a response.
     *
     * @param Zend_XmlRpc_Request $request
     * @param null|Zend_XmlRpc_Response $response
     * @param string $userAgent
     * @return void
     */
    public function doRequest($request, $response = null, $userAgent = 'Zend_XmlRpc_Client')
    {
        $this->_lastRequest = $request;

        iconv_set_encoding('input_encoding', 'UTF-8');
        iconv_set_encoding('output_encoding', 'UTF-8');
        iconv_set_encoding('internal_encoding', 'UTF-8');

        $http = $this->getHttpClient();
        $http->setUri($this->_serverAddress);

        $http->setHeaders(array(
            'Content-Type: text/xml; charset=utf-8',
            "User-Agent: $userAgent" // HERE IS THE CHANGE added by Mikayel Sukiasyan (userAgent param)
            //'User-Agent: Zend_XmlRpc_Client' // HERE IS THE PREVIOUS CODE
        ));

        $xml = $this->_lastRequest->__toString();
        $http->setRawData($xml);
        $httpResponse = $http->request(Zend_Http_Client::POST);

        if (! $httpResponse->isSuccessful()) {
            throw new Zend_XmlRpc_Client_HttpException(
                                    $httpResponse->getMessage(),
                                    $httpResponse->getStatus());
        }

        if ($response === null) {
            $response = new Zend_XmlRpc_Response();
        }
        $this->_lastResponse = $response;
        $this->_lastResponse->loadXml($httpResponse->getBody());
    }

Comments

Changing to Feature Request and downgrading priority to Minor. The user agent string is already present, so there are no violations of the HTTP spec. The request is to be able to modify the user agent. I do not think using the patch makes sense; instead, it should be done via accessors:


public function setUserAgent($userAgent)
{
}

public function getUserAgent()
{
}

Scheduling for next minor release.

Satoru -- please do not work on this issue. There are some good reasons for some of the variables to be private, and I will work with the maintainer, Mike Naberezny, to address the issues reported.

Hi Matthew. I see. But the issue you say about variables may be ZF-3415. I will reopen ZF-3415 for you.

It's now possible to set a custom UA: either set a custom HTTP client object with your custom user agent or do getHttpClient() and set it accordingly. Fixed in r17752