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.
5 #include "ios/net/http_response_headers_util.h"
7 #include <Foundation/Foundation.h>
9 #include "base/logging.h"
10 #include "base/strings/sys_string_conversions.h"
13 // String format used to create the http status line from the status code and
14 // its localized description.
15 NSString* const kHttpStatusLineFormat = @"HTTP %ld %s";
16 // String format used to pass the header name/value pairs to the
17 // HttpResponseHeaders.
18 NSString* const kHeaderLineFormat = @"%@: %@";
23 const std::string kDummyHttpStatusDescription = "DummyStatusDescription";
25 scoped_refptr<HttpResponseHeaders> CreateHeadersFromNSHTTPURLResponse(
26 NSHTTPURLResponse* response) {
28 // Create the status line and initialize the headers.
29 NSInteger status_code = response.statusCode;
30 std::string status_line = base::SysNSStringToUTF8([NSString
31 stringWithFormat:kHttpStatusLineFormat, static_cast<long>(status_code),
32 kDummyHttpStatusDescription.c_str()]);
33 scoped_refptr<HttpResponseHeaders> http_headers(
34 new HttpResponseHeaders(status_line));
35 // Iterate through |response|'s headers and add them to |http_headers|.
36 [response.allHeaderFields
37 enumerateKeysAndObjectsUsingBlock:^(NSString* header_name,
38 NSString* value, BOOL*) {
39 NSString* header_line =
40 [NSString stringWithFormat:kHeaderLineFormat, header_name, value];
41 http_headers->AddHeader(base::SysNSStringToUTF8(header_line));
43 return http_headers.Pass();