From 40a355a42d4a9444dc753c04c6608dade2f06a23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:27 +0200 Subject: Adding upstream version 125.0.1. Signed-off-by: Daniel Baumann --- netwerk/base/ContentRange.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 netwerk/base/ContentRange.cpp (limited to 'netwerk/base/ContentRange.cpp') diff --git a/netwerk/base/ContentRange.cpp b/netwerk/base/ContentRange.cpp new file mode 100644 index 0000000000..dfae132550 --- /dev/null +++ b/netwerk/base/ContentRange.cpp @@ -0,0 +1,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); +} -- cgit v1.2.3