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/. */
7 #include "mozilla/css/StreamLoader.h"
9 #include "mozilla/Encoding.h"
10 #include "nsContentUtils.h"
11 #include "nsIChannel.h"
12 #include "nsIInputStream.h"
16 using namespace mozilla
;
21 StreamLoader::StreamLoader(SheetLoadData
& aSheetLoadData
)
22 : mSheetLoadData(&aSheetLoadData
), mStatus(NS_OK
) {}
24 StreamLoader::~StreamLoader() {
26 MOZ_RELEASE_ASSERT(mOnStopRequestCalled
|| mChannelOpenFailed
);
30 NS_IMPL_ISUPPORTS(StreamLoader
, nsIStreamListener
)
32 /* nsIRequestObserver implementation */
34 StreamLoader::OnStartRequest(nsIRequest
* aRequest
) {
36 mSheetLoadData
->NotifyStart(aRequest
);
38 // It's kinda bad to let Web content send a number that results
39 // in a potentially large allocation directly, but efficiency of
40 // compression bombs is so great that it doesn't make much sense
41 // to require a site to send one before going ahead and allocating.
42 if (nsCOMPtr
<nsIChannel
> channel
= do_QueryInterface(aRequest
)) {
44 nsresult rv
= channel
->GetContentLength(&length
);
45 if (NS_SUCCEEDED(rv
) && length
> 0) {
46 CheckedInt
<nsACString::size_type
> checkedLength(length
);
47 if (!checkedLength
.isValid()) {
48 return (mStatus
= NS_ERROR_OUT_OF_MEMORY
);
50 if (!mBytes
.SetCapacity(checkedLength
.value(), fallible
)) {
51 return (mStatus
= NS_ERROR_OUT_OF_MEMORY
);
59 StreamLoader::OnStopRequest(nsIRequest
* aRequest
, nsresult aStatus
) {
61 MOZ_RELEASE_ASSERT(!mOnStopRequestCalled
);
62 mOnStopRequestCalled
= true;
65 nsresult rv
= mStatus
;
69 // Hold the nsStringBuffer for the bytes from the stack to ensure release
70 // no matter which return branch is taken.
71 nsCString
bytes(mBytes
);
74 nsCOMPtr
<nsIChannel
> channel
= do_QueryInterface(aRequest
);
76 if (NS_FAILED(mStatus
)) {
77 mSheetLoadData
->VerifySheetReadyToParse(mStatus
, ""_ns
, ""_ns
, channel
);
81 rv
= mSheetLoadData
->VerifySheetReadyToParse(aStatus
, mBOMBytes
, bytes
,
83 if (rv
!= NS_OK_PARSE_SHEET
) {
87 // BOM detection generally happens during the write callback, but that won't
88 // have happened if fewer than three bytes were received.
89 if (mEncodingFromBOM
.isNothing()) {
91 MOZ_ASSERT(mEncodingFromBOM
.isSome());
94 // The BOM handling has happened, but we still may not have an encoding if
95 // there was no BOM. Ensure we have one.
96 const Encoding
* encoding
= mEncodingFromBOM
.value();
99 encoding
= mSheetLoadData
->DetermineNonBOMEncoding(bytes
, channel
);
101 mSheetLoadData
->mEncoding
= encoding
;
103 size_t validated
= 0;
104 if (encoding
== UTF_8_ENCODING
) {
105 validated
= Encoding::UTF8ValidUpTo(bytes
);
108 if (validated
== bytes
.Length()) {
109 // Either this is UTF-8 and all valid, or it's not UTF-8 but is an
110 // empty string. This assumes that an empty string in any encoding
111 // decodes to empty string, which seems like a plausible assumption.
112 utf8String
.Assign(bytes
);
114 rv
= encoding
->DecodeWithoutBOMHandling(bytes
, utf8String
, validated
);
115 NS_ENSURE_SUCCESS(rv
, rv
);
117 } // run destructor for `bytes`
119 auto info
= nsContentUtils::GetSubresourceCacheValidationInfo(
120 aRequest
, mSheetLoadData
->mURI
);
122 // For now, we never cache entries that we have to revalidate, or whose
123 // channel don't support caching.
124 if (!info
.mExpirationTime
|| info
.mMustRevalidate
) {
125 info
.mExpirationTime
=
126 Some(nsContentUtils::SecondsFromPRTime(PR_Now()) - 1);
128 mSheetLoadData
->mExpirationTime
= *info
.mExpirationTime
;
130 // For reasons I don't understand, factoring the below lines into
131 // a method on SheetLoadData resulted in a linker error. Hence,
132 // accessing fields of mSheetLoadData from here.
133 mSheetLoadData
->mLoader
->ParseSheet(utf8String
, *mSheetLoadData
,
134 Loader::AllowAsyncParse::Yes
);
139 /* nsIStreamListener implementation */
141 StreamLoader::OnDataAvailable(nsIRequest
*, nsIInputStream
* aInputStream
,
142 uint64_t, uint32_t aCount
) {
143 if (NS_FAILED(mStatus
)) {
147 return aInputStream
->ReadSegments(WriteSegmentFun
, this, aCount
, &dummy
);
150 void StreamLoader::HandleBOM() {
151 MOZ_ASSERT(mEncodingFromBOM
.isNothing());
152 MOZ_ASSERT(mBytes
.IsEmpty());
154 auto [encoding
, bomLength
] = Encoding::ForBOM(mBOMBytes
);
155 mEncodingFromBOM
.emplace(encoding
); // Null means no BOM.
157 // BOMs are three bytes at most, but may be fewer. Copy over anything
158 // that wasn't part of the BOM to mBytes. Note that we need to track
159 // any BOM bytes as well for SRI handling.
160 mBytes
.Append(Substring(mBOMBytes
, bomLength
));
161 mBOMBytes
.Truncate(bomLength
);
164 nsresult
StreamLoader::WriteSegmentFun(nsIInputStream
*, void* aClosure
,
165 const char* aSegment
, uint32_t,
166 uint32_t aCount
, uint32_t* aWriteCount
) {
168 StreamLoader
* self
= static_cast<StreamLoader
*>(aClosure
);
169 if (NS_FAILED(self
->mStatus
)) {
170 return self
->mStatus
;
173 // If we haven't done BOM detection yet, divert bytes into the special buffer.
174 if (self
->mEncodingFromBOM
.isNothing()) {
175 size_t bytesToCopy
= std::min
<size_t>(3 - self
->mBOMBytes
.Length(), aCount
);
176 self
->mBOMBytes
.Append(aSegment
, bytesToCopy
);
177 aSegment
+= bytesToCopy
;
178 *aWriteCount
+= bytesToCopy
;
179 aCount
-= bytesToCopy
;
181 if (self
->mBOMBytes
.Length() == 3) {
188 if (!self
->mBytes
.Append(aSegment
, aCount
, fallible
)) {
189 self
->mBytes
.Truncate();
190 return (self
->mStatus
= NS_ERROR_OUT_OF_MEMORY
);
193 *aWriteCount
+= aCount
;
198 } // namespace mozilla