This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CHMURLProtocol.m
138 lines (113 loc) · 4.55 KB
/
CHMURLProtocol.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// Chmox a CHM file viewer for Mac OS X
// Copyright (c) 2004 Stéphane Boisson.
// Copyright (c) 2017 Alessandro Gatti.
//
// Chmox is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// Chmox is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Foobar; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#import "CHMURLProtocol.h"
#import "CHMContainer.h"
static NSMutableDictionary *kContainers = nil;
static NSMutableDictionary *kBaseURLs = nil;
@implementation CHMURLProtocol
- (instancetype)initWithRequest:(NSURLRequest *)request
cachedResponse:(NSCachedURLResponse *)cachedResponse
client:(id<NSURLProtocolClient>)client {
return (CHMURLProtocol *)[super initWithRequest:request
cachedResponse:cachedResponse
client:client];
}
+ (void)registerContainer:(nonnull CHMContainer *)container {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
kContainers = [NSMutableDictionary new];
kBaseURLs = [NSMutableDictionary new];
});
NSString *key = container.uniqueId;
NSURL *baseURL = [NSURL
URLWithString:[NSString stringWithFormat:@"chmox-internal://%@/", key]];
kContainers[key] = container;
kBaseURLs[key] = baseURL;
}
+ (nullable CHMContainer *)containerForUniqueId:(nonnull NSString *)uniqueId {
return kContainers[uniqueId];
}
+ (void)unregisterContainer:(nonnull CHMContainer *)container {
NSString *key = container.uniqueId;
[kContainers removeObjectForKey:key];
[kBaseURLs removeObjectForKey:key];
}
+ (nullable NSURL *)URLWithPath:(nonnull NSString *)path
inContainer:(nonnull CHMContainer *)container {
NSURL *baseURL = kBaseURLs[container.uniqueId];
NSURL *url = [NSURL URLWithString:path relativeToURL:baseURL];
if (baseURL && url == nil) {
// Something is wrong, perhaps path is not well-formed. Try percent-
// escaping characters. It's not clear what encoding should be used,
// but for now let's just use Latin1.
url = [NSURL
URLWithString:[path stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet
characterSetWithCharactersInString:@"%#"]]
relativeToURL:baseURL];
}
return url;
}
+ (BOOL)canHandleURL:(nonnull NSURL *)anURL {
return [anURL.scheme isEqualToString:@"chmox-internal"];
}
+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
return [self canHandleURL:request.URL];
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
return request;
}
- (void)startLoading {
NSURL *url = self.request.URL;
CHMContainer *container = [CHMURLProtocol containerForUniqueId:url.host];
if (!container) {
[self.client URLProtocol:self
didFailWithError:[NSError errorWithDomain:NSURLErrorDomain
code:0
userInfo:nil]];
return;
}
NSData *data = [container
dataWithContentsOfObject:url.parameterString
? [NSString
stringWithFormat:@"%@;%@", url.path,
url.parameterString]
: url.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:@"application/octet-stream"
expectedContentLength:data.length
textEncodingName:nil];
[self.client URLProtocol:self
didReceiveResponse:response
cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[self.client URLProtocol:self didLoadData:data];
[self.client URLProtocolDidFinishLoading:self];
}
- (void)stopLoading {
}
@end