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 (*headers)[name] += '\0' + value; |
| 31 } |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
23 bool SpdyHeadersToHttpResponse(const SpdyHeaderBlock& headers, | 36 bool SpdyHeadersToHttpResponse(const SpdyHeaderBlock& headers, |
24 SpdyMajorVersion protocol_version, | 37 SpdyMajorVersion protocol_version, |
25 HttpResponseInfo* response) { | 38 HttpResponseInfo* response) { |
26 std::string status_key = (protocol_version >= SPDY3) ? ":status" : "status"; | 39 std::string status_key = (protocol_version >= SPDY3) ? ":status" : "status"; |
27 std::string version_key = | 40 std::string version_key = |
28 (protocol_version >= SPDY3) ? ":version" : "version"; | 41 (protocol_version >= SPDY3) ? ":version" : "version"; |
29 std::string version; | 42 std::string version; |
30 std::string status; | 43 std::string status; |
31 | 44 |
32 // The "status" header is required. "version" is required below SPDY4. | 45 // 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'); | 89 raw_headers.push_back('\0'); |
77 start = end + 1; | 90 start = end + 1; |
78 } while (end != value.npos); | 91 } while (end != value.npos); |
79 } | 92 } |
80 | 93 |
81 response->headers = new HttpResponseHeaders(raw_headers); | 94 response->headers = new HttpResponseHeaders(raw_headers); |
82 response->was_fetched_via_spdy = true; | 95 response->was_fetched_via_spdy = true; |
83 return true; | 96 return true; |
84 } | 97 } |
85 | 98 |
| 99 void CreateSpdyHeadersFromHttpResponse( |
| 100 scoped_refptr<HttpResponseHeaders> response_headers, |
| 101 SpdyHeaderBlock* headers, |
| 102 SpdyMajorVersion protocol_version) { |
| 103 std::string status_key = (protocol_version >= SPDY3) ? ":status" : "status"; |
| 104 std::string version_key = |
| 105 (protocol_version >= SPDY3) ? ":version" : "version"; |
| 106 |
| 107 std::string status_line = response_headers->GetStatusLine(); |
| 108 std::string::iterator after_version = |
| 109 std::find(status_line.begin(), status_line.end(), ' '); |
| 110 if (protocol_version < SPDY4) { |
| 111 (*headers)[version_key] = std::string(status_line.begin(), after_version); |
| 112 } |
| 113 (*headers)[status_key] = std::string(after_version + 1, status_line.end()); |
| 114 |
| 115 void* iter = NULL; |
| 116 std::string raw_name, value; |
| 117 while (response_headers->EnumerateHeaderLines(&iter, &raw_name, &value)) { |
| 118 std::string name = StringToLowerASCII(raw_name); |
| 119 AddSpdyHeader(headers, name, value); |
| 120 } |
| 121 } |
| 122 |
86 void CreateSpdyHeadersFromHttpRequest(const HttpRequestInfo& info, | 123 void CreateSpdyHeadersFromHttpRequest(const HttpRequestInfo& info, |
87 const HttpRequestHeaders& request_headers, | 124 const HttpRequestHeaders& request_headers, |
88 SpdyHeaderBlock* headers, | 125 SpdyHeaderBlock* headers, |
89 SpdyMajorVersion protocol_version, | 126 SpdyMajorVersion protocol_version, |
90 bool direct) { | 127 bool direct) { |
91 | 128 |
92 HttpRequestHeaders::Iterator it(request_headers); | 129 HttpRequestHeaders::Iterator it(request_headers); |
93 while (it.GetNext()) { | 130 while (it.GetNext()) { |
94 std::string name = StringToLowerASCII(it.name()); | 131 std::string name = StringToLowerASCII(it.name()); |
95 if (name == "connection" || name == "proxy-connection" || | 132 if (name == "connection" || name == "proxy-connection" || |
96 name == "transfer-encoding" || name == "host") { | 133 name == "transfer-encoding" || name == "host") { |
97 continue; | 134 continue; |
98 } | 135 } |
99 if (headers->find(name) == headers->end()) { | 136 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 } | 137 } |
108 static const char kHttpProtocolVersion[] = "HTTP/1.1"; | 138 static const char kHttpProtocolVersion[] = "HTTP/1.1"; |
109 | 139 |
110 if (protocol_version < SPDY3) { | 140 if (protocol_version < SPDY3) { |
111 (*headers)["version"] = kHttpProtocolVersion; | 141 (*headers)["version"] = kHttpProtocolVersion; |
112 (*headers)["method"] = info.method; | 142 (*headers)["method"] = info.method; |
113 (*headers)["host"] = GetHostAndOptionalPort(info.url); | 143 (*headers)["host"] = GetHostAndOptionalPort(info.url); |
114 (*headers)["scheme"] = info.url.scheme(); | 144 (*headers)["scheme"] = info.url.scheme(); |
115 if (direct) | 145 if (direct) |
116 (*headers)["url"] = HttpUtil::PathForRequest(info.url); | 146 (*headers)["url"] = HttpUtil::PathForRequest(info.url); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 if (it != headers.end()) | 223 if (it != headers.end()) |
194 path = it->second; | 224 path = it->second; |
195 | 225 |
196 std::string url = (scheme.empty() || host_port.empty() || path.empty()) | 226 std::string url = (scheme.empty() || host_port.empty() || path.empty()) |
197 ? std::string() | 227 ? std::string() |
198 : scheme + "://" + host_port + path; | 228 : scheme + "://" + host_port + path; |
199 return GURL(url); | 229 return GURL(url); |
200 } | 230 } |
201 | 231 |
202 } // namespace net | 232 } // namespace net |
OLD | NEW |