Skip to content

Commit

Permalink
Bug 1797449 - [websocket] Allow relative URLs and http(s) scheme r=va…
Browse files Browse the repository at this point in the history
…lentin

Add support to WebSocket for http: and https: URLs,
as well as having them be relative to a base URI.

Spec: whatwg/websockets#20

Passes new WPT tests for this in /websockets, with other tests unaffected.

Differential Revision: https://phabricator.services.mozilla.com/D160330
  • Loading branch information
Oliver Medhurst committed Jan 22, 2024
1 parent 8720810 commit b94e95b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 95 deletions.
40 changes: 28 additions & 12 deletions dom/websocket/WebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#include "nsProxyRelease.h"
#include "nsWeakReference.h"
#include "nsIWebSocketImpl.h"
#include "nsIURIMutator.h"

#define OPEN_EVENT_STRING u"open"_ns
#define MESSAGE_EVENT_STRING u"message"_ns
Expand Down Expand Up @@ -163,7 +164,7 @@ class WebSocketImpl final : public nsIInterfaceRequestor,
const nsACString& aNegotiatedExtensions,
UniquePtr<SerializedStackHolder> aOriginStack);

nsresult ParseURL(const nsAString& aURL);
nsresult ParseURL(const nsAString& aURL, nsIURI* aBaseURI);
nsresult InitializeConnection(nsIPrincipal* aPrincipal,
nsICookieJarSettings* aCookieJarSettings);

Expand Down Expand Up @@ -1130,11 +1131,11 @@ class InitRunnable final : public WebSocketMainThreadRunnable {
return true;
}

nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
mErrorCode = mImpl->Init(
jsapi.cx(), mWorkerPrivate->GetPrincipal()->SchemeIs("https"),
doc->NodePrincipal(), mClientInfo, mWorkerPrivate->CSPEventListener(),
mIsServerSide, mURL, mProtocolArray, mScriptFile, mScriptLine,
mScriptColumn);
jsapi.cx(), principal->SchemeIs("https"), principal, mClientInfo,
mWorkerPrivate->CSPEventListener(), mIsServerSide, mURL, mProtocolArray,
mScriptFile, mScriptLine, mScriptColumn);
return true;
}

Expand Down Expand Up @@ -1650,7 +1651,8 @@ nsresult WebSocketImpl::Init(JSContext* aCx, bool aIsSecure,
mIsChromeContext = aPrincipal->IsSystemPrincipal();

// parses the url
rv = ParseURL(aURL);
nsCOMPtr<nsIURI> baseURI = aPrincipal->GetURI();
rv = ParseURL(aURL, baseURI);
NS_ENSURE_SUCCESS(rv, rv);

nsCOMPtr<Document> originDoc = mWebSocket->GetDocumentIfCurrent();
Expand Down Expand Up @@ -2100,7 +2102,7 @@ nsresult WebSocket::CreateAndDispatchCloseEvent(bool aWasClean, uint16_t aCode,
return err.StealNSResult();
}

nsresult WebSocketImpl::ParseURL(const nsAString& aURL) {
nsresult WebSocketImpl::ParseURL(const nsAString& aURL, nsIURI* aBaseURI) {
AssertIsOnMainThread();
NS_ENSURE_TRUE(!aURL.IsEmpty(), NS_ERROR_DOM_SYNTAX_ERR);

Expand All @@ -2112,21 +2114,35 @@ nsresult WebSocketImpl::ParseURL(const nsAString& aURL) {
}

nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL);
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL, nullptr, aBaseURI);
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);

nsCOMPtr<nsIURL> parsedURL = do_QueryInterface(uri, &rv);
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);

bool hasRef;
rv = parsedURL->GetHasRef(&hasRef);
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !hasRef, NS_ERROR_DOM_SYNTAX_ERR);

nsAutoCString scheme;
rv = parsedURL->GetScheme(scheme);
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !scheme.IsEmpty(),
NS_ERROR_DOM_SYNTAX_ERR);

// If |urlRecord|'s [=url/scheme=] is "`http`", then set |urlRecord|'s
// [=url/scheme=] to "`ws`". Otherwise, if |urlRecord|'s [=url/scheme=] is
// "`https`", set |urlRecord|'s [=url/scheme=] to "`wss`".
// https://websockets.spec.whatwg.org/#dom-websocket-websocket

if (scheme == "http" || scheme == "https") {
scheme = scheme == "https" ? "wss"_ns : "ws"_ns;

NS_MutateURI mutator(parsedURL);
mutator.SetScheme(scheme);
rv = mutator.Finalize(parsedURL);
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
}

bool hasRef;
rv = parsedURL->GetHasRef(&hasRef);
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !hasRef, NS_ERROR_DOM_SYNTAX_ERR);

nsAutoCString host;
rv = parsedURL->GetAsciiHost(host);
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !host.IsEmpty(), NS_ERROR_DOM_SYNTAX_ERR);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit b94e95b

Please sign in to comment.