Skip to content

Commit 1ea38c9

Browse files
committed
Add the possiblity to play with raw request body
Now the Request class may store the request body content as raw, when nosuitable way to parse it into a RequestData was found. This behaviour may change in the future, using WaterPipeConfig.
1 parent abc4775 commit 1ea38c9

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

src/WaterPipe/HTTP/Request/Request.php

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class Request
5656
*/
5757
private $_body;
5858

59+
/**
60+
* @var string
61+
*/
62+
private $_rawBody;
63+
5964
/**
6065
* @var RequestData
6166
*/
@@ -126,21 +131,29 @@ public function setParams(RequestData $params): void
126131
/**
127132
* Returns the request body.
128133
*
129-
* @return RequestData
134+
* @return RequestData|string
130135
*/
131-
public function getBody(): RequestData
136+
public function getBody()
132137
{
133-
return $this->_body;
138+
return $this->_body != null
139+
? $this->_body
140+
: $this->_rawBody;
134141
}
135142

136143
/**
137144
* Sets the request body.
138145
*
139-
* @param RequestData $body
146+
* @param RequestData|string $body
140147
*/
141-
public function setBody(RequestData $body): void
148+
public function setBody($body): void
142149
{
143-
$this->_body = $body;
150+
if ($body instanceof RequestData) {
151+
$this->_body = $body;
152+
$this->_rawBody = null;
153+
} else {
154+
$this->_body = null;
155+
$this->_rawBody = $body;
156+
}
144157
}
145158

146159
/**
@@ -204,16 +217,26 @@ public function isAjax(): bool
204217
public function send()
205218
{
206219
$parameters = $this->getParams()->toArray();
207-
$body = $this->getBody()->toArray();
220+
$body = $this->getBody();
221+
222+
if ($body instanceof RequestData) {
223+
if ($this->_header->getContentType() === "application/json") {
224+
$body = json_encode($body->toArray());
225+
} else {
226+
$body = http_build_query($body->toArray());
227+
}
228+
}
208229

209230
$path = $this->uri->getUri();
210-
$path .= "?" . http_build_query($parameters);
231+
232+
if (count($parameters) > 0)
233+
$path .= "?" . http_build_query($parameters);
211234

212235
if (!($curl = @\curl_init($path)))
213236
throw new RequestException("Unable to initialize cURL.");
214237

215238
register_shutdown_function(function () use (&$curl) {
216-
curl_close($curl);
239+
\curl_close($curl);
217240
});
218241

219242
$headers = new ResponseHeader();
@@ -223,10 +246,8 @@ public function send()
223246
$len = strlen($header);
224247
$header = explode(':', $header, 2);
225248

226-
if (count($header) < 2)
227-
return $len;
228-
229-
$headers->setField(trim($header[0]), trim($header[1]));
249+
if (count($header) >= 2)
250+
$headers->setField(trim($header[0]), trim($header[1]));
230251

231252
return $len;
232253
});
@@ -237,17 +258,26 @@ public function send()
237258

238259
case RequestMethod::POST:
239260
\curl_setopt($curl, CURLOPT_POST, true);
240-
\curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($body));
261+
\curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
241262
break;
242263

243264
case RequestMethod::DELETE:
244265
\curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
245-
\curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($body));
266+
\curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
246267
break;
247268

248269
case RequestMethod::PUT:
249270
\curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
250-
\curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($body));
271+
\curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
272+
break;
273+
274+
case RequestMethod::PATCH:
275+
\curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
276+
\curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
277+
break;
278+
279+
case RequestMethod::HEAD:
280+
\curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "HEAD");
251281
break;
252282

253283
default:

src/WaterPipe/Routing/Router.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private function _detectUri()
173173

174174
$this->_request->setParams(new RequestData($_GET));
175175

176-
$data = array();
176+
$data = new RequestData();
177177

178178
$contentType = trim(explode(";", $this->_request->getHeader()->getContentType())[0]);
179179

@@ -187,32 +187,40 @@ private function _detectUri()
187187

188188
switch ($contentType) {
189189
case "application/json":
190-
$data = json_decode($rawData, true);
190+
$data = new RequestData(json_decode($rawData, true));
191191
break;
192192
case "application/xml":
193-
$data = (array)simplexml_load_string($rawData);
193+
$data = new RequestData((array)simplexml_load_string($rawData));
194194
break;
195195
case "application/x-www-form-urlencoded":
196196
parse_str($rawData, $data);
197+
$data = new RequestData($data);
198+
break;
199+
default:
200+
$data = $rawData;
197201
break;
198202
}
199203
} elseif ($this->_request->getMethod() !== RequestMethod::GET) {
200204
$rawData = file_get_contents("php://input");
201205

202206
switch ($contentType) {
203207
case "application/json":
204-
$data = json_decode($rawData, true);
208+
$data = new RequestData(json_decode($rawData, true));
205209
break;
206210
case "application/xml":
207-
$data = (array)simplexml_load_string($rawData);
211+
$data = new RequestData((array)simplexml_load_string($rawData));
208212
break;
209213
case "application/x-www-form-urlencoded":
210214
parse_str($rawData, $data);
215+
$data = new RequestData($data);
211216
break;
212-
}
217+
default:
218+
$data = $rawData;
219+
break;
220+
}
213221
}
214222

215-
$this->_request->setBody(new RequestData($data));
223+
$this->_request->setBody($data);
216224

217225
$this->_request->setCookies(new RequestData($_COOKIE));
218226

0 commit comments

Comments
 (0)