summaryrefslogtreecommitdiffstats
path: root/netwerk/base/ContentRange.cpp
blob: dfae132550efa1fce80e8545d76515208d35ec64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ContentRange.h"
#include "nsContentUtils.h"

mozilla::net::ContentRange::ContentRange(const nsACString& aRangeHeader,
                                         uint64_t aSize)
    : mStart(0), mEnd(0), mSize(0) {
  auto parsed = nsContentUtils::ParseSingleRangeRequest(aRangeHeader, true);
  // https://fetch.spec.whatwg.org/#ref-for-simple-range-header-value%E2%91%A1
  // If rangeValue is failure, then return a network error.
  if (!parsed) {
    return;
  }

  // Sanity check: ParseSingleRangeRequest should handle these two cases.
  // If rangeEndValue and rangeStartValue are null, then return failure.
  MOZ_ASSERT(parsed->Start().isSome() || parsed->End().isSome());
  // If rangeStartValue and rangeEndValue are numbers, and rangeStartValue
  // is greater than rangeEndValue, then return failure.
  MOZ_ASSERT(parsed->Start().isNothing() || parsed->End().isNothing() ||
             *parsed->Start() <= *parsed->End());

  // https://fetch.spec.whatwg.org/#ref-for-simple-range-header-value%E2%91%A1
  // If rangeStart is null:
  if (parsed->Start().isNothing()) {
    // Set rangeStart to fullLength − rangeEnd.
    mStart = aSize - *parsed->End();

    // Set rangeEnd to rangeStart + rangeEnd − 1.
    mEnd = mStart + *parsed->End() - 1;

    // Otherwise:
  } else {
    // If rangeStart is greater than or equal to fullLength, then return a
    // network error.
    if (*parsed->Start() >= aSize) {
      return;
    }
    mStart = *parsed->Start();

    // If rangeEnd is null or rangeEnd is greater than or equal to fullLength,
    // then set rangeEnd to fullLength − 1.
    if (parsed->End().isNothing() || *parsed->End() >= aSize) {
      mEnd = aSize - 1;
    } else {
      mEnd = *parsed->End();
    }
  }
  mSize = aSize;
}

void mozilla::net::ContentRange::AsHeader(nsACString& aOutString) const {
  aOutString.Assign("bytes "_ns);
  aOutString.AppendInt(mStart);
  aOutString.AppendLiteral("-");
  aOutString.AppendInt(mEnd);
  aOutString.AppendLiteral("/");
  aOutString.AppendInt(mSize);
}