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

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