1<?php
2/**
3 * Copyright 2007-2016 Horde LLC (http://www.horde.org/)
4 *
5 * @author   Chuck Hagenbuch <chuck@horde.org>
6 * @license  http://www.horde.org/licenses/bsd BSD
7 * @category Horde
8 * @package  Http
9 */
10
11/**
12 * @author   Chuck Hagenbuch <chuck@horde.org>
13 * @license  http://www.horde.org/licenses/bsd BSD
14 * @category Horde
15 * @package  Http
16 */
17class Horde_Http_Request_Peclhttp 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_AUTH_ANY,
27        Horde_Http::AUTH_BASIC => HTTP_AUTH_BASIC,
28        Horde_Http::AUTH_DIGEST => HTTP_AUTH_DIGEST,
29        Horde_Http::AUTH_GSSNEGOTIATE => HTTP_AUTH_GSSNEG,
30        Horde_Http::AUTH_NTLM => HTTP_AUTH_NTLM,
31    );
32
33    /**
34     * Constructor
35     *
36     * @throws Horde_Http_Exception
37     */
38    public function __construct($args = array())
39    {
40        if (!class_exists('HttpRequest', false)) {
41            throw new Horde_Http_Exception('The pecl_http extension is not installed. See http://php.net/http.install');
42        }
43
44        parent::__construct($args);
45    }
46
47    /**
48     * Send this HTTP request
49     *
50     * @throws Horde_Http_Exception
51     * @return Horde_Http_Response_Base
52     */
53    public function send()
54    {
55        if (!defined('HTTP_METH_' . $this->method)) {
56            throw new Horde_Http_Exception('Method ' . $this->method . ' not supported.');
57        }
58
59        $httpRequest = new HttpRequest((string)$this->uri, constant('HTTP_METH_' . $this->method));
60
61        $data = $this->data;
62        if (is_array($data)) {
63            $httpRequest->setPostFields($data);
64        } else {
65            if ($this->method == 'PUT') {
66                $httpRequest->setPutData($data);
67            } else {
68                $httpRequest->setBody($data);
69            }
70        }
71
72        $httpRequest->setOptions($this->_httpOptions());
73
74        try {
75            $httpResponse = $httpRequest->send();
76        } catch (HttpException $e) {
77            if (isset($e->innerException)){
78                throw new Horde_Http_Exception($e->innerException);
79            } else {
80                throw new Horde_Http_Exception($e);
81            }
82        }
83
84        return new Horde_Http_Response_Peclhttp((string)$this->uri, $httpResponse);
85    }
86}
87