F4BDEB71187252F30069D0CE /* KCDTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F4BDEB70187252F30069D0CE /* KCDTests.m */; };
F4BDEB7B187253130069D0CE /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4BDEB7A187253130069D0CE /* WebKit.framework */; };
F4C041BA1875CAF5009273EB /* JavaScriptCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4C041B91875CAF5009273EB /* JavaScriptCore.framework */; };
- F4C118F218A521FF005D5B25 /* HMHTTPProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C118F118A521FF005D5B25 /* HMHTTPProtocol.m */; };
F4C118FA18A67B1C005D5B25 /* CacheStoragePolicy.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C118F518A67B1C005D5B25 /* CacheStoragePolicy.m */; };
F4C118FB18A67B1C005D5B25 /* CanonicalRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C118F718A67B1C005D5B25 /* CanonicalRequest.m */; };
F4C118FC18A67B1C005D5B25 /* CustomHTTPProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C118F918A67B1C005D5B25 /* CustomHTTPProtocol.m */; };
+ F4C1190218A6FFC2005D5B25 /* HMQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C1190118A6FFC2005D5B25 /* HMQueue.m */; };
+ F4C1190518A71AEA005D5B25 /* HMJSONReciever.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C1190418A71AEA005D5B25 /* HMJSONReciever.m */; };
+ F4C1190818A746EB005D5B25 /* HMJSONTracker.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C1190718A746EB005D5B25 /* HMJSONTracker.m */; };
+ F4C1190B18A74DF0005D5B25 /* HMJSONCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C1190A18A74DF0005D5B25 /* HMJSONCommand.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
F4BDEB70187252F30069D0CE /* KCDTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KCDTests.m; sourceTree = "<group>"; };
F4BDEB7A187253130069D0CE /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
F4C041B91875CAF5009273EB /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; };
- F4C118F018A521FF005D5B25 /* HMHTTPProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMHTTPProtocol.h; sourceTree = "<group>"; };
- F4C118F118A521FF005D5B25 /* HMHTTPProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMHTTPProtocol.m; sourceTree = "<group>"; };
F4C118F418A67B1C005D5B25 /* CacheStoragePolicy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CacheStoragePolicy.h; sourceTree = "<group>"; };
F4C118F518A67B1C005D5B25 /* CacheStoragePolicy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CacheStoragePolicy.m; sourceTree = "<group>"; };
F4C118F618A67B1C005D5B25 /* CanonicalRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CanonicalRequest.h; sourceTree = "<group>"; };
F4C118F718A67B1C005D5B25 /* CanonicalRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CanonicalRequest.m; sourceTree = "<group>"; };
F4C118F818A67B1C005D5B25 /* CustomHTTPProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomHTTPProtocol.h; sourceTree = "<group>"; };
F4C118F918A67B1C005D5B25 /* CustomHTTPProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CustomHTTPProtocol.m; sourceTree = "<group>"; };
+ F4C1190018A6FFC2005D5B25 /* HMQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMQueue.h; sourceTree = "<group>"; };
+ F4C1190118A6FFC2005D5B25 /* HMQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMQueue.m; sourceTree = "<group>"; };
+ F4C1190318A71AEA005D5B25 /* HMJSONReciever.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMJSONReciever.h; sourceTree = "<group>"; };
+ F4C1190418A71AEA005D5B25 /* HMJSONReciever.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMJSONReciever.m; sourceTree = "<group>"; };
+ F4C1190618A746EB005D5B25 /* HMJSONTracker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMJSONTracker.h; sourceTree = "<group>"; };
+ F4C1190718A746EB005D5B25 /* HMJSONTracker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMJSONTracker.m; sourceTree = "<group>"; };
+ F4C1190918A74DF0005D5B25 /* HMJSONCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMJSONCommand.h; sourceTree = "<group>"; };
+ F4C1190A18A74DF0005D5B25 /* HMJSONCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMJSONCommand.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
isa = PBXGroup;
children = (
F4C118F318A67B05005D5B25 /* New Group */,
- F4C118F018A521FF005D5B25 /* HMHTTPProtocol.h */,
- F4C118F118A521FF005D5B25 /* HMHTTPProtocol.m */,
F4BDEB58187252F30069D0CE /* HMAppDelegate.h */,
F4BDEB59187252F30069D0CE /* HMAppDelegate.m */,
+ F4C1190318A71AEA005D5B25 /* HMJSONReciever.h */,
+ F4C1190418A71AEA005D5B25 /* HMJSONReciever.m */,
+ F4C1190618A746EB005D5B25 /* HMJSONTracker.h */,
+ F4C1190718A746EB005D5B25 /* HMJSONTracker.m */,
+ F4C1190918A74DF0005D5B25 /* HMJSONCommand.h */,
+ F4C1190A18A74DF0005D5B25 /* HMJSONCommand.m */,
+ F4C1190018A6FFC2005D5B25 /* HMQueue.h */,
+ F4C1190118A6FFC2005D5B25 /* HMQueue.m */,
F4BDEB5B187252F30069D0CE /* MainMenu.xib */,
F4BDEB5E187252F30069D0CE /* Images.xcassets */,
F4BDEB4D187252F30069D0CE /* Supporting Files */,
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
+ F4C1190218A6FFC2005D5B25 /* HMQueue.m in Sources */,
F4C118FA18A67B1C005D5B25 /* CacheStoragePolicy.m in Sources */,
+ F4C1190518A71AEA005D5B25 /* HMJSONReciever.m in Sources */,
+ F4C1190B18A74DF0005D5B25 /* HMJSONCommand.m in Sources */,
F4BDEB53187252F30069D0CE /* main.m in Sources */,
F4C118FC18A67B1C005D5B25 /* CustomHTTPProtocol.m in Sources */,
+ F4C1190818A746EB005D5B25 /* HMJSONTracker.m in Sources */,
F4C118FB18A67B1C005D5B25 /* CanonicalRequest.m in Sources */,
- F4C118F218A521FF005D5B25 /* HMHTTPProtocol.m in Sources */,
F4BDEB5A187252F30069D0CE /* HMAppDelegate.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
// protocol will not be nil
// challenge will not be nil
+- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didRecieveResponse:(NSURLResponse *)response;
+- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didRecieveData:(NSData *)data;
+- (void)customHTTPProtocolDidFinishLoading:(CustomHTTPProtocol *)protocol;
+- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didFailWithError:(NSError *)error;
+
+
- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol logWithFormat:(NSString *)format arguments:(va_list)argList;
// called on an arbitrary thread
// protocol may be nil, implying a log message from the class itself
cacheStoragePolicy = NSURLCacheStorageAllowedInMemoryOnly;
}
}
+
+ id<CustomHTTPProtocolDelegate> delegate = [[self class] delegate];
+ if([delegate respondsToSelector:@selector(customHTTPProtocol:didRecieveResponse:)]) {
+ [delegate customHTTPProtocol:self didRecieveResponse:response];
+ }
[[self class] customHTTPProtocol:self logWithFormat:@"received response %@ with cache storage policy %zu", response, (size_t) cacheStoragePolicy];
if (NO) {
[[self class] customHTTPProtocol:self logWithFormat:@"received %zu bytes of data", (size_t) [data length]];
}
+
+ id<CustomHTTPProtocolDelegate> delegate = [[self class] delegate];
+ if([delegate respondsToSelector:@selector(customHTTPProtocol:didRecieveData:)]) {
+ [delegate customHTTPProtocol:self didRecieveData:data];
+ }
+
+
[[self client] URLProtocol:self didLoadData:data];
}
assert([NSThread currentThread] == self.clientThread);
[[self class] customHTTPProtocol:self logWithFormat:@"success"];
+
+ id<CustomHTTPProtocolDelegate> delegate = [[self class] delegate];
+ if([delegate respondsToSelector:@selector(customHTTPProtocolDidFinishLoading:)]) {
+ [delegate customHTTPProtocolDidFinishLoading:self];
+ }
// Just pass the call on to our client.
assert([NSThread currentThread] == self.clientThread);
[[self class] customHTTPProtocol:self logWithFormat:@"error %@ / %d", [error domain], (int) [error code]];
+
+ id<CustomHTTPProtocolDelegate> delegate = [[self class] delegate];
+ if([delegate respondsToSelector:@selector(customHTTPProtocol:didFailWithError:)]) {
+ [delegate customHTTPProtocol:self didFailWithError:error];
+ }
// Just pass the call on to our client.
@interface HMAppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
-
@property (assign) IBOutlet WebView *webView;
+
+- (void)logLineReturn:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
+- (void)log:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
+
+
@end
#import "HMAppDelegate.h"
#import "CustomHTTPProtocol.h"
-
+#import "HMJSONTracker.h"
#import <JavaScriptCore/JavaScriptCore.h>
-@interface HMAppDelegate (hoge) <CustomHTTPProtocolDelegate>
-
+@interface HMAppDelegate ()
+@property (retain) HMJSONTracker *tracker;
@end
@implementation HMAppDelegate
}
}
-- (void)showWebFrame:(WebFrame *)webFrame floor:(NSUInteger)floor
-{
- NSUInteger f = floor;
- while(f--) [self log:@" "];
- [self logLineReturn:@"%@", [webFrame name]];
- [self logLineReturn:@"%@", [[[webFrame dataSource] representation] documentSource]];
-
- for(WebFrame *sub in [webFrame childFrames]) {
- [self showWebFrame:sub floor:floor + 1];
- }
-}
-- (void)showDOM:(DOMNode *)node floor:(NSUInteger)floor
-{
- NSUInteger f = floor;
- while(f--) [self log:@" "];
- [self logLineReturn:@"%@ -- %d", [node nodeName], [node nodeType]];
-
- DOMNodeList *lists = [node childNodes];
- unsigned length = [lists length];
- for(unsigned i = 0; i < length; i++) {
- [self showDOM:[lists item:i] floor:floor + 1];
- }
-}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// [self.webView setMainFrameURL:@"http://www.google.com/"];
- [CustomHTTPProtocol setDelegate:self];
-}
-
-- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol logWithFormat:(NSString *)format arguments:(va_list)argList
-{
- [self logLineReturn:[[NSString alloc] initWithFormat:format arguments:argList]];
+ self.tracker = [HMJSONTracker new];
}
-//
-//- (id)webView:(WebView *)sender identifierForInitialRequest:(NSURLRequest *)request fromDataSource:(WebDataSource *)dataSource
-//{
-// NSURL *url = [request URL];
-// NSArray *pathComponents = [url pathComponents];
-// if([pathComponents containsObject:@"kcsapi"]) {
-//// [self logLineReturn:@"\nSending Request\n===>\t%@", url];
-////
-//// NSData *body = [request HTTPBody];
-//// if(body && [body length] != 0) {
-//// NSString *bodyString = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
-//// bodyString = [bodyString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
-//// [self logLineReturn:@"===>\t%@", bodyString];
-//// }
-//
-// return [pathComponents count] > 3 ?
-// [NSString stringWithFormat:@"hoge-api-%@-%@", [pathComponents objectAtIndex:2], [pathComponents objectAtIndex:3]] :
-// [NSString stringWithFormat:@"hoge-api-%@", [pathComponents objectAtIndex:2]];
-// }
-//
-// return [NSObject new];
-//
-//}
-//
-//- (NSURLRequest *)webView:(WebView *)sender
-// resource:(id)identifier
-// willSendRequest:(NSURLRequest *)request
-// redirectResponse:(NSURLResponse *)redirectResponse
-// fromDataSource:(WebDataSource *)dataSource
-//{
-// NSURL *url = [request URL];
-// NSString *filename = [url lastPathComponent];
-//
-// if([@"181.swf" isEqualToString:filename]
-// || [@"182.swf" isEqualToString:filename]
-// || [@"183.swf" isEqualToString:filename] ) {
-// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-// NSURLResponse *response = nil;
-// NSError *error = nil;
-// NSData *data =
-// [NSURLConnection sendSynchronousRequest:request
-// returningResponse:&response
-// error:&error];
-// if(error) {
-// NSLog(@"Could not download main.swf. Reason: %@", error);
-// return;
-// }
-// if(!data || [data length] == 0) {
-// NSLog(@"Counld not download main.swf. data is zero.");
-// }
-// [data writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:filename]
-// atomically:YES];
-// NSLog(@"finish download main.swf.");
-//
-// });
-// }
-//
-// return request;
-//}
-//
-//- (void)dumpPropertiesObject:(JSObjectRef)object context:(JSContextRef)context floor:(NSUInteger)floor limit:(NSUInteger)limit
-//{
-// if(floor > limit) return;
-//
-// JSPropertyNameArrayRef names = JSObjectCopyPropertyNames(context, object);
-// size_t count = JSPropertyNameArrayGetCount(names);
-// size_t i = 0;
-// for(i = 0; i < count; i++) {
-// @autoreleasepool {
-// JSStringRef name = JSPropertyNameArrayGetNameAtIndex(names, i);
-// JSValueRef value = JSObjectGetProperty(context, object, name, NULL);
-// NSString *nameString = (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, name);
-// if([nameString isEqualToString:@"inherits"]) continue;
-// if([nameString isEqualToString:@"document"]) continue;
-//
-// JSType type = JSValueGetType(context, value);
-// NSUInteger f = floor;
-// while(f--) [self log:@" "];
-// [self logLineReturn:@"%@ (%d)", nameString, type];
-// if(type == kJSTypeObject) {
-// [self dumpPropertiesObject:value context:context floor:floor + 1 limit:limit];
-// }
-// }
-// }
-//}
-
-#if 0
-- (void)webView:(WebView *)sender resource:(id)identifier didFinishLoadingFromDataSource:(WebDataSource *)dataSource
-{
- if([identifier isKindOfClass:[NSString class]]) {
-#if 1
-// static BOOL isFirst = YES;
-// if(!isFirst) return;
-// isFirst = NO;
-
- JSValueRef error = NULL;
- JSContextRef context = [[dataSource webFrame] globalContext];
- JSObjectRef object = JSContextGetGlobalObject(context);
-
-// [self logLineReturn:@"global object properties"];
-// [self dumpPropertiesObject:object context:context floor:0 limit:5];
-
- JSValueRef gadgets = JSObjectGetProperty(context, object, JSStringCreateWithUTF8CString("gadgets"), &error);
- if(error) {
- NSLog(@"ERROR get property gadgets -> %@", JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error));
- return;
- }
-
-// [self logLineReturn:@"gadgets properties"];
-// [self dumpPropertiesObject:gadgets context:context floor:0 limit:1];
-
- JSValueRef jsonData = JSObjectGetProperty(context, gadgets, JSStringCreateWithUTF8CString("json"), &error);
- if(error) {
- NSLog(@"ERROR get property json -> %@", (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error));
- return;
- }
- [self logLineReturn:@"JSON Data Value type -> %d", JSValueGetType(context, jsonData)];
- [self logLineReturn:@"JSON DATA LENGTH -> %d", JSStringGetLength((JSStringRef)jsonData)];
-
- JSStringRef json = JSValueCreateJSONString(context, jsonData, 8, &error);
- if(error) {
- NSLog(@"ERROR create json -> %@", (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error));
- return;
- }
- NSString *jsonString = (__bridge NSString *)(JSStringCopyCFString(kCFAllocatorDefault, json));
- [self logLineReturn:@"JSON -> \n%@", jsonString];
-
- // global JSON
- JSValueRef globalJSONData = JSObjectGetProperty(context, object, JSStringCreateWithUTF8CString("JSON"), &error);
- if(error) {
- NSLog(@"ERROR get property JSON -> %@", (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error));
- return;
- }
- [self logLineReturn:@"JSON Data Value type -> %d", JSValueGetType(context, globalJSONData)];
- [self logLineReturn:@"WINDOW JSON DATA LENGTH -> %d", JSStringGetLength((JSStringRef)globalJSONData)];
- JSStringRef globalJSON = JSValueCreateJSONString(context, globalJSONData, 8, &error);
- if(error) {
- NSLog(@"ERROR create json -> %@", (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error));
- return;
- }
- NSString *globalJSONString = (__bridge NSString *)(JSStringCopyCFString(kCFAllocatorDefault, globalJSON));
- [self logLineReturn:@"WINDOW JSON -> \n%@", globalJSONString];
-
- // window JSON
- JSObjectRef window = JSObjectGetProperty(context, object, JSStringCreateWithUTF8CString("window"), &error);
-
- JSValueRef windowJSONData = JSObjectGetProperty(context, window, JSStringCreateWithUTF8CString("JSON"), &error);
- if(error) {
- NSLog(@"ERROR get property JSON -> %@", (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error));
- return;
- }
- [self logLineReturn:@"JSON Data Value type -> %d", JSValueGetType(context, windowJSONData)];
- [self logLineReturn:@"WINDOW JSON DATA LENGTH -> %d", JSStringGetLength((JSStringRef)windowJSONData)];
- JSStringRef windowJSON = JSValueCreateJSONString(context, windowJSONData, 8, &error);
- if(error) {
- NSLog(@"ERROR create json -> %@", (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error));
- return;
- }
- NSString *windowJSONString = (__bridge NSString *)(JSStringCopyCFString(kCFAllocatorDefault, windowJSON));
- [self logLineReturn:@"WINDOW JSON -> \n%@", windowJSONString];
-#endif
-
-// [self showWebFrame:[dataSource webFrame] floor:0];
-
-// [self showDOM:[[dataSource webFrame] frameElement] floor:0];
- }
-}
-#endif
-
-//- (void)webView:(WebView *)sender resource:(id)identifier didFinishLoadingFromDataSource:(WebDataSource *)dataSource
-//{
-// NSData *data = [dataSource data];
-// NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
-//
-// [self logLineReturn:@"%@\n%@\n\n\n", dataSource.initialRequest.URL, string];
-//}
-
@end
+++ /dev/null
-//
-// HMHTTPProtocol.h
-// KCD
-//
-// Created by Hori,Masaki on 2014/02/07.
-// Copyright (c) 2014年 Hori,Masaki. All rights reserved.
-//
-
-#import <Foundation/Foundation.h>
-
-@interface HMHTTPProtocol : NSURLProtocol <NSURLProtocolClient>
-
-@end
+++ /dev/null
-//
-// HMHTTPProtocol.m
-// KCD
-//
-// Created by Hori,Masaki on 2014/02/07.
-// Copyright (c) 2014年 Hori,Masaki. All rights reserved.
-//
-
-#import "HMHTTPProtocol.h"
-
-@class NSCFURLProtocol;
-
-static Class cfURLProtocolClass = Nil;
-static id semaphore = nil;
-static BOOL inCreation = NO;
-
-@interface HMHTTPProtocol ()
-@property (retain) id origin;
-@end
-
-@implementation HMHTTPProtocol
-
-//+ (void)load
-//{
-// static dispatch_once_t onceToken;
-// dispatch_once(&onceToken, ^{
-// [NSURLProtocol registerClass:[self class]];
-// cfURLProtocolClass = NSClassFromString(@"NSCFURLProtocol");
-// semaphore = [NSObject new];
-// });
-//}
-
-- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id <NSURLProtocolClient>)client
-{
- self = [super initWithRequest:request cachedResponse:cachedResponse client:client];
- if(self) {
-// @synchronized(semaphore) {
- inCreation = YES;
- _origin = [[cfURLProtocolClass alloc] initWithRequest:request cachedResponse:cachedResponse client:self];
- inCreation = NO;
- }
-// }
-
- return self;
-}
-
-+ (BOOL)canInitWithRequest:(NSURLRequest *)request
-{
- return !inCreation;
-}
-
-+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
-{
- NSURLRequest *res = nil;
-// @synchronized(semaphore) {
- inCreation = YES;
- res = [cfURLProtocolClass canonicalRequestForRequest:request];
- inCreation = NO;
-// }
- return res;
-}
-+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b
-{
- BOOL res = NO;
-// @synchronized(semaphore) {
- inCreation = YES;
- res = [cfURLProtocolClass requestIsCacheEquivalent:a toRequest:b];
- inCreation = NO;
-// }
- return res;
-}
-- (void)startLoading
-{
- [self.origin startLoading];
-}
-- (void)stopLoading
-{
- [self.origin stopLoading];
-}
-
-//
-- (void)URLProtocol:(NSURLProtocol *)protocol wasRedirectedToRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
-{
- [self.client URLProtocol:protocol wasRedirectedToRequest:request redirectResponse:redirectResponse];
-}
-- (void)URLProtocol:(NSURLProtocol *)protocol cachedResponseIsValid:(NSCachedURLResponse *)cachedResponse
-{
- [self.client URLProtocol:protocol cachedResponseIsValid:cachedResponse];
-}
-- (void)URLProtocol:(NSURLProtocol *)protocol didReceiveResponse:(NSURLResponse *)response cacheStoragePolicy:(NSURLCacheStoragePolicy)policy
-{
- //
- //
- NSLog(@"Enter %s", __PRETTY_FUNCTION__);
-
-
- [self.client URLProtocol:protocol didReceiveResponse:response cacheStoragePolicy:policy];
-}
-- (void)URLProtocol:(NSURLProtocol *)protocol didLoadData:(NSData *)data
-{
- //
- //
- NSLog(@"Enter %s", __PRETTY_FUNCTION__);
-
-
- [self.client URLProtocol:protocol didLoadData:data];
-}
-- (void)URLProtocolDidFinishLoading:(NSURLProtocol *)protocol
-{
- [self.client URLProtocolDidFinishLoading:protocol];
-}
-- (void)URLProtocol:(NSURLProtocol *)protocol didFailWithError:(NSError *)error
-{
- [self.client URLProtocol:protocol didFailWithError:error];
-}
-- (void)URLProtocol:(NSURLProtocol *)protocol didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
-{
- [self.client URLProtocol:protocol didReceiveAuthenticationChallenge:challenge];
-}
-- (void)URLProtocol:(NSURLProtocol *)protocol didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
-{
- [self.client URLProtocol:protocol didCancelAuthenticationChallenge:challenge];
-}
-
-
-@end
--- /dev/null
+//
+// HMJSONCommand.h
+// KCD
+//
+// Created by Hori,Masaki on 2014/02/09.
+// Copyright (c) 2014年 Hori,Masaki. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface HMJSONCommand : NSObject
+
++ (HMJSONCommand *)commandForAPI:(NSString *)api;
+
+@property (retain) NSString *argumentsString;
+- (void)doCommand:(id)json;
+
+@end
--- /dev/null
+//
+// HMJSONCommand.m
+// KCD
+//
+// Created by Hori,Masaki on 2014/02/09.
+// Copyright (c) 2014年 Hori,Masaki. All rights reserved.
+//
+
+#import "HMJSONCommand.h"
+
+#import "HMAppDelegate.h"
+
+
+
+@implementation HMJSONCommand
+
++ (HMJSONCommand *)commandForAPI:(NSString *)api
+{
+
+ [[NSApp delegate] logLineReturn:@"%@", api];
+
+ return nil;
+}
+
+- (void)doCommand:(id)json
+{
+ NSLog(@"Enter %s", __PRETTY_FUNCTION__);
+ assert(NO);
+}
+
+@end
--- /dev/null
+//
+// HMJSONReciever.h
+// KCD
+//
+// Created by Hori,Masaki on 2014/02/09.
+// Copyright (c) 2014年 Hori,Masaki. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+#import "CustomHTTPProtocol.h"
+#import "HMQueue.h"
+
+@interface HMJSONReciever : NSObject <CustomHTTPProtocolDelegate>
+
+@property (retain) HMQueue *queueu;
+
+@end
--- /dev/null
+//
+// HMJSONReciever.m
+// KCD
+//
+// Created by Hori,Masaki on 2014/02/09.
+// Copyright (c) 2014年 Hori,Masaki. All rights reserved.
+//
+
+#import "HMJSONReciever.h"
+
+#import "CustomHTTPProtocol.h"
+#import "HMAppDelegate.h"
+
+
+@interface HMJSONReciever ()
+@property (retain) NSMutableDictionary *recievers;
+@end
+
+
+@implementation HMJSONReciever
+
+- (id)init
+{
+ self = [super init];
+ if(self) {
+ _recievers = [NSMutableDictionary new];
+
+ [CustomHTTPProtocol setDelegate:self];
+ }
+ return self;
+}
+
+- (void)setProtocol:(NSURLProtocol *)protocol
+{
+ [self.recievers setObject:[NSMutableData data]
+ forKey:[NSValue valueWithPointer:(__bridge const void *)(protocol)]];
+}
+- (NSMutableData *)dataForProtocol:(NSURLProtocol *)protocol
+{
+ return [self.recievers objectForKey:[NSValue valueWithPointer:(__bridge const void *)(protocol)]];
+}
+- (void)removeDataForProtocol:(NSURLProtocol *)protocol
+{
+ [self.recievers removeObjectForKey:[NSValue valueWithPointer:(__bridge const void *)(protocol)]];
+}
+
+- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didRecieveResponse:(NSHTTPURLResponse *)response
+{
+ NSURL *url = protocol.request.URL;
+ NSString *path = url.path;
+ while(![path isEqualToString:@"/"]) {
+ if([path hasSuffix:@"kcsapi"]) {
+ [self setProtocol:protocol];
+ }
+ path = [path stringByDeletingLastPathComponent];
+ }
+}
+
+- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didRecieveData:(NSData *)data
+{
+ NSMutableData *loadedData = [self dataForProtocol:protocol];
+ if(!loadedData) return;
+
+ [loadedData appendData:data];
+}
+
+- (void)customHTTPProtocolDidFinishLoading:(CustomHTTPProtocol *)protocol
+{
+ NSData *data = [self dataForProtocol:protocol];
+ if(!data) return;
+
+#define JSON_LOG_STRING 0
+#if JSON_LOG_STRING
+ NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
+ [self logLineReturn:@"body -> \n%@", string];
+#else
+ NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
+ NSArray *elements = [string componentsSeparatedByString:@"="];
+ if([elements count] != 2) {
+ [[NSApp delegate] logLineReturn:@"\e[1mwe could not compose data. api -> %@. Number of elements:\e[22m %ld", protocol.request.URL.path, [elements count]];
+ [[NSApp delegate] logLineReturn:@"Original strings -> %@", string];
+ [self removeDataForProtocol:protocol];
+ return;
+ }
+
+
+ NSData *JSONData = [elements[1] dataUsingEncoding:NSUTF8StringEncoding];
+ NSError *error = nil;
+ id json = [NSJSONSerialization JSONObjectWithData:JSONData
+ options:NSJSONReadingAllowFragments
+ error:&error];
+ if(error) {
+ [[NSApp delegate] logLineReturn:@"\e[1m\e[31mFail decode JSON data\e[39m\e[22m %@", error];
+ } else {
+ NSData *requestBodyData = [protocol.request HTTPBody];
+ NSString *requestBodyString = [[NSString alloc] initWithData:requestBodyData encoding:NSUTF8StringEncoding];
+ [self.queueu enqueue:@{@"api" : protocol.request.URL.path, @"arg": requestBodyString, @"json" : json}];
+ }
+#endif
+
+
+ [self removeDataForProtocol:protocol];
+}
+- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didFailWithError:(NSError *)error
+{
+ [self removeDataForProtocol:protocol];
+}
+
+
+//- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol logWithFormat:(NSString *)format arguments:(va_list)argList
+//{
+// [self logLineReturn:[[NSString alloc] initWithFormat:format arguments:argList]];
+//}
+
+
+@end
--- /dev/null
+//
+// HMJSONTracker.h
+// KCD
+//
+// Created by Hori,Masaki on 2014/02/09.
+// Copyright (c) 2014年 Hori,Masaki. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+#import "HMJSONReciever.h"
+
+
+@interface HMJSONTracker : NSObject
+
+@end
--- /dev/null
+//
+// HMJSONTracker.m
+// KCD
+//
+// Created by Hori,Masaki on 2014/02/09.
+// Copyright (c) 2014年 Hori,Masaki. All rights reserved.
+//
+
+#import "HMJSONTracker.h"
+
+#import "HMQueue.h"
+#import "HMJSONCommand.h"
+
+
+@interface HMJSONTracker ()
+@property (retain) HMQueue *queue;
+
+@property (retain) HMJSONReciever *reciever;
+
+@end
+
+@implementation HMJSONTracker
+
+- (id)init
+{
+ self = [super init];
+ if(self) {
+ _queue = [HMQueue new];
+ _reciever = [HMJSONReciever new];
+ self.reciever.queueu = self.queue;
+ [self start];
+ }
+ return self;
+}
+
+- (void)start
+{
+ dispatch_queue_t queue = dispatch_queue_create("HMJSONTracker", 0);
+ dispatch_async(queue, ^{
+ while(YES) {
+ NSDictionary *item = [self.queue dequeue];
+ HMJSONCommand *command = [HMJSONCommand commandForAPI:[item objectForKey:@"api"]];
+ command.argumentsString = [item objectForKey:@"arg"];
+ [command doCommand:[item objectForKey:@"json"]];
+
+ }
+ });
+}
+
+@end
--- /dev/null
+//
+// HMQueue.h
+// KCD
+//
+// Created by Hori,Masaki on 2014/02/09.
+// Copyright (c) 2014年 Hori,Masaki. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface HMQueue : NSObject
+
+- (id)dequeue;
+- (void)enqueue:(id)object;
+
+@end
--- /dev/null
+//
+// HMQueue.m
+// KCD
+//
+// Created by Hori,Masaki on 2014/02/09.
+// Copyright (c) 2014年 Hori,Masaki. All rights reserved.
+//
+
+#import "HMQueue.h"
+
+@interface HMQueue ()
+@property (retain) NSMutableArray *contents;
+@property (retain) NSCondition *lock;
+@end
+
+@implementation HMQueue
+
+- (id)init
+{
+ self = [super init];
+ if(self) {
+ _contents = [NSMutableArray array];
+ _lock = [NSCondition new];
+ }
+ return self;
+}
+
+- (id)dequeue
+{
+ id object = nil;
+
+ [self.lock lock];
+ @try {
+ while([self.contents count] == 0) {
+ [self.lock wait];
+ }
+ object = [self.contents lastObject];
+ [self.contents removeLastObject];
+ }
+ @finally {
+ [self.lock unlock];
+ }
+
+ return object;
+}
+
+- (void)enqueue:(id)object
+{
+ [self.lock lock];
+ @try {
+ [self.contents addObject:object];
+ [self.lock broadcast];
+ }
+ @finally {
+ [self.lock unlock];
+ }
+}
+
+@end