forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiTermHTTPConnection.m
361 lines (326 loc) · 9.84 KB
/
iTermHTTPConnection.m
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//
// iTermHTTPConnection.m
// iTerm2
//
// Created by George Nachman on 11/4/16.
//
//
#import "iTermHTTPConnection.h"
#import "DebugLogging.h"
#import "iTermSocketAddress.h"
#include <sys/select.h>
#include <sys/time.h>
@implementation iTermHTTPConnection {
int _fd; // @synchronized(_fdSync)
NSObject *_fdSync;
NSURLRequest *_request;
NSTimeInterval _deadline;
NSMutableData *_buffer;
}
- (instancetype)initWithFileDescriptor:(int)fd
clientAddress:(iTermSocketAddress *)address
euid:(NSNumber *)euid {
self = [super init];
if (self) {
_fd = fd;
_fdSync = [[NSObject alloc] init];
_buffer = [[NSMutableData alloc] init];
_clientAddress = address;
_euid = euid;
_queue = dispatch_queue_create("com.iterm2.httpconn", NULL);
}
return self;
}
- (dispatch_io_t)newChannelOnQueue:(dispatch_queue_t)queue {
@synchronized(_fdSync) {
return dispatch_io_create(DISPATCH_IO_STREAM, _fd, queue, ^(int error) {
DLog(@"Channel closed");
});
}
}
- (void)threadSafeClose {
@synchronized(_fdSync) {
if (_fd >= 0) {
DLog(@"Close http connection from %@", [NSThread callStackSymbols]);
int rc = close(_fd);
if (rc != 0) {
XLog(@"close failed with %s", strerror(errno));
}
_fd = -1;
}
}
}
- (BOOL)sendResponseWithCode:(int)code reason:(NSString *)reason headers:(NSDictionary *)headers {
@synchronized(_fdSync) {
if (_fd < 0) {
return NO;
}
DLog(@"Send %d code", code);
BOOL ok;
ok = [self writeString:[NSString stringWithFormat:@"HTTP/1.1 %d %@\r\n", code, reason]];
if (!ok) {
[self threadSafeClose];
return NO;
}
for (NSString *key in headers) {
ok = [self writeString:[NSString stringWithFormat:@"%@: %@\r\n", key, headers[key]]];
if (!ok) {
[self threadSafeClose];
return NO;
}
}
ok = [self writeString:[NSString stringWithFormat:@"\r\n"]];
if (!ok) {
[self threadSafeClose];
return NO;
}
if (code >= 100 && code < 200) {
DLog(@"Removing deadline because code is in the 100s");
_deadline = INFINITY;
} else {
DLog(@"Non-10x code, closing connection");
[self threadSafeClose];
}
}
return YES;
}
- (void)badRequest {
[self sendResponseWithCode:400 reason:@"Bad Request" headers:@{}];
}
- (void)unacceptable {
[self sendResponseWithCode:406 reason:@"Unacceptable (upgrade Python library)" headers:@{}];
}
- (void)unauthorized {
[self sendResponseWithCode:401 reason:@"Unauthorized" headers:@{}];
}
// queue
- (NSURLRequest *)readRequest {
DLog(@"Begin reading request. Begin clock towards deadline.");
_deadline = [NSDate timeIntervalSinceReferenceDate] + 30;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *requestLine = [self nextLine];
if (!requestLine) {
DLog(@"No request line");
[self badRequest];
return nil;
}
NSArray<NSString *> *parts = [requestLine componentsSeparatedByString:@" "];
if (parts.count != 3) {
DLog(@"Wrong number of parts in request: %@", parts);
[self badRequest];
return nil;
}
request.HTTPMethod = parts[0];
request.URL = [NSURL URLWithString:parts[1]];
NSString *protocol = parts[2];
if (![protocol isEqualToString:@"HTTP/1.1"]) {
DLog(@"Protocol not 1.1: %@", protocol);
[self badRequest];
return nil;
}
NSMutableDictionary<NSString *, NSString *> *headers = [self readHeaders];
if (!headers) {
DLog(@"No headers");
[self badRequest];
return nil;
}
request.allHTTPHeaderFields = headers;
// Requests with contents are not supported.
if (headers[@"content-length"]) {
DLog(@"Received unsupported header content-length");
[self badRequest];
return nil;
}
DLog(@"Request looks good");
return request;
}
- (NSMutableDictionary<NSString *, NSString *> *)readHeaders {
DLog(@"Reading headers");
NSMutableDictionary<NSString *, NSString *> *headers = [NSMutableDictionary dictionary];
const int kMaxHeaders = 100;
while (headers.count < kMaxHeaders) {
NSString *line = [self nextLine];
if (line == nil) {
// Timeout, EOF, or error
return nil;
}
if (line.length == 0) {
DLog(@"found end of headers");
break;
}
NSInteger colon = [line rangeOfString:@":"].location;
if (colon == NSNotFound || colon + 1 == line.length) {
DLog(@"header has no colon: %@", line);
return nil;
}
NSString *key = [[line substringToIndex:colon] lowercaseString];
NSString *value = [line substringFromIndex:colon + 1];
headers[key] = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
DLog(@"Add header: %@: %@", key, value);
}
if (headers.count == kMaxHeaders) {
DLog(@"Too many headers");
return nil;
}
return headers;
}
- (NSString *)nextLine {
NSMutableData *bytes = [NSMutableData data];
NSData *crlfData = [NSData dataWithBytes:"\r\n" length:2];
// Upper bound on length of a line
while (bytes.length < 4096) {
NSData *data = [self nextByte];
if (data) {
[bytes appendData:data];
if (bytes.length >= 2 && [[bytes subdataWithRange:NSMakeRange(bytes.length - 2, 2)] isEqualToData:crlfData]) {
return [[NSString alloc] initWithData:[bytes subdataWithRange:NSMakeRange(0, bytes.length - 2)]
encoding:NSISOLatin1StringEncoding];
}
} else {
return nil;
}
}
DLog(@"Overly long line");
return nil;
}
- (NSMutableData *)readSynchronously {
if (!_buffer.length) {
[self readFromFileDescriptor];
}
if (_buffer.length) {
NSMutableData *result = _buffer;
_buffer = [NSMutableData data];
return result;
} else {
return nil;
}
}
- (NSData *)nextByte {
return [self nextBytes:1];
}
- (NSMutableData *)nextBytes:(int64_t)length {
int64_t bytesLeftToRead = length;
NSMutableData *bytes = [NSMutableData data];
while (bytes.length < length) {
if (_buffer.length == 0) {
[self readFromFileDescriptor];
}
if (_buffer.length > 0) {
int64_t chunkSize = MIN(_buffer.length, bytesLeftToRead);
[bytes appendData:[_buffer subdataWithRange:NSMakeRange(0, chunkSize)]];
bytesLeftToRead -= chunkSize;
[_buffer replaceBytesInRange:NSMakeRange(0, chunkSize) withBytes:"" length:0];
} else {
return nil;
}
}
return bytes;
}
- (BOOL)readFromFileDescriptor {
if (![self waitForIO:YES]) {
return NO;
}
int fd;
@synchronized(_fdSync) {
fd = _fd;
}
if (fd < 0) {
return NO;
}
char buffer[4096];
int rc;
do {
rc = read(fd, buffer, sizeof(buffer));
} while (rc == -1 && (errno == EINTR || errno == EAGAIN));
if (rc <= 0) {
if (rc < 0) {
DLog(@"Read failed with %s", strerror(errno));
} else {
DLog(@"EOF reached");
}
@synchronized(_fdSync) {
_fd = -1;
}
return NO;
}
[_buffer appendBytes:buffer length:rc];
return YES;
}
- (BOOL)waitForIO:(BOOL)read {
int fd;
fd_set set;
struct timeval timeout;
struct timeval *timeoutPointer = NULL;
@synchronized(_fdSync) {
if (_fd < 0) {
DLog(@"Tried select on closed file descriptor");
return NO;
}
FD_ZERO(&set);
FD_SET(_fd, &set);
if (!isinf(_deadline)) {
CGFloat dt = _deadline - [NSDate timeIntervalSinceReferenceDate];
if (!read) {
// The deadline for writes is extended so we can send an error after timing out.
dt += 5;
}
if (dt < 0) {
return NO;
}
timeout.tv_sec = floor(dt);
timeout.tv_usec = fmod(dt, 1.0) * 1000000;
timeoutPointer = &timeout;
}
fd = _fd;
}
int rc;
do {
if (read) {
rc = select(fd + 1, &set, NULL, NULL, timeoutPointer);
} else {
rc = select(fd + 1, NULL, &set, NULL, timeoutPointer);
}
} while (rc == -1 && (errno == EINTR || errno == EAGAIN));
if (rc == 0) {
DLog(@"select timed out");
return NO;
} else if (rc < 0) {
DLog(@"Select failed with %s", strerror(errno));
return NO;
}
return YES;
}
- (BOOL)writeString:(NSString *)string {
return [self writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
}
- (BOOL)writeData:(NSData *)data {
DLog(@"Want to write %d bytes of data", (int)data.length);
NSInteger offset = 0;
while (offset < data.length) {
if (![self waitForIO:NO]) {
return NO;
}
int fd;
@synchronized(_fdSync) {
fd = _fd;
}
int rc;
do {
rc = write(fd, data.bytes + offset, data.length - offset);
} while (rc == -1 && (errno == EINTR || errno == EAGAIN));
if (rc <= 0) {
if (rc < 0) {
DLog(@"Write failed with %s", strerror(errno));
} else {
DLog(@"EOF reached");
}
@synchronized(_fdSync) {
_fd = -1;
}
return NO;
}
offset += rc;
}
return YES;
}
@end