twitter-archive / CocoaSPDY

SPDY for iOS and OS X
Apache License 2.0
2.39k stars 233 forks source link

"allSPDYHeaderFields" get path error #146

Open szrexzhu opened 8 years ago

szrexzhu commented 8 years ago

I try to use CocoaSPDY visit our SPDY Proxy Server in my QQ Browser,meet a bug:

Visit http://m.sohu.com/n/452167131/?wscrid=15084_2,get a 301 redirect,the reason is that we have a error path:'/n/452167131?wscrid=15084_2',lost '/' when we use 'url.path'.

- (NSDictionary *)allSPDYHeaderFields
{
    NSDictionary *httpHeaders = self.allHTTPHeaderFields;
    NSURL *url = self.URL;

    static NSSet *invalidKeys;
    static NSSet *reservedKeys;
    static dispatch_once_t initialized;
    dispatch_once(&initialized, ^{
        invalidKeys = [[NSSet alloc] initWithObjects:
            @"connection", @"keep-alive", @"proxy-connection", @"transfer-encoding", nil
        ];

        reservedKeys = [[NSSet alloc] initWithObjects:
            @"method", @"path", @"version", @"host", @"scheme", nil
        ];
    });

    //----by rex----begin
    //Error:
    //Visit http://m.sohu.com/n/452167131/?wscrid=15084_2,get a 301 redirect,
    //the reason is that we have a error path:'/n/452167131?wscrid=15084_2',lost '/'
    //when we use 'url.path'.
    //
    //Reason:
    //Apple doc for 'url.path': If the path has a trailing slash, it is stripped.
    //
    //Solution:
    //http://markmail.org/message/cigyetgyjcvwsyx5#query:+page:1+mid:g4pmkeourikefc3p+state:results
    //NSString* s = @"http://example.com/path/to/id%3D8528/" ;
    //    NSLog(@"       given: %@", s) ;
    //    NSURL* url = [NSURL URLWithString:s] ;
    //    NSString* cocoaPath = [url path] ;
    //    NSString* cfPath = (NSString*)CFBridgingRelease(CFURLCopyPath((CFURLRef)url));
    //    NSString* cfstrictPath = (NSString*)CFBridgingRelease(CFURLCopyStrictPath((CFURLRef)url, NULL)) ;
    //    NSLog(@"   cocoaPath: %@", cocoaPath) ;
    //    NSLog(@"      cfPath: %@", cfPath) ;
    //    NSLog(@"cfstrictPath: %@", cfstrictPath) ;
    //
    //Result:
    //given: http://example.com/path/to/id%3D8528/
    //cocoaPath: /path/to/id=8528
    //cfPath: /path/to/id%3D8528/
    //cfstrictPath: path/to/id%3D8528/
    //

//    NSString *escapedPath = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
//            kCFAllocatorDefault,
//            (__bridge CFStringRef)url.path,
//            NULL,
//            CFSTR("?"),
//            kCFStringEncodingUTF8));

//    NSMutableString *path = [[NSMutableString alloc] initWithString:escapedPath];

    NSMutableString *path = [[NSMutableString alloc] initWithString:(NSString*)CFBridgingRelease(CFURLCopyPath((CFURLRef)url))];
    //----end----

    NSString *query = url.query;
    if (query) {
        [path appendFormat:@"?%@", query];
    }

    NSString *fragment = url.fragment;
    if (fragment) {
        [path appendFormat:@"#%@", fragment];
    }
......
kgoodier commented 8 years ago

This looks like the code out of the 'master' branch. The 'develop' branch has changed this a bit, to use NSURLComponents for the url pieces. It seems to behave correctly for the path segment of your url. You may want to try running the 'develop' branch code for this issue, and possibly your others. In the meantime, I'll merge 'develop' into 'master' and create a new release but it may take a few weeks.

szrexzhu commented 8 years ago

Thanks