| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Http; |
| 4: | |
| 5: | use Psr\Http\Message\MessageInterface as PsrMessageInterface; |
| 6: | use Psr\Http\Message\RequestInterface as PsrRequestInterface; |
| 7: | use Psr\Http\Message\StreamInterface as PsrStreamInterface; |
| 8: | use Psr\Http\Message\UriInterface as PsrUriInterface; |
| 9: | use Salient\Contract\Core\Arrayable; |
| 10: | use Salient\Contract\Core\DateFormatterInterface; |
| 11: | use Salient\Contract\Http\Message\MultipartStreamInterface; |
| 12: | use Salient\Contract\Http\HasFormDataFlag; |
| 13: | use Salient\Contract\Http\HasHttpHeader; |
| 14: | use Salient\Contract\Http\HasMediaType; |
| 15: | use Salient\Contract\Http\HasRequestMethod; |
| 16: | use Salient\Contract\HasHmacHashAlgorithm; |
| 17: | use Salient\Http\Exception\InvalidHeaderException; |
| 18: | use Salient\Http\Internal\FormDataEncoder; |
| 19: | use Salient\Http\Internal\TOTPGenerator; |
| 20: | use Salient\Utility\AbstractUtility; |
| 21: | use Salient\Utility\Date; |
| 22: | use Salient\Utility\Package; |
| 23: | use Salient\Utility\Reflect; |
| 24: | use Salient\Utility\Regex; |
| 25: | use Salient\Utility\Str; |
| 26: | use Salient\Utility\Test; |
| 27: | use DateTimeInterface; |
| 28: | use DateTimeZone; |
| 29: | use Stringable; |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | final class HttpUtil extends AbstractUtility implements |
| 35: | HasFormDataFlag, |
| 36: | HasHmacHashAlgorithm, |
| 37: | HasHttpHeader, |
| 38: | HasHttpRegex, |
| 39: | HasMediaType, |
| 40: | HasRequestMethod |
| 41: | { |
| 42: | |
| 43: | |
| 44: | |
| 45: | private const SUFFIX_TYPE = [ |
| 46: | 'gzip' => self::TYPE_GZIP, |
| 47: | 'json' => self::TYPE_JSON, |
| 48: | 'jwt' => self::TYPE_JWT, |
| 49: | 'xml' => self::TYPE_XML, |
| 50: | 'yaml' => self::TYPE_YAML, |
| 51: | 'zip' => self::TYPE_ZIP, |
| 52: | ]; |
| 53: | |
| 54: | private const ALIAS_TYPE = [ |
| 55: | 'text/xml' => self::TYPE_XML, |
| 56: | ]; |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | public static function getContentLength($headersOrPayload): ?int |
| 65: | { |
| 66: | $headers = Headers::from($headersOrPayload); |
| 67: | if (!$headers->hasHeader(self::HEADER_CONTENT_LENGTH)) { |
| 68: | return null; |
| 69: | } |
| 70: | $length = $headers->getOnlyHeaderValue(self::HEADER_CONTENT_LENGTH, true); |
| 71: | if (!Test::isInteger($length) || (int) $length < 0) { |
| 72: | throw new InvalidHeaderException(sprintf( |
| 73: | 'Invalid value for HTTP header %s: %s', |
| 74: | self::HEADER_CONTENT_LENGTH, |
| 75: | $length, |
| 76: | )); |
| 77: | } |
| 78: | return (int) $length; |
| 79: | } |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | public static function getMultipartBoundary($headersOrPayload): ?string |
| 88: | { |
| 89: | $headers = Headers::from($headersOrPayload); |
| 90: | if (!$headers->hasHeader(self::HEADER_CONTENT_TYPE)) { |
| 91: | return null; |
| 92: | } |
| 93: | $type = $headers->getLastHeaderValue(self::HEADER_CONTENT_TYPE); |
| 94: | return self::getParameters($type, false, false)['boundary'] ?? null; |
| 95: | } |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | public static function getPreferences($headersOrPayload): array |
| 104: | { |
| 105: | $headers = Headers::from($headersOrPayload); |
| 106: | if (!$headers->hasHeader(self::HEADER_PREFER)) { |
| 107: | return []; |
| 108: | } |
| 109: | foreach ($headers->getHeaderValues(self::HEADER_PREFER) as $pref) { |
| 110: | |
| 111: | $params = self::getParameters($pref, true); |
| 112: | if (!$params) { |
| 113: | continue; |
| 114: | } |
| 115: | $value = reset($params); |
| 116: | $name = key($params); |
| 117: | unset($params[$name]); |
| 118: | $prefs[$name] ??= ['value' => $value, 'parameters' => $params]; |
| 119: | } |
| 120: | return $prefs ?? []; |
| 121: | } |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | public static function mergePreferences(array $preferences): string |
| 129: | { |
| 130: | foreach ($preferences as $name => $pref) { |
| 131: | $lower = Str::lower($name); |
| 132: | $prefs[$lower] ??= self::mergeParameters( |
| 133: | is_string($pref) |
| 134: | ? [$name => $pref] |
| 135: | : [$name => $pref['value']] + ($pref['parameters'] ?? []), |
| 136: | ); |
| 137: | } |
| 138: | return implode(', ', $prefs ?? []); |
| 139: | } |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | public static function getRetryAfter($headersOrPayload): ?int |
| 149: | { |
| 150: | $headers = Headers::from($headersOrPayload); |
| 151: | $after = $headers->getHeaderLine(self::HEADER_RETRY_AFTER); |
| 152: | if (!Test::isInteger($after)) { |
| 153: | $after = strtotime($after); |
| 154: | return $after === false |
| 155: | ? null |
| 156: | : max(0, $after - time()); |
| 157: | } |
| 158: | return (int) $after < 0 |
| 159: | ? null |
| 160: | : (int) $after; |
| 161: | } |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | public static function getParameters( |
| 169: | string $value, |
| 170: | bool $firstIsParameter = false, |
| 171: | bool $unquote = true, |
| 172: | bool $strict = false |
| 173: | ): array { |
| 174: | foreach (Str::splitDelimited(';', $value, false) as $i => $param) { |
| 175: | if ($i === 0 && !$firstIsParameter) { |
| 176: | $params[] = $unquote |
| 177: | ? self::unquoteString($param) |
| 178: | : $param; |
| 179: | } elseif (Regex::match( |
| 180: | '/^(' . self::HTTP_TOKEN . ')(?:\h*+=\h*+(.*))?$/D', |
| 181: | $param, |
| 182: | $matches, |
| 183: | )) { |
| 184: | $param = $matches[2] ?? ''; |
| 185: | $params[Str::lower($matches[1])] = $unquote |
| 186: | ? self::unquoteString($param) |
| 187: | : $param; |
| 188: | } elseif ($strict) { |
| 189: | throw new InvalidHeaderException( |
| 190: | sprintf('Invalid HTTP header parameter: %s', $param), |
| 191: | ); |
| 192: | } |
| 193: | } |
| 194: | return $params ?? []; |
| 195: | } |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | public static function mergeParameters(array $parameters): string |
| 203: | { |
| 204: | if (!$parameters) { |
| 205: | return ''; |
| 206: | } |
| 207: | |
| 208: | foreach ($parameters as $key => $param) { |
| 209: | $value = is_int($key) ? [] : [$key]; |
| 210: | if ($param !== '') { |
| 211: | $value[] = self::maybeQuoteString($param); |
| 212: | } |
| 213: | $merged[] = $last = implode('=', $value); |
| 214: | } |
| 215: | $merged = implode('; ', $merged); |
| 216: | |
| 217: | return $last === '' && $merged !== '' |
| 218: | ? substr($merged, 0, -1) |
| 219: | : $merged; |
| 220: | } |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | |
| 227: | public static function isRequestMethod(string $method): bool |
| 228: | { |
| 229: | return Reflect::hasConstantWithValue(HasRequestMethod::class, $method); |
| 230: | } |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: | |
| 241: | public static function isAuthorityForm(string $target): bool |
| 242: | { |
| 243: | return (bool) Regex::match(self::AUTHORITY_FORM_REGEX, $target); |
| 244: | } |
| 245: | |
| 246: | |
| 247: | |
| 248: | |
| 249: | |
| 250: | |
| 251: | |
| 252: | |
| 253: | public static function mediaTypeIs(string $type, string $mimeType): bool |
| 254: | { |
| 255: | |
| 256: | [$type] = explode(';', $type, 2); |
| 257: | $type = Str::lower(rtrim($type)); |
| 258: | if ((self::ALIAS_TYPE[$type] ?? $type) === $mimeType) { |
| 259: | return true; |
| 260: | } |
| 261: | |
| 262: | |
| 263: | $pos = strrpos($type, '+'); |
| 264: | if ($pos !== false) { |
| 265: | $suffix = substr($type, $pos + 1); |
| 266: | $type = substr($type, 0, $pos); |
| 267: | return (self::ALIAS_TYPE[$type] ?? $type) === $mimeType |
| 268: | || (self::SUFFIX_TYPE[$suffix] ?? null) === $mimeType; |
| 269: | } |
| 270: | return false; |
| 271: | } |
| 272: | |
| 273: | |
| 274: | |
| 275: | |
| 276: | public static function getDate(?DateTimeInterface $date = null): string |
| 277: | { |
| 278: | return Date::immutable($date) |
| 279: | ->setTimezone(new DateTimeZone('UTC')) |
| 280: | ->format('D, d M Y H:i:s \G\M\T'); |
| 281: | } |
| 282: | |
| 283: | |
| 284: | |
| 285: | |
| 286: | |
| 287: | public static function getProduct(): string |
| 288: | { |
| 289: | return sprintf( |
| 290: | '%s/%s php/%s', |
| 291: | str_replace('/', '~', Package::name()), |
| 292: | Package::version(true, true), |
| 293: | \PHP_VERSION, |
| 294: | ); |
| 295: | } |
| 296: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | public static function getMultipartMediaType(MultipartStreamInterface $stream): string |
| 301: | { |
| 302: | return sprintf( |
| 303: | '%s; boundary=%s', |
| 304: | self::TYPE_FORM_MULTIPART, |
| 305: | self::maybeQuoteString($stream->getBoundary()), |
| 306: | ); |
| 307: | } |
| 308: | |
| 309: | |
| 310: | |
| 311: | |
| 312: | |
| 313: | public static function maybeQuoteString(string $string): string |
| 314: | { |
| 315: | return Regex::match(self::HTTP_TOKEN_REGEX, $string) |
| 316: | ? $string |
| 317: | : self::quoteString($string); |
| 318: | } |
| 319: | |
| 320: | |
| 321: | |
| 322: | |
| 323: | |
| 324: | public static function quoteString(string $string): string |
| 325: | { |
| 326: | return '"' . str_replace(['\\', '"'], ['\\\\', '\"'], $string) . '"'; |
| 327: | } |
| 328: | |
| 329: | |
| 330: | |
| 331: | |
| 332: | |
| 333: | public static function unquoteString(string $string): string |
| 334: | { |
| 335: | $string = Regex::replace('/^"(.*)"$/D', '$1', $string, -1, $count); |
| 336: | return $count |
| 337: | ? Regex::replace('/\\\\(.)/', '$1', $string) |
| 338: | : $string; |
| 339: | } |
| 340: | |
| 341: | |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | |
| 348: | |
| 349: | |
| 350: | |
| 351: | public static function mergeQuery( |
| 352: | $value, |
| 353: | array $data, |
| 354: | int $flags = HttpUtil::DATA_PRESERVE_NUMERIC_KEYS | HttpUtil::DATA_PRESERVE_STRING_KEYS, |
| 355: | ?DateFormatterInterface $dateFormatter = null |
| 356: | ) { |
| 357: | |
| 358: | parse_str(($value instanceof PsrRequestInterface |
| 359: | ? $value->getUri() |
| 360: | : ($value instanceof PsrUriInterface |
| 361: | ? $value |
| 362: | : new Uri((string) $value)))->getQuery(), $query); |
| 363: | $data = array_replace_recursive($query, $data); |
| 364: | return self::replaceQuery($value, $data, $flags, $dateFormatter); |
| 365: | } |
| 366: | |
| 367: | |
| 368: | |
| 369: | |
| 370: | |
| 371: | |
| 372: | |
| 373: | |
| 374: | |
| 375: | |
| 376: | |
| 377: | public static function replaceQuery( |
| 378: | $value, |
| 379: | array $data, |
| 380: | int $flags = HttpUtil::DATA_PRESERVE_NUMERIC_KEYS | HttpUtil::DATA_PRESERVE_STRING_KEYS, |
| 381: | ?DateFormatterInterface $dateFormatter = null |
| 382: | ) { |
| 383: | $query = (new FormDataEncoder($flags, $dateFormatter))->getQuery($data); |
| 384: | return $value instanceof PsrRequestInterface |
| 385: | ? $value->withUri($value->getUri()->withQuery($query)) |
| 386: | : ($value instanceof PsrUriInterface |
| 387: | ? $value |
| 388: | : new Uri((string) $value))->withQuery($query); |
| 389: | } |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: | public static function getStreamContents(PsrStreamInterface $from): string |
| 395: | { |
| 396: | $buffer = ''; |
| 397: | while (!$from->eof()) { |
| 398: | $data = $from->read(1048576); |
| 399: | if ($data === '') { |
| 400: | break; |
| 401: | } |
| 402: | $buffer .= $data; |
| 403: | } |
| 404: | return $buffer; |
| 405: | } |
| 406: | |
| 407: | |
| 408: | |
| 409: | |
| 410: | public static function copyStream(PsrStreamInterface $from, PsrStreamInterface $to): void |
| 411: | { |
| 412: | $buffer = ''; |
| 413: | while (!$from->eof()) { |
| 414: | $data = $from->read(8192); |
| 415: | if ($data === '') { |
| 416: | break; |
| 417: | } |
| 418: | $buffer .= $data; |
| 419: | $buffer = substr($buffer, $to->write($buffer)); |
| 420: | } |
| 421: | while ($buffer !== '') { |
| 422: | $buffer = substr($buffer, $to->write($buffer)); |
| 423: | } |
| 424: | } |
| 425: | |
| 426: | |
| 427: | |
| 428: | |
| 429: | |
| 430: | |
| 431: | |
| 432: | public static function getNameValuePairs(iterable $items): iterable |
| 433: | { |
| 434: | foreach ($items as $item) { |
| 435: | yield $item['name'] => $item['value']; |
| 436: | } |
| 437: | } |
| 438: | |
| 439: | |
| 440: | |
| 441: | |
| 442: | |
| 443: | |
| 444: | |
| 445: | public static function getTOTP( |
| 446: | string $key, |
| 447: | int $digits = 6, |
| 448: | string $algorithm = HttpUtil::ALGORITHM_SHA1, |
| 449: | int $timeStep = 30, |
| 450: | int $startTime = 0 |
| 451: | ): string { |
| 452: | return (new TOTPGenerator($digits, $algorithm, $timeStep, $startTime))->getPassword($key); |
| 453: | } |
| 454: | } |
| 455: | |