http_response_headers_util.mm 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ios/net/http_response_headers_util.h"
  5. #import <Foundation/Foundation.h>
  6. #include "base/check.h"
  7. #include "base/format_macros.h"
  8. #include "base/strings/stringprintf.h"
  9. #include "base/strings/sys_string_conversions.h"
  10. #include "net/http/http_util.h"
  11. #if !defined(__has_feature) || !__has_feature(objc_arc)
  12. #error "This file requires ARC support."
  13. #endif
  14. namespace {
  15. // String format used to create the http status line from the status code and
  16. // its localized description.
  17. char const kHttpStatusLineFormat[] = "HTTP %" PRIdNS " DummyStatusDescription";
  18. // Tries to decode `string` as if it was a "utf-8" encoded string incorrectly
  19. // decoded as "latin1" encoded string. Returns the original string if it does
  20. // not appear to be a mis-decoded string.
  21. //
  22. // HTTP headers have no encoding, but NSHTTPURLResponse decode them as if they
  23. // were using "latin1" encoding as the rfc recommends (see [1]). Servers do
  24. // sometime sends data in "utf-8" encoding. Interpreting "utf-8" encoded string
  25. // as "latin1" results in garbled characters as soon as the string contains non
  26. // US-ASCII characters (aka mojibake). Hopefully, this incorrect decoding is
  27. // reversible as "latin1" maps each bytes to the same unicode character (i.e.
  28. // byte '\xab' maps to '\u00ab'). See [2] for example of a web server serving
  29. // "utf-8" encoded string incorrectly decoded.
  30. //
  31. // [1]: https://www.rfc-editor.org/rfc/rfc7230#section-3.2.4.
  32. // [2]: https://crbug.com/1333351
  33. NSString* FixNSStringIncorrectlyDecodedAsLatin1(NSString* string) {
  34. // Check if the string contains any character above '\u007f'. If not,
  35. // then the "latin1" and "utf-8" representation are identical, and there
  36. // is no need to allocate memory for the conversion.
  37. static NSCharacterSet* non_ascii_characters = [NSCharacterSet
  38. characterSetWithRange:NSMakeRange(0x0080, 0x10ffff - 0x0080)];
  39. NSRange range = [string rangeOfCharacterFromSet:non_ascii_characters];
  40. if (range.location == NSNotFound && range.length == 0)
  41. return string;
  42. // Try to save the string as "latin1". Will fail if it does contains
  43. // characters that falls out of the "latin1" range (i.e. after '\u00ff').
  44. NSData* data = [string dataUsingEncoding:NSISOLatin1StringEncoding
  45. allowLossyConversion:NO];
  46. if (!data)
  47. return string;
  48. // Try to load the saved data as "utf-8". Will fail if the string is
  49. // not encoded in "utf-8". In that case, it was probably not garbled
  50. // and the original string needs to be used. This will be the case for
  51. // strings that are genuinely encoded in "latin1".
  52. NSString* decoded = [[NSString alloc] initWithData:data
  53. encoding:NSUTF8StringEncoding];
  54. if (!decoded)
  55. return string;
  56. return decoded;
  57. }
  58. } // anonymous namespace
  59. namespace net {
  60. scoped_refptr<HttpResponseHeaders> CreateHeadersFromNSHTTPURLResponse(
  61. NSHTTPURLResponse* response) {
  62. DCHECK(response);
  63. // Initialize the header with a generated status line.
  64. scoped_refptr<HttpResponseHeaders> http_headers(new HttpResponseHeaders(
  65. base::StringPrintf(kHttpStatusLineFormat, response.statusCode)));
  66. // Iterate through |response|'s headers and add them to |http_headers|.
  67. [response.allHeaderFields enumerateKeysAndObjectsUsingBlock:^(
  68. NSString* name, NSString* value, BOOL*) {
  69. const std::string header_name = base::SysNSStringToUTF8(name);
  70. if (!HttpUtil::IsValidHeaderName(header_name))
  71. return;
  72. const std::string header_value =
  73. base::SysNSStringToUTF8(FixNSStringIncorrectlyDecodedAsLatin1(value));
  74. if (!HttpUtil::IsValidHeaderValue(header_value))
  75. return;
  76. http_headers->AddHeader(header_name, header_value);
  77. }];
  78. return http_headers;
  79. }
  80. } // namespace net