2012-12-30 14:23:49 +08:00
|
|
|
/*
|
2014-02-07 10:44:04 +08:00
|
|
|
Copyright (c) 2012-2014, Pierre-Olivier Latour
|
2013-12-30 11:02:01 +08:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
* The name of Pierre-Olivier Latour may not be used to endorse
|
|
|
|
or promote products derived from this software without specific
|
|
|
|
prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL PIERRE-OLIVIER LATOUR BE LIABLE FOR ANY
|
|
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2012-12-30 14:23:49 +08:00
|
|
|
|
2014-01-24 03:33:33 +08:00
|
|
|
#import <TargetConditionals.h>
|
2014-01-30 01:14:23 +08:00
|
|
|
#import <AvailabilityMacros.h>
|
2014-01-24 03:33:33 +08:00
|
|
|
|
2014-01-09 14:27:15 +08:00
|
|
|
#if __has_feature(objc_arc)
|
|
|
|
#define ARC_BRIDGE __bridge
|
|
|
|
#define ARC_BRIDGE_RELEASE(__OBJECT__) CFBridgingRelease(__OBJECT__)
|
|
|
|
#define ARC_RETAIN(__OBJECT__) __OBJECT__
|
|
|
|
#define ARC_RELEASE(__OBJECT__)
|
|
|
|
#define ARC_AUTORELEASE(__OBJECT__) __OBJECT__
|
|
|
|
#define ARC_DEALLOC(__OBJECT__)
|
2014-01-24 03:33:33 +08:00
|
|
|
#if (TARGET_OS_IPHONE && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0)) || (!TARGET_OS_IPHONE && (__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_8))
|
|
|
|
#define ARC_DISPATCH_RELEASE(__OBJECT__)
|
|
|
|
#else
|
|
|
|
#define ARC_DISPATCH_RELEASE(__OBJECT__) dispatch_release(__OBJECT__)
|
|
|
|
#endif
|
2014-01-09 14:27:15 +08:00
|
|
|
#else
|
|
|
|
#define ARC_BRIDGE
|
|
|
|
#define ARC_BRIDGE_RELEASE(__OBJECT__) [(id)__OBJECT__ autorelease]
|
|
|
|
#define ARC_RETAIN(__OBJECT__) [__OBJECT__ retain]
|
|
|
|
#define ARC_RELEASE(__OBJECT__) [__OBJECT__ release]
|
|
|
|
#define ARC_AUTORELEASE(__OBJECT__) [__OBJECT__ autorelease]
|
|
|
|
#define ARC_DEALLOC(__OBJECT__) [__OBJECT__ dealloc]
|
2014-01-24 03:33:33 +08:00
|
|
|
#define ARC_DISPATCH_RELEASE(__OBJECT__) dispatch_release(__OBJECT__)
|
2014-01-09 14:27:15 +08:00
|
|
|
#endif
|
|
|
|
|
2014-04-08 10:27:58 +08:00
|
|
|
#import "GCDWebServerHTTPStatusCodes.h"
|
|
|
|
|
2012-12-31 01:42:19 +08:00
|
|
|
#import "GCDWebServerConnection.h"
|
2012-12-30 14:23:49 +08:00
|
|
|
|
2014-04-07 02:32:07 +08:00
|
|
|
#import "GCDWebServerDataRequest.h"
|
|
|
|
#import "GCDWebServerFileRequest.h"
|
|
|
|
#import "GCDWebServerMultiPartFormRequest.h"
|
|
|
|
#import "GCDWebServerURLEncodedFormRequest.h"
|
|
|
|
|
|
|
|
#import "GCDWebServerDataResponse.h"
|
|
|
|
#import "GCDWebServerFileResponse.h"
|
2014-04-08 04:10:17 +08:00
|
|
|
#import "GCDWebServerStreamingResponse.h"
|
2014-04-07 02:32:07 +08:00
|
|
|
|
2012-12-31 12:38:31 +08:00
|
|
|
#ifdef __GCDWEBSERVER_LOGGING_HEADER__
|
2012-12-30 14:23:49 +08:00
|
|
|
|
2012-12-31 00:53:44 +08:00
|
|
|
// Define __GCDWEBSERVER_LOGGING_HEADER__ as a preprocessor constant to redirect GCDWebServer logging to your own system
|
|
|
|
#import __GCDWEBSERVER_LOGGING_HEADER__
|
2012-12-30 14:23:49 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2014-03-27 00:25:10 +08:00
|
|
|
extern void GCDLogMessage(long level, NSString* format, ...) NS_FORMAT_FUNCTION(2, 3);
|
|
|
|
|
|
|
|
#define LOG_VERBOSE(...) GCDLogMessage(1, __VA_ARGS__)
|
|
|
|
#define LOG_INFO(...) GCDLogMessage(2, __VA_ARGS__)
|
|
|
|
#define LOG_WARNING(...) GCDLogMessage(3, __VA_ARGS__)
|
|
|
|
#define LOG_ERROR(...) GCDLogMessage(4, __VA_ARGS__)
|
|
|
|
#define LOG_EXCEPTION(__EXCEPTION__) GCDLogMessage(5, @"%@", __EXCEPTION__)
|
2012-12-30 14:23:49 +08:00
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
|
|
|
|
#define DCHECK(__CONDITION__)
|
|
|
|
#define DNOT_REACHED()
|
|
|
|
#define LOG_DEBUG(...)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define DCHECK(__CONDITION__) \
|
|
|
|
do { \
|
|
|
|
if (!(__CONDITION__)) { \
|
|
|
|
abort(); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define DNOT_REACHED() abort()
|
2014-03-27 00:25:10 +08:00
|
|
|
#define LOG_DEBUG(...) GCDLogMessage(0, __VA_ARGS__)
|
2012-12-30 14:23:49 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define kGCDWebServerDefaultMimeType @"application/octet-stream"
|
2013-04-02 06:42:16 +08:00
|
|
|
#define kGCDWebServerGCDQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
|
2014-04-07 03:09:44 +08:00
|
|
|
#define kGCDWebServerErrorDomain @"GCDWebServerErrorDomain"
|
2012-12-30 14:23:49 +08:00
|
|
|
|
2014-04-08 10:29:04 +08:00
|
|
|
static inline BOOL GCDWebServerIsValidByteRange(NSRange range) {
|
|
|
|
return ((range.location != NSNotFound) || (range.length > 0));
|
|
|
|
}
|
|
|
|
|
2014-04-07 02:32:07 +08:00
|
|
|
extern NSString* GCDWebServerExtractHeaderParameter(NSString* header, NSString* attribute);
|
|
|
|
extern NSStringEncoding GCDWebServerStringEncodingFromCharset(NSString* charset);
|
|
|
|
|
2012-12-31 01:42:19 +08:00
|
|
|
@interface GCDWebServerConnection ()
|
2014-03-30 12:17:51 +08:00
|
|
|
- (id)initWithServer:(GCDWebServer*)server localAddress:(NSData*)localAddress remoteAddress:(NSData*)remoteAddress socket:(CFSocketNativeHandle)socket;
|
2012-12-31 01:42:19 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GCDWebServer ()
|
|
|
|
@property(nonatomic, readonly) NSArray* handlers;
|
|
|
|
@end
|
|
|
|
|
2014-03-20 00:19:59 +08:00
|
|
|
@interface GCDWebServerHandler : NSObject
|
2012-12-31 01:42:19 +08:00
|
|
|
@property(nonatomic, readonly) GCDWebServerMatchBlock matchBlock;
|
|
|
|
@property(nonatomic, readonly) GCDWebServerProcessBlock processBlock;
|
2012-12-31 10:48:25 +08:00
|
|
|
- (id)initWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock;
|
2012-12-31 01:42:19 +08:00
|
|
|
@end
|
2014-04-07 01:44:53 +08:00
|
|
|
|
2014-04-07 03:09:44 +08:00
|
|
|
@interface GCDWebServerRequest ()
|
2014-04-08 03:21:19 +08:00
|
|
|
@property(nonatomic, readonly) BOOL usesChunkedTransferEncoding;
|
2014-04-07 03:09:44 +08:00
|
|
|
- (BOOL)performOpen:(NSError**)error;
|
|
|
|
- (BOOL)performWriteData:(NSData*)data error:(NSError**)error;
|
|
|
|
- (BOOL)performClose:(NSError**)error;
|
|
|
|
@end
|
|
|
|
|
2014-04-07 01:44:53 +08:00
|
|
|
@interface GCDWebServerResponse ()
|
|
|
|
@property(nonatomic, readonly) NSDictionary* additionalHeaders;
|
2014-04-08 04:09:03 +08:00
|
|
|
@property(nonatomic, readonly) BOOL usesChunkedTransferEncoding;
|
2014-04-07 01:44:53 +08:00
|
|
|
- (BOOL)performOpen:(NSError**)error;
|
|
|
|
- (NSData*)performReadData:(NSError**)error;
|
|
|
|
- (void)performClose;
|
|
|
|
@end
|