tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ContentRange.cpp (2362B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "ContentRange.h"
      8 #include "nsContentUtils.h"
      9 
     10 mozilla::net::ContentRange::ContentRange(const nsACString& aRangeHeader,
     11                                         uint64_t aSize)
     12    : mStart(0), mEnd(0), mSize(0) {
     13  auto parsed = nsContentUtils::ParseSingleRangeRequest(aRangeHeader, true);
     14  // https://fetch.spec.whatwg.org/#ref-for-simple-range-header-value%E2%91%A1
     15  // If rangeValue is failure, then return a network error.
     16  if (!parsed) {
     17    return;
     18  }
     19 
     20  // Sanity check: ParseSingleRangeRequest should handle these two cases.
     21  // If rangeEndValue and rangeStartValue are null, then return failure.
     22  MOZ_ASSERT(parsed->Start().isSome() || parsed->End().isSome());
     23  // If rangeStartValue and rangeEndValue are numbers, and rangeStartValue
     24  // is greater than rangeEndValue, then return failure.
     25  MOZ_ASSERT(parsed->Start().isNothing() || parsed->End().isNothing() ||
     26             *parsed->Start() <= *parsed->End());
     27 
     28  // https://fetch.spec.whatwg.org/#ref-for-simple-range-header-value%E2%91%A1
     29  // If rangeStart is null:
     30  if (parsed->Start().isNothing()) {
     31    // Set rangeStart to fullLength − rangeEnd.
     32    mStart = aSize - *parsed->End();
     33 
     34    // Set rangeEnd to rangeStart + rangeEnd − 1.
     35    mEnd = mStart + *parsed->End() - 1;
     36 
     37    // Otherwise:
     38  } else {
     39    // If rangeStart is greater than or equal to fullLength, then return a
     40    // network error.
     41    if (*parsed->Start() >= aSize) {
     42      return;
     43    }
     44    mStart = *parsed->Start();
     45 
     46    // If rangeEnd is null or rangeEnd is greater than or equal to fullLength,
     47    // then set rangeEnd to fullLength − 1.
     48    if (parsed->End().isNothing() || *parsed->End() >= aSize) {
     49      mEnd = aSize - 1;
     50    } else {
     51      mEnd = *parsed->End();
     52    }
     53  }
     54  mSize = aSize;
     55 }
     56 
     57 void mozilla::net::ContentRange::AsHeader(nsACString& aOutString) const {
     58  aOutString.Assign("bytes "_ns);
     59  aOutString.AppendInt(mStart);
     60  aOutString.AppendLiteral("-");
     61  aOutString.AppendInt(mEnd);
     62  aOutString.AppendLiteral("/");
     63  aOutString.AppendInt(mSize);
     64 }