| @@ 645-702 (lines=58) @@ | ||
| 642 | * @throws \PHPDaemon\Request\RequestHeadersAlreadySent |
|
| 643 | * @return boolean Success |
|
| 644 | */ |
|
| 645 | public function header($s, $replace = true, $code = false) { |
|
| 646 | if (!$code) { |
|
| 647 | $this->status($code); |
|
| 648 | } |
|
| 649 | ||
| 650 | if ($this->headers_sent) { |
|
| 651 | throw new RequestHeadersAlreadySent; |
|
| 652 | } |
|
| 653 | $s = strtr($s, "\r\n", ' '); |
|
| 654 | ||
| 655 | $e = explode(':', $s, 2); |
|
| 656 | ||
| 657 | if (!isset($e[1])) { |
|
| 658 | $e[0] = 'STATUS'; |
|
| 659 | ||
| 660 | if (strncmp($s, 'HTTP/', 5) === 0) { |
|
| 661 | $s = substr($s, 9); |
|
| 662 | } |
|
| 663 | } |
|
| 664 | ||
| 665 | $k = strtr(strtoupper($e[0]), Generic::$htr); |
|
| 666 | ||
| 667 | if ($k === 'CONTENT_TYPE') { |
|
| 668 | self::parse_str(strtolower($e[1]), $ctype, true); |
|
| 669 | if (!isset($ctype['charset'])) { |
|
| 670 | $ctype['charset'] = $this->upstream->pool->config->defaultcharset->value; |
|
| 671 | ||
| 672 | $s = $e[0] . ': '; |
|
| 673 | $i = 0; |
|
| 674 | foreach ($ctype as $k => $v) { |
|
| 675 | $s .= ($i > 0 ? '; ' : '') . $k . ($v !== '' ? '=' . $v : ''); |
|
| 676 | ++$i; |
|
| 677 | } |
|
| 678 | } |
|
| 679 | } |
|
| 680 | ||
| 681 | if ($k === 'SET_COOKIE') { |
|
| 682 | $k .= '_' . ++$this->cookieNum; |
|
| 683 | } |
|
| 684 | elseif (!$replace && isset($this->headers[$k])) { |
|
| 685 | return false; |
|
| 686 | } |
|
| 687 | ||
| 688 | $this->headers[$k] = $s; |
|
| 689 | ||
| 690 | if ($k === 'CONTENT_LENGTH') { |
|
| 691 | $this->contentLength = (int)$e[1]; |
|
| 692 | } |
|
| 693 | elseif ($k === 'LOCATION') { |
|
| 694 | $this->status(301); |
|
| 695 | } |
|
| 696 | ||
| 697 | if (Daemon::$compatMode) { |
|
| 698 | is_callable('header_native') ? header_native($s) : header($s); |
|
| 699 | } |
|
| 700 | ||
| 701 | return true; |
|
| 702 | } |
|
| 703 | ||
| 704 | /** |
|
| 705 | * Removes a header |
|
| @@ 473-529 (lines=57) @@ | ||
| 470 | * @throws \PHPDaemon\Request\RequestHeadersAlreadySent |
|
| 471 | * @return boolean Success |
|
| 472 | */ |
|
| 473 | public function header($s, $replace = true, $code = false) { |
|
| 474 | if ($code) { |
|
| 475 | $this->status($code); |
|
| 476 | } |
|
| 477 | ||
| 478 | if ($this->headers_sent) { |
|
| 479 | throw new RequestHeadersAlreadySent; |
|
| 480 | } |
|
| 481 | $s = strtr($s, "\r\n", ' '); |
|
| 482 | ||
| 483 | $e = explode(':', $s, 2); |
|
| 484 | ||
| 485 | if (!isset($e[1])) { |
|
| 486 | $e[0] = 'STATUS'; |
|
| 487 | ||
| 488 | if (strncmp($s, 'HTTP/', 5) === 0) { |
|
| 489 | $s = substr($s, 9); |
|
| 490 | } |
|
| 491 | } |
|
| 492 | ||
| 493 | $k = strtr(strtoupper($e[0]), Generic::$htr); |
|
| 494 | ||
| 495 | if ($k === 'CONTENT_TYPE') { |
|
| 496 | self::parse_str(strtolower($e[1]), $ctype, true); |
|
| 497 | if (!isset($ctype['charset'])) { |
|
| 498 | $ctype['charset'] = $this->upstream->pool->config->defaultcharset->value; |
|
| 499 | ||
| 500 | $s = $e[0] . ': '; |
|
| 501 | $i = 0; |
|
| 502 | foreach ($ctype as $k => $v) { |
|
| 503 | $s .= ($i > 0 ? '; ' : '') . $k . ($v !== '' ? '=' . $v : ''); |
|
| 504 | ++$i; |
|
| 505 | } |
|
| 506 | } |
|
| 507 | } |
|
| 508 | if ($k === 'SET_COOKIE') { |
|
| 509 | $k .= '_' . ++$this->cookieNum; |
|
| 510 | } |
|
| 511 | elseif (!$replace && isset($this->headers[$k])) { |
|
| 512 | return false; |
|
| 513 | } |
|
| 514 | ||
| 515 | $this->headers[$k] = $s; |
|
| 516 | ||
| 517 | if ($k === 'CONTENT_LENGTH') { |
|
| 518 | $this->contentLength = (int)$e[1]; |
|
| 519 | } |
|
| 520 | elseif ($k === 'LOCATION') { |
|
| 521 | $this->status(301); |
|
| 522 | } |
|
| 523 | ||
| 524 | if (Daemon::$compatMode) { |
|
| 525 | is_callable('header_native') ? header_native($s) : header($s); |
|
| 526 | } |
|
| 527 | ||
| 528 | return true; |
|
| 529 | } |
|
| 530 | } |
|
| 531 | ||