Skip to content

Commit

Permalink
accepting the packfile data to a temp file
Browse files Browse the repository at this point in the history
  • Loading branch information
schacon committed Apr 6, 2010
1 parent 5409326 commit 417071e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 47 deletions.
8 changes: 8 additions & 0 deletions Controllers/AppController.m
Expand Up @@ -36,10 +36,12 @@ @implementation AppController
@synthesize tabBarController; @synthesize tabBarController;
@synthesize serverViewController; @synthesize serverViewController;


// creates [userDocs]/git path for git repos
- (NSString *)getGitPath - (NSString *)getGitPath
{ {
NSArray *paths; NSArray *paths;
NSString *gitPath = @""; NSString *gitPath = @"";
NSString *tmpPath = @"";
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 0) { if ([paths count] > 0) {
gitPath = [NSString stringWithString:[[paths objectAtIndex:0] stringByAppendingPathComponent:@"git"]]; gitPath = [NSString stringWithString:[[paths objectAtIndex:0] stringByAppendingPathComponent:@"git"]];
Expand All @@ -49,6 +51,12 @@ - (NSString *)getGitPath
if (![fm fileExistsAtPath:gitPath isDirectory:&isDir] && isDir) { if (![fm fileExistsAtPath:gitPath isDirectory:&isDir] && isDir) {
[fm createDirectoryAtPath:gitPath attributes:nil]; [fm createDirectoryAtPath:gitPath attributes:nil];
} }

tmpPath = [NSString stringWithString:[[paths objectAtIndex:0] stringByAppendingPathComponent:@"gitTmp"]];

if (![fm fileExistsAtPath:tmpPath isDirectory:&isDir] && isDir) {
[fm createDirectoryAtPath:tmpPath attributes:nil];
}
} }
return gitPath; return gitPath;
} }
Expand Down
3 changes: 2 additions & 1 deletion Controllers/GitHTTPConnection.h
Expand Up @@ -10,6 +10,7 @@
@interface GitHTTPConnection : HTTPConnection @interface GitHTTPConnection : HTTPConnection
{ {
NSString* gitService; NSString* gitService;
NSFileHandle* packfile;
} }


- (BOOL)isBrowseable:(NSString *)path; - (BOOL)isBrowseable:(NSString *)path;
Expand All @@ -23,7 +24,7 @@
- (NSObject<HTTPResponse> *)receivePack:(NSString *)project; - (NSObject<HTTPResponse> *)receivePack:(NSString *)project;
- (NSObject<HTTPResponse> *)uploadPack:(NSString *)project; - (NSObject<HTTPResponse> *)uploadPack:(NSString *)project;


- (NSData*) packetData:(NSString*) info;
- (NSString*) prependPacketLine:(NSString*) info; - (NSString*) prependPacketLine:(NSString*) info;
- (NSData*) packetData:(NSString*) info;


@end @end
109 changes: 63 additions & 46 deletions Controllers/GitHTTPConnection.m
Expand Up @@ -156,52 +156,38 @@ - (NSData *)preprocessResponse:(CFHTTPMessageRef)response
return [super preprocessResponse:response]; return [super preprocessResponse:response];
} }


- (NSData*) packetData:(NSString*) info
{
return [[self prependPacketLine:info] dataUsingEncoding:NSUTF8StringEncoding];
}

#define hex(a) (hexchar[(a) & 15])
- (NSString*) prependPacketLine:(NSString*) info
{
static char hexchar[] = "0123456789abcdef";
uint8_t buffer[5];


unsigned int length = [info length] + 4;

buffer[0] = hex(length >> 12);
buffer[1] = hex(length >> 8);
buffer[2] = hex(length >> 4);
buffer[3] = hex(length);

NSLog(@"write len [%c %c %c %c]", buffer[0], buffer[1], buffer[2], buffer[3]);

NSData *data=[[NSData alloc] initWithBytes:buffer length:4];
NSString *lenStr = [[NSString alloc]
initWithData:data
encoding:NSUTF8StringEncoding];

return [NSString stringWithFormat:@"%@%@", lenStr, info];
}


