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

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