-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathInternal.h
325 lines (304 loc) · 11.1 KB
/
Internal.h
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
Copyright (C) 2010 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <[email protected]>
Date: September 2010
This file is part of the WebServer Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
$Date: 2010-09-17 16:47:13 +0100 (Fri, 17 Sep 2010) $ $Revision: 31364 $
*/
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSData.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSException.h>
#import <Foundation/NSFileHandle.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSRunLoop.h>
#import <Foundation/NSString.h>
#import <Foundation/NSThread.h>
#import <Foundation/NSTimer.h>
#import <GNUstepBase/GSMime.h>
#import <Performance/GSLinkedList.h>
@class WebServer;
@class WebServerConfig;
@class WebServerConnection;
@class WebServerRequest;
@class WebServerResponse;
/* Class to manage an I/O thread and the connections running on it.
*
* The -run method of this class is called in the thread used by each
* instance, and this method runs a runloop to handle I/O and timeouts.
*
* Each instance runs a repeating timer which checks the connections
* to see if any have timed out (and also keeps the runloop alive when
* there are currently no connections performing I/O).
*
* The connections are held in three linked lists according to their state
* (which determines how long the connection can be idle before timing out).
*
* Whenever an event occurs on a connection, the 'ticker' timestamp for the
* connection is updated and the connection is moved to the end of its list,
* so the timeout process knows the connections in the list are ordered,
* and it doesn't need to check further than the first connection which
* has not timed out.
*/
@interface IOThread : NSObject
{
@public
WebServer *server; // The owner of this thread (not retained).
NSThread *thread; // The actual thread being used.
NSLock *threadLock; // Protect ivars from changes.
NSTimer *timer; // Repeated regular timer (not retained).
NSTimeInterval cTimeout; // Timeout period for connections.
GSLinkedList *processing; // Connections processing a request.
GSLinkedList *handshakes; // Connections performing SSL handshake
GSLinkedList *readwrites; // Connections performing read or write.
GSLinkedList *keepalives; // Connections waiting for a new request.
uint16_t keepaliveCount; // Number of connections in keepalive.
uint16_t keepaliveMax; // Maximum connections kept alive.
unsigned number; // The identifier for this thread.
}
- (void) run;
- (void) timeout: (NSTimer*)t;
@end
/* This class is used to hold configuration information needed by a single
* connection ... once set up an instance is never modified so it can be
* shared between threads. When configuration is modified, it is replaced
* by a new instance.
*/
@interface WebServerConfig: NSObject
{
@public
BOOL verbose; // logging type is detailed/verbose
BOOL durations; // log request and connection times
BOOL reverse; // should do reverse DNS lookup
BOOL secureProxy; // using a secure proxy
BOOL logRawIO; // log raw I/O on connection
BOOL foldHeaders; // Whether long headers are folded
NSUInteger maxBodySize;
NSUInteger maxRequestSize;
NSUInteger maxConnectionRequests;
NSTimeInterval maxConnectionDuration;
NSSet *permittedMethods;
}
@end
@interface WebServerRequest : GSMimeDocument
- (NSString*) address;
@end
/* We need to ensure that our map table holds response information safely
* and efficiently ... so we use a subclass where we control -hash and
* -isEqual: to ensure that each object is unique and quick.
* We also store a pointer to the owning connection so that we can find
* the connections from the response really quickly.
*/
@interface WebServerResponse : GSMimeDocument
{
WebServerConnection *webServerConnection;
NSObject *userInfo;
BOOL prepared; // request/response pair is set up
BOOL foldHeaders;
BOOL completing;
}
- (BOOL) completing;
- (BOOL) foldHeaders;
- (BOOL) prepared;
- (void) setFoldHeaders: (BOOL)aFlag;
- (void) setCompleting;
- (void) setPrepared;
- (void) setUserInfo: (NSObject*)info;
- (void) setWebServerConnection: (WebServerConnection*)c;
- (NSObject*) userInfo;
- (WebServerConnection*) webServerConnection;
@end
typedef enum {
WSHCountRequests,
WSHCountConnections,
WSHCountConnectedHosts
} WSHType;
/* Special header used to store information in a request.
*/
@interface WebServerHeader : GSMimeHeader
{
WSHType wshType;
NSObject *wshObject;
}
- (id) initWithType: (WSHType)t andObject: (NSObject*)o;
@end
@interface WebServerAuthenticationFailure : NSObject
{
@private
NSDate *_date;
NSTimeInterval _banTime;
}
+ (id) failureWithBanTime: (NSTimeInterval)banTime;
- (NSDate*) date;
- (NSTimeInterval) banTime;
- (NSDate*) blockUntil;
@end
@interface WebServerAuthenticationFailureLog : NSObject
{
@private
NSTimeInterval _findTime;
NSMutableDictionary *_failuresByAddress;
NSMutableDictionary *_banUntilByAddress;
NSLock *_lock;
NSTimeInterval _cleanupInterval;
NSTimer *_cleanupTimer;
}
- (NSTimeInterval) findTime;
- (NSTimeInterval) cleanupInterval;
- (void) setFindTime: (NSTimeInterval)findTime;
- (void) setCleanupInterval: (NSTimeInterval)interval;
- (void) addFailureForAddress: (NSString*)address
banTime: (NSTimeInterval)banTime;
- (void) removeFailuresForAddress: (NSString*)address;
- (NSUInteger) failureCountForAddress: (NSString*)address
blockUntil: (NSDate**)until;
- (void) banAddress: (NSString*)address
until: (NSDate*)until;
- (NSDate*) isBanned: (NSString*)address;
@end
@interface WebServerConnection : GSListLink
{
NSNotificationCenter *nc;
IOThread *ioThread;
WebServer *server;
WebServerResponse *response;
WebServerConfig *conf;
NSString *address; // Client address (for host limiting)
NSString *command; // Command sent by client
NSString *agent; // User-Agent header
NSString *result; // Result sent back
NSString *user; // The remote user
NSFileHandle *handle;
GSMimeParser *parser;
NSMutableData *buffer;
NSData *excess;
NSUInteger byteCount;
NSUInteger identity;
NSUInteger requestCount;
NSTimeInterval requestStart;
NSTimeInterval connectionStart;
NSTimeInterval duration;
NSTimeInterval handshakeRetry;
NSTimer *handshakeTimer;
NSUInteger requests;
NSUInteger bodyLength;
BOOL shouldClose;
BOOL hasReset;
BOOL simple;
BOOL hadHeader; // Header has been completely read?
BOOL hadRequest; // Request has been completely read?
BOOL autoBlock; // May perform auth fail blocking.
BOOL quiet; // Suppress log of warning/debug info?
BOOL ssl; // Should perform SSL negotiation?
BOOL responding; // Writing to remote system
BOOL streaming; // Need to write more data?
BOOL chunked; // Stream in chunks?
uint32_t incremental; // Incremental parsing of request?
NSMutableData *outBuffer;
NSString *frameOpts;
NSString *locAddr; // local IP address
NSString *remAddr; // remote IP address
NSString *locPort; // local IP port
NSString *remPort; // remote IP port
NSString *descIn; // Cached description (incoming)
NSString *descOut; // Cached description (outgoing)
@public
NSTimeInterval ticked;
NSTimeInterval extended;
}
- (NSString*) address;
- (NSString*) audit;
- (void) block: (NSTimeInterval)ti;
- (NSTimeInterval) connectionDuration: (NSTimeInterval)now;
- (NSString*) description;
- (NSString*) descriptionOut;
- (void) end;
- (BOOL) ended;
- (NSData*) excess;
- (BOOL) foldHeaders;
- (NSFileHandle*) handle;
- (void) handshake;
- (BOOL) hasReset;
- (NSUInteger) identity;
- (id) initWithHandle: (NSFileHandle*)hdl
onThread: (IOThread*)t
for: (WebServer*)svr
config: (WebServerConfig*)c
quiet: (BOOL)q
ssl: (BOOL)s
refusal: (NSString*)r;
- (IOThread*) ioThread;
- (NSString*) localAddress;
- (NSString*) localPort;
- (NSUInteger) moreBytes: (NSUInteger)count;
- (GSMimeParser*) parser;
- (BOOL) processing;
- (BOOL) quiet;
- (NSString*) remoteAddress;
- (NSString*) remotePort;
- (WebServerRequest*) request;
- (NSTimeInterval) requestDuration: (NSTimeInterval)now;
- (void) reset;
- (void) respond: (NSData*)stream;
- (WebServerResponse*) response;
- (void) run;
- (void) setConnectionStart: (NSTimeInterval)when;
- (void) setExcess: (NSData*)d;
- (void) setParser: (GSMimeParser*)aParser;
- (void) setProcessing: (BOOL)aFlag;
- (void) setQuiet: (BOOL)aFlag;
- (void) setRequestEnd: (NSTimeInterval)when;
- (void) setRequestStart: (NSTimeInterval)when;
- (void) setResult: (NSString*)aString;
- (void) setShouldClose: (BOOL)aFlag;
- (void) setSimple: (BOOL)aFlag;
- (void) setTicked: (NSTimeInterval)t;
- (void) setUser: (NSString*)aString;
- (BOOL) shouldClose;
- (void) shutdown;
- (void) start;
- (BOOL) verbose;
- (void) _didData: (NSData*)d;
- (void) _didRead: (NSNotification*)notification;
- (void) _didWrite: (NSNotification*)notification;
- (void) _keepalive;
- (void) _timeout: (NSTimer*)t;
@end
@interface WebServer (Internal)
- (void) _alert: (NSString*)fmt, ...;
- (void) _audit: (WebServerConnection*)connection;
- (void) _blockAddress: (NSString*)address forInterval: (NSTimeInterval)ti;
- (NSDate*) _blocked: (NSString*)address;
- (void) _completedResponse: (WebServerResponse*)r duration: (NSTimeInterval)t;
- (BOOL) _connection: (WebServerConnection*)conn
changedAddressFrom: (NSString*)oldAddress;
- (void) _didConnect: (NSNotification*)notification;
- (void) _endConnect: (WebServerConnection*)connection;
- (NSString*) _ioThreadDescription;
- (uint32_t) _incremental: (WebServerConnection*)connection;
- (void) _listen;
- (void) _log: (NSString*)fmt, ...;
- (NSString*) _poolDescription;
- (void) _process1: (WebServerConnection*)connection;
- (void) _process2: (WebServerConnection*)connection;
- (void) _removeConnection: (WebServerConnection*)connection;
- (void) _setup;
- (NSUInteger) _setIncrementalBytes: (const void*)bytes
length: (NSUInteger)length
forRequest: (WebServerRequest*)request;
- (NSString*) _xCountRequests;
- (NSString*) _xCountConnections;
- (NSString*) _xCountConnectedHosts;
@end