| @@ 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 |
|
| @@ 492-547 (lines=56) @@ | ||
| 489 | * @throws \PHPDaemon\Request\RequestHeadersAlreadySent |
|
| 490 | * @return boolean Success |
|
| 491 | */ |
|
| 492 | public function header($s, $replace = true, $code = false) |
|
| 493 | { |
|
| 494 | if ($code) { |
|
| 495 | $this->status($code); |
|
| 496 | } |
|
| 497 | ||
| 498 | if ($this->headers_sent) { |
|
| 499 | throw new RequestHeadersAlreadySent; |
|
| 500 | } |
|
| 501 | $s = strtr($s, "\r\n", ' '); |
|
| 502 | ||
| 503 | $e = explode(':', $s, 2); |
|
| 504 | ||
| 505 | if (!isset($e[1])) { |
|
| 506 | $e[0] = 'STATUS'; |
|
| 507 | ||
| 508 | if (strncmp($s, 'HTTP/', 5) === 0) { |
|
| 509 | $s = substr($s, 9); |
|
| 510 | } |
|
| 511 | } |
|
| 512 | ||
| 513 | $k = strtr(strtoupper($e[0]), Generic::$htr); |
|
| 514 | ||
| 515 | if ($k === 'CONTENT_TYPE') { |
|
| 516 | self::parseStr(strtolower($e[1]), $ctype, true); |
|
| 517 | if (!isset($ctype['charset'])) { |
|
| 518 | $ctype['charset'] = $this->upstream->pool->config->defaultcharset->value; |
|
| 519 | ||
| 520 | $s = $e[0] . ': '; |
|
| 521 | $i = 0; |
|
| 522 | foreach ($ctype as $k => $v) { |
|
| 523 | $s .= ($i > 0 ? '; ' : '') . $k . ($v !== '' ? '=' . $v : ''); |
|
| 524 | ++$i; |
|
| 525 | } |
|
| 526 | } |
|
| 527 | } |
|
| 528 | if ($k === 'SET_COOKIE') { |
|
| 529 | $k .= '_' . ++$this->cookieNum; |
|
| 530 | } elseif (!$replace && isset($this->headers[$k])) { |
|
| 531 | return false; |
|
| 532 | } |
|
| 533 | ||
| 534 | $this->headers[$k] = $s; |
|
| 535 | ||
| 536 | if ($k === 'CONTENT_LENGTH') { |
|
| 537 | $this->contentLength = (int)$e[1]; |
|
| 538 | } elseif ($k === 'LOCATION') { |
|
| 539 | $this->status(301); |
|
| 540 | } |
|
| 541 | ||
| 542 | if (Daemon::$compatMode) { |
|
| 543 | is_callable('header_native') ? header_native($s) : header($s); |
|
| 544 | } |
|
| 545 | ||
| 546 | return true; |
|
| 547 | } |
|
| 548 | } |
|
| 549 | ||