Skip to content

Commit fde978b

Browse files
authored
Init v3.2 (#7278)
1 parent 534ce81 commit fde978b

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
"http-message"
1111
],
1212
"require": {
13-
"php": ">=8.1",
14-
"hyperf/codec": "~3.1.0",
13+
"php": ">=8.2",
14+
"hyperf/codec": "~3.2.0",
1515
"hyperf/engine": "^2.11",
16-
"hyperf/support": "~3.1.0",
17-
"laminas/laminas-mime": "^2.7",
16+
"hyperf/support": "~3.2.0",
1817
"psr/http-message": "^1.0 || ^2.0",
1918
"swow/psr7-plus": "^1.0"
2019
},

src/Base/MessageTrait.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use Hyperf\HttpMessage\Stream\SwooleStream;
1616
use InvalidArgumentException;
17-
use Laminas\Mime\Decode;
1817
use Psr\Http\Message\StreamInterface;
1918
use RuntimeException;
2019
use Throwable;
@@ -299,7 +298,7 @@ public function withBody(StreamInterface $body): static
299298
*/
300299
public function getHeaderField(string $name, string $wantedPart = '0', string $firstName = '0')
301300
{
302-
return Decode::splitHeaderField($this->getHeaderLine($name), $wantedPart, $firstName);
301+
return $this->splitHeaderField($this->getHeaderLine($name), $wantedPart, $firstName);
303302
}
304303

305304
public function getContentType(): string
@@ -447,4 +446,55 @@ private function trimHeaderValues(array $values): array
447446
}
448447
return $result;
449448
}
449+
450+
/**
451+
* split a header field like content type in its different parts.
452+
*
453+
* @param string $field header field
454+
* @param string $wantedPart the wanted part, else an array with all parts is returned
455+
* @param string $firstName key name for the first part
456+
* @return array|string wanted part or all parts as array($firstName => firstPart, partname => value)
457+
* @throws RuntimeException
458+
*/
459+
private function splitHeaderField(string $field, ?string $wantedPart = null, string $firstName = '0'): array|string
460+
{
461+
$wantedPart = strtolower($wantedPart ?? '');
462+
$firstName = strtolower($firstName);
463+
464+
// special case - a bit optimized
465+
if ($firstName === $wantedPart) {
466+
$field = strtok($field, ';');
467+
return $field[0] === '"' ? substr($field, 1, -1) : $field;
468+
}
469+
470+
$field = $firstName . '=' . $field;
471+
if (! preg_match_all('%([^=\s]+)\s*=\s*("[^"]+"|[^;]+)(;\s*|$)%', $field, $matches)) {
472+
throw new RuntimeException('not a valid header field');
473+
}
474+
475+
if ($wantedPart) {
476+
foreach ($matches[1] as $key => $name) {
477+
if (strcasecmp($name, $wantedPart)) {
478+
continue;
479+
}
480+
if ($matches[2][$key][0] !== '"') {
481+
return $matches[2][$key];
482+
}
483+
return substr($matches[2][$key], 1, -1);
484+
}
485+
return '';
486+
}
487+
488+
$split = [];
489+
foreach ($matches[1] as $key => $name) {
490+
$name = strtolower($name);
491+
if ($matches[2][$key][0] === '"') {
492+
$split[$name] = substr($matches[2][$key], 1, -1);
493+
} else {
494+
$split[$name] = $matches[2][$key];
495+
}
496+
}
497+
498+
return $split;
499+
}
450500
}

0 commit comments

Comments
 (0)