Skip to content

Commit

Permalink
Bug 1263991 part 5: Create API on FlyWebPublishedServer for getting b…
Browse files Browse the repository at this point in the history
…ack a nsITransport when accepting a websocket request, rather than getting back a WebSocket DOM object. r=djvj
  • Loading branch information
sicking committed Jun 7, 2016
1 parent 4ab94e9 commit b799ebc
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 71 deletions.
121 changes: 70 additions & 51 deletions dom/flyweb/FlyWebPublishedServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mozilla/Preferences.h"
#include "mozilla/unused.h"
#include "nsGlobalWindow.h"
#include "WebSocketChannel.h"

namespace mozilla {
namespace dom {
Expand Down Expand Up @@ -111,6 +112,51 @@ FlyWebPublishedServer::PublishedServerStarted(nsresult aStatus)
}
}

already_AddRefed<WebSocket>
FlyWebPublishedServer::OnWebSocketAccept(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
ErrorResult& aRv)
{
MOZ_ASSERT(aConnectRequest);

LOG_I("FlyWebPublishedServer::OnWebSocketAccept(%p)", this);

nsCOMPtr<nsITransportProvider> provider =
OnWebSocketAcceptInternal(aConnectRequest,
aProtocol,
aRv);
if (aRv.Failed()) {
return nullptr;
}
MOZ_ASSERT(provider);

nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(GetOwner());
AutoJSContext cx;
GlobalObject global(cx, nsGlobalWindow::Cast(window)->FastGetGlobalJSObject());

nsAutoCString extensions, negotiatedExtensions;
aConnectRequest->Headers()->
Get(NS_LITERAL_CSTRING("Sec-WebSocket-Extensions"), extensions, aRv);
mozilla::net::ProcessServerWebSocketExtensions(extensions,
negotiatedExtensions);

nsCString url;
aConnectRequest->GetURL(url);
Sequence<nsString> protocols;
if (aProtocol.WasPassed() &&
!protocols.AppendElement(aProtocol.Value(), fallible)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}

return WebSocket::ConstructorCommon(global,
NS_ConvertUTF8toUTF16(url),
protocols,
provider,
negotiatedExtensions,
aRv);
}

/******** FlyWebPublishedServerImpl ********/

NS_IMPL_ISUPPORTS_INHERITED0(FlyWebPublishedServerImpl, mozilla::DOMEventTargetHelper)
Expand Down Expand Up @@ -166,52 +212,6 @@ FlyWebPublishedServerImpl::OnFetchResponse(InternalRequest* aRequest,
}
}

already_AddRefed<WebSocket>
FlyWebPublishedServerImpl::OnWebSocketAccept(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
ErrorResult& aRv)
{
MOZ_ASSERT(aConnectRequest);

LOG_I("FlyWebPublishedMDNSServer::OnWebSocketAccept(%p)", this);

if (!mHttpServer) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}

nsAutoCString negotiatedExtensions;
nsCOMPtr<nsITransportProvider> provider =
mHttpServer->AcceptWebSocket(aConnectRequest,
aProtocol,
negotiatedExtensions,
aRv);
if (aRv.Failed()) {
return nullptr;
}
MOZ_ASSERT(provider);

nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(GetOwner());
AutoJSContext cx;
GlobalObject global(cx, nsGlobalWindow::Cast(window)->FastGetGlobalJSObject());

nsCString url;
aConnectRequest->GetURL(url);
Sequence<nsString> protocols;
if (aProtocol.WasPassed() &&
!protocols.AppendElement(aProtocol.Value(), fallible)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}

return WebSocket::ConstructorCommon(global,
NS_ConvertUTF8toUTF16(url),
protocols,
provider,
negotiatedExtensions,
aRv);
}

void
FlyWebPublishedServerImpl::OnWebSocketResponse(InternalRequest* aConnectRequest,
InternalResponse* aResponse)
Expand All @@ -226,6 +226,23 @@ FlyWebPublishedServerImpl::OnWebSocketResponse(InternalRequest* aConnectRequest,
}
}

already_AddRefed<nsITransportProvider>
FlyWebPublishedServerImpl::OnWebSocketAcceptInternal(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
ErrorResult& aRv)
{
LOG_I("FlyWebPublishedServerImpl::OnWebSocketAcceptInternal(%p)", this);

if (!mHttpServer) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}

return mHttpServer->AcceptWebSocket(aConnectRequest,
aProtocol,
aRv);
}

/******** FlyWebPublishedServerChild ********/

FlyWebPublishedServerChild::FlyWebPublishedServerChild(nsPIDOMWindowInner* aOwner,
Expand Down Expand Up @@ -310,12 +327,14 @@ FlyWebPublishedServerChild::OnFetchResponse(InternalRequest* aRequest,
}
}

already_AddRefed<WebSocket>
FlyWebPublishedServerChild::OnWebSocketAccept(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
ErrorResult& aRv)
already_AddRefed<nsITransportProvider>
FlyWebPublishedServerChild::OnWebSocketAcceptInternal(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
ErrorResult& aRv)
{
// Send ipdl message to parent
LOG_I("FlyWebPublishedServerChild::OnWebSocketAcceptInternal(%p)", this);

aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
return nullptr;
}

Expand Down
10 changes: 7 additions & 3 deletions dom/flyweb/FlyWebPublishedServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "mozilla/MozPromise.h"

class nsPIDOMWindowInner;
class nsITransportProvider;