- (NSObject<HTTPResponse> *)receivePack:(NSString *)project - (NSObject<HTTPResponse> *)receivePack:(NSString *)project
{ {
NSLog(@"ACCEPT PACKFILE"); NSLog(@"ACCEPT PACKFILE");
if (requestContentLength > 0) // Process POST data
{ NSData *requestData = [(NSData *)CFHTTPMessageCopySerializedMessage(request) autorelease];
NSLog(@"processing post data: %i", requestContentLength); NSString *requestStr = [[[NSString alloc] initWithData:requestData encoding:NSASCIIStringEncoding] autorelease];
[[NSNotificationCenter defaultCenter] postNotificationName:@"NewFileUploaded" object:nil]; NSLog(@"\n=== Request ====================\n%@\n================================", requestStr);
requestContentLength = 0;
} NSLog(@"file offset: %d", [packfile offsetInFile]);
[packfile seekToFileOffset:0];
NSData* pktlen = [packfile readDataOfLength:4];
NSLog(@"pkt-ln: %@", pktlen);

// readRefs
// readPack
// writeRefs
// send updated refs (oks)
// packetFlush

return nil; return nil;
} }



- (NSObject<HTTPResponse> *)uploadPack:(NSString *)project - (NSObject<HTTPResponse> *)uploadPack:(NSString *)project
{ {
NSLog(@"GENERATE AND TRANSFER PACKFILE"); NSLog(@"GENERATE AND TRANSFER PACKFILE");
return nil; return nil;
} }



- (NSObject<HTTPResponse> *)plainResponse:(NSString *)project path:(NSString *)path - (NSObject<HTTPResponse> *)plainResponse:(NSString *)project path:(NSString *)path
{ {
NSData *requestData = [(NSData *)CFHTTPMessageCopySerializedMessage(request) autorelease]; NSData *requestData = [(NSData *)CFHTTPMessageCopySerializedMessage(request) autorelease];
Expand Down Expand Up @@ -232,28 +218,59 @@ - (NSString*) prependPacketLine:(NSString*) info
} }




// TODO: Add random and move to temp?
- (void)prepareForBodyWithSize:(UInt64)contentLength
{
NSArray *paths;
NSString *tmpPath = @"";
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
tmpPath = [NSString stringWithString:[[paths objectAtIndex:0] stringByAppendingPathComponent:@"gitTmp"]];
NSString* packfilePath = [tmpPath stringByAppendingPathComponent:@"pack-upload.data"];
NSLog(@"PREPARE %@", packfilePath);
[[NSFileManager defaultManager] createFileAtPath: packfilePath
contents: nil attributes: nil];
packfile = [[NSFileHandle fileHandleForUpdatingAtPath:packfilePath] retain];
[packfile truncateFileAtOffset: 0];
}

/** /**
* This method is called to handle data read from a POST. * This method is called to handle data read from a POST.
* The given data is part of the POST body. * The given data is part of the POST body.
**/ **/
- (void)processDataChunk:(NSData *)postDataChunk - (void)processDataChunk:(NSData *)postDataChunk
{ {
// Override me to do something useful with a POST. [packfile writeData:postDataChunk];
// If the post is small, such as a simple form, you may want to simply append the data to the request. }
// If the post is big, such as a file upload, you may want to store the file to disk.
//
// Remember: In order to support LARGE POST uploads, the data is read in chunks.
// This prevents a 50 MB upload from being stored in RAM.
// The size of the chunks are limited by the POST_CHUNKSIZE definition.
// Therefore, this method may be called multiple times for the same POST request.

NSLog(@"processPostDataChunk");
NSLog(@"data: %@", postDataChunk);


#define hex(a) (hexchar[(a) & 15])
- (NSString*) prependPacketLine:(NSString*) info
{
static char hexchar[] = "0123456789abcdef";
uint8_t buffer[5];


if (requestContentLength > 0) { unsigned int length = [info length] + 4;
} else {
} buffer[0] = hex(length >> 12);
buffer[1] = hex(length >> 8);
buffer[2] = hex(length >> 4);
buffer[3] = hex(length);

NSLog(@"write len [%c %c %c %c]", buffer[0], buffer[1], buffer[2], buffer[3]);

NSData *data=[[NSData alloc] initWithBytes:buffer length:4];
NSString *lenStr = [[NSString alloc]
initWithData:data
encoding:NSUTF8StringEncoding];

return [NSString stringWithFormat:@"%@%@", lenStr, info];
}


- (NSData*) packetData:(NSString*) info
{
return [[self prependPacketLine:info] dataUsingEncoding:NSUTF8StringEncoding];
} }



@end @end

0 comments on commit 417071e

Please sign in to comment.