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" | 17 #include "net/http/http_response_headers.h" |
18 #include "net/http/http_response_info.h" | 18 #include "net/http/http_response_info.h" |
19 #include "net/http/http_util.h" | 19 #include "net/http/http_util.h" |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 | 22 |
23 bool SpdyHeadersToHttpResponse(const SpdyHeaderBlock& headers, | 23 bool SpdyHeadersToHttpResponse(const SpdyHeaderBlock& headers, |
24 int protocol_version, | 24 SpdyMajorVersion protocol_version, |
25 HttpResponseInfo* response) { | 25 HttpResponseInfo* response) { |
26 std::string status_key = (protocol_version >= 3) ? ":status" : "status"; | 26 std::string status_key = |
27 std::string version_key = (protocol_version >= 3) ? ":version" : "version"; | 27 (protocol_version >= SPDY3) ? ":status" : "status"; |
| 28 std::string version_key = |
| 29 (protocol_version >= SPDY3) ? ":version" : "version"; |
28 std::string version; | 30 std::string version; |
29 std::string status; | 31 std::string status; |
30 | 32 |
31 // The "status" and "version" headers are required. | 33 // The "status" (and "version" headers for SPDY3 and below) are required. |
32 SpdyHeaderBlock::const_iterator it; | 34 SpdyHeaderBlock::const_iterator it; |
33 it = headers.find(status_key); | 35 it = headers.find(status_key); |
34 if (it == headers.end()) | 36 if (it == headers.end()) |
35 return false; | 37 return false; |
36 status = it->second; | 38 status = it->second; |
37 | 39 |
38 it = headers.find(version_key); | 40 if (protocol_version < SPDY4) { |
39 if (it == headers.end()) | 41 it = headers.find(version_key); |
40 return false; | 42 if (it == headers.end()) |
41 version = it->second; | 43 return false; |
| 44 version = it->second; |
| 45 } else { |
| 46 version = "HTTP/1.1"; |
| 47 } |
42 | 48 |
43 std::string raw_headers(version); | 49 std::string raw_headers(version); |
44 raw_headers.push_back(' '); | 50 raw_headers.push_back(' '); |
45 raw_headers.append(status); | 51 raw_headers.append(status); |
46 raw_headers.push_back('\0'); | 52 raw_headers.push_back('\0'); |
47 for (it = headers.begin(); it != headers.end(); ++it) { | 53 for (it = headers.begin(); it != headers.end(); ++it) { |
48 // For each value, if the server sends a NUL-separated | 54 // For each value, if the server sends a NUL-separated |
49 // list of values, we separate that back out into | 55 // list of values, we separate that back out into |
50 // individual headers for each value in the list. | 56 // individual headers for each value in the list. |
51 // e.g. | 57 // e.g. |
52 // Set-Cookie "foo\0bar" | 58 // Set-Cookie "foo\0bar" |
53 // becomes | 59 // becomes |
54 // Set-Cookie: foo\0 | 60 // Set-Cookie: foo\0 |
55 // Set-Cookie: bar\0 | 61 // Set-Cookie: bar\0 |
56 std::string value = it->second; | 62 std::string value = it->second; |
57 size_t start = 0; | 63 size_t start = 0; |
58 size_t end = 0; | 64 size_t end = 0; |
59 do { | 65 do { |
60 end = value.find('\0', start); | 66 end = value.find('\0', start); |
61 std::string tval; | 67 std::string tval; |
62 if (end != value.npos) | 68 if (end != value.npos) |
63 tval = value.substr(start, (end - start)); | 69 tval = value.substr(start, (end - start)); |
64 else | 70 else |
65 tval = value.substr(start); | 71 tval = value.substr(start); |
66 if (protocol_version >= 3 && it->first[0] == ':') | 72 if (protocol_version >= SPDY3 && it->first[0] == ':') |
67 raw_headers.append(it->first.substr(1)); | 73 raw_headers.append(it->first.substr(1)); |
68 else | 74 else |
69 raw_headers.append(it->first); | 75 raw_headers.append(it->first); |
70 raw_headers.push_back(':'); | 76 raw_headers.push_back(':'); |
71 raw_headers.append(tval); | 77 raw_headers.append(tval); |
72 raw_headers.push_back('\0'); | 78 raw_headers.push_back('\0'); |
73 start = end + 1; | 79 start = end + 1; |
74 } while (end != value.npos); | 80 } while (end != value.npos); |
75 } | 81 } |
76 | 82 |
77 response->headers = new HttpResponseHeaders(raw_headers); | 83 response->headers = new HttpResponseHeaders(raw_headers); |
78 response->was_fetched_via_spdy = true; | 84 response->was_fetched_via_spdy = true; |
79 return true; | 85 return true; |
80 } | 86 } |
81 | 87 |
82 void CreateSpdyHeadersFromHttpRequest(const HttpRequestInfo& info, | 88 void CreateSpdyHeadersFromHttpRequest(const HttpRequestInfo& info, |
83 const HttpRequestHeaders& request_headers, | 89 const HttpRequestHeaders& request_headers, |
84 SpdyHeaderBlock* headers, | 90 SpdyHeaderBlock* headers, |
85 int protocol_version, | 91 SpdyMajorVersion protocol_version, |
86 bool direct) { | 92 bool direct) { |
87 | 93 |
88 HttpRequestHeaders::Iterator it(request_headers); | 94 HttpRequestHeaders::Iterator it(request_headers); |
89 while (it.GetNext()) { | 95 while (it.GetNext()) { |
90 std::string name = StringToLowerASCII(it.name()); | 96 std::string name = StringToLowerASCII(it.name()); |
91 if (name == "connection" || name == "proxy-connection" || | 97 if (name == "connection" || name == "proxy-connection" || |
92 name == "transfer-encoding") { | 98 name == "transfer-encoding") { |
93 continue; | 99 continue; |
94 } | 100 } |
95 if (headers->find(name) == headers->end()) { | 101 if (headers->find(name) == headers->end()) { |
96 (*headers)[name] = it.value(); | 102 (*headers)[name] = it.value(); |
97 } else { | 103 } else { |
98 std::string new_value = (*headers)[name]; | 104 std::string new_value = (*headers)[name]; |
99 new_value.append(1, '\0'); // +=() doesn't append 0's | 105 new_value.append(1, '\0'); // +=() doesn't append 0's |
100 new_value += it.value(); | 106 new_value += it.value(); |
101 (*headers)[name] = new_value; | 107 (*headers)[name] = new_value; |
102 } | 108 } |
103 } | 109 } |
104 static const char kHttpProtocolVersion[] = "HTTP/1.1"; | 110 static const char kHttpProtocolVersion[] = "HTTP/1.1"; |
105 | 111 |
106 if (protocol_version < 3) { | 112 if (protocol_version < SPDY3) { |
107 (*headers)["version"] = kHttpProtocolVersion; | 113 (*headers)["version"] = kHttpProtocolVersion; |
108 (*headers)["method"] = info.method; | 114 (*headers)["method"] = info.method; |
109 (*headers)["host"] = GetHostAndOptionalPort(info.url); | 115 (*headers)["host"] = GetHostAndOptionalPort(info.url); |
110 (*headers)["scheme"] = info.url.scheme(); | 116 (*headers)["scheme"] = info.url.scheme(); |
111 if (direct) | 117 if (direct) |
112 (*headers)["url"] = HttpUtil::PathForRequest(info.url); | 118 (*headers)["url"] = HttpUtil::PathForRequest(info.url); |
113 else | 119 else |
114 (*headers)["url"] = HttpUtil::SpecForRequest(info.url); | 120 (*headers)["url"] = HttpUtil::SpecForRequest(info.url); |
115 } else { | 121 } else { |
116 (*headers)[":version"] = kHttpProtocolVersion; | 122 // Don't send :version for HTTP/2. |
117 (*headers)[":method"] = info.method; | 123 (*headers)[":method"] = info.method; |
118 (*headers)[":host"] = GetHostAndOptionalPort(info.url); | 124 (*headers)[":host"] = GetHostAndOptionalPort(info.url); |
119 (*headers)[":scheme"] = info.url.scheme(); | 125 (*headers)[":scheme"] = info.url.scheme(); |
120 (*headers)[":path"] = HttpUtil::PathForRequest(info.url); | 126 (*headers)[":path"] = HttpUtil::PathForRequest(info.url); |
121 headers->erase("host"); // this is kinda insane, spdy 3 spec. | 127 headers->erase("host"); // this is kinda insane, spdy 3 spec. |
122 } | 128 } |
123 | 129 |
124 } | 130 } |
125 | 131 |
126 COMPILE_ASSERT(HIGHEST - LOWEST < 4 && | 132 COMPILE_ASSERT(HIGHEST - LOWEST < 4 && |
127 HIGHEST - MINIMUM_PRIORITY < 5, | 133 HIGHEST - MINIMUM_PRIORITY < 5, |
128 request_priority_incompatible_with_spdy); | 134 request_priority_incompatible_with_spdy); |
129 | 135 |
130 SpdyPriority ConvertRequestPriorityToSpdyPriority( | 136 SpdyPriority ConvertRequestPriorityToSpdyPriority( |
131 const RequestPriority priority, | 137 const RequestPriority priority, |
132 int protocol_version) { | 138 SpdyMajorVersion protocol_version) { |
133 DCHECK_GE(priority, MINIMUM_PRIORITY); | 139 DCHECK_GE(priority, MINIMUM_PRIORITY); |
134 DCHECK_LT(priority, NUM_PRIORITIES); | 140 DCHECK_LT(priority, NUM_PRIORITIES); |
135 if (protocol_version == 2) { | 141 if (protocol_version == SPDY2) { |
136 // SPDY 2 only has 2 bits of priority, but we have 5 RequestPriorities. | 142 // SPDY 2 only has 2 bits of priority, but we have 5 RequestPriorities. |
137 // Map IDLE => 3, LOWEST => 2, LOW => 2, MEDIUM => 1, HIGHEST => 0. | 143 // Map IDLE => 3, LOWEST => 2, LOW => 2, MEDIUM => 1, HIGHEST => 0. |
138 if (priority > LOWEST) { | 144 if (priority > LOWEST) { |
139 return static_cast<SpdyPriority>(HIGHEST - priority); | 145 return static_cast<SpdyPriority>(HIGHEST - priority); |
140 } else { | 146 } else { |
141 return static_cast<SpdyPriority>(HIGHEST - priority - 1); | 147 return static_cast<SpdyPriority>(HIGHEST - priority - 1); |
142 } | 148 } |
143 } else { | 149 } else { |
144 return static_cast<SpdyPriority>(HIGHEST - priority); | 150 return static_cast<SpdyPriority>(HIGHEST - priority); |
145 } | 151 } |
146 } | 152 } |
147 | 153 |
148 NET_EXPORT_PRIVATE RequestPriority ConvertSpdyPriorityToRequestPriority( | 154 NET_EXPORT_PRIVATE RequestPriority ConvertSpdyPriorityToRequestPriority( |
149 SpdyPriority priority, | 155 SpdyPriority priority, |
150 int protocol_version) { | 156 SpdyMajorVersion protocol_version) { |
151 // Handle invalid values gracefully, and pick LOW to map 2 back | 157 // Handle invalid values gracefully, and pick LOW to map 2 back |
152 // to for SPDY/2. | 158 // to for SPDY/2. |
153 SpdyPriority idle_cutoff = (protocol_version == 2) ? 3 : 5; | 159 SpdyPriority idle_cutoff = (protocol_version == SPDY2) ? 3 : 5; |
154 return (priority >= idle_cutoff) ? | 160 return (priority >= idle_cutoff) ? |
155 IDLE : static_cast<RequestPriority>(HIGHEST - priority); | 161 IDLE : static_cast<RequestPriority>(HIGHEST - priority); |
156 } | 162 } |
157 | 163 |
158 GURL GetUrlFromHeaderBlock(const SpdyHeaderBlock& headers, | 164 GURL GetUrlFromHeaderBlock(const SpdyHeaderBlock& headers, |
159 int protocol_version, | 165 SpdyMajorVersion protocol_version, |
160 bool pushed) { | 166 bool pushed) { |
161 // SPDY 2 server push urls are specified in a single "url" header. | 167 // SPDY 2 server push urls are specified in a single "url" header. |
162 if (pushed && protocol_version == 2) { | 168 if (pushed && protocol_version == SPDY2) { |
163 std::string url; | 169 std::string url; |
164 SpdyHeaderBlock::const_iterator it; | 170 SpdyHeaderBlock::const_iterator it; |
165 it = headers.find("url"); | 171 it = headers.find("url"); |
166 if (it != headers.end()) | 172 if (it != headers.end()) |
167 url = it->second; | 173 url = it->second; |
168 return GURL(url); | 174 return GURL(url); |
169 } | 175 } |
170 | 176 |
171 const char* scheme_header = protocol_version >= 3 ? ":scheme" : "scheme"; | 177 const char* scheme_header = protocol_version >= SPDY3 ? ":scheme" : "scheme"; |
172 const char* host_header = protocol_version >= 3 ? ":host" : "host"; | 178 const char* host_header = protocol_version >= SPDY3 ? ":host" : "host"; |
173 const char* path_header = protocol_version >= 3 ? ":path" : "url"; | 179 const char* path_header = protocol_version >= SPDY3 ? ":path" : "url"; |
174 | 180 |
175 std::string scheme; | 181 std::string scheme; |
176 std::string host_port; | 182 std::string host_port; |
177 std::string path; | 183 std::string path; |
178 SpdyHeaderBlock::const_iterator it; | 184 SpdyHeaderBlock::const_iterator it; |
179 it = headers.find(scheme_header); | 185 it = headers.find(scheme_header); |
180 if (it != headers.end()) | 186 if (it != headers.end()) |
181 scheme = it->second; | 187 scheme = it->second; |
182 it = headers.find(host_header); | 188 it = headers.find(host_header); |
183 if (it != headers.end()) | 189 if (it != headers.end()) |
(...skipping 10 matching lines...) Expand all Loading... |
194 | 200 |
195 bool ShouldShowHttpHeaderValue(const std::string& header_name) { | 201 bool ShouldShowHttpHeaderValue(const std::string& header_name) { |
196 #if defined(SPDY_PROXY_AUTH_ORIGIN) | 202 #if defined(SPDY_PROXY_AUTH_ORIGIN) |
197 if (header_name == "proxy-authorization") | 203 if (header_name == "proxy-authorization") |
198 return false; | 204 return false; |
199 #endif | 205 #endif |
200 return true; | 206 return true; |
201 } | 207 } |
202 | 208 |
203 } // namespace net | 209 } // namespace net |
OLD | NEW |