1 | <?php
|
---|
2 | /**
|
---|
3 | * Base class for HTTP_Request2 adapters
|
---|
4 | *
|
---|
5 | * PHP version 5
|
---|
6 | *
|
---|
7 | * LICENSE:
|
---|
8 | *
|
---|
9 | * Copyright (c) 2008-2012, Alexey Borzov <avb@php.net>
|
---|
10 | * All rights reserved.
|
---|
11 | *
|
---|
12 | * Redistribution and use in source and binary forms, with or without
|
---|
13 | * modification, are permitted provided that the following conditions
|
---|
14 | * are met:
|
---|
15 | *
|
---|
16 | * * Redistributions of source code must retain the above copyright
|
---|
17 | * notice, this list of conditions and the following disclaimer.
|
---|
18 | * * Redistributions in binary form must reproduce the above copyright
|
---|
19 | * notice, this list of conditions and the following disclaimer in the
|
---|
20 | * documentation and/or other materials provided with the distribution.
|
---|
21 | * * The names of the authors may not be used to endorse or promote products
|
---|
22 | * derived from this software without specific prior written permission.
|
---|
23 | *
|
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
---|
25 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
---|
26 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
27 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
---|
28 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
30 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
31 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
32 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
35 | *
|
---|
36 | * @category HTTP
|
---|
37 | * @package HTTP_Request2
|
---|
38 | * @author Alexey Borzov <avb@php.net>
|
---|
39 | * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
---|
40 | * @version SVN: $Id: Adapter.php 324415 2012-03-21 10:50:50Z avb $
|
---|
41 | * @link http://pear.php.net/package/HTTP_Request2
|
---|
42 | */
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Class representing a HTTP response
|
---|
46 | */
|
---|
47 | require_once 'HTTP/Request2/Response.php';
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Base class for HTTP_Request2 adapters
|
---|
51 | *
|
---|
52 | * HTTP_Request2 class itself only defines methods for aggregating the request
|
---|
53 | * data, all actual work of sending the request to the remote server and
|
---|
54 | * receiving its response is performed by adapters.
|
---|
55 | *
|
---|
56 | * @category HTTP
|
---|
57 | * @package HTTP_Request2
|
---|
58 | * @author Alexey Borzov <avb@php.net>
|
---|
59 | * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
---|
60 | * @version Release: 2.1.1
|
---|
61 | * @link http://pear.php.net/package/HTTP_Request2
|
---|
62 | */
|
---|
63 | abstract class HTTP_Request2_Adapter
|
---|
64 | {
|
---|
65 | /**
|
---|
66 | * A list of methods that MUST NOT have a request body, per RFC 2616
|
---|
67 | * @var array
|
---|
68 | */
|
---|
69 | protected static $bodyDisallowed = array('TRACE');
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Methods having defined semantics for request body
|
---|
73 | *
|
---|
74 | * Content-Length header (indicating that the body follows, section 4.3 of
|
---|
75 | * RFC 2616) will be sent for these methods even if no body was added
|
---|
76 | *
|
---|
77 | * @var array
|
---|
78 | * @link http://pear.php.net/bugs/bug.php?id=12900
|
---|
79 | * @link http://pear.php.net/bugs/bug.php?id=14740
|
---|
80 | */
|
---|
81 | protected static $bodyRequired = array('POST', 'PUT');
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Request being sent
|
---|
85 | * @var HTTP_Request2
|
---|
86 | */
|
---|
87 | protected $request;
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Request body
|
---|
91 | * @var string|resource|HTTP_Request2_MultipartBody
|
---|
92 | * @see HTTP_Request2::getBody()
|
---|
93 | */
|
---|
94 | protected $requestBody;
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Length of the request body
|
---|
98 | * @var integer
|
---|
99 | */
|
---|
100 | protected $contentLength;
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Sends request to the remote server and returns its response
|
---|
104 | *
|
---|
105 | * @param HTTP_Request2 $request HTTP request message
|
---|
106 | *
|
---|
107 | * @return HTTP_Request2_Response
|
---|
108 | * @throws HTTP_Request2_Exception
|
---|
109 | */
|
---|
110 | abstract public function sendRequest(HTTP_Request2 $request);
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Calculates length of the request body, adds proper headers
|
---|
114 | *
|
---|
115 | * @param array &$headers associative array of request headers, this method
|
---|
116 | * will add proper 'Content-Length' and 'Content-Type'
|
---|
117 | * headers to this array (or remove them if not needed)
|
---|
118 | */
|
---|
119 | protected function calculateRequestLength(&$headers)
|
---|
120 | {
|
---|
121 | $this->requestBody = $this->request->getBody();
|
---|
122 |
|
---|
123 | if (is_string($this->requestBody)) {
|
---|
124 | $this->contentLength = strlen($this->requestBody);
|
---|
125 | } elseif (is_resource($this->requestBody)) {
|
---|
126 | $stat = fstat($this->requestBody);
|
---|
127 | $this->contentLength = $stat['size'];
|
---|
128 | rewind($this->requestBody);
|
---|
129 | } else {
|
---|
130 | $this->contentLength = $this->requestBody->getLength();
|
---|
131 | $headers['content-type'] = 'multipart/form-data; boundary=' .
|
---|
132 | $this->requestBody->getBoundary();
|
---|
133 | $this->requestBody->rewind();
|
---|
134 | }
|
---|
135 |
|
---|
136 | if (in_array($this->request->getMethod(), self::$bodyDisallowed)
|
---|
137 | || 0 == $this->contentLength
|
---|
138 | ) {
|
---|
139 | // No body: send a Content-Length header nonetheless (request #12900),
|
---|
140 | // but do that only for methods that require a body (bug #14740)
|
---|
141 | if (in_array($this->request->getMethod(), self::$bodyRequired)) {
|
---|
142 | $headers['content-length'] = 0;
|
---|
143 | } else {
|
---|
144 | unset($headers['content-length']);
|
---|
145 | // if the method doesn't require a body and doesn't have a
|
---|
146 | // body, don't send a Content-Type header. (request #16799)
|
---|
147 | unset($headers['content-type']);
|
---|
148 | }
|
---|
149 | } else {
|
---|
150 | if (empty($headers['content-type'])) {
|
---|
151 | $headers['content-type'] = 'application/x-www-form-urlencoded';
|
---|
152 | }
|
---|
153 | $headers['content-length'] = $this->contentLength;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 | ?>
|
---|