forked from veryweblog/ichm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ITSSProtocol.m
109 lines (86 loc) · 2.48 KB
/
ITSSProtocol.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
//
// ITSSProtocol.m
// ichm
//
// Created by Robin Lu on 7/16/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "ITSSProtocol.h"
#import "CHMDocument.h"
@implementation ITSSProtocol
-(id)initWithRequest:(NSURLRequest *)request
cachedResponse:(NSCachedURLResponse *)cachedResponse
client:(id <NSURLProtocolClient>)client
{
return [super initWithRequest:request cachedResponse:cachedResponse client:client];
}
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
BOOL canHandle = [[[request URL] scheme] isEqualToString:@"itss"];
return canHandle;
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
return request;
}
-(void)stopLoading
{
}
-(void)startLoading
{
NSURL *url = [[self request] URL];
CHMDocument *doc = [[self request] chmDoc];
NSString *encoding = [[self request] encodingName];
if( !doc ) {
[[self client] URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:nil]];
return;
}
NSData *data;
NSString *path;
if( [url parameterString] ) {
path = [NSString stringWithFormat:@"%@;%@", [url path], [url parameterString]];
}
else {
path = [url path];
}
if (![doc exist:path])
{
path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
data = [doc content:path];
if( !data ) {
[[self client] URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:nil]];
return;
}
NSURLResponse *response = [[NSURLResponse alloc] initWithURL: [[self request] URL]
MIMEType:nil
expectedContentLength:[data length]
textEncodingName:encoding];
[[self client] URLProtocol:self
didReceiveResponse:response
cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[[self client] URLProtocol:self didLoadData:data];
[[self client] URLProtocolDidFinishLoading:self];
[response release];
}
@end
@implementation NSURLRequest (SpecialProtocol)
- (CHMDocument *)chmDoc
{
return [NSURLProtocol propertyForKey:@"chmdoc" inRequest:self];
}
- (NSString *)encodingName
{
return [NSURLProtocol propertyForKey:@"encoding" inRequest:self];
}
@end
@implementation NSMutableURLRequest (SpecialProtocol)
- (void)setChmDoc:(CHMDocument *)doc
{
[NSURLProtocol setProperty:doc forKey:@"chmdoc" inRequest:self];
}
- (void)setEncodingName:(NSString *)name
{
[NSURLProtocol setProperty:name forKey:@"encoding" inRequest:self];
}
@end