This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
TDHTTPConnection.m
70 lines (57 loc) · 2.02 KB
/
TDHTTPConnection.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
//
// TDHTTPConnection.m
// TouchDB
//
// Created by Jens Alfke on 12/29/11.
// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
//
#import "TDHTTPConnection.h"
#import "TDHTTPServer.h"
#import "TDHTTPResponse.h"
#import "TDListener.h"
#import "TDServer.h"
#import "TDRouter.h"
#import "HTTPMessage.h"
#import "HTTPDataResponse.h"
@implementation TDHTTPConnection
- (BOOL)supportsMethod:(NSString *)method atPath:(NSString *)path
{
return YES;
}
- (NSObject<HTTPResponse>*)httpResponseForMethod:(NSString *)method URI:(NSString *)path {
NSLog(@"TDListener: %@ %@", method, path); //TEMP
NSURL* url = [NSURL URLWithString: [@"touchdb://" stringByAppendingString: path]];
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL: url];
urlRequest.HTTPMethod = method;
urlRequest.HTTPBody = request.body;
NSDictionary* headers = request.allHeaderFields;
for (NSString* header in headers) {
[urlRequest setValue: [headers objectForKey: header] forHTTPHeaderField: header];
}
TDRouter* router = [[TDRouter alloc] initWithServer: ((TDHTTPServer*)config.server).tdServer
request: urlRequest];
__block bool finished = false;
__block TDResponse* routerResponse = nil;
NSMutableData* data = [NSMutableData data];
router.onResponseReady = ^(TDResponse* r) {
routerResponse = r;
};
router.onDataAvailable = ^(NSData* content) {
[data appendData: content];
};
router.onFinished = ^{
finished = true;
};
[((TDHTTPServer*)config.server).listener onServerThread: ^{[router start];}];
NSAssert(finished, @"Router didn't finish");
#if DEBUG
BOOL pretty = YES;
#else
BOOL pretty = [router boolQuery: @"pretty"];
#endif
TDHTTPResponse* response = [[[TDHTTPResponse alloc] initWithTDResponse: routerResponse
pretty: pretty] autorelease];
[router release];
return response;
}
@end