Example #1
0
USING_ARCHIVEREADER_NAMESPACE

/* static */ already_AddRefed<ArchiveReader>
ArchiveReader::Constructor(const GlobalObject& aGlobal,
                           Blob& aBlob,
                           const ArchiveReaderOptions& aOptions,
                           ErrorResult& aError)
{
  nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
  if (!window) {
    aError.Throw(NS_ERROR_UNEXPECTED);
    return nullptr;
  }

  nsAutoCString encoding;
  if (!EncodingUtils::FindEncodingForLabelNoReplacement(aOptions.mEncoding,
                                                        encoding)) {
    aError.ThrowRangeError(MSG_ENCODING_NOT_SUPPORTED, &aOptions.mEncoding);
    return nullptr;
  }

  nsRefPtr<ArchiveReader> reader =
    new ArchiveReader(aBlob, window, encoding);
  return reader.forget();
}
Example #2
0
void
TextDecoder::Init(const nsAString& aLabel, const bool aFatal,
                  ErrorResult& aRv)
{
  nsAutoCString encoding;
  // Let encoding be the result of getting an encoding from label.
  // If encoding is failure or replacement, throw a RangeError
  // (https://encoding.spec.whatwg.org/#dom-textdecoder).
  if (!EncodingUtils::FindEncodingForLabelNoReplacement(aLabel, encoding)) {
    nsAutoString label(aLabel);
    EncodingUtils::TrimSpaceCharacters(label);
    aRv.ThrowRangeError(MSG_ENCODING_NOT_SUPPORTED, &label);
    return;
  }
  InitWithEncoding(encoding, aFatal);
}
Example #3
0
/*static*/ already_AddRefed<Response>
Response::Constructor(const GlobalObject& aGlobal,
                      const Optional<ArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams>& aBody,
                      const ResponseInit& aInit, ErrorResult& aRv)
{
    nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());

    if (aInit.mStatus < 200 || aInit.mStatus > 599) {
        aRv.ThrowRangeError(MSG_INVALID_RESPONSE_STATUSCODE_ERROR);
        return nullptr;
    }

    nsCString statusText;
    if (aInit.mStatusText.WasPassed()) {
        statusText = aInit.mStatusText.Value();
        nsACString::const_iterator start, end;
        statusText.BeginReading(start);
        statusText.EndReading(end);
        if (FindCharInReadable('\r', start, end)) {
            aRv.ThrowTypeError(MSG_RESPONSE_INVALID_STATUSTEXT_ERROR);
            return nullptr;
        }
        // Reset iterator since FindCharInReadable advances it.
        statusText.BeginReading(start);
        if (FindCharInReadable('\n', start, end)) {
            aRv.ThrowTypeError(MSG_RESPONSE_INVALID_STATUSTEXT_ERROR);
            return nullptr;
        }
    } else {
        // Since we don't support default values for ByteString.
        statusText = NS_LITERAL_CSTRING("OK");
    }

    nsRefPtr<InternalResponse> internalResponse =
        new InternalResponse(aInit.mStatus, statusText);

    nsRefPtr<Response> r = new Response(global, internalResponse);

    if (aInit.mHeaders.WasPassed()) {
        internalResponse->Headers()->Clear();

        // Instead of using Fill, create an object to allow the constructor to
        // unwrap the HeadersInit.
        nsRefPtr<Headers> headers =
            Headers::Create(global, aInit.mHeaders.Value(), aRv);
        if (aRv.Failed()) {
            return nullptr;
        }

        internalResponse->Headers()->Fill(*headers->GetInternalHeaders(), aRv);
        if (NS_WARN_IF(aRv.Failed())) {
            return nullptr;
        }
    }

    if (aBody.WasPassed()) {
        nsCOMPtr<nsIInputStream> bodyStream;
        nsCString contentType;
        aRv = ExtractByteStreamFromBody(aBody.Value(), getter_AddRefs(bodyStream), contentType);
        internalResponse->SetBody(bodyStream);

        if (!contentType.IsVoid() &&
                !internalResponse->Headers()->Has(NS_LITERAL_CSTRING("Content-Type"), aRv)) {
            internalResponse->Headers()->Append(NS_LITERAL_CSTRING("Content-Type"), contentType, aRv);
        }

        if (aRv.Failed()) {
            return nullptr;
        }
    }

    r->SetMimeType();
    return r.forget();
}