namespace mozilla {

Expand Down Expand Up @@ -58,12 +59,16 @@ class FlyWebPublishedServer : public mozilla::DOMEventTargetHelper

virtual void OnFetchResponse(InternalRequest* aRequest,
InternalResponse* aResponse) = 0;
virtual already_AddRefed<WebSocket>
already_AddRefed<WebSocket>
OnWebSocketAccept(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
ErrorResult& aRv) = 0;
ErrorResult& aRv);
virtual void OnWebSocketResponse(InternalRequest* aConnectRequest,
InternalResponse* aResponse) = 0;
virtual already_AddRefed<nsITransportProvider>
OnWebSocketAcceptInternal(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
ErrorResult& aRv) = 0;

virtual void Close();

Expand All @@ -82,7 +87,6 @@ class FlyWebPublishedServer : public mozilla::DOMEventTargetHelper
}

protected:

virtual ~FlyWebPublishedServer()
{
MOZ_ASSERT(!mIsRegistered, "Subclass dtor forgot to call Close()");
Expand Down
16 changes: 8 additions & 8 deletions dom/flyweb/FlyWebPublishedServerIPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class FlyWebPublishedServerImpl final : public FlyWebPublishedServer

virtual void OnFetchResponse(InternalRequest* aRequest,
InternalResponse* aResponse) override;
virtual already_AddRefed<WebSocket>
OnWebSocketAccept(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
ErrorResult& aRv) override;
virtual void OnWebSocketResponse(InternalRequest* aConnectRequest,
InternalResponse* aResponse) override;
virtual already_AddRefed<nsITransportProvider>
OnWebSocketAcceptInternal(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
ErrorResult& aRv) override;

void SetCancelRegister(nsICancelable* aCancelRegister)
{
Expand Down Expand Up @@ -100,12 +100,12 @@ class FlyWebPublishedServerChild final : public FlyWebPublishedServer

virtual void OnFetchResponse(InternalRequest* aRequest,
InternalResponse* aResponse) override;
virtual already_AddRefed<WebSocket>
OnWebSocketAccept(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
ErrorResult& aRv) override;
virtual void OnWebSocketResponse(InternalRequest* aConnectRequest,
InternalResponse* aResponse) override;
virtual already_AddRefed<nsITransportProvider>
OnWebSocketAcceptInternal(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
ErrorResult& aRv) override;

virtual void Close() override;

Expand Down
12 changes: 5 additions & 7 deletions dom/flyweb/HttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,14 @@ HttpServer::SendResponse(InternalRequest* aRequest, InternalResponse* aResponse)
already_AddRefed<nsITransportProvider>
HttpServer::AcceptWebSocket(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
nsACString& aNegotiatedExtensions,
ErrorResult& aRv)
{
for (Connection* conn : mConnections) {
if (!conn->HasPendingWebSocketRequest(aConnectRequest)) {
continue;
}
nsCOMPtr<nsITransportProvider> provider =
conn->HandleAcceptWebSocket(aProtocol, aNegotiatedExtensions, aRv);
conn->HandleAcceptWebSocket(aProtocol, aRv);
if (aRv.Failed()) {
conn->Close();
}
Expand Down Expand Up @@ -790,7 +789,6 @@ HttpServer::Connection::TryHandleResponse(InternalRequest* aRequest,

already_AddRefed<nsITransportProvider>
HttpServer::Connection::HandleAcceptWebSocket(const Optional<nsAString>& aProtocol,
nsACString& aNegotiatedExtensions,
ErrorResult& aRv)
{
MOZ_ASSERT(mPendingWebSocketRequest);
Expand Down Expand Up @@ -830,14 +828,14 @@ HttpServer::Connection::HandleAcceptWebSocket(const Optional<nsAString>& aProtoc
}
headers->Set(NS_LITERAL_CSTRING("Sec-WebSocket-Accept"), hash, aRv);

nsAutoCString extensions;
nsAutoCString extensions, negotiatedExtensions;
mPendingWebSocketRequest->Headers()->
Get(NS_LITERAL_CSTRING("Sec-WebSocket-Extensions"), extensions, aRv);
mozilla::net::ProcessServerWebSocketExtensions(extensions,
aNegotiatedExtensions);
if (!aNegotiatedExtensions.IsEmpty()) {
negotiatedExtensions);
if (!negotiatedExtensions.IsEmpty()) {
headers->Set(NS_LITERAL_CSTRING("Sec-WebSocket-Extensions"),
aNegotiatedExtensions, aRv);
negotiatedExtensions, aRv);
}

RefPtr<TransportProvider> result = new TransportProvider();
Expand Down
2 changes: 0 additions & 2 deletions dom/flyweb/HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class HttpServer final : public nsIServerSocketListener,
already_AddRefed<nsITransportProvider>
AcceptWebSocket(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
nsACString& aNegotiatedExtensions,
ErrorResult& aRv);
void SendWebSocketResponse(InternalRequest* aConnectRequest,
InternalResponse* aResponse);
Expand Down Expand Up @@ -113,7 +112,6 @@ class HttpServer final : public nsIServerSocketListener,
InternalResponse* aResponse);
already_AddRefed<nsITransportProvider>
HandleAcceptWebSocket(const Optional<nsAString>& aProtocol,
nsACString& aNegotiatedExtensions,
ErrorResult& aRv);
void HandleWebSocketResponse(InternalResponse* aResponse);
bool HasPendingWebSocketRequest(InternalRequest* aRequest)
Expand Down

0 comments on commit b799ebc

Please sign in to comment.