FetchUtil.h (3842B)
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 #ifndef mozilla_dom_FetchUtil_h 8 #define mozilla_dom_FetchUtil_h 9 10 #include "mozilla/dom/File.h" 11 #include "mozilla/dom/FormData.h" 12 #include "nsError.h" 13 #include "nsString.h" 14 15 #define WASM_CONTENT_TYPE "application/wasm" 16 17 class nsIPrincipal; 18 class nsIHttpChannel; 19 20 namespace mozilla::dom { 21 22 class Document; 23 class InternalRequest; 24 class WorkerPrivate; 25 26 #define FETCH_KEEPALIVE_MAX_SIZE 65536 27 28 class FetchUtil final { 29 private: 30 static nsCString WasmAltDataType; 31 FetchUtil() = delete; 32 33 public: 34 /** 35 * Sets outMethod to a valid HTTP request method string based on an input 36 * method. Implements checks and normalization as specified by the Fetch 37 * specification. Returns NS_ERROR_DOM_SECURITY_ERR if the method is invalid. 38 * Otherwise returns NS_OK and the normalized method via outMethod. 39 */ 40 static nsresult GetValidRequestMethod(const nsACString& aMethod, 41 nsCString& outMethod); 42 /** 43 * Extracts an HTTP header from a substring range. 44 */ 45 static bool ExtractHeader(nsACString::const_iterator& aStart, 46 nsACString::const_iterator& aEnd, 47 nsCString& aHeaderName, nsCString& aHeaderValue, 48 bool* aWasEmptyHeader); 49 50 static nsresult SetRequestReferrer(nsIPrincipal* aPrincipal, Document* aDoc, 51 nsIHttpChannel* aChannel, 52 InternalRequest& aRequest); 53 54 /** 55 * The WebAssembly alt data type includes build-id, cpu-id and other relevant 56 * state that is necessary to ensure the validity of caching machine code and 57 * metadata in alt data. InitWasmAltDataType() must be called during startup 58 * before the first fetch(), ensuring that GetWasmAltDataType() is valid. 59 */ 60 static inline const nsCString& GetWasmAltDataType() { 61 MOZ_ASSERT(!WasmAltDataType.IsEmpty()); 62 return WasmAltDataType; 63 } 64 static void InitWasmAltDataType(); 65 66 /** 67 * Check that the given object is a Response and, if so, stream to the given 68 * JS consumer. On any failure, this function will report an error on the 69 * given JSContext before returning false. If executing in a worker, the 70 * WorkerPrivate must be given. 71 */ 72 static bool StreamResponseToJS(JSContext* aCx, JS::Handle<JSObject*> aObj, 73 JS::MimeType aMimeType, 74 JS::StreamConsumer* aConsumer, 75 WorkerPrivate* aMaybeWorker); 76 77 /** 78 * Called by JS to report (i.e., throw) an error that was passed to the 79 * JS::StreamConsumer::streamError() method on a random stream thread. 80 * This method is passed by function pointer to the JS engine hence the 81 * untyped 'size_t' instead of Gecko 'nsresult'. 82 */ 83 static void ReportJSStreamError(JSContext* aCx, size_t aErrorCode); 84 85 /** 86 * Implements fetch spec 87 * https://fetch.spec.whatwg.org/#http-network-or-cache-fetch for 88 * bounding the keepalive request size 89 */ 90 static bool IncrementPendingKeepaliveRequestSize(nsILoadGroup* aLoadGroup, 91 const uint64_t aBodyLength); 92 93 static void DecrementPendingKeepaliveRequestSize(nsILoadGroup* aLoadGroup, 94 const uint64_t aBodyLength); 95 96 /** 97 * Wrapper to fetch loadgroup from the global object 98 */ 99 static nsCOMPtr<nsILoadGroup> GetLoadGroupFromGlobal( 100 nsIGlobalObject* aGlobal); 101 }; 102 103 } // namespace mozilla::dom 104 #endif