OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "net/spdy/spdy_http_utils.h" | 5 #include "net/spdy/spdy_http_utils.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "net/base/escape.h" | 12 #include "net/base/escape.h" |
13 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
14 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
15 #include "net/http/http_request_headers.h" | 15 #include "net/http/http_request_headers.h" |
16 #include "net/http/http_request_info.h" | 16 #include "net/http/http_request_info.h" |
17 #include "net/http/http_response_headers.h" | |
18 #include "net/http/http_response_info.h" | 17 #include "net/http/http_response_info.h" |
19 #include "net/http/http_util.h" | 18 #include "net/http/http_util.h" |
20 | 19 |
21 namespace net { | 20 namespace net { |
22 | 21 |
22 namespace { | |
23 | |
24 void AddSpdyHeader(SpdyHeaderBlock* headers, | |
25 const std::string& name, | |
26 const std::string& value) { | |
27 if (headers->find(name) == headers->end()) { | |
28 (*headers)[name] = value; | |
29 } else { | |
30 std::string new_value = (*headers)[name]; | |
31 new_value.append(1, '\0'); // +=() doesn't append 0's | |
32 new_value += value; | |
33 (*headers)[name] = new_value; | |
Ryan Hamilton
2014/06/19 00:15:44
Would this work:
(*headers)[name] += "\0" + value
dmz
2014/06/19 21:17:02
No, but this does:
(*headers)[name] += '\0' + valu
| |
34 } | |
35 } | |
36 } | |
Ryan Hamilton
2014/06/19 00:15:44
newline before. Add a comment:
} // namespace
dmz
2014/06/19 21:17:02
Done.
| |
37 | |
23 bool SpdyHeadersToHttpResponse(const SpdyHeaderBlock& headers, | 38 bool SpdyHeadersToHttpResponse(const SpdyHeaderBlock& headers, |
24 SpdyMajorVersion protocol_version, | 39 SpdyMajorVersion protocol_version, |
25 HttpResponseInfo* response) { | 40 HttpResponseInfo* response) { |
26 std::string status_key = (protocol_version >= SPDY3) ? ":status" : "status"; | 41 std::string status_key = (protocol_version >= SPDY3) ? ":status" : "status"; |
27 std::string version_key = | 42 std::string version_key = |
28 (protocol_version >= SPDY3) ? ":version" : "version"; | 43 (protocol_version >= SPDY3) ? ":version" : "version"; |
29 std::string version; | 44 std::string version; |
30 std::string status; | 45 std::string status; |
31 | 46 |
32 // The "status" header is required. "version" is required below SPDY4. | 47 // The "status" header is required. "version" is required below SPDY4. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 raw_headers.push_back('\0'); | 91 raw_headers.push_back('\0'); |
77 start = end + 1; | 92 start = end + 1; |
78 } while (end != value.npos); | 93 } while (end != value.npos); |
79 } | 94 } |
80 | 95 |
81 response->headers = new HttpResponseHeaders(raw_headers); | 96 response->headers = new HttpResponseHeaders(raw_headers); |
82 response->was_fetched_via_spdy = true; | 97 response->was_fetched_via_spdy = true; |
83 return true; | 98 return true; |
84 } | 99 } |
85 | 100 |
101 void CreateSpdyHeadersFromHttpResponse( | |
102 scoped_refptr<HttpResponseHeaders> response_headers, | |
103 SpdyHeaderBlock* headers, | |
104 SpdyMajorVersion protocol_version) { | |
105 std::string status_key = (protocol_version >= SPDY3) ? ":status" : "status"; | |
106 std::string version_key = | |
107 (protocol_version >= SPDY3) ? ":version" : "version"; | |
108 | |
109 std::string status_line = response_headers->GetStatusLine(); | |
110 std::string::iterator after_version = | |
111 std::find(status_line.begin(), status_line.end(), ' '); | |
112 if (protocol_version < SPDY4) { | |
113 (*headers)[version_key] = std::string(status_line.begin(), after_version); | |
114 } | |
115 (*headers)[status_key] = std::string(after_version + 1, status_line.end()); | |
116 | |
117 void* iter = NULL; | |
118 std::string raw_name, value; | |
119 while (response_headers->EnumerateHeaderLines(&iter, &raw_name, &value)) { | |
120 std::string name = StringToLowerASCII(raw_name); | |
121 AddSpdyHeader(headers, name, value); | |
122 } | |
123 } | |
124 | |
86 void CreateSpdyHeadersFromHttpRequest(const HttpRequestInfo& info, | 125 void CreateSpdyHeadersFromHttpRequest(const HttpRequestInfo& info, |
87 const HttpRequestHeaders& request_headers, | 126 const HttpRequestHeaders& request_headers, |
88 SpdyHeaderBlock* headers, | 127 SpdyHeaderBlock* headers, |
89 SpdyMajorVersion protocol_version, | 128 SpdyMajorVersion protocol_version, |
90 bool direct) { | 129 bool direct) { |
91 | 130 |
92 HttpRequestHeaders::Iterator it(request_headers); | 131 HttpRequestHeaders::Iterator it(request_headers); |
93 while (it.GetNext()) { | 132 while (it.GetNext()) { |
94 std::string name = StringToLowerASCII(it.name()); | 133 std::string name = StringToLowerASCII(it.name()); |
95 if (name == "connection" || name == "proxy-connection" || | 134 if (name == "connection" || name == "proxy-connection" || |
96 name == "transfer-encoding" || name == "host") { | 135 name == "transfer-encoding" || name == "host") { |
97 continue; | 136 continue; |
98 } | 137 } |
99 if (headers->find(name) == headers->end()) { | 138 AddSpdyHeader(headers, name, it.value()); |
100 (*headers)[name] = it.value(); | |
101 } else { | |
102 std::string new_value = (*headers)[name]; | |
103 new_value.append(1, '\0'); // +=() doesn't append 0's | |
104 new_value += it.value(); | |
105 (*headers)[name] = new_value; | |
106 } | |
107 } | 139 } |
108 static const char kHttpProtocolVersion[] = "HTTP/1.1"; | 140 static const char kHttpProtocolVersion[] = "HTTP/1.1"; |
109 | 141 |
110 if (protocol_version < SPDY3) { | 142 if (protocol_version < SPDY3) { |
111 (*headers)["version"] = kHttpProtocolVersion; | 143 (*headers)["version"] = kHttpProtocolVersion; |
112 (*headers)["method"] = info.method; | 144 (*headers)["method"] = info.method; |
113 (*headers)["host"] = GetHostAndOptionalPort(info.url); | 145 (*headers)["host"] = GetHostAndOptionalPort(info.url); |
114 (*headers)["scheme"] = info.url.scheme(); | 146 (*headers)["scheme"] = info.url.scheme(); |
115 if (direct) | 147 if (direct) |
116 (*headers)["url"] = HttpUtil::PathForRequest(info.url); | 148 (*headers)["url"] = HttpUtil::PathForRequest(info.url); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 if (it != headers.end()) | 225 if (it != headers.end()) |
194 path = it->second; | 226 path = it->second; |
195 | 227 |
196 std::string url = (scheme.empty() || host_port.empty() || path.empty()) | 228 std::string url = (scheme.empty() || host_port.empty() || path.empty()) |
197 ? std::string() | 229 ? std::string() |
198 : scheme + "://" + host_port + path; | 230 : scheme + "://" + host_port + path; |
199 return GURL(url); | 231 return GURL(url); |
200 } | 232 } |
201 | 233 |
202 } // namespace net | 234 } // namespace net |
OLD | NEW |