Chromium Code Reviews| 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/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 #include "net/base/escape.h" | |
| 12 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
| 13 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
| 14 #include "net/http/http_request_headers.h" | 15 #include "net/http/http_request_headers.h" |
| 15 #include "net/http/http_request_info.h" | 16 #include "net/http/http_request_info.h" |
| 16 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
| 17 #include "net/http/http_response_info.h" | 18 #include "net/http/http_response_info.h" |
| 18 #include "net/http/http_util.h" | 19 #include "net/http/http_util.h" |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 21 | 22 |
| 22 bool SpdyHeadersToHttpResponse(const SpdyHeaderBlock& headers, | 23 bool SpdyHeadersToHttpResponse(const SpdyHeaderBlock& headers, |
| 24 int protocol_version, | |
| 23 HttpResponseInfo* response) { | 25 HttpResponseInfo* response) { |
| 26 std::string status_key = (protocol_version >= 3) ? ":status" : "status"; | |
| 27 std::string version_key = (protocol_version >= 3) ? ":version" : "version"; | |
| 24 std::string version; | 28 std::string version; |
| 25 std::string status; | 29 std::string status; |
| 26 | 30 |
| 27 // The "status" and "version" headers are required. | 31 // The "status" and "version" headers are required. |
| 28 SpdyHeaderBlock::const_iterator it; | 32 SpdyHeaderBlock::const_iterator it; |
| 29 it = headers.find("status"); | 33 it = headers.find(status_key); |
| 30 if (it == headers.end()) | 34 if (it == headers.end()) |
| 31 return false; | 35 return false; |
| 32 status = it->second; | 36 status = it->second; |
| 33 | 37 |
| 34 // Grab the version. If not provided by the server, | 38 // Grab the version. If not provided by the server, |
| 35 it = headers.find("version"); | 39 it = headers.find(version_key); |
|
ramant (doing other things)
2012/03/30 22:19:58
nit: Please consider deleting the comment or fixin
Ryan Hamilton
2012/03/30 22:46:02
Done.
| |
| 36 if (it == headers.end()) | 40 if (it == headers.end()) |
| 37 return false; | 41 return false; |
| 38 version = it->second; | 42 version = it->second; |
| 39 | 43 |
| 40 response->response_time = base::Time::Now(); | 44 response->response_time = base::Time::Now(); |
| 41 | 45 |
| 42 std::string raw_headers(version); | 46 std::string raw_headers(version); |
| 43 raw_headers.push_back(' '); | 47 raw_headers.push_back(' '); |
| 44 raw_headers.append(status); | 48 raw_headers.append(status); |
| 45 raw_headers.push_back('\0'); | 49 raw_headers.push_back('\0'); |
| 46 for (it = headers.begin(); it != headers.end(); ++it) { | 50 for (it = headers.begin(); it != headers.end(); ++it) { |
| 47 // For each value, if the server sends a NUL-separated | 51 // For each value, if the server sends a NUL-separated |
| 48 // list of values, we separate that back out into | 52 // list of values, we separate that back out into |
| 49 // individual headers for each value in the list. | 53 // individual headers for each value in the list. |
| 50 // e.g. | 54 // e.g. |
| 51 // Set-Cookie "foo\0bar" | 55 // Set-Cookie "foo\0bar" |
| 52 // becomes | 56 // becomes |
| 53 // Set-Cookie: foo\0 | 57 // Set-Cookie: foo\0 |
| 54 // Set-Cookie: bar\0 | 58 // Set-Cookie: bar\0 |
| 55 std::string value = it->second; | 59 std::string value = it->second; |
| 56 size_t start = 0; | 60 size_t start = 0; |
| 57 size_t end = 0; | 61 size_t end = 0; |
| 58 do { | 62 do { |
| 59 end = value.find('\0', start); | 63 end = value.find('\0', start); |
| 60 std::string tval; | 64 std::string tval; |
| 61 if (end != value.npos) | 65 if (end != value.npos) |
| 62 tval = value.substr(start, (end - start)); | 66 tval = value.substr(start, (end - start)); |
| 63 else | 67 else |
| 64 tval = value.substr(start); | 68 tval = value.substr(start); |
| 65 raw_headers.append(it->first); | 69 if (protocol_version >= 3 && it->first[0] == ':') |
| 70 raw_headers.append(it->first.substr(1)); | |
| 71 else | |
| 72 raw_headers.append(it->first); | |
| 66 raw_headers.push_back(':'); | 73 raw_headers.push_back(':'); |
| 67 raw_headers.append(tval); | 74 raw_headers.append(tval); |
| 68 raw_headers.push_back('\0'); | 75 raw_headers.push_back('\0'); |
| 69 start = end + 1; | 76 start = end + 1; |
| 70 } while (end != value.npos); | 77 } while (end != value.npos); |
| 71 } | 78 } |
| 72 | 79 |
| 73 response->headers = new HttpResponseHeaders(raw_headers); | 80 response->headers = new HttpResponseHeaders(raw_headers); |
| 74 response->was_fetched_via_spdy = true; | 81 response->was_fetched_via_spdy = true; |
| 75 return true; | 82 return true; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 switch (priority) { | 133 switch (priority) { |
| 127 case LOWEST: | 134 case LOWEST: |
| 128 return SPDY_PRIORITY_LOWEST - 1; | 135 return SPDY_PRIORITY_LOWEST - 1; |
| 129 case IDLE: | 136 case IDLE: |
| 130 return SPDY_PRIORITY_LOWEST; | 137 return SPDY_PRIORITY_LOWEST; |
| 131 default: | 138 default: |
| 132 return priority; | 139 return priority; |
| 133 } | 140 } |
| 134 } | 141 } |
| 135 | 142 |
| 143 GURL GetUrlFromHeaderBlock(const SpdyHeaderBlock& headers, | |
| 144 int protocol_version, | |
| 145 bool pushed) { | |
| 146 // SPDY 2 server push urls are specified in an single "url" header. | |
|
ramant (doing other things)
2012/03/30 22:19:58
nit: an single -> a single
Ryan Hamilton
2012/03/30 22:46:02
Done.
| |
| 147 if (pushed && protocol_version == 2) { | |
| 148 // assemble from the response | |
|
ramant (doing other things)
2012/03/30 22:19:58
nit: Consider "Assemble the response from headers.
Ryan Hamilton
2012/03/30 22:46:02
Done.
| |
| 149 std::string url; | |
| 150 SpdyHeaderBlock::const_iterator it; | |
| 151 it = headers.find("url"); | |
| 152 if (it != headers.end()) | |
| 153 url = it->second; | |
| 154 return GURL(url); | |
| 155 } | |
| 156 | |
| 157 const char* scheme_header = protocol_version >= 3 ? ":scheme" : "scheme"; | |
| 158 const char* host_header = protocol_version >= 3 ? ":host" : "host"; | |
| 159 const char* path_header = protocol_version >= 3 ? ":path" : "url"; | |
| 160 | |
| 161 std::string scheme; | |
| 162 std::string host_port; | |
| 163 std::string path; | |
| 164 SpdyHeaderBlock::const_iterator it; | |
| 165 it = headers.find(scheme_header); | |
| 166 if (it != headers.end()) | |
| 167 scheme = it->second; | |
| 168 it = headers.find(host_header); | |
| 169 if (it != headers.end()) | |
| 170 host_port = it->second; | |
| 171 it = headers.find(path_header); | |
| 172 if (it != headers.end()) | |
| 173 path = it->second; | |
| 174 | |
| 175 std::string url = (scheme.empty() || host_port.empty() || path.empty()) | |
| 176 ? "" : scheme + "://" + host_port + path; | |
| 177 return GURL(url); | |
| 178 } | |
| 179 | |
| 136 } // namespace net | 180 } // namespace net |
| OLD | NEW |