GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: An iPhoto plugin to export photos to Gallery.
Homepage: http://zwily.com/iphoto
Clone URL: git://github.com/zwily/iphototogallery.git
iphototogallery / Source / ZWMutableURLRequest.m
100644 106 lines (77 sloc) 3.102 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// ZWMutableURLRequest.m
// iPhotoToGallery
//
// Created by Zach Wily on 7/1/05.
// Copyright 2005 __MyCompanyName__. All rights reserved.
//
 
#import "ZWMutableURLRequest.h"
 
 
@implementation ZWMutableURLRequest
 
- (id)init
{
    if (self = [super init]) {
        encoding = NSUTF8StringEncoding; // default to UTF-8
    }
    
    return self;
}
 
- (void)dealloc
{
    [boundaryData release];
    [requestBodyData release];
    
    [super dealloc];
}
 
- (CFHTTPMessageRef)copyCFHTTPMessageRef
{
    return NULL;
}
 
- (void)setVariation:(ZWURLRequestPOSTVariation)newVariation
{
    variation = newVariation;
}
 
- (ZWURLRequestPOSTVariation)variation
{
    return variation;
}
 
- (void)setEncoding:(NSStringEncoding)newEncoding
{
    encoding = newEncoding;
}
 
- (NSStringEncoding)encoding
{
    return encoding;
}
 
- (void)addString:(NSString *)string forName:(NSString *)name
{
    [self addData:[string dataUsingEncoding:encoding] forName:name filename:nil contentType:nil];
}
 
- (void)addData:(NSData *)data forName:(NSString *)name filename:(NSString *)filename contentType:(NSString *)contentType
{
    if (!requestBodyData)
        requestBodyData = [[NSMutableData alloc] init];
    
    if (variation == ZSURLEncodedVariation) {
        
    }
    else if (variation == ZSURLMultipartVariation) {
        if (!boundaryData) {
            // ProcessInfo is just an easy way to get a nice long string that's always changing and we're pretty sure
            // won't show up in any of the post data
            NSString *boundary = [NSString stringWithFormat:@"--%@--", [[NSProcessInfo processInfo] globallyUniqueString]];
            [self setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
 
            // The actual boundary we'll be using has to start with "--" and end with \r\n
            boundaryData = [[[[@"--" stringByAppendingString:boundary] stringByAppendingString:@"\r\n"] dataUsingEncoding:encoding] retain];
            
            [requestBodyData appendData:boundaryData];
        }
        
        [requestBodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; "] dataUsingEncoding:encoding]];
        
        if (name)
            [requestBodyData appendData:[[NSString stringWithFormat:@"name=\"%@\"; ", name] dataUsingEncoding:encoding]];
        
        if (filename)
            [requestBodyData appendData:[[NSString stringWithFormat:@"filename=\"%@\"; ", filename] dataUsingEncoding:encoding]];
        
        if (contentType)
            [requestBodyData appendData:[[NSString stringWithFormat:@"Content-Type: %@", contentType] dataUsingEncoding:encoding]];
        
        [requestBodyData appendData:[[NSString stringWithFormat:@"\r\n\r\n"] dataUsingEncoding:encoding]];
        
        [requestBodyData appendData:data];
        
        [requestBodyData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:encoding]];
 
        [requestBodyData appendData:boundaryData];
    }
    
    [self setHTTPBody:requestBodyData];
}
 
@end