2011-07-07 12:39:24 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2011-07-04 21:18:34 -07:00
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
2012-05-21 04:12:37 -07: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-21 18:27:52 -07:00
|
|
|
|
2011-07-04 21:18:33 -07:00
|
|
|
#ifndef mozilla_net_WebSocketChannel_h
|
|
|
|
#define mozilla_net_WebSocketChannel_h
|
2011-05-21 18:27:52 -07:00
|
|
|
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIProtocolHandler.h"
|
|
|
|
#include "nsISocketTransport.h"
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIAsyncOutputStream.h"
|
|
|
|
#include "nsILoadGroup.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIDNSListener.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsIChannelEventSink.h"
|
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
|
|
|
#include "nsIStringStream.h"
|
|
|
|
#include "nsIHttpChannelInternal.h"
|
2011-06-02 17:48:06 -07:00
|
|
|
#include "nsIRandomGenerator.h"
|
2011-05-04 06:36:23 -07:00
|
|
|
#include "BaseWebSocketChannel.h"
|
2011-05-21 18:27:52 -07:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsDeque.h"
|
|
|
|
|
|
|
|
namespace mozilla { namespace net {
|
|
|
|
|
2011-12-15 15:20:17 -08:00
|
|
|
class OutboundMessage;
|
|
|
|
class OutboundEnqueuer;
|
2011-05-21 18:27:52 -07:00
|
|
|
class nsWSAdmissionManager;
|
|
|
|
class nsWSCompression;
|
2011-10-20 01:27:13 -07:00
|
|
|
class CallOnMessageAvailable;
|
|
|
|
class CallOnStop;
|
|
|
|
class CallOnServerClose;
|
|
|
|
class CallAcknowledge;
|
2011-05-21 18:27:52 -07:00
|
|
|
|
2012-06-28 17:53:50 -07:00
|
|
|
// Used to enforce "1 connecting websocket per host" rule, and reconnect delays
|
|
|
|
enum wsConnectingState {
|
|
|
|
NOT_CONNECTING = 0, // Not yet (or no longer) trying to open connection
|
|
|
|
CONNECTING_QUEUED, // Waiting for other ws to same host to finish opening
|
|
|
|
CONNECTING_DELAYED, // Delayed by "reconnect after failure" algorithm
|
|
|
|
CONNECTING_IN_PROGRESS // Started connection: waiting for result
|
|
|
|
};
|
|
|
|
|
2011-07-04 21:18:33 -07:00
|
|
|
class WebSocketChannel : public BaseWebSocketChannel,
|
|
|
|
public nsIHttpUpgradeListener,
|
|
|
|
public nsIStreamListener,
|
|
|
|
public nsIInputStreamCallback,
|
|
|
|
public nsIOutputStreamCallback,
|
|
|
|
public nsITimerCallback,
|
|
|
|
public nsIDNSListener,
|
|
|
|
public nsIInterfaceRequestor,
|
|
|
|
public nsIChannelEventSink
|
2011-05-21 18:27:52 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIHTTPUPGRADELISTENER
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
NS_DECL_NSIINPUTSTREAMCALLBACK
|
|
|
|
NS_DECL_NSIOUTPUTSTREAMCALLBACK
|
|
|
|
NS_DECL_NSITIMERCALLBACK
|
|
|
|
NS_DECL_NSIDNSLISTENER
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
|
|
|
|
2011-07-04 21:18:32 -07:00
|
|
|
// nsIWebSocketChannel methods BaseWebSocketChannel didn't implement for us
|
2011-05-04 06:36:23 -07:00
|
|
|
//
|
|
|
|
NS_IMETHOD AsyncOpen(nsIURI *aURI,
|
|
|
|
const nsACString &aOrigin,
|
|
|
|
nsIWebSocketListener *aListener,
|
|
|
|
nsISupports *aContext);
|
2011-08-03 12:15:25 -07:00
|
|
|
NS_IMETHOD Close(PRUint16 aCode, const nsACString & aReason);
|
2011-05-04 06:36:23 -07:00
|
|
|
NS_IMETHOD SendMsg(const nsACString &aMsg);
|
|
|
|
NS_IMETHOD SendBinaryMsg(const nsACString &aMsg);
|
2011-12-15 15:20:17 -08:00
|
|
|
NS_IMETHOD SendBinaryStream(nsIInputStream *aStream, PRUint32 length);
|
2011-05-04 06:36:23 -07:00
|
|
|
NS_IMETHOD GetSecurityInfo(nsISupports **aSecurityInfo);
|
|
|
|
|
2011-07-04 21:18:33 -07:00
|
|
|
WebSocketChannel();
|
2011-05-21 18:27:52 -07:00
|
|
|
static void Shutdown();
|
2011-07-04 21:18:34 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
enum {
|
2011-07-04 21:18:34 -07:00
|
|
|
// Non Control Frames
|
|
|
|
kContinuation = 0x0,
|
|
|
|
kText = 0x1,
|
|
|
|
kBinary = 0x2,
|
|
|
|
|
|
|
|
// Control Frames
|
|
|
|
kClose = 0x8,
|
|
|
|
kPing = 0x9,
|
|
|
|
kPong = 0xA
|
2011-05-21 18:27:52 -07:00
|
|
|
};
|
2011-07-04 21:18:34 -07:00
|
|
|
|
|
|
|
const static PRUint32 kControlFrameMask = 0x8;
|
|
|
|
const static PRUint8 kMaskBit = 0x80;
|
|
|
|
const static PRUint8 kFinalFragBit = 0x80;
|
2011-05-21 18:27:52 -07:00
|
|
|
|
|
|
|
protected:
|
2011-07-04 21:18:33 -07:00
|
|
|
virtual ~WebSocketChannel();
|
2011-05-04 06:36:23 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
private:
|
2011-12-15 15:20:17 -08:00
|
|
|
friend class OutboundEnqueuer;
|
2011-05-21 18:27:52 -07:00
|
|
|
friend class nsWSAdmissionManager;
|
2012-06-28 17:53:50 -07:00
|
|
|
friend class FailDelayManager;
|
2011-10-20 01:27:13 -07:00
|
|
|
friend class CallOnMessageAvailable;
|
|
|
|
friend class CallOnStop;
|
|
|
|
friend class CallOnServerClose;
|
|
|
|
friend class CallAcknowledge;
|
2011-06-07 13:12:19 -07:00
|
|
|
|
2011-12-15 15:20:17 -08:00
|
|
|
// Common send code for binary + text msgs
|
|
|
|
nsresult SendMsgCommon(const nsACString *aMsg, bool isBinary,
|
|
|
|
PRUint32 length, nsIInputStream *aStream = NULL);
|
|
|
|
|
|
|
|
void EnqueueOutgoingMessage(nsDeque &aQueue, OutboundMessage *aMsg);
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
void PrimeNewOutgoingMessage();
|
2012-01-31 20:41:47 -08:00
|
|
|
void DeleteCurrentOutGoingMessage();
|
2011-05-21 18:27:52 -07:00
|
|
|
void GeneratePong(PRUint8 *payload, PRUint32 len);
|
|
|
|
void GeneratePing();
|
|
|
|
|
2012-07-09 18:20:21 -07:00
|
|
|
void BeginOpen();
|
2011-05-21 18:27:52 -07:00
|
|
|
nsresult HandleExtensions();
|
|
|
|
nsresult SetupRequest();
|
|
|
|
nsresult ApplyForAdmission();
|
2011-06-25 11:28:27 -07:00
|
|
|
nsresult StartWebsocketData();
|
2011-06-21 07:49:38 -07:00
|
|
|
PRUint16 ResultToCloseCode(nsresult resultCode);
|
2011-07-04 21:18:34 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
void StopSession(nsresult reason);
|
|
|
|
void AbortSession(nsresult reason);
|
|
|
|
void ReleaseSession();
|
2011-06-21 07:49:50 -07:00
|
|
|
void CleanupConnection();
|
2012-05-31 16:41:24 -07:00
|
|
|
void IncrementSessionCount();
|
|
|
|
void DecrementSessionCount();
|
2011-05-21 18:27:52 -07:00
|
|
|
|
|
|
|
void EnsureHdrOut(PRUint32 size);
|
|
|
|
void ApplyMask(PRUint32 mask, PRUint8 *data, PRUint64 len);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsPersistentFramePtr();
|
2011-05-21 18:27:52 -07:00
|
|
|
nsresult ProcessInput(PRUint8 *buffer, PRUint32 count);
|
2011-12-20 00:20:12 -08:00
|
|
|
bool UpdateReadBuffer(PRUint8 *buffer, PRUint32 count,
|
|
|
|
PRUint32 accumulatedFragments,
|
|
|
|
PRUint32 *available);
|
2011-05-21 18:27:52 -07:00
|
|
|
|
2011-07-04 21:18:34 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
nsCOMPtr<nsIEventTarget> mSocketThread;
|
|
|
|
nsCOMPtr<nsIHttpChannelInternal> mChannel;
|
|
|
|
nsCOMPtr<nsIHttpChannel> mHttpChannel;
|
|
|
|
nsCOMPtr<nsICancelable> mDNSRequest;
|
|
|
|
nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
|
2011-06-02 17:48:06 -07:00
|
|
|
nsCOMPtr<nsIRandomGenerator> mRandomGenerator;
|
2011-07-04 21:18:34 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
nsCString mHashedSecret;
|
2012-06-28 17:53:50 -07:00
|
|
|
|
|
|
|
// Used as key for connection managment: Initially set to hostname from URI,
|
|
|
|
// then to IP address (unless we're leaving DNS resolution to a proxy server)
|
2011-05-21 18:27:52 -07:00
|
|
|
nsCString mAddress;
|
2012-06-28 17:53:50 -07:00
|
|
|
PRInt32 mPort; // WS server port
|
2011-05-21 18:27:52 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsISocketTransport> mTransport;
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> mSocketIn;
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> mSocketOut;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mCloseTimer;
|
|
|
|
PRUint32 mCloseTimeout; /* milliseconds */
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mOpenTimer;
|
|
|
|
PRUint32 mOpenTimeout; /* milliseconds */
|
2012-06-28 17:53:50 -07:00
|
|
|
wsConnectingState mConnecting; /* 0 if not connecting */
|
|
|
|
nsCOMPtr<nsITimer> mReconnectDelayTimer;
|
2011-05-21 18:27:52 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mPingTimer;
|
|
|
|
PRUint32 mPingTimeout; /* milliseconds */
|
|
|
|
PRUint32 mPingResponseTimeout; /* milliseconds */
|
2011-07-04 21:18:34 -07:00
|
|
|
|
2011-06-21 07:49:50 -07:00
|
|
|
nsCOMPtr<nsITimer> mLingeringCloseTimer;
|
|
|
|
const static PRInt32 kLingeringCloseTimeout = 1000;
|
|
|
|
const static PRInt32 kLingeringCloseThreshold = 50;
|
|
|
|
|
2011-12-15 15:20:17 -08:00
|
|
|
PRInt32 mMaxConcurrentConnections;
|
2011-06-25 11:28:27 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
PRUint32 mRecvdHttpOnStartRequest : 1;
|
|
|
|
PRUint32 mRecvdHttpUpgradeTransport : 1;
|
|
|
|
PRUint32 mRequestedClose : 1;
|
|
|
|
PRUint32 mClientClosed : 1;
|
|
|
|
PRUint32 mServerClosed : 1;
|
|
|
|
PRUint32 mStopped : 1;
|
|
|
|
PRUint32 mCalledOnStop : 1;
|
|
|
|
PRUint32 mPingOutstanding : 1;
|
|
|
|
PRUint32 mAllowCompression : 1;
|
|
|
|
PRUint32 mAutoFollowRedirects : 1;
|
|
|
|
PRUint32 mReleaseOnTransmit : 1;
|
2011-06-21 07:49:50 -07:00
|
|
|
PRUint32 mTCPClosed : 1;
|
2012-07-09 18:20:25 -07:00
|
|
|
PRUint32 mWasOpened : 1;
|
|
|
|
PRUint32 mOpenedHttpChannel : 1;
|
2012-04-25 19:02:12 -07:00
|
|
|
PRUint32 mDataStarted : 1;
|
2012-05-31 16:41:24 -07:00
|
|
|
PRUint32 mIncrementedSessionCount : 1;
|
|
|
|
PRUint32 mDecrementedSessionCount : 1;
|
2011-07-04 21:18:34 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
PRInt32 mMaxMessageSize;
|
|
|
|
nsresult mStopOnClose;
|
2011-08-03 12:15:25 -07:00
|
|
|
PRUint16 mServerCloseCode;
|
|
|
|
nsCString mServerCloseReason;
|
|
|
|
PRUint16 mScriptCloseCode;
|
|
|
|
nsCString mScriptCloseReason;
|
2011-05-21 18:27:52 -07:00
|
|
|
|
|
|
|
// These are for the read buffers
|
2011-12-20 00:20:12 -08:00
|
|
|
const static PRUint32 kIncomingBufferInitialSize = 16 * 1024;
|
|
|
|
// We're ok with keeping a buffer this size or smaller around for the life of
|
|
|
|
// the websocket. If a particular message needs bigger than this we'll
|
|
|
|
// increase the buffer temporarily, then drop back down to this size.
|
|
|
|
const static PRUint32 kIncomingBufferStableSize = 128 * 1024;
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
PRUint8 *mFramePtr;
|
|
|
|
PRUint8 *mBuffer;
|
|
|
|
PRUint8 mFragmentOpcode;
|
|
|
|
PRUint32 mFragmentAccumulator;
|
|
|
|
PRUint32 mBuffered;
|
|
|
|
PRUint32 mBufferSize;
|
|
|
|
nsCOMPtr<nsIStreamListener> mInflateReader;
|
|
|
|
nsCOMPtr<nsIStringInputStream> mInflateStream;
|
|
|
|
|
|
|
|
// These are for the send buffers
|
|
|
|
const static PRInt32 kCopyBreak = 1000;
|
2011-07-04 21:18:34 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
OutboundMessage *mCurrentOut;
|
|
|
|
PRUint32 mCurrentOutSent;
|
|
|
|
nsDeque mOutgoingMessages;
|
|
|
|
nsDeque mOutgoingPingMessages;
|
|
|
|
nsDeque mOutgoingPongMessages;
|
|
|
|
PRUint32 mHdrOutToSend;
|
|
|
|
PRUint8 *mHdrOut;
|
|
|
|
PRUint8 mOutHeader[kCopyBreak + 16];
|
|
|
|
nsWSCompression *mCompressor;
|
|
|
|
PRUint32 mDynamicOutputSize;
|
|
|
|
PRUint8 *mDynamicOutput;
|
|
|
|
};
|
|
|
|
|
2011-07-04 21:18:33 -07:00
|
|
|
class WebSocketSSLChannel : public WebSocketChannel
|
2011-05-21 18:27:52 -07:00
|
|
|
{
|
|
|
|
public:
|
2011-10-17 07:59:28 -07:00
|
|
|
WebSocketSSLChannel() { BaseWebSocketChannel::mEncrypted = true; }
|
2011-05-21 18:27:52 -07:00
|
|
|
protected:
|
2011-07-04 21:18:33 -07:00
|
|
|
virtual ~WebSocketSSLChannel() {}
|
2011-05-21 18:27:52 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}} // namespace mozilla::net
|
2011-05-04 06:36:23 -07:00
|
|
|
|
2011-07-04 21:18:33 -07:00
|
|
|
#endif // mozilla_net_WebSocketChannel_h
|