Skip to content

Commit 3311288

Browse files
committed
Merge pull request #43 from longbai/upport
upport
2 parents ba15745 + 49c7ba2 commit 3311288

File tree

8 files changed

+58
-8
lines changed

8 files changed

+58
-8
lines changed

QiniuSDK/Http/QNHttpManager.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ @interface QNHttpManager ()
2121
@property (nonatomic) NSString *backupIp;
2222
@end
2323

24+
static NSURL *buildUrl(NSString *host, NSNumber *port, NSString *path){
25+
port = port == nil? [NSNumber numberWithInt:80]:port;
26+
NSString *p = [[NSString alloc] initWithFormat:@"http://%@:%@%@", host, port, path];
27+
return [[NSURL alloc] initWithString:p];
28+
}
29+
2430
@implementation QNHttpManager
2531

2632
- (instancetype)initWithTimeout:(UInt32)timeout
@@ -74,8 +80,7 @@ - (void) sendRequest:(NSMutableURLRequest *)request
7480
__block NSString *ip = nil;
7581
if (_converter != nil) {
7682
url = [[NSURL alloc] initWithString:_converter(u)];
77-
}
78-
else {
83+
} else {
7984
if (_backupIp != nil && ![_backupIp isEqualToString:@""]) {
8085
NSString *host = url.host;
8186
ip = [QNDns getAddress:host];
@@ -86,7 +91,7 @@ - (void) sendRequest:(NSMutableURLRequest *)request
8691
if (path == nil || [@"" isEqualToString:path]) {
8792
path = @"/";
8893
}
89-
url = [[NSURL alloc] initWithScheme:url.scheme host:ip path:path];
94+
url = buildUrl(ip, url.port, path);
9095
[request setValue:host forHTTPHeaderField:@"Host"];
9196
}
9297
}

QiniuSDK/Http/QNSessionManager.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
2424
- (instancetype)initWithProgress:(QNInternalProgressBlock)progressBlock;
2525
@end
2626

27+
static NSURL *buildUrl(NSString *host, NSNumber *port, NSString *path){
28+
port = port == nil? [NSNumber numberWithInt:80]:port;
29+
NSString *p = [[NSString alloc] initWithFormat:@"http://%@:%@%@", host, port, path];
30+
return [[NSURL alloc] initWithString:p];
31+
}
32+
33+
2734
@implementation QNProgessDelegate
2835
- (instancetype)initWithProgress:(QNInternalProgressBlock)progressBlock {
2936
if (self = [super init]) {
@@ -142,6 +149,7 @@ - (void) sendRequest:(NSMutableURLRequest *)request
142149
path = @"/";
143150
}
144151
url = [[NSURL alloc] initWithScheme:url.scheme host:ip path:path];
152+
url = buildUrl(ip, url.port, path);
145153
NSLog(@"%@", url);
146154
[request setValue:host forHTTPHeaderField:@"Host"];
147155
}

QiniuSDK/Storage/QNConfiguration.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ typedef void (^QNConfigurationBuilderBlock)(QNConfigurationBuilder *builder);
5151
*/
5252
@property (copy, nonatomic, readonly) NSString *upIp;
5353

54+
/**
55+
* 上传端口
56+
*/
57+
@property (nonatomic, readonly) UInt32 upPort;
58+
5459
/**
5560
* 断点上传时的分片大小
5661
*/
@@ -137,6 +142,10 @@ typedef void (^QNConfigurationBuilderBlock)(QNConfigurationBuilder *builder);
137142
*/
138143
@property (nonatomic, strong) QNZone *zone;
139144

145+
/**
146+
* 上传端口
147+
*/
148+
@property (nonatomic, readonly) UInt32 upPort;
140149

