1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#ifndef mozilla_dom_HttpServer_h
#define mozilla_dom_HttpServer_h
#include "nsISupportsImpl.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "nsITLSServerSocket.h"
#include "nsIAsyncInputStream.h"
#include "nsIAsyncOutputStream.h"
#include "mozilla/Variant.h"
#include "nsIRequestObserver.h"
#include "mozilla/MozPromise.h"
#include "nsITransportProvider.h"
#include "nsILocalCertService.h"
class nsIX509Cert;
namespace mozilla {
namespace dom {
extern bool
ContainsToken(const nsCString& aList, const nsCString& aToken);
class InternalRequest;
class InternalResponse;
class HttpServerListener
{
public:
// switch to NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING when that lands
NS_IMETHOD_(MozExternalRefCountType) AddRef(void) = 0;
NS_IMETHOD_(MozExternalRefCountType) Release(void) = 0;
virtual void OnServerStarted(nsresult aStatus) = 0;
virtual void OnRequest(InternalRequest* aRequest) = 0;
virtual void OnWebSocket(InternalRequest* aConnectRequest) = 0;
virtual void OnServerClose() = 0;
};
class HttpServer final : public nsIServerSocketListener,
public nsILocalCertGetCallback
{
public:
HttpServer();
NS_DECL_ISUPPORTS
NS_DECL_NSISERVERSOCKETLISTENER
NS_DECL_NSILOCALCERTGETCALLBACK
void Init(int32_t aPort, bool aHttps, HttpServerListener* aListener);
void SendResponse(InternalRequest* aRequest, InternalResponse* aResponse);
already_AddRefed<nsITransportProvider>
AcceptWebSocket(InternalRequest* aConnectRequest,
const Optional<nsAString>& aProtocol,
ErrorResult& aRv);
void SendWebSocketResponse(InternalRequest* aConnectRequest,
InternalResponse* aResponse);
void Close();
void GetCertKey(nsACString& aKey);
int32_t GetPort()
{
return mPort;
}
private:
~HttpServer();
nsresult StartServerSocket(nsIX509Cert* aCert);
void NotifyStarted(nsresult aStatus);
class TransportProvider final : public nsITransportProvider
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSITRANSPORTPROVIDER
void SetTransport(nsISocketTransport* aTransport,
nsIAsyncInputStream* aInput,
nsIAsyncOutputStream* aOutput);
private:
virtual ~TransportProvider();
void MaybeNotify();
nsCOMPtr<nsIHttpUpgradeListener> mListener;
nsCOMPtr<nsISocketTransport> mTransport;
nsCOMPtr<nsIAsyncInputStream> mInput;
nsCOMPtr<nsIAsyncOutputStream> mOutput;
};
class Connection final : public nsIInputStreamCallback
, public nsIOutputStreamCallback
, public nsITLSServerSecurityObserver
{
public:
Connection(nsISocketTransport* aTransport,
HttpServer* aServer,
nsresult& rv);
NS_DECL_ISUPPORTS
NS_DECL_NSIINPUTSTREAMCALLBACK
NS_DECL_NSIOUTPUTSTREAMCALLBACK
NS_DECL_NSITLSSERVERSECURITYOBSERVER
bool TryHandleResponse(InternalRequest* aRequest,
InternalResponse* aResponse);
already_AddRefed<nsITransportProvider>
HandleAcceptWebSocket(const Optional<nsAString>& aProtocol,
ErrorResult& aRv);
void HandleWebSocketResponse(InternalResponse* aResponse);
bool HasPendingWebSocketRequest(InternalRequest* aRequest)
{
return aRequest == mPendingWebSocketRequest;
}
void Close();
private:
~Connection();
void SetSecurityObserver(bool aListen);
static nsresult ReadSegmentsFunc(nsIInputStream* aIn,
void* aClosure,
const char* aBuffer,
uint32_t aToOffset,
uint32_t aCount,
uint32_t* aWriteCount);
nsresult ConsumeInput(const char*& aBuffer,
const char* aEnd);
nsresult ConsumeLine(const char* aBuffer,
size_t aLength);
void MaybeAddPendingHeader();
void QueueResponse(InternalResponse* aResponse);
RefPtr<HttpServer> mServer;
nsCOMPtr<nsISocketTransport> mTransport;
nsCOMPtr<nsIAsyncInputStream> mInput;
nsCOMPtr<nsIAsyncOutputStream> mOutput;
enum { eRequestLine, eHeaders, eBody, ePause } mState;
RefPtr<InternalRequest> mPendingReq;
uint32_t mPendingReqVersion;
nsCString mInputBuffer;
nsCString mPendingHeaderName;
nsCString mPendingHeaderValue;
uint32_t mRemainingBodySize;
nsCOMPtr<nsIAsyncOutputStream> mCurrentRequestBody;
bool mCloseAfterRequest;
typedef Pair<RefPtr<InternalRequest>,
RefPtr<InternalResponse>> PendingRequest;
nsTArray<PendingRequest> mPendingRequests;
RefPtr<MozPromise<nsresult, bool, false>> mOutputCopy;
RefPtr<InternalRequest> mPendingWebSocketRequest;
RefPtr<TransportProvider> mWebSocketTransportProvider;
struct OutputBuffer {
nsCString mString;
nsCOMPtr<nsIInputStream> mStream;
bool mChunked;
};
nsTArray<OutputBuffer> mOutputBuffers;
};
friend class Connection;
RefPtr<HttpServerListener> mListener;
nsCOMPtr<nsIServerSocket> mServerSocket;
nsCOMPtr<nsIX509Cert> mCert;
nsTArray<RefPtr<Connection>> mConnections;
int32_t mPort;
bool mHttps;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_HttpServer_h
|