Page MenuHomePhabricator

No OneTemporary

diff --git a/.gitignore b/.gitignore
index 57872d0..34f7f8f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
/vendor/
+/nbproject/private
\ No newline at end of file
diff --git a/src/Request.php b/src/Request.php
index f144dde..9c70112 100644
--- a/src/Request.php
+++ b/src/Request.php
@@ -1,122 +1,155 @@
<?php namespace spitfire\io\request;
use magic3w\http\url\reflection\URLReflection;
/*
* The MIT License
*
* Copyright 2018 César de la Cal Bretschneider <cesar@magic3w.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class Request
{
private $method = 'GET';
private $url;
private $headers = [];
private $body = [];
private $follow = true;
private $stream = false;
+ /**
+ * The user agent allows the application issuing this request to define which user
+ * agent the request should use to send the request. This is specially useful when
+ * the application whishes to proxy a request for a user, or when writing an app
+ * that crawls the web (and wishes to identify itself as a spider).
+ *
+ * @var string
+ */
+ private $userAgent = null;
+
public function __construct($url) {
$this->url = URLReflection::fromURL($url);
}
public function url($url) {
$this->url = URLReflection::fromURL($url);
}
public function header($name, $value) {
+
+ /**
+ * While HTTP makes no distinction, cURL does indeed have a separate setting for the
+ * user agent. When the user is trying to set the user agent as a header (which is a
+ * legitimate expectation), we direct it to the appropriate method.
+ */
+ if ($name == 'User-Agent') {
+ return $this->userAgent($value);
+ }
+
$this->headers[$name] = $value;
return $this;
}
+
+ public function userAgent($set) {
+ $this->userAgent = $set;
+ return $this;
+ }
public function follow($set = true) {
$this->follow = !!$set;
}
public function get($parameter, $value = null) {
$qs = $this->url->getQueryString();
$qs[$parameter] = $value;
$this->url->setQueryString($qs);
return $this;
}
public function post($parameter, $value = null) {
$this->method = 'POST';
if ($value === null) {
$this->body = $parameter;
}
else {
$this->body[$parameter] = $value;
}
return $this;
}
public function stream($callable) {
$this->stream = function ($ign, $d) use ($callable) { return (int)$callable($d); };
return $this;
}
public function send($progress = null) {
$ch = curl_init((string)$this->url);
if ($this->stream) {
curl_setopt($ch, CURLOPT_WRITEFUNCTION, $this->stream);
}
else {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
if (!empty($this->body)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
}
+
+ /*
+ * If the user provided a user agent for the request, the request is sent with the user
+ * choice. Otherwise, we let cURL pick it's default user-agent string.
+ */
+ if ($this->userAgent) {
+ curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
+ }
/*
* Set the appropriate headers for the request (in case we need some special
* headers that do not conform)
*/
curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function ($k, $v) {
return "{$k}:{$v}";
}, array_keys($this->headers), $this->headers));
$progress && curl_setopt($ch, CURLOPT_NOPROGRESS, false);
$progress && curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, $progress);
$this->follow && curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow);
$_ret = curl_exec($ch);
$meta = curl_getinfo($ch);
return new Response($meta['http_code'], $_ret, $meta['content_type']);
}
}

File Metadata

Mime Type
text/x-diff
Expires
Wed, Apr 14, 9:18 PM (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5331
Default Alt Text
(4 KB)

Event Timeline