Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

122 lines (93 sloc) 4.167 kb
//
// CBLHTTPConnection.m
// CouchbaseLite
//
// Created by Jens Alfke on 12/29/11.
// Copyright (c) 2011-2013 Couchbase, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
//
// Based on CocoaHTTPServer/Samples/PostHTTPServer/MyHTTPConnection.m
#import "CBLHTTPConnection.h"
#import "CBLHTTPServer.h"
#import "CBLHTTPResponse.h"
#import "CBLListener.h"
#import "CBL_Server.h"
#import "CBL_Router.h"
#import "HTTPMessage.h"
#import "HTTPDataResponse.h"
#import "Test.h"
@implementation CBLHTTPConnection
- (CBLListener*) listener {
return ((CBLHTTPServer*)config.server).listener;
}
- (BOOL)isPasswordProtected:(NSString *)path {
return self.listener.requiresAuth;
}
- (NSString*) realm {
return self.listener.realm;
}
- (NSString*) passwordForUser: (NSString*)username {
LogTo(CBLListener, @"Login attempted for user '%@'", username);
return [self.listener passwordForUser: username];
}
- (BOOL)isSecureServer {
return self.listener.SSLIdentity != nil;
}
- (NSArray *)sslIdentityAndCertificates {
NSMutableArray* result = [NSMutableArray arrayWithObject: (__bridge id)self.listener.SSLIdentity];
NSArray* certs = self.listener.SSLExtraCertificates;
if (certs.count)
[result addObjectsFromArray: certs];
return result;
}
- (BOOL)supportsMethod:(NSString *)method atPath:(NSString *)path {
return $equal(method, @"POST") || $equal(method, @"PUT") || $equal(method, @"DELETE")
|| [super supportsMethod: method atPath: path];
}
- (NSObject<HTTPResponse>*)httpResponseForMethod:(NSString *)method URI:(NSString *)path {
if (requestContentLength > 0)
LogTo(CBLListener, @"%@ %@ {+%u}", method, path, (unsigned)requestContentLength);
else
LogTo(CBLListener, @"%@ %@", method, path);
// Construct an NSURLRequest from the HTTPRequest:
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL: request.url];
urlRequest.HTTPMethod = method;
urlRequest.HTTPBody = request.body;
NSDictionary* headers = request.allHeaderFields;
for (NSString* header in headers)
[urlRequest setValue: headers[header] forHTTPHeaderField: header];
// Create a CBL_Router:
CBL_Router* router = [[CBL_Router alloc] initWithServer: ((CBLHTTPServer*)config.server).tdServer
request: urlRequest
isLocal: NO];
router.processRanges = NO; // The HTTP server framework does this already
CBLHTTPResponse* response = [[CBLHTTPResponse alloc] initWithRouter: router
forConnection: self];
return response;
}
- (BOOL)expectsRequestBodyFromMethod:(NSString *)method atPath:(NSString *)path {
if ($equal(method, @"PUT")) {
// Allow PUT to /newdbname without a request body.
return ! $equal([path stringByDeletingLastPathComponent], @"/");
}
return $equal(method, @"POST") || [super expectsRequestBodyFromMethod:method atPath:path];
}
- (void)prepareForBodyWithSize:(UInt64)contentLength {
// Could use this method to open a temp file for large uploads
}
- (void)processBodyData:(NSData *)postDataChunk {
// Remember: In order to support LARGE POST uploads, the data is read in chunks.
// This prevents a 50 MB upload from being stored in RAM.
// The size of the chunks are limited by the POST_CHUNKSIZE definition.
// Therefore, this method may be called multiple times for the same POST request.
if (![request appendData:postDataChunk])
Warn(@"CBLHTTPConnection: couldn't append data chunk");
}
@end
Jump to Line
Something went wrong with that request. Please try again.