Skip to content

Commit 1731b00

Browse files
committed
StreamDriver ready
1 parent 868adab commit 1731b00

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ Temporary Items
4646
.phpintel/
4747
.idea/
4848
.vscode/
49+
cache/*

src/Drivers/StreamDriver.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,41 +78,42 @@ public function exec($url, &$httpStatus, &$contentType, &$errorCode, &$errorMess
7878
$this->context = stream_context_create($options);
7979
}
8080

81-
$handle = fopen($url, 'r', false, $this->context);
81+
$handle = fopen($url, 'rb', false, $this->context);
8282

8383
if ($handle) {
8484
$temp = $this->proxy->getTemporary();
8585
$timeout = $this->timeout;
8686
$timedOut = false;
8787
$start = microtime(true);
8888

89-
$first = true;
90-
$body = false;
89+
$meta_data = stream_get_meta_data($handle);
9190

92-
while (feof($handle) === false) {
93-
if ($timeout < (microtime(true) - $start)) {
94-
$timedOut = true;
95-
$errorCode = 0;
96-
$errorMessage = 'Connection timed out';
97-
break;
91+
foreach ($meta_data['wrapper_data'] as $index => $header) {
92+
if ($index === 0) {
93+
if (preg_match('#HTTP/\d+\.\d+\s+(\d+)#', $header, $match)) {
94+
$httpStatus = (int) $match;
95+
} else {
96+
$httpStatus = 0;
97+
$errorCode = 0;
98+
$errorMessage = 'Invalid response';
99+
break;
100+
}
101+
} elseif (stripos($header, 'content-type:') === 0) {
102+
$contentType = substr($header, 13);
98103
}
104+
}
99105

100-
$content = fgets($handle, 4096);
101-
102-
if ($first) {
103-
$first = false;
104-
105-
if (preg_match('#^HTTP/1\.\d\s+(\d{3})\s+#', $content, $match)) {
106-
$httpStatus = $match[1];
107-
} else {
106+
if ($httpStatus !== 0) {
107+
while (feof($handle) === false) {
108+
if ($timeout < (microtime(true) - $start)) {
109+
$timedOut = true;
110+
$errorCode = 0;
111+
$errorMessage = 'Connection timed out';
108112
break;
109113
}
110-
} elseif ($body) {
114+
115+
$content = fgets($handle, 4096);
111116
fwrite($temp, $content);
112-
} elseif (trim($content) === '') {
113-
$body = true;
114-
} elseif (stripos($content, 'content-type:') === 0) {
115-
$contentType = trim($content);
116117
}
117118
}
118119

src/Proxy.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,12 @@ public function response()
250250

251251
$this->httpCache();
252252

253-
while(feof($this->temporary) === false) {
254-
echo fgets($this->temporary, 4096);
253+
$handle = $this->temporary;
254+
255+
rewind($handle);
256+
257+
while (feof($handle) === false) {
258+
echo fgets($handle, 4096);
255259
}
256260
}
257261

@@ -287,6 +291,8 @@ public function jsonp($callback, $public = false)
287291

288292
$handle = $this->temporary;
289293

294+
rewind($handle);
295+
290296
while (feof($handle) === false) {
291297
$raw = fread($handle, 8151);
292298
echo base64_encode($raw);

0 commit comments

Comments
 (0)