1<?php
2/**
3 * Copyright 2007-2016 Horde LLC (http://www.horde.org/)
4 *
5 * @author   Michael Cramer <michael@bigmichi1.de>
6 * @license  http://www.horde.org/licenses/bsd BSD
7 * @category Horde
8 * @package  Http
9 */
10
11/**
12 * @author   Michael Cramer <michael@bigmichi1.de>
13 * @license  http://www.horde.org/licenses/bsd BSD
14 * @category Horde
15 * @package  Http
16 */
17class Horde_Http_Request_Peclhttp2 extends Horde_Http_Request_PeclhttpBase
18{
19    /**
20     * Map of HTTP authentication schemes from Horde_Http constants to
21     * HTTP_AUTH constants.
22     *
23     * @var array
24     */
25    protected $_httpAuthSchemes = array(
26        Horde_Http::AUTH_ANY => \http\Client\Curl\AUTH_ANY,
27        Horde_Http::AUTH_BASIC => \http\Client\Curl\AUTH_BASIC,
28        Horde_Http::AUTH_DIGEST => \http\Client\Curl\AUTH_DIGEST,
29        Horde_Http::AUTH_GSSNEGOTIATE => \http\Client\Curl\AUTH_GSSNEG,
30        Horde_Http::AUTH_NTLM => \http\Client\Curl\AUTH_NTLM,
31    );
32
33    /**
34     * Map of proxy types from Horde_Http to implementation specific constants.
35     * @var array
36     */
37    protected $_proxyTypes = array(
38        Horde_Http::PROXY_SOCKS4 => \http\Client\Curl\PROXY_SOCKS4,
39        Horde_Http::PROXY_SOCKS5 => \http\Client\Curl\PROXY_SOCKS5
40    );
41
42    /**
43     * Constructor
44     *
45     * @throws Horde_Http_Exception
46     */
47    public function __construct($args = array())
48    {
49        if (!class_exists('\http\Client', false)) {
50            throw new Horde_Http_Exception('The pecl_http extension is not installed. See http://php.net/http.install');
51        }
52
53        parent::__construct($args);
54    }
55
56    /**
57     * Send this HTTP request
58     *
59     * @throws Horde_Http_Exception
60     * @return Horde_Http_Response_Base
61     */
62    public function send()
63    {
64        // at this time only the curl driver is supported
65        $client = new \http\Client('curl');
66
67        $body = new \http\Message\Body();
68        $data = $this->data;
69        if (is_array($data)) {
70            $body->addForm($data);
71        } else {
72            $body->append($data);
73        }
74
75        $httpRequest = new \http\Client\Request($this->method, (string)$this->uri, $this->headers, $body);
76
77        $client->setOptions($this->_httpOptions());
78
79        $client->enqueue($httpRequest);
80
81        try {
82            $client->send();
83            $httpResponse = $client->getResponse($httpRequest);
84        } catch (\http\Exception $e) {
85            throw new Horde_Http_Exception($e);
86        }
87
88        return new Horde_Http_Response_Peclhttp2((string)$this->uri, $httpResponse);
89    }
90}