2011-05-04 13:36:23 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-04 13:36:23 +00:00
|
|
|
|
|
|
|
#include "WebSocketLog.h"
|
|
|
|
#include "BaseWebSocketChannel.h"
|
2014-03-27 20:58:19 +00:00
|
|
|
#include "MainThreadUtils.h"
|
2011-05-04 13:36:23 +00:00
|
|
|
#include "nsILoadGroup.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsStandardURL.h"
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
2012-07-30 14:20:58 +00:00
|
|
|
PRLogModuleInfo *webSocketLog = nullptr;
|
2011-05-04 13:36:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
BaseWebSocketChannel::BaseWebSocketChannel()
|
2013-03-28 22:52:16 +00:00
|
|
|
: mEncrypted(0)
|
|
|
|
, mWasOpened(0)
|
|
|
|
, mClientSetPingInterval(0)
|
|
|
|
, mClientSetPingTimeout(0)
|
2014-08-25 03:21:00 +00:00
|
|
|
, mPingForced(0)
|
2013-03-28 22:52:16 +00:00
|
|
|
, mPingInterval(0)
|
|
|
|
, mPingResponseTimeout(10000)
|
2011-05-04 13:36:23 +00:00
|
|
|
{
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (!webSocketLog)
|
|
|
|
webSocketLog = PR_NewLogModule("nsWebSocket");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-07-05 04:18:32 +00:00
|
|
|
// BaseWebSocketChannel::nsIWebSocketChannel
|
2011-05-04 13:36:23 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::GetOriginalURI(nsIURI **aOriginalURI)
|
|
|
|
{
|
|
|
|
LOG(("BaseWebSocketChannel::GetOriginalURI() %p\n", this));
|
|
|
|
|
|
|
|
if (!mOriginalURI)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
NS_ADDREF(*aOriginalURI = mOriginalURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::GetURI(nsIURI **aURI)
|
|
|
|
{
|
|
|
|
LOG(("BaseWebSocketChannel::GetURI() %p\n", this));
|
|
|
|
|
|
|
|
if (!mOriginalURI)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (mURI)
|
|
|
|
NS_ADDREF(*aURI = mURI);
|
|
|
|
else
|
|
|
|
NS_ADDREF(*aURI = mOriginalURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::
|
|
|
|
GetNotificationCallbacks(nsIInterfaceRequestor **aNotificationCallbacks)
|
|
|
|
{
|
|
|
|
LOG(("BaseWebSocketChannel::GetNotificationCallbacks() %p\n", this));
|
|
|
|
NS_IF_ADDREF(*aNotificationCallbacks = mCallbacks);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::
|
|
|
|
SetNotificationCallbacks(nsIInterfaceRequestor *aNotificationCallbacks)
|
|
|
|
{
|
|
|
|
LOG(("BaseWebSocketChannel::SetNotificationCallbacks() %p\n", this));
|
|
|
|
mCallbacks = aNotificationCallbacks;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::GetLoadGroup(nsILoadGroup **aLoadGroup)
|
|
|
|
{
|
|
|
|
LOG(("BaseWebSocketChannel::GetLoadGroup() %p\n", this));
|
|
|
|
NS_IF_ADDREF(*aLoadGroup = mLoadGroup);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::SetLoadGroup(nsILoadGroup *aLoadGroup)
|
|
|
|
{
|
|
|
|
LOG(("BaseWebSocketChannel::SetLoadGroup() %p\n", this));
|
|
|
|
mLoadGroup = aLoadGroup;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-13 20:26:17 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::SetLoadInfo(nsILoadInfo* aLoadInfo)
|
|
|
|
{
|
|
|
|
mLoadInfo = aLoadInfo;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::GetLoadInfo(nsILoadInfo** aLoadInfo)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aLoadInfo = mLoadInfo);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-04 03:46:13 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::GetExtensions(nsACString &aExtensions)
|
|
|
|
{
|
|
|
|
LOG(("BaseWebSocketChannel::GetExtensions() %p\n", this));
|
|
|
|
aExtensions = mNegotiatedExtensions;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:36:23 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::GetProtocol(nsACString &aProtocol)
|
|
|
|
{
|
|
|
|
LOG(("BaseWebSocketChannel::GetProtocol() %p\n", this));
|
|
|
|
aProtocol = mProtocol;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::SetProtocol(const nsACString &aProtocol)
|
|
|
|
{
|
|
|
|
LOG(("BaseWebSocketChannel::SetProtocol() %p\n", this));
|
|
|
|
mProtocol = aProtocol; /* the sub protocol */
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-28 22:52:16 +00:00
|
|
|
NS_IMETHODIMP
|
2013-04-05 20:52:12 +00:00
|
|
|
BaseWebSocketChannel::GetPingInterval(uint32_t *aSeconds)
|
2013-03-28 22:52:16 +00:00
|
|
|
{
|
2013-04-05 20:52:12 +00:00
|
|
|
// stored in ms but should only have second resolution
|
|
|
|
MOZ_ASSERT(!(mPingInterval % 1000));
|
|
|
|
|
|
|
|
*aSeconds = mPingInterval / 1000;
|
2013-03-28 22:52:16 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-05 20:52:12 +00:00
|
|
|
BaseWebSocketChannel::SetPingInterval(uint32_t aSeconds)
|
2013-03-28 22:52:16 +00:00
|
|
|
{
|
|
|
|
if (mWasOpened) {
|
|
|
|
return NS_ERROR_IN_PROGRESS;
|
|
|
|
}
|
|
|
|
|
2013-04-05 20:52:12 +00:00
|
|
|
mPingInterval = aSeconds * 1000;
|
2013-03-28 22:52:16 +00:00
|
|
|
mClientSetPingInterval = 1;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-05 20:52:12 +00:00
|
|
|
BaseWebSocketChannel::GetPingTimeout(uint32_t *aSeconds)
|
2013-03-28 22:52:16 +00:00
|
|
|
{
|
2013-04-05 20:52:12 +00:00
|
|
|
// stored in ms but should only have second resolution
|
|
|
|
MOZ_ASSERT(!(mPingResponseTimeout % 1000));
|
|
|
|
|
|
|
|
*aSeconds = mPingResponseTimeout / 1000;
|
2013-03-28 22:52:16 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-05 20:52:12 +00:00
|
|
|
BaseWebSocketChannel::SetPingTimeout(uint32_t aSeconds)
|
2013-03-28 22:52:16 +00:00
|
|
|
{
|
|
|
|
if (mWasOpened) {
|
|
|
|
return NS_ERROR_IN_PROGRESS;
|
|
|
|
}
|
|
|
|
|
2013-04-05 20:52:12 +00:00
|
|
|
mPingResponseTimeout = aSeconds * 1000;
|
2013-03-28 22:52:16 +00:00
|
|
|
mClientSetPingTimeout = 1;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:36:23 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// BaseWebSocketChannel::nsIProtocolHandler
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::GetScheme(nsACString &aScheme)
|
|
|
|
{
|
2011-07-05 04:18:33 +00:00
|
|
|
LOG(("BaseWebSocketChannel::GetScheme() %p\n", this));
|
2011-05-04 13:36:23 +00:00
|
|
|
|
|
|
|
if (mEncrypted)
|
|
|
|
aScheme.AssignLiteral("wss");
|
|
|
|
else
|
|
|
|
aScheme.AssignLiteral("ws");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
BaseWebSocketChannel::GetDefaultPort(int32_t *aDefaultPort)
|
2011-05-04 13:36:23 +00:00
|
|
|
{
|
2011-07-05 04:18:33 +00:00
|
|
|
LOG(("BaseWebSocketChannel::GetDefaultPort() %p\n", this));
|
2011-05-04 13:36:23 +00:00
|
|
|
|
|
|
|
if (mEncrypted)
|
|
|
|
*aDefaultPort = kDefaultWSSPort;
|
|
|
|
else
|
|
|
|
*aDefaultPort = kDefaultWSPort;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
BaseWebSocketChannel::GetProtocolFlags(uint32_t *aProtocolFlags)
|
2011-05-04 13:36:23 +00:00
|
|
|
{
|
2011-07-05 04:18:33 +00:00
|
|
|
LOG(("BaseWebSocketChannel::GetProtocolFlags() %p\n", this));
|
2011-05-04 13:36:23 +00:00
|
|
|
|
|
|
|
*aProtocolFlags = URI_NORELATIVE | URI_NON_PERSISTABLE | ALLOWS_PROXY |
|
|
|
|
ALLOWS_PROXY_HTTP | URI_DOES_NOT_RETURN_DATA | URI_DANGEROUS_TO_LOAD;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::NewURI(const nsACString & aSpec, const char *aOriginCharset,
|
2012-07-06 20:14:07 +00:00
|
|
|
nsIURI *aBaseURI, nsIURI **_retval)
|
2011-05-04 13:36:23 +00:00
|
|
|
{
|
2011-07-05 04:18:33 +00:00
|
|
|
LOG(("BaseWebSocketChannel::NewURI() %p\n", this));
|
2011-05-04 13:36:23 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t port;
|
2011-05-04 13:36:23 +00:00
|
|
|
nsresult rv = GetDefaultPort(&port);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsRefPtr<nsStandardURL> url = new nsStandardURL();
|
|
|
|
rv = url->Init(nsIStandardURL::URLTYPE_AUTHORITY, port, aSpec,
|
|
|
|
aOriginCharset, aBaseURI);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
NS_ADDREF(*_retval = url);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-23 00:16:57 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::NewChannel2(nsIURI* aURI,
|
|
|
|
nsILoadInfo* aLoadInfo,
|
|
|
|
nsIChannel** outChannel)
|
|
|
|
{
|
|
|
|
LOG(("BaseWebSocketChannel::NewChannel2() %p\n", this));
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:36:23 +00:00
|
|
|
NS_IMETHODIMP
|
2012-07-06 20:14:07 +00:00
|
|
|
BaseWebSocketChannel::NewChannel(nsIURI *aURI, nsIChannel **_retval)
|
2011-05-04 13:36:23 +00:00
|
|
|
{
|
2011-07-05 04:18:33 +00:00
|
|
|
LOG(("BaseWebSocketChannel::NewChannel() %p\n", this));
|
2011-05-04 13:36:23 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
BaseWebSocketChannel::AllowPort(int32_t port, const char *scheme,
|
2012-07-06 20:14:07 +00:00
|
|
|
bool *_retval)
|
2011-05-04 13:36:23 +00:00
|
|
|
{
|
2011-07-05 04:18:33 +00:00
|
|
|
LOG(("BaseWebSocketChannel::AllowPort() %p\n", this));
|
2011-05-04 13:36:23 +00:00
|
|
|
|
|
|
|
// do not override any blacklisted ports
|
2011-10-17 14:59:28 +00:00
|
|
|
*_retval = false;
|
2011-05-04 13:36:23 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-27 20:58:19 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// BaseWebSocketChannel::nsIThreadRetargetableRequest
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BaseWebSocketChannel::RetargetDeliveryTo(nsIEventTarget* aTargetThread)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aTargetThread);
|
|
|
|
MOZ_ASSERT(!mTargetThread, "Delivery target should be set once, before AsyncOpen");
|
|
|
|
MOZ_ASSERT(!mWasOpened, "Should not be called after AsyncOpen!");
|
|
|
|
|
|
|
|
mTargetThread = do_QueryInterface(aTargetThread);
|
|
|
|
MOZ_ASSERT(mTargetThread);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:36:23 +00:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|