Uploaded image for project: 'Swift'
  1. Swift
  2. SR-2429

Regression: HTTPURLResponse allHeaderFields is now case-sensitive

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Foundation
    • Labels:
      None
    • Environment:

      Xcode Version 8.0 beta 6 (8S201h)

      Description

      HTTPURLResponse normalizes header names following RFC 2616 as described in https://developer.apple.com/reference/foundation/httpurlresponse/1417930-allheaderfields

      Access to these header values is case-insensitive as described in the documentation. This is broken in the latest Xcode8 beta6. The access is now case-sensitive.

      Additionally the "ETag" header name is normalized to "Etag" instead of "ETag" as described in RFC 2616

      This works correctly on Xcode8 beta4

      Steps to Reproduce:
      Run following Swift code:

      let url = URL(string: "http://www.apple.com")!
      
      let headers = [
          "ETag" : "123456",
          "content-type": "application/json",
          ]
      
      let urlResponse = HTTPURLResponse(url: url,
                                        statusCode: 200,
                                        httpVersion: nil,
                                        headerFields: headers)
      
      print("ETag: \(urlResponse?.allHeaderFields["ETag"])")
      print("Etag: \(urlResponse?.allHeaderFields["Etag"])")
      print("ETAG: \(urlResponse?.allHeaderFields["ETAG"])")
      print("etag: \(urlResponse?.allHeaderFields["etag"])")
      print("content-type: \(urlResponse?.allHeaderFields["content-type"])")
      print("Content-Type: \(urlResponse?.allHeaderFields["Content-Type"])")
      print("CONTENT-TYPE: \(urlResponse?.allHeaderFields["CONTENT-TYPE"])")
      

      Same code in Objective-C works as expected:

      NSURL *url = [[NSURL alloc] initWithString:@"http://www.apple.com"];
      
      NSDictionary *headers = @{
                                @"ETag": @"123456",
                                @"content-type": @"application/json",
      
                                };
      
      NSURL *url = [[NSURL alloc] initWithString:@"http://www.apple.com"];
      
      NSDictionary *headers = @{
                                @"ETag": @"123456",
                                @"content-type": @"application/json",
      
                                };
      
      NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:url
                                                                statusCode:200
                                                               HTTPVersion:nil
                                                              headerFields:headers];
      
      NSLog(@"Headers: %@", response.allHeaderFields);
      
      NSLog(@"ETag: %@", response.allHeaderFields[@"ETag"]);
      NSLog(@"Etag: %@", response.allHeaderFields[@"Etag"]);
      NSLog(@"etag: %@", response.allHeaderFields[@"etag"]);
      NSLog(@"ETAG: %@", response.allHeaderFields[@"ETAG"]);
      NSLog(@"content-type: %@", response.allHeaderFields[@"content-type"]);
      NSLog(@"Content-Type: %@", response.allHeaderFields[@"Content-Type"]);
      NSLog(@"CONTENT-TYPE: %@", response.allHeaderFields[@"CONTENT-TYPE"]);
      

      Expected Results:

      Headers: Optional([AnyHashable("Content-Type"): application/json, AnyHashable("Etag"): 123456])
      ETag: Optional(123456)
      Etag: Optional(123456)
      ETAG: Optional(123456)
      etag: Optional(123456)
      content-type: Optional(application/json)
      Content-Type: Optional(application/json)
      CONTENT-TYPE: Optional(application/json)
      

      Actual Results:

      Headers: Optional([AnyHashable("Content-Type"): application/json, AnyHashable("Etag"): 123456])
      ETag: nil
      Etag: Optional(123456)
      ETAG: nil
      etag: nil
      content-type: nil
      Content-Type: Optional(application/json)
      CONTENT-TYPE: nil
      

      Version:
      Version 8.0 beta 6 (8S201h)

        Attachments

          Activity

            People

            • Assignee:
              airspeedswift Ben Cohen
              Reporter:
              alexsporn Alexander Sporn
            • Votes:
              27 Vote for this issue
              Watchers:
              37 Start watching this issue

              Dates

              • Created:
                Updated: