OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/net/protocol_handler_util.h" | 5 #import "ios/net/protocol_handler_util.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "ios/net/crn_http_url_response.h" | 15 #include "ios/net/crn_http_url_response.h" |
16 #import "net/base/mac/url_conversions.h" | 16 #import "net/base/mac/url_conversions.h" |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 #include "net/http/http_request_headers.h" | 18 #include "net/http/http_request_headers.h" |
19 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
20 #include "net/http/http_version.h" | 20 #include "net/http/http_version.h" |
21 #include "net/url_request/url_request.h" | 21 #include "net/url_request/url_request.h" |
22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
23 #include "url/url_features.h" | 23 #include "url/url_features.h" |
24 | 24 |
25 #if !BUILDFLAG(USE_PLATFORM_ICU_ALTERNATIVES) | 25 #if !BUILDFLAG(USE_PLATFORM_ICU_ALTERNATIVES) |
26 #include "base/i18n/encoding_detection.h" // nogncheck | 26 #include "base/i18n/encoding_detection.h" // nogncheck |
27 #include "base/i18n/icu_string_conversions.h" // nogncheck | 27 #include "base/i18n/icu_string_conversions.h" // nogncheck |
| 28 |
| 29 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 30 #error "This file requires ARC support." |
| 31 #endif |
28 #endif // !BUILDFLAG(USE_PLATFORM_ICU_ALTERNATIVES) | 32 #endif // !BUILDFLAG(USE_PLATFORM_ICU_ALTERNATIVES) |
29 | 33 |
30 namespace { | 34 namespace { |
31 | 35 |
32 // "Content-Type" HTTP header. | 36 // "Content-Type" HTTP header. |
33 NSString* const kContentType = @"Content-Type"; | 37 NSString* const kContentType = @"Content-Type"; |
34 | 38 |
35 } // namespace | 39 } // namespace |
36 | 40 |
37 namespace net { | 41 namespace net { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 request->GetMimeType(&mt); | 81 request->GetMimeType(&mt); |
78 NSString* mime_type = base::SysUTF8ToNSString(mt); | 82 NSString* mime_type = base::SysUTF8ToNSString(mt); |
79 DCHECK(mime_type); | 83 DCHECK(mime_type); |
80 std::string cs; | 84 std::string cs; |
81 request->GetCharset(&cs); | 85 request->GetCharset(&cs); |
82 NSString* charset = base::SysUTF8ToNSString(cs); | 86 NSString* charset = base::SysUTF8ToNSString(cs); |
83 DCHECK(charset); | 87 DCHECK(charset); |
84 // The default iOS stack computes the length of the decoded string. If we | 88 // The default iOS stack computes the length of the decoded string. If we |
85 // wanted to do that we would have to decode the string now. However, using | 89 // wanted to do that we would have to decode the string now. However, using |
86 // the unknown length (-1) seems to be working. | 90 // the unknown length (-1) seems to be working. |
87 return [[[NSURLResponse alloc] initWithURL:url | 91 return [[NSURLResponse alloc] initWithURL:url |
88 MIMEType:mime_type | 92 MIMEType:mime_type |
89 expectedContentLength:-1 | 93 expectedContentLength:-1 |
90 textEncodingName:charset] autorelease]; | 94 textEncodingName:charset]; |
91 } else { | 95 } else { |
92 // Iterate over all the headers and copy them. | 96 // Iterate over all the headers and copy them. |
93 bool has_content_type_header = false; | 97 bool has_content_type_header = false; |
94 NSMutableDictionary* header_fields = [NSMutableDictionary dictionary]; | 98 NSMutableDictionary* header_fields = [NSMutableDictionary dictionary]; |
95 HttpResponseHeaders* headers = request->response_headers(); | 99 HttpResponseHeaders* headers = request->response_headers(); |
96 if (headers != nullptr) { | 100 if (headers != nullptr) { |
97 size_t iter = 0; | 101 size_t iter = 0; |
98 std::string name, value; | 102 std::string name, value; |
99 while (headers->EnumerateHeaderLines(&iter, &name, &value)) { | 103 while (headers->EnumerateHeaderLines(&iter, &name, &value)) { |
100 NSString* key = base::SysUTF8ToNSString(name); | 104 NSString* key = base::SysUTF8ToNSString(name); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 170 |
167 // Parse the HTTP version. | 171 // Parse the HTTP version. |
168 NSString* version_string = @"HTTP/1.1"; | 172 NSString* version_string = @"HTTP/1.1"; |
169 if (headers) { | 173 if (headers) { |
170 const HttpVersion& http_version = headers->GetHttpVersion(); | 174 const HttpVersion& http_version = headers->GetHttpVersion(); |
171 version_string = [NSString stringWithFormat:@"HTTP/%hu.%hu", | 175 version_string = [NSString stringWithFormat:@"HTTP/%hu.%hu", |
172 http_version.major_value(), | 176 http_version.major_value(), |
173 http_version.minor_value()]; | 177 http_version.minor_value()]; |
174 } | 178 } |
175 | 179 |
176 return [[[CRNHTTPURLResponse alloc] initWithURL:url | 180 return [[CRNHTTPURLResponse alloc] initWithURL:url |
177 statusCode:request->GetResponseCode() | 181 statusCode:request->GetResponseCode() |
178 HTTPVersion:version_string | 182 HTTPVersion:version_string |
179 headerFields:header_fields] autorelease]; | 183 headerFields:header_fields]; |
180 } | 184 } |
181 } | 185 } |
182 | 186 |
183 void CopyHttpHeaders(NSURLRequest* in_request, URLRequest* out_request) { | 187 void CopyHttpHeaders(NSURLRequest* in_request, URLRequest* out_request) { |
184 DCHECK(out_request->extra_request_headers().IsEmpty()); | 188 DCHECK(out_request->extra_request_headers().IsEmpty()); |
185 NSDictionary* headers = [in_request allHTTPHeaderFields]; | 189 NSDictionary* headers = [in_request allHTTPHeaderFields]; |
186 HttpRequestHeaders net_headers; | 190 HttpRequestHeaders net_headers; |
187 NSString* key; | 191 NSString* key; |
188 for (key in headers) { | 192 for (key in headers) { |
189 if ([key isEqualToString:@"Referer"]) { | 193 if ([key isEqualToString:@"Referer"]) { |
(...skipping 23 matching lines...) Expand all Loading... |
213 if (out_request->has_upload() && out_request->method() == "POST") { | 217 if (out_request->has_upload() && out_request->method() == "POST") { |
214 DLOG_IF(WARNING, !net_headers.HasHeader(HttpRequestHeaders::kContentType)) | 218 DLOG_IF(WARNING, !net_headers.HasHeader(HttpRequestHeaders::kContentType)) |
215 << "Missing \"Content-Type\" header in POST request."; | 219 << "Missing \"Content-Type\" header in POST request."; |
216 net_headers.SetHeaderIfMissing(HttpRequestHeaders::kContentType, | 220 net_headers.SetHeaderIfMissing(HttpRequestHeaders::kContentType, |
217 "application/x-www-form-urlencoded"); | 221 "application/x-www-form-urlencoded"); |
218 } | 222 } |
219 out_request->SetExtraRequestHeaders(net_headers); | 223 out_request->SetExtraRequestHeaders(net_headers); |
220 } | 224 } |
221 | 225 |
222 } // namespace net | 226 } // namespace net |
OLD | NEW |