| @@ 490-546 (lines=57) @@ | ||
| 487 | * @throws \PHPDaemon\Request\RequestHeadersAlreadySent |
|
| 488 | * @return boolean Success |
|
| 489 | */ |
|
| 490 | public function header($s, $replace = true, $code = false) |
|
| 491 | { |
|
| 492 | if ($code) { |
|
| 493 | $this->status($code); |
|
| 494 | } |
|
| 495 | ||
| 496 | if ($this->headers_sent) { |
|
| 497 | throw new RequestHeadersAlreadySent; |
|
| 498 | } |
|
| 499 | $s = strtr($s, "\r\n", ' '); |
|
| 500 | ||
| 501 | $e = explode(':', $s, 2); |
|
| 502 | ||
| 503 | if (!isset($e[1])) { |
|
| 504 | $e[0] = 'STATUS'; |
|
| 505 | ||
| 506 | if (strncmp($s, 'HTTP/', 5) === 0) { |
|
| 507 | $s = substr($s, 9); |
|
| 508 | } |
|
| 509 | } |
|
| 510 | ||
| 511 | $k = strtr(strtoupper($e[0]), Generic::$htr); |
|
| 512 | ||
| 513 | if ($k === 'CONTENT_TYPE') { |
|
| 514 | self::parse_str(strtolower($e[1]), $ctype, true); |
|
| 515 | if (!isset($ctype['charset'])) { |
|
| 516 | $ctype['charset'] = $this->upstream->pool->config->defaultcharset->value; |
|
| 517 | ||
| 518 | $s = $e[0] . ': '; |
|
| 519 | $i = 0; |
|
| 520 | foreach ($ctype as $k => $v) { |
|
| 521 | $s .= ($i > 0 ? '; ' : '') . $k . ($v !== '' ? '=' . $v : ''); |
|
| 522 | ++$i; |
|
| 523 | } |
|
| 524 | } |
|
| 525 | } |
|
| 526 | if ($k === 'SET_COOKIE') { |
|
| 527 | $k .= '_' . ++$this->cookieNum; |
|
| 528 | } elseif (!$replace && isset($this->headers[$k])) { |
|
| 529 | return false; |
|
| 530 | } |
|
| 531 | ||
| 532 | $this->headers[$k] = $s; |
|
| 533 | ||
| 534 | if ($k === 'CONTENT_LENGTH') { |
|
| 535 | $this->contentLength = (int)$e[1]; |
|
| 536 | } elseif ($k === 'LOCATION') { |
|
| 537 | $this->status(301); |
|
| 538 | } |
|
| 539 | ||
| 540 | if (Daemon::$compatMode) { |
|
| 541 | is_callable('header_native') ? header_native($s) : header($s); |
|
| 542 | } |
|
| 543 | ||
| 544 | return true; |
|
| 545 | } |
|
| 546 | } |
|
| 547 | ||
| @@ 671-727 (lines=57) @@ | ||
| 668 | * @throws \PHPDaemon\Request\RequestHeadersAlreadySent |
|
| 669 | * @return boolean Success |
|
| 670 | */ |
|
| 671 | public function header($s, $replace = true, $code = false) |
|
| 672 | { |
|
| 673 | if (!$code) { |
|
| 674 | $this->status($code); |
|
| 675 | } |
|
| 676 | ||
| 677 | if ($this->headers_sent) { |
|
| 678 | throw new RequestHeadersAlreadySent; |
|
| 679 | } |
|
| 680 | $s = strtr($s, "\r\n", ' '); |
|
| 681 | ||
| 682 | $e = explode(':', $s, 2); |
|
| 683 | ||
| 684 | if (!isset($e[1])) { |
|
| 685 | $e[0] = 'STATUS'; |
|
| 686 | ||
| 687 | if (strncmp($s, 'HTTP/', 5) === 0) { |
|
| 688 | $s = substr($s, 9); |
|
| 689 | } |
|
| 690 | } |
|
| 691 | ||
| 692 | $k = strtr(strtoupper($e[0]), Generic::$htr); |
|
| 693 | ||
| 694 | if ($k === 'CONTENT_TYPE') { |
|
| 695 | self::parseStr(strtolower($e[1]), $ctype, true); |
|
| 696 | if (!isset($ctype['charset'])) { |
|
| 697 | $ctype['charset'] = $this->upstream->pool->config->defaultcharset->value; |
|
| 698 | ||
| 699 | $s = $e[0] . ': '; |
|
| 700 | $i = 0; |
|
| 701 | foreach ($ctype as $k => $v) { |
|
| 702 | $s .= ($i > 0 ? '; ' : '') . $k . ($v !== '' ? '=' . $v : ''); |
|
| 703 | ++$i; |
|
| 704 | } |
|
| 705 | } |
|
| 706 | } |
|
| 707 | ||
| 708 | if ($k === 'SET_COOKIE') { |
|
| 709 | $k .= '_' . ++$this->cookieNum; |
|
| 710 | } elseif (!$replace && isset($this->headers[$k])) { |
|
| 711 | return false; |
|
| 712 | } |
|
| 713 | ||
| 714 | $this->headers[$k] = $s; |
|
| 715 | ||
| 716 | if ($k === 'CONTENT_LENGTH') { |
|
| 717 | $this->contentLength = (int)$e[1]; |
|
| 718 | } elseif ($k === 'LOCATION') { |
|
| 719 | $this->status(301); |
|
| 720 | } |
|
| 721 | ||
| 722 | if (Daemon::$compatMode) { |
|
| 723 | is_callable('header_native') ? header_native($s) : header($s); |
|
| 724 | } |
|
| 725 | ||
| 726 | return true; |
|
| 727 | } |
|
| 728 | ||
| 729 | /** |
|
| 730 | * Removes a header |
|