5 // Created by Pieter de Bie on 01-11-08.
6 // Copyright 2008 Pieter de Bie. All rights reserved.
9 #import "PBGitXProtocol.h"
12 @implementation PBGitXProtocol
14 + (BOOL) canInitWithRequest:(NSURLRequest *)request
16 return [[[request URL] scheme] isEqualToString:@"GitX"];
19 + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
26 NSURL *url = [[self request] URL];
27 PBGitRepository *repo = [[self request] repository];
30 [[self client] URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:nil]];
34 NSString *specifier = [NSString stringWithFormat:@"%@:%@", [url host], [[url path] substringFromIndex:1]];
35 handle = [repo handleInWorkDirForArguments:[NSArray arrayWithObjects:@"cat-file", @"blob", specifier, nil]];
36 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishFileLoad:) name:NSFileHandleReadToEndOfFileCompletionNotification object:handle];
37 [handle readToEndOfFileInBackgroundAndNotify];
39 NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[[self request] URL]
41 expectedContentLength:-1
42 textEncodingName:nil];
44 [[self client] URLProtocol:self
45 didReceiveResponse:response
46 cacheStoragePolicy:NSURLCacheStorageNotAllowed];
49 - (void) didFinishFileLoad:(NSNotification *)notification
51 NSData *data = [[notification userInfo] valueForKey:NSFileHandleNotificationDataItem];
52 [[self client] URLProtocol:self didLoadData:data];
53 [[self client] URLProtocolDidFinishLoading:self];
58 [[NSNotificationCenter defaultCenter] removeObserver:self];
63 @implementation NSURLRequest (PBGitXProtocol)
65 - (PBGitRepository *) repository
67 return [NSURLProtocol propertyForKey:@"PBGitRepository" inRequest:self];
71 @implementation NSMutableURLRequest (PBGitXProtocol)
74 - (void) setRepository:(PBGitRepository *)repository
76 [NSURLProtocol setProperty:repository forKey:@"PBGitRepository" inRequest:self];