source: trunk/www.guidonia.net/wp/wp-content/plugins/tubepress/classes/net/php/pear/HTTP/Request2/Adapter.class.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 5.3 KB
Line 
1<?php
2/**
3 * Base class for HTTP_Request2 adapters
4 *
5 * PHP version 5
6 *
7 * LICENSE:
8 *
9 * Copyright (c) 2008, 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 CVS: $Id: Adapter.php,v 1.2 2008/11/17 12:30:12 avb Exp $
41 * @link http://pear.php.net/package/HTTP_Request2
42 */
43
44/**
45 * Class representing a HTTP response
46 */
47
48/**
49 * Base class for HTTP_Request2 adapters
50 *
51 * HTTP_Request2 class itself only defines methods for aggregating the request
52 * data, all actual work of sending the request to the remote server and
53 * receiving its response is performed by adapters.
54 *
55 * @category HTTP
56 * @package HTTP_Request2
57 * @author Alexey Borzov <avb@php.net>
58 * @version Release: 0.1.0
59 */
60abstract class net_php_pear_HTTP_Request2_Adapter
61{
62 /**
63 * A list of methods that MUST NOT have a request body, per RFC 2616
64 * @var array
65 */
66 protected static $bodyDisallowed = array('TRACE');
67
68 /**
69 * Methods having defined semantics for request body
70 *
71 * Content-Length header (indicating that the body follows, section 4.3 of
72 * RFC 2616) will be sent for these methods even if no body was added
73 *
74 * @var array
75 * @link http://pear.php.net/bugs/bug.php?id=12900
76 * @link http://pear.php.net/bugs/bug.php?id=14740
77 */
78 protected static $bodyRequired = array('POST', 'PUT');
79
80 /**
81 * Request being sent
82 * @var HTTP_Request2
83 */
84 protected $request;
85
86 /**
87 * Request body
88 * @var string|resource|net_php_pear_HTTP_Request2_MultipartBody
89 * @see HTTP_Request2::getBody()
90 */
91 protected $requestBody;
92
93 /**
94 * Length of the request body
95 * @var integer
96 */
97 protected $contentLength;
98
99 /**
100 * Sends request to the remote server and returns its response
101 *
102 * @param HTTP_Request2
103 * @return net_php_pear_HTTP_Request2_Response
104 * @throws net_php_pear_HTTP_Request2_Exception
105 */
106 abstract public function sendRequest(net_php_pear_HTTP_Request2 $request);
107
108 /**
109 * Calculates length of the request body, adds proper headers
110 *
111 * @param array associative array of request headers, this method will
112 * add proper 'Content-Length' and 'Content-Type' headers
113 * to this array (or remove them if not needed)
114 */
115 protected function calculateRequestLength(&$headers)
116 {
117 $this->requestBody = $this->request->getBody();
118
119 if (is_string($this->requestBody)) {
120 $this->contentLength = strlen($this->requestBody);
121 } elseif (is_resource($this->requestBody)) {
122 $stat = fstat($this->requestBody);
123 $this->contentLength = $stat['size'];
124 } else {
125 $this->contentLength = $this->requestBody->getLength();
126 $headers['content-type'] = 'multipart/form-data; boundary=' .
127 $this->requestBody->getBoundary();
128 }
129
130 if (in_array($this->request->getMethod(), self::$bodyDisallowed) ||
131 0 == $this->contentLength
132 ) {
133 unset($headers['content-type']);
134 // No body: send a Content-Length header nonetheless (request #12900),
135 // but do that only for methods that require a body (bug #14740)
136 if (in_array($this->request->getMethod(), self::$bodyRequired)) {
137 $headers['content-length'] = 0;
138 } else {
139 unset($headers['content-length']);
140 }
141 } else {
142 if (empty($headers['content-type'])) {
143 $headers['content-type'] = 'application/x-www-form-urlencoded';
144 }
145 $headers['content-length'] = $this->contentLength;
146 }
147 }
148}
149?>
Note: See TracBrowser for help on using the repository browser.