141150
/**
142151
* 断点上传时的分片大小

QiniuSDK/Storage/QNConfiguration.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ - (instancetype)initWithBuilder:(QNConfigurationBuilder *)builder {
2323
_upHost = builder.zone.upHost;
2424
_upHostBackup = builder.zone.upHostBackup;
2525

26+
_upPort = builder.upPort;
27+
2628
_chunkSize = builder.chunkSize;
2729
_putThreshold = builder.putThreshold;
2830
_retryMax = builder.retryMax;
@@ -58,6 +60,8 @@ - (instancetype)init {
5860

5961
_proxy = nil;
6062
_converter = nil;
63+
64+
_upPort = 80;
6165
}
6266
return self;
6367
}

QiniuSDK/Storage/QNFormUpload.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ - (void)put {
9898
_complete(info, _key, resp);
9999
};
100100

101-
[_httpManager multipartPost:[NSString stringWithFormat:@"http://%@", nextHost]
101+
[_httpManager multipartPost:[NSString stringWithFormat:@"http://%@:%u/", nextHost, (unsigned int)_config.upPort]
102102
withData:_data
103103
withParams:parameters
104104
withFileName:fileName
@@ -108,7 +108,7 @@ - (void)put {
108108
withCancelBlock:nil];
109109
};
110110

111-
[_httpManager multipartPost:[NSString stringWithFormat:@"http://%@", _config.upHost]
111+
[_httpManager multipartPost:[NSString stringWithFormat:@"http://%@:%u/", _config.upHost, (unsigned int)_config.upPort]
112112
withData:_data
113113
withParams:parameters
114114
withFileName:fileName

QiniuSDK/Storage/QNResumeUpload.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ - (void)makeBlock:(NSString *)uphost
260260
progress:(QNInternalProgressBlock)progressBlock
261261
complete:(QNCompleteBlock)complete {
262262
NSData *data = [self.data subdataWithRange:NSMakeRange(offset, (unsigned int)chunkSize)];
263-
NSString *url = [[NSString alloc] initWithFormat:@"http://%@/mkblk/%u", uphost, (unsigned int)blockSize];
263+
NSString *url = [[NSString alloc] initWithFormat:@"http://%@:%u/mkblk/%u", uphost, (unsigned int)_config.upPort, (unsigned int)blockSize];
264264
_chunkCrc = [QNCrc32 data:data];
265265
[self post:url withData:data withCompleteBlock:complete withProgressBlock:progressBlock];
266266
}
@@ -273,7 +273,7 @@ - (void)putChunk:(NSString *)uphost
273273
complete:(QNCompleteBlock)complete {
274274
NSData *data = [self.data subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
275275
UInt32 chunkOffset = offset % kQNBlockSize;
276-
NSString *url = [[NSString alloc] initWithFormat:@"http://%@/bput/%@/%u", uphost, context, (unsigned int)chunkOffset];
276+
NSString *url = [[NSString alloc] initWithFormat:@"http://%@:%u/bput/%@/%u", uphost, (unsigned int)_config.upPort, context, (unsigned int)chunkOffset];
277277
_chunkCrc = [QNCrc32 data:data];
278278
[self post:url withData:data withCompleteBlock:complete withProgressBlock:progressBlock];
279279
}
@@ -286,7 +286,7 @@ - (void)makeFile:(NSString *)uphost
286286
complete:(QNCompleteBlock)complete {
287287
NSString *mime = [[NSString alloc] initWithFormat:@"/mimeType/%@", [QNUrlSafeBase64 encodeString:self.option.mimeType]];
288288

289-
__block NSString *url = [[NSString alloc] initWithFormat:@"http://%@/mkfile/%u%@", uphost, (unsigned int)self.size, mime];
289+
__block NSString *url = [[NSString alloc] initWithFormat:@"http://%@:%u/mkfile/%u%@", uphost, (unsigned int)_config.upPort, (unsigned int)self.size, mime];
290290

291291
if (self.key != nil) {
292292
NSString *keyStr = [[NSString alloc] initWithFormat:@"/key/%@", [QNUrlSafeBase64 encodeString:self.key]];

QiniuSDKTests/QNHttpTest.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,16 @@ - (void)testPostIp {
113113
XCTAssert(testInfo.reqId, @"Pass");
114114
}
115115

116+
- (void)testPostNoPort {
117+
__block QNResponseInfo *testInfo = nil;
118+
QNHttpManager *httpManager = [[QNHttpManager alloc] initWithTimeout:60 urlConverter:nil backupIp:nil];
119+
[httpManager post:@"http://up.qiniu.com:12345/" withData:nil withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
120+
testInfo = info;
121+
} withProgressBlock:nil withCancelBlock:nil];
122+
123+
AGWW_WAIT_WHILE(testInfo == nil, 100.0);
124+
NSLog(@"%@", testInfo);
125+
XCTAssert(testInfo.statusCode < 0, @"Pass");
126+
}
127+
116128
@end

QiniuSDKTests/QNSessionTest.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ - (void)testPostIp {
135135
XCTAssert(testInfo.reqId, @"Pass");
136136
}
137137

138+
- (void)testPostNoPort {
139+
__block QNResponseInfo *testInfo = nil;
140+
QNSessionManager *httpManager = [[QNSessionManager alloc] initWithProxy:nil timeout:60 urlConverter:nil backupIp:nil];
141+
[httpManager post:@"http://up.qiniu.com:12345/" withData:nil withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
142+
testInfo = info;
143+
} withProgressBlock:nil withCancelBlock:nil];
144+
145+
AGWW_WAIT_WHILE(testInfo == nil, 100.0);
146+
NSLog(@"%@", testInfo);
147+
XCTAssert(testInfo.statusCode < 0, @"Pass");
148+
}
149+
138150
@end
139151

140152
#endif

0 commit comments

Comments
 (0)