XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
Adapter.php
Go to the documentation of this file.
1 <?php
24 require_once 'HTTP/Request2/Response.php';
25 
40 abstract class HTTP_Request2_Adapter
41 {
46  protected static $bodyDisallowed = array('TRACE');
47 
58  protected static $bodyRequired = array('POST', 'PUT');
59 
64  protected $request;
65 
71  protected $requestBody;
72 
77  protected $contentLength;
78 
87  abstract public function sendRequest(HTTP_Request2 $request);
88 
96  protected function calculateRequestLength(&$headers)
97  {
98  $this->requestBody = $this->request->getBody();
99 
100  if (is_string($this->requestBody)) {
101  $this->contentLength = strlen($this->requestBody);
102  } elseif (is_resource($this->requestBody)) {
103  $stat = fstat($this->requestBody);
104  $this->contentLength = $stat['size'];
105  rewind($this->requestBody);
106  } else {
107  $this->contentLength = $this->requestBody->getLength();
108  $headers['content-type'] = 'multipart/form-data; boundary=' .
109  $this->requestBody->getBoundary();
110  $this->requestBody->rewind();
111  }
112 
113  if (in_array($this->request->getMethod(), self::$bodyDisallowed)
114  || 0 == $this->contentLength
115  ) {
116  // No body: send a Content-Length header nonetheless (request #12900),
117  // but do that only for methods that require a body (bug #14740)
118  if (in_array($this->request->getMethod(), self::$bodyRequired)) {
119  $headers['content-length'] = 0;
120  } else {
121  unset($headers['content-length']);
122  // if the method doesn't require a body and doesn't have a
123  // body, don't send a Content-Type header. (request #16799)
124  unset($headers['content-type']);
125  }
126  } else {
127  if (empty($headers['content-type'])) {
128  $headers['content-type'] = 'application/x-www-form-urlencoded';
129  }
130  // Content-Length should not be sent for chunked Transfer-Encoding (bug #20125)
131  if (!isset($headers['transfer-encoding'])) {
132  $headers['content-length'] = $this->contentLength;
133  }
134  }
135  }
136 }
137 ?>
sendRequest(HTTP_Request2 $request)
calculateRequestLength(&$headers)
Definition: Adapter.php:96
static $bodyDisallowed
Definition: Adapter.php:46