Handle relative redirects without crashing, and fix URLs that require trailing slash on path
parent
4e2b6ea9d8
commit
fa6c02d3e8
|
@ -155,8 +155,9 @@
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
else if (301 == statusCode || 302 == statusCode) { // Redirect
|
else if (301 == statusCode || 302 == statusCode) { // Redirect
|
||||||
NSURL *redirectURL = [[NSURL alloc] initWithString:[self valueForResponseHeader:@"Location"]];
|
// Handle relative redirects as well
|
||||||
[self setURL:redirectURL];
|
NSURL *redirectURL = [[NSURL alloc] initWithString:[self valueForResponseHeader:@"Location"] relativeToURL:[self URL]];
|
||||||
|
[self setURL:[redirectURL absoluteURL]];
|
||||||
[self close];
|
[self close];
|
||||||
return [self connect];
|
return [self connect];
|
||||||
}
|
}
|
||||||
|
@ -169,7 +170,25 @@
|
||||||
{
|
{
|
||||||
NSURL *url = [self URL];
|
NSURL *url = [self URL];
|
||||||
|
|
||||||
NSString *path = [url path];
|
NSString *path;
|
||||||
|
NSString *host;
|
||||||
|
NSNumber *port;
|
||||||
|
|
||||||
|
if (NSClassFromString(@"NSURLComponents")) {
|
||||||
|
// Resolves trailing slash issue, but requires 10.9+
|
||||||
|
|
||||||
|
NSURLComponents * urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:YES];
|
||||||
|
|
||||||
|
path = urlComponents.path;
|
||||||
|
host = urlComponents.host;
|
||||||
|
port = urlComponents.port;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
path = [url path];
|
||||||
|
host = [url host];
|
||||||
|
port = [url port];
|
||||||
|
}
|
||||||
|
|
||||||
if (nil == path || [path isEqualToString:@""]) {
|
if (nil == path || [path isEqualToString:@""]) {
|
||||||
path = @"/";
|
path = @"/";
|
||||||
}
|
}
|
||||||
|
@ -178,9 +197,8 @@
|
||||||
NSMutableString *requestString = [[NSMutableString alloc] initWithFormat:@"GET %@ HTTP/1.0\r\n", path];
|
NSMutableString *requestString = [[NSMutableString alloc] initWithFormat:@"GET %@ HTTP/1.0\r\n", path];
|
||||||
|
|
||||||
// Make sure there is a Host entry
|
// Make sure there is a Host entry
|
||||||
NSString *host = [url host];
|
if (nil != port) {
|
||||||
if (nil != [url port]) {
|
host = [NSString stringWithFormat:@"%@:%@", host, port];
|
||||||
host = [NSString stringWithFormat:@"%@:%@", [url host], [url port]];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[self setValue:host forRequestHeader:@"Host"];
|
[self setValue:host forRequestHeader:@"Host"];
|
||||||
|
@ -197,7 +215,7 @@
|
||||||
|
|
||||||
// Get the bytes out of it
|
// Get the bytes out of it
|
||||||
const char *requestBytes = [requestString UTF8String];
|
const char *requestBytes = [requestString UTF8String];
|
||||||
int requestLength = strlen(requestBytes);
|
long requestLength = strlen(requestBytes);
|
||||||
|
|
||||||
// Send it off!
|
// Send it off!
|
||||||
NSInteger sent = [_socket send:requestBytes amount:requestLength];
|
NSInteger sent = [_socket send:requestBytes amount:requestLength];
|
||||||
|
@ -214,11 +232,22 @@
|
||||||
- (BOOL)connect
|
- (BOOL)connect
|
||||||
{
|
{
|
||||||
NSURL *url = [self URL];
|
NSURL *url = [self URL];
|
||||||
NSString *host = [url host];
|
NSString *host;
|
||||||
|
NSNumber *portNumber;
|
||||||
|
|
||||||
|
if (NSClassFromString(@"NSURLComponents")) {
|
||||||
|
NSURLComponents * urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:YES];
|
||||||
|
|
||||||
|
host = urlComponents.host;
|
||||||
|
portNumber = urlComponents.port;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
host = [url host];
|
||||||
|
portNumber = [url port];
|
||||||
|
}
|
||||||
|
|
||||||
// Get the port number
|
// Get the port number
|
||||||
NSNumber *portNumber = [url port];
|
int port = (int) [portNumber integerValue];
|
||||||
NSInteger port = [portNumber integerValue];
|
|
||||||
if (portNumber == nil) {
|
if (portNumber == nil) {
|
||||||
port = 80; // Default for HTTP
|
port = 80; // Default for HTTP
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue