]> git.saurik.com Git - cydia.git/blame - CyteKit/URLProtocol.mm
Swap awkward va_list hack for awkward switch hack.
[cydia.git] / CyteKit / URLProtocol.mm
CommitLineData
a02d4e98
JF
1/* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
3*/
4
5/* GNU General Public License, Version 3 {{{ */
6/*
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
19**/
20/* }}} */
21
22#include "CyteKit/UCPlatform.h"
23
24#include "CyteKit/URLProtocol.h"
25
26#include "iPhonePrivate.h"
27#include <Menes/ObjectHandle.h>
28
29@implementation CyteURLProtocol {
30}
31
32+ (NSString *) scheme {
33 return NULL;
34}
35
36+ (BOOL) canInitWithRequest:(NSURLRequest *)request {
37 NSURL *url([request URL]);
38 if (url == nil)
39 return NO;
40
41 auto local([self scheme]);
42 _assert(local != NULL);
43
44 NSString *scheme([[url scheme] lowercaseString]);
45 if (scheme != nil && [scheme isEqualToString:local])
46 return YES;
47 if ([[url absoluteString] hasPrefix:[NSString stringWithFormat:@"about:%@-", local]])
48 return YES;
49
50 return NO;
51}
52
53+ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
54 return request;
55}
56
57- (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
58 id<NSURLProtocolClient> client([self client]);
59 if (icon == nil)
60 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
61 else {
62 NSData *data(UIImagePNGRepresentation(icon));
63
64 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
65 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
66 [client URLProtocol:self didLoadData:data];
67 [client URLProtocolDidFinishLoading:self];
68 }
69}
70
71- (bool) loadForPath:(NSString *)path ofRequest:(NSURLRequest *)request {
72 return false;
73}
74
75- (void) startLoading {
76 id<NSURLProtocolClient> client([self client]);
77 NSURLRequest *request([self request]);
78
79 NSURL *url([request URL]);
80 NSString *href([url absoluteString]);
81 NSString *scheme([[url scheme] lowercaseString]);
82
83 NSString *path;
84
85 if ([scheme isEqualToString:@"cydia"])
86 path = [href substringFromIndex:8];
87 else if ([scheme isEqualToString:@"about"])
88 path = [href substringFromIndex:12];
89 else _assert(false);
90
91 if (![self loadForPath:path ofRequest:request])
92 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
93}
94
95- (void) stopLoading {
96}
97
98@end