-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCurlHeaderCollector.php
More file actions
149 lines (112 loc) · 3.25 KB
/
CurlHeaderCollector.php
File metadata and controls
149 lines (112 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* Created by PhpStorm.
* User: evaisse
* Date: 19/05/15
* Time: 10:15
*/
namespace evaisse\SimpleHttpBundle\Curl;
use evaisse\SimpleHttpBundle\Curl\Collector\HeaderCollector;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\BrowserKit\Cookie as CookieParser;
class CurlHeaderCollector extends HeaderCollector
{
private $version;
private $code;
private $message;
/**
* @var array
*/
private $headers = [];
/**
* @var array A list of prepended headers that give infos about transaction process
* (i.e. HTTP/1.1 100 Continue, HTTP/1.1 200 Connection established)
*/
private $transactionHeaders = [];
/**
* @var array
*/
private $cookies = [];
public function collect() {
list($handle, $headerString) = func_get_args();
$cleanHeader = trim($headerString);
// The HTTP/1.0 200 OK header is also passed through this function
// and must be parsed differently than the other HTTP headers
if(false !== stripos($cleanHeader,"http/")) {
$this->parseHttp($cleanHeader);
} else {
$this->parseHeader($cleanHeader);
}
return strlen($headerString);
}
/**
* Parse the `HTTP/1.0 200 OK' header into the proper
* Status Code/Message and Protocol Version fields
*
* @param string $header
*/
private function parseHttp($header) {
list($version,$code,$message) = explode(" ", $header);
$this->transactionHeaders[] = $header;
$versionParts = explode("/",$version);
$this->version = end($versionParts);
$this->code = $code;
$this->message = $message;
}
/**
* Parse the standard `Header-name: value' headers into
* individual header name/value pairs
*
* @param string $header
*/
private function parseHeader($header) {
if (empty($header)) {
return;
}
$pos = strpos($header, ": ");
if (false !== $pos) {
$name = trim(substr($header, 0, $pos));
$value = substr($header, $pos+2);
if (strtolower($name) == "set-cookie") {
$cookie = CookieParser::fromString($value);
$this->cookies[] = new Cookie(
$cookie->getName(),
$cookie->getRawValue(),
(int)$cookie->getExpiresTime(),
$cookie->getPath(),
$cookie->getDomain(),
$cookie->isSecure(),
$cookie->isHttpOnly()
);
} else {
$this->headers[$name] = $value;
}
}
}
public function retrieve() {
return $this->headers;
}
public function getVersion() {
return $this->version;
}
public function getMessage() {
return $this->message;
}
public function getCode() {
return $this->code;
}
/**
* @return array|Cookie get a list of Cookie instances
*/
public function getCookies()
{
return $this->cookies;
}
/**
* @return array
*/
public function getTransactionHeaders()
{
return $this->transactionHeaders;
}
}