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