1 : <?php
2 : /**
3 : * Base class for HTTP_Request2 adapters
4 : *
5 : * PHP version 5
6 : *
7 : * LICENSE:
8 : *
9 : * Copyright (c) 2008-2011, 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 308322 2011-02-14 13:58:03Z 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 : * @version Release: 2.0.0
60 : */
61 : abstract class HTTP_Request2_Adapter
62 : {
63 : /**
64 : * A list of methods that MUST NOT have a request body, per RFC 2616
65 : * @var array
66 : */
67 : protected static $bodyDisallowed = array('TRACE');
68 :
69 : /**
70 : * Methods having defined semantics for request body
71 : *
72 : * Content-Length header (indicating that the body follows, section 4.3 of
73 : * RFC 2616) will be sent for these methods even if no body was added
74 : *
75 : * @var array
76 : * @link http://pear.php.net/bugs/bug.php?id=12900
77 : * @link http://pear.php.net/bugs/bug.php?id=14740
78 : */
79 : protected static $bodyRequired = array('POST', 'PUT');
80 :
81 : /**
82 : * Request being sent
83 : * @var HTTP_Request2
84 : */
85 : protected $request;
86 :
87 : /**
88 : * Request body
89 : * @var string|resource|HTTP_Request2_MultipartBody
90 : * @see HTTP_Request2::getBody()
91 : */
92 : protected $requestBody;
93 :
94 : /**
95 : * Length of the request body
96 : * @var integer
97 : */
98 : protected $contentLength;
99 :
100 : /**
101 : * Sends request to the remote server and returns its response
102 : *
103 : * @param HTTP_Request2
104 : * @return HTTP_Request2_Response
105 : * @throws HTTP_Request2_Exception
106 : */
107 : abstract public function sendRequest(HTTP_Request2 $request);
108 :
109 : /**
110 : * Calculates length of the request body, adds proper headers
111 : *
112 : * @param array associative array of request headers, this method will
113 : * add proper 'Content-Length' and 'Content-Type' headers
114 : * to this array (or remove them if not needed)
115 : */
116 : protected function calculateRequestLength(&$headers)
117 : {
118 0 : $this->requestBody = $this->request->getBody();
119 :
120 0 : if (is_string($this->requestBody)) {
121 0 : $this->contentLength = strlen($this->requestBody);
122 0 : } elseif (is_resource($this->requestBody)) {
123 0 : $stat = fstat($this->requestBody);
124 0 : $this->contentLength = $stat['size'];
125 0 : rewind($this->requestBody);
126 0 : } else {
127 0 : $this->contentLength = $this->requestBody->getLength();
128 0 : $headers['content-type'] = 'multipart/form-data; boundary=' .
129 0 : $this->requestBody->getBoundary();
130 0 : $this->requestBody->rewind();
131 : }
132 :
133 0 : if (in_array($this->request->getMethod(), self::$bodyDisallowed) ||
134 0 : 0 == $this->contentLength
135 0 : ) {
136 : // No body: send a Content-Length header nonetheless (request #12900),
137 : // but do that only for methods that require a body (bug #14740)
138 0 : if (in_array($this->request->getMethod(), self::$bodyRequired)) {
139 0 : $headers['content-length'] = 0;
140 0 : } else {
141 0 : unset($headers['content-length']);
142 : // if the method doesn't require a body and doesn't have a
143 : // body, don't send a Content-Type header. (request #16799)
144 0 : unset($headers['content-type']);
145 : }
146 0 : } else {
147 0 : if (empty($headers['content-type'])) {
148 0 : $headers['content-type'] = 'application/x-www-form-urlencoded';
149 0 : }
150 0 : $headers['content-length'] = $this->contentLength;
151 : }
152 0 : }
153 : }
154 : ?>
|