Advertisement
stuppid_bot

Untitled

Oct 22nd, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.   * Удаляет сегменты с точками из пути.
  3.   *
  4.   * Алгоритм описан по ссылке:
  5.   * https://tools.ietf.org/html/rfc3986#section-5.2.4
  6.   *
  7.   */
  8. function removeDotSegments(path) {
  9.   var output = [];
  10.   var offset = 0;
  11.   var position;
  12.   var segment;
  13.   // While the input buffer is not empty, loop as follows:
  14.   for (;;) {
  15.     console.log(path.substr(offset));
  16.     // A.  If the input buffer begins with a prefix of "../" or "./",
  17.     //     then remove that prefix from the input buffer; otherwise,
  18.     if (path.substr(offset, 3) === '../') {
  19.       offset += 2;
  20.       continue;
  21.     }
  22.     if (path.substr(offset, 2) === './') {
  23.       ++offset;
  24.       continue;
  25.     }
  26.     // B.  if the input buffer begins with a prefix of "/./" or "/.",
  27.     //     where "." is a complete path segment, then replace that
  28.     //     prefix with "/" in the input buffer; otherwise,
  29.     if (path.substr(offset, 3) === '/./') {
  30.       offset += 2;
  31.       continue;
  32.     }
  33.     // C.  if the input buffer begins with a prefix of "/../" or "/..",
  34.     //     where ".." is a complete path segment, then replace that
  35.     //     prefix with "/" in the input buffer and remove the last
  36.     //     segment and its preceding "/" (if any) from the output
  37.     //     buffer; otherwise,
  38.     if (path.substr(offset, 4) === '/../') {
  39.       offset += 3;
  40.       output.pop();
  41.       continue;
  42.     }
  43.     // D.  if the input buffer consists only of "." or "..", then remove
  44.     //     that from the input buffer; otherwise,
  45.     //
  46.     // E.  move the first path segment in the input buffer to the end of
  47.     //     the output buffer, including the initial "/" character (if
  48.     //     any) and any subsequent characters up to, but not including,
  49.     //     the next "/" character or the end of the input buffer.
  50.     position = path.indexOf('/', offset + 1);
  51.     if (position === -1) {
  52.       segment = path.substr(offset);
  53.       if (segment === '/.') {
  54.         // Второе условие A
  55.         output.push('/');
  56.       } else if (segment === '/..') {
  57.         // Второе условие B
  58.         output.pop();
  59.         output.push('/');
  60.       } else if (segment !== '.' && segment !== '..') {
  61.         // D
  62.         output.push(segment);
  63.       }
  64.       break;
  65.     }
  66.     // E
  67.     segment = path.substr(offset, position);
  68.     output.push(segment);
  69.     offset = position;
  70.   }
  71.   return output.join('');
  72. }
  73.  
  74.  
  75. var paths = [
  76.   '/a/b/c/./../../g',
  77.   'mid/content=5/../6',
  78. ];
  79. paths.forEach((path) => {
  80.   console.log('%s --> %s', path, removeDotSegments(path));
  81. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement