Permalink
Browse files

follow the changes of gist HTML

  • Loading branch information...
1 parent a31f44a commit 5f4f11937000873f4467f864f616b99e01ab8fd4 @psychs psychs committed Dec 6, 2010
Showing with 86 additions and 11 deletions.
  1. +15 −1 Classes/Library/GistClient.h
  2. +71 −10 Classes/Library/GistClient.m
@@ -4,16 +4,30 @@
#import <Foundation/Foundation.h>
+typedef enum {
+ kGistClientGetTop,
+ kGistClientPost,
+} GistClientStage;
+
+
@interface GistClient : NSObject
{
id delegate;
+
+ GistClientStage stage;
+ NSString* text;
+ NSString* fileType;
+ BOOL isPrivate;
+
NSURLConnection* conn;
+ NSMutableData* buf;
+ NSString* destUrl;
}
@property (nonatomic, assign) id delegate;
- (void)cancel;
-- (void)send:(NSString*)text fileType:(NSString*)fileType private:(BOOL)private;
+- (void)send:(NSString*)text fileType:(NSString*)fileType private:(BOOL)isPrivate;
@end
@@ -5,7 +5,8 @@
#import "GTMNSString+URLArguments.h"
-#define GIST_URL @"http://gist.github.com/gists"
+#define GIST_TOP_URL @"https://gist.github.com/"
+#define GIST_POST_URL @"https://gist.github.com/gists"
#define TIMEOUT 10
@@ -27,6 +28,8 @@ - (id)init
- (void)dealloc
{
[self cancel];
+ [text release];
+ [fileType release];
[super dealloc];
}
@@ -35,6 +38,9 @@ - (void)cancel
[conn cancel];
[conn autorelease];
conn = nil;
+
+ [buf release];
+ buf = [NSMutableData new];
}
- (NSString*)formatParameters:(NSDictionary*)params
@@ -48,20 +54,43 @@ - (NSString*)formatParameters:(NSDictionary*)params
return [ary componentsJoinedByString:@"&"];
}
-- (void)send:(NSString*)text fileType:(NSString*)fileType private:(BOOL)private
+- (void)send:(NSString*)aText fileType:(NSString*)aFileType private:(BOOL)aIsPrivate
{
[self cancel];
+ [destUrl autorelease];
+ destUrl = nil;
+ stage = kGistClientGetTop;
+
+ [text autorelease];
+ text = [aText retain];
+ [aFileType autorelease];
+ fileType = [aFileType retain];
+ isPrivate = aIsPrivate;
+
+ NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:GIST_TOP_URL] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:TIMEOUT];
+ conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
+}
+
+- (void)postDataWithAutheToken:(NSString*)authToken
+{
+ [self cancel];
+ stage = kGistClientPost;
NSMutableDictionary* params = [NSMutableDictionary dictionary];
+ [params setObject:@"" forKey:@"description"];
+ [params setObject:@"" forKey:@"file_name[gistfile1]"];
[params setObject:text forKey:@"file_contents[gistfile1]"];
[params setObject:fileType forKey:@"file_ext[gistfile1]"];
- if (private) {
- [params setObject:@"on" forKey:@"private"];
+ if (isPrivate) {
+ [params setObject:@"private" forKey:@"action_button"];
+ }
+ if (authToken) {
+ [params setObject:authToken forKey:@"authenticity_token"];
}
NSData* body = [[self formatParameters:params] dataUsingEncoding:NSUTF8StringEncoding];
- NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:GIST_URL] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:TIMEOUT];
+ NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:GIST_POST_URL] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:TIMEOUT];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:body];
@@ -76,6 +105,35 @@ - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheRespo
return nil;
}
+- (void)connection:(NSURLConnection *)sender didReceiveData:(NSData *)data
+{
+ if (conn != sender) return;
+
+ [buf appendData:data];
+}
+
+- (void)connectionDidFinishLoading:(NSURLConnection *)sender
+{
+ if (conn != sender) return;
+
+ if (stage == kGistClientGetTop) {
+ NSString* s = [[[NSString alloc] initWithData:buf encoding:NSUTF8StringEncoding] autorelease];
+ NSRange start = [s rangeOfString:@"window._auth_token = \""];
+ if (start.location != NSNotFound) {
+ NSRange end = [s rangeOfString:@"\"" options:0 range:NSMakeRange(start.location, s.length - start.location)];
+ if (end.location != NSNotFound) {
+ NSString* authToken = [s substringWithRange:NSMakeRange(start.location, end.location - start.location)];
+ [self postDataWithAutheToken:authToken];
+ }
+ }
+ }
+ else {
+ if ([delegate respondsToSelector:@selector(gistClient:didReceiveResponse:)]) {
+ [delegate gistClient:self didReceiveResponse:destUrl];
+ }
+ }
+}
+
- (void)connection:(NSURLConnection*)sender didFailWithError:(NSError*)error
{
if (conn != sender) return;
@@ -89,13 +147,16 @@ - (void)connection:(NSURLConnection*)sender didFailWithError:(NSError*)error
- (NSURLRequest *)connection:(NSURLConnection *)sender willSendRequest:(NSURLRequest *)req redirectResponse:(NSHTTPURLResponse *)res
{
- if (conn != sender) return nil;
+ if (conn != sender) return req;
- if (res && res.statusCode == 302) {
- if ([delegate respondsToSelector:@selector(gistClient:didReceiveResponse:)]) {
- [delegate gistClient:self didReceiveResponse:req.URL.absoluteString];
+ if (stage == kGistClientPost) {
+ if (res && res.statusCode == 302) {
+ [destUrl autorelease];
+ destUrl = [req.URL.absoluteString retain];
+
+ // Do not cancel request here.
+ // It causes memory leak in NSURLConnection.
}
- return nil;
}
return req;

0 comments on commit 5f4f119

Please sign in to comment.