示例#1
0
文件: net.php 项目: tapiau/muyo
 /**
  * @param string $packet
  * @return array
  */
 function http_response_deassemble($packet)
 {
     $pivot = strpos($packet, "\r\n\r\n");
     // header
     $header = array_chain(str_first($packet, $pivot), str_explode_dg("\r\n"));
     $statusLine = array_shift($header);
     $versionEndPos = strpos($statusLine, " ");
     $version = str_first($statusLine, $versionEndPos);
     $statusLine = str_from($statusLine, $versionEndPos + 1);
     $code = str_first($statusLine, 3);
     $reason = str_from($statusLine, 4);
     debug_enforce(str_first($version, 5) === 'HTTP/' && $version[6] === '.', var_dump_human_compact($version));
     $statusLine = ['Version' => ['Major' => $version[5], 'Minor' => $version[7]], 'Code' => $code, 'Reason' => $reason];
     $header = array_chain($header, array_map_key_dg(str_find_before_dg(':')), array_map_val_dg(str_find_after_dg(':')), array_map_val_dg(trim_dg()));
     //content
     $content = str_from($packet, $pivot + 4);
     if (array_key_exists('Transfer-Encoding', $header) && $header['Transfer-Encoding'] === 'chunked') {
         $chunks = '';
         while (!empty($content)) {
             $pivot = strpos($content, "\r\n");
             $length = intval(str_first($content, $pivot), 16);
             $chunks .= str_first(str_from($content, $pivot + 2), $length);
             $content = str_from($content, $pivot + 2 + $length);
             debug_enforce(str_startswith($content, "\r\n"));
             $content = str_from($content, 2);
         }
         $content = $chunks;
     }
     return array_merge(['Status Line' => $statusLine, 'Content' => $content], $header);
 }
示例#2
0
 /**
  * @param string $from absolute path from
  * @param string $basedir absolute path to
  * @param bool $basedir_as_root
  * @return string
  */
 function path_rel($from, $basedir, $basedir_as_root = false)
 {
     $basedir = ensure($basedir, str_endswith_dg(DIRECTORY_SEPARATOR), str_append_dg(DIRECTORY_SEPARATOR));
     path_common($from, $basedir, $from_suffix, $basedir_suffix);
     if (true === $basedir_as_root) {
         if (in_array($basedir_suffix, [$from_suffix, $from_suffix . DIRECTORY_SEPARATOR])) {
             $ret = '';
         } else {
             debug_assert_empty($basedir_suffix);
             $ret = $from_suffix;
         }
         $ret = DIRECTORY_SEPARATOR . $ret;
     } else {
         $basedir_len = strlen($basedir_suffix);
         for ($i = 0; $i < $basedir_len; $i++) {
             for ($j = $i + 1; $j < $basedir_len; $j++) {
                 if ($basedir_suffix[$j] === DIRECTORY_SEPARATOR) {
                     // clean-up extra separators
                     if ($basedir_suffix[$j - 1] === DIRECTORY_SEPARATOR) {
                         continue;
                     } else {
                         $basedir_suffix = str_first($basedir_suffix, $i) . '..' . str_from($basedir_suffix, $j);
                         $basedir_len -= $j - $i;
                         $basedir_len += 2;
                         $i += 2;
                         break;
                     }
                 }
             }
         }
         $ret = $basedir_suffix . $from_suffix;
         if ($ret === '') {
             $ret = '.';
         }
     }
     return $ret;
 }