Skip to content

Commit 1f9a0d3

Browse files
committed
Split class files
1 parent 81638ad commit 1f9a0d3

24 files changed

Lines changed: 1316 additions & 846 deletions

CGDWebServer/GCDWebServer.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,33 @@ void GCDLogMessage(long level, NSString* format, ...) {
8888

8989
#endif
9090

91+
NSString* GCDWebServerExtractHeaderParameter(NSString* header, NSString* attribute) {
92+
NSString* value = nil;
93+
if (header) {
94+
NSScanner* scanner = [[NSScanner alloc] initWithString:header];
95+
NSString* string = [NSString stringWithFormat:@"%@=", attribute];
96+
if ([scanner scanUpToString:string intoString:NULL]) {
97+
[scanner scanString:string intoString:NULL];
98+
if ([scanner scanString:@"\"" intoString:NULL]) {
99+
[scanner scanUpToString:@"\"" intoString:&value];
100+
} else {
101+
[scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:&value];
102+
}
103+
}
104+
ARC_RELEASE(scanner);
105+
}
106+
return value;
107+
}
108+
109+
// http://www.w3schools.com/tags/ref_charactersets.asp
110+
NSStringEncoding GCDWebServerStringEncodingFromCharset(NSString* charset) {
111+
NSStringEncoding encoding = kCFStringEncodingInvalidId;
112+
if (charset) {
113+
encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)charset));
114+
}
115+
return (encoding != kCFStringEncodingInvalidId ? encoding : NSUTF8StringEncoding);
116+
}
117+
91118
NSString* GCDWebServerGetMimeTypeForExtension(NSString* extension) {
92119
static NSDictionary* _overrides = nil;
93120
if (_overrides == nil) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright (c) 2012-2014, Pierre-Olivier Latour
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
* The name of Pierre-Olivier Latour may not be used to endorse
13+
or promote products derived from this software without specific
14+
prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL PIERRE-OLIVIER LATOUR BE LIABLE FOR ANY
20+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#import "GCDWebServerRequest.h"
29+
30+
@interface GCDWebServerDataRequest : GCDWebServerRequest
31+
@property(nonatomic, readonly) NSData* data; // Only valid after open / write / close sequence
32+
@end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright (c) 2012-2014, Pierre-Olivier Latour
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
* The name of Pierre-Olivier Latour may not be used to endorse
13+
or promote products derived from this software without specific
14+
prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL PIERRE-OLIVIER LATOUR BE LIABLE FOR ANY
20+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#import "GCDWebServerPrivate.h"
29+
30+
@interface GCDWebServerDataRequest () {
31+
@private
32+
NSMutableData* _data;
33+
}
34+
@end
35+
36+
@implementation GCDWebServerDataRequest
37+
38+
@synthesize data=_data;
39+
40+
- (void)dealloc {
41+
DCHECK(_data != nil);
42+
ARC_RELEASE(_data);
43+
44+
ARC_DEALLOC(super);
45+
}
46+
47+
- (BOOL)open {
48+
DCHECK(_data == nil);
49+
_data = [[NSMutableData alloc] initWithCapacity:self.contentLength];
50+
return _data ? YES : NO;
51+
}
52+
53+
- (NSInteger)write:(const void*)buffer maxLength:(NSUInteger)length {
54+
DCHECK(_data != nil);
55+
[_data appendBytes:buffer length:length];
56+
return length;
57+
}
58+
59+
- (BOOL)close {
60+
DCHECK(_data != nil);
61+
return YES;
62+
}
63+
64+
@end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright (c) 2012-2014, Pierre-Olivier Latour
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
* The name of Pierre-Olivier Latour may not be used to endorse
13+
or promote products derived from this software without specific
14+
prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL PIERRE-OLIVIER LATOUR BE LIABLE FOR ANY
20+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#import "GCDWebServerResponse.h"
29+
30+
@interface GCDWebServerDataResponse : GCDWebServerResponse
31+
+ (GCDWebServerDataResponse*)responseWithData:(NSData*)data contentType:(NSString*)type;
32+
- (id)initWithData:(NSData*)data contentType:(NSString*)type;
33+
@end
34+
35+
@interface GCDWebServerDataResponse (Extensions)
36+
+ (GCDWebServerDataResponse*)responseWithText:(NSString*)text;
37+
+ (GCDWebServerDataResponse*)responseWithHTML:(NSString*)html;
38+
+ (GCDWebServerDataResponse*)responseWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables;
39+
+ (GCDWebServerDataResponse*)responseWithJSONObject:(id)object;
40+
+ (GCDWebServerDataResponse*)responseWithJSONObject:(id)object contentType:(NSString*)type;
41+
- (id)initWithText:(NSString*)text; // Encodes using UTF-8
42+
- (id)initWithHTML:(NSString*)html; // Encodes using UTF-8
43+
- (id)initWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables; // Simple template system that replaces all occurences of "%variable%" with corresponding value (encodes using UTF-8)
44+
- (id)initWithJSONObject:(id)object;
45+
- (id)initWithJSONObject:(id)object contentType:(NSString*)type;
46+
@end
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
Copyright (c) 2012-2014, Pierre-Olivier Latour
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
* The name of Pierre-Olivier Latour may not be used to endorse
13+
or promote products derived from this software without specific
14+
prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL PIERRE-OLIVIER LATOUR BE LIABLE FOR ANY
20+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#import "GCDWebServerPrivate.h"
29+
30+
@interface GCDWebServerDataResponse () {
31+
@private
32+
NSData* _data;
33+
BOOL _done;
34+
}
35+
@end
36+
37+
@implementation GCDWebServerDataResponse
38+
39+
+ (GCDWebServerDataResponse*)responseWithData:(NSData*)data contentType:(NSString*)type {
40+
return ARC_AUTORELEASE([[[self class] alloc] initWithData:data contentType:type]);
41+
}
42+
43+
- (id)initWithData:(NSData*)data contentType:(NSString*)type {
44+
if (data == nil) {
45+
DNOT_REACHED();
46+
ARC_RELEASE(self);
47+
return nil;
48+
}
49+
50+
if ((self = [super init])) {
51+
_data = ARC_RETAIN(data);
52+
53+
self.contentType = type;
54+
self.contentLength = data.length;
55+
}
56+
return self;
57+
}
58+
59+
- (void)dealloc {
60+
ARC_RELEASE(_data);
61+
62+
ARC_DEALLOC(super);
63+
}
64+
65+
- (NSData*)readData:(NSError**)error {
66+
NSData* data;
67+
if (_done) {
68+
data = [NSData data];
69+
} else {
70+
data = _data;
71+
_done = YES;
72+
}
73+
return data;
74+
}
75+
76+
@end
77+
78+
@implementation GCDWebServerDataResponse (Extensions)
79+
80+
+ (GCDWebServerDataResponse*)responseWithText:(NSString*)text {
81+
return ARC_AUTORELEASE([[self alloc] initWithText:text]);
82+
}
83+
84+
+ (GCDWebServerDataResponse*)responseWithHTML:(NSString*)html {
85+
return ARC_AUTORELEASE([[self alloc] initWithHTML:html]);
86+
}
87+
88+
+ (GCDWebServerDataResponse*)responseWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables {
89+
return ARC_AUTORELEASE([[self alloc] initWithHTMLTemplate:path variables:variables]);
90+
}
91+
92+
+ (GCDWebServerDataResponse*)responseWithJSONObject:(id)object {
93+
return ARC_AUTORELEASE([[self alloc] initWithJSONObject:object]);
94+
}
95+
96+
+ (GCDWebServerDataResponse*)responseWithJSONObject:(id)object contentType:(NSString*)type {
97+
return ARC_AUTORELEASE([[self alloc] initWithJSONObject:object contentType:type]);
98+
}
99+
100+
- (id)initWithText:(NSString*)text {
101+
NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding];
102+
if (data == nil) {
103+
DNOT_REACHED();
104+
ARC_RELEASE(self);
105+
return nil;
106+
}
107+
return [self initWithData:data contentType:@"text/plain; charset=utf-8"];
108+
}
109+
110+
- (id)initWithHTML:(NSString*)html {
111+
NSData* data = [html dataUsingEncoding:NSUTF8StringEncoding];
112+
if (data == nil) {
113+
DNOT_REACHED();
114+
ARC_RELEASE(self);
115+
return nil;
116+
}
117+
return [self initWithData:data contentType:@"text/html; charset=utf-8"];
118+
}
119+
120+
- (id)initWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables {
121+
NSMutableString* html = [[NSMutableString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
122+
[variables enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSString* value, BOOL* stop) {
123+
[html replaceOccurrencesOfString:[NSString stringWithFormat:@"%%%@%%", key] withString:value options:0 range:NSMakeRange(0, html.length)];
124+
}];
125+
id response = [self initWithHTML:html];
126+
ARC_RELEASE(html);
127+
return response;
128+
}
129+
130+
- (id)initWithJSONObject:(id)object {
131+
return [self initWithJSONObject:object contentType:@"application/json"];
132+
}
133+
134+
- (id)initWithJSONObject:(id)object contentType:(NSString*)type {
135+
NSData* data = [NSJSONSerialization dataWithJSONObject:object options:0 error:NULL];
136+
if (data == nil) {
137+
ARC_RELEASE(self);
138+
return nil;
139+
}
140+
return [self initWithData:data contentType:type];
141+
}
142+
143+
@end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright (c) 2012-2014, Pierre-Olivier Latour
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
* The name of Pierre-Olivier Latour may not be used to endorse
13+
or promote products derived from this software without specific
14+
prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL PIERRE-OLIVIER LATOUR BE LIABLE FOR ANY
20+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#import "GCDWebServerRequest.h"
29+
30+
@interface GCDWebServerFileRequest : GCDWebServerRequest
31+
@property(nonatomic, readonly) NSString* filePath; // Only valid after open / write / close sequence
32+
@end

0 commit comments

Comments
 (0)