Code Duplication    Length = 57-58 lines in 2 locations

PHPDaemon/HTTPRequest/Generic.php 1 location

@@ 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

PHPDaemon/Servers/WebSocket/Connection.php 1 location

@@ 464-520 (lines=57) @@
461
	 * @throws \PHPDaemon\Request\RequestHeadersAlreadySent
462
	 * @return boolean          Success
463
	 */
464
	public function header($s, $replace = true, $code = false) {
465
		if ($code) {
466
			$this->status($code);
467
		}
468
469
		if ($this->headers_sent) {
470
			throw new RequestHeadersAlreadySent;
471
		}
472
		$s = strtr($s, "\r\n", '  ');
473
474
		$e = explode(':', $s, 2);
475
476
		if (!isset($e[1])) {
477
			$e[0] = 'STATUS';
478
479
			if (strncmp($s, 'HTTP/', 5) === 0) {
480
				$s = substr($s, 9);
481
			}
482
		}
483
484
		$k = strtr(strtoupper($e[0]), Generic::$htr);
485
486
		if ($k === 'CONTENT_TYPE') {
487
			self::parse_str(strtolower($e[1]), $ctype, true);
488
			if (!isset($ctype['charset'])) {
489
				$ctype['charset'] = $this->upstream->pool->config->defaultcharset->value;
490
491
				$s = $e[0] . ': ';
492
				$i = 0;
493
				foreach ($ctype as $k => $v) {
494
					$s .= ($i > 0 ? '; ' : '') . $k . ($v !== '' ? '=' . $v : '');
495
					++$i;
496
				}
497
			}
498
		}
499
		if ($k === 'SET_COOKIE') {
500
			$k .= '_' . ++$this->cookieNum;
501
		}
502
		elseif (!$replace && isset($this->headers[$k])) {
503
			return false;
504
		}
505
506
		$this->headers[$k] = $s;
507
508
		if ($k === 'CONTENT_LENGTH') {
509
			$this->contentLength = (int)$e[1];
510
		}
511
		elseif ($k === 'LOCATION') {
512
			$this->status(301);
513
		}
514
515
		if (Daemon::$compatMode) {
516
			is_callable('header_native') ? header_native($s) : header($s);
517
		}
518
519
		return true;
520
	}
521
}
522