| 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> | |
| 8 | |
| 9 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
| 11 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 12 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 13 #include "net/base/escape.h" | 11 #include "net/base/escape.h" |
| 14 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
| 15 #include "net/base/url_util.h" | 13 #include "net/base/url_util.h" |
| 16 #include "net/http/http_request_headers.h" | 14 #include "net/http/http_request_headers.h" |
| 17 #include "net/http/http_request_info.h" | 15 #include "net/http/http_request_info.h" |
| 18 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
| 19 #include "net/http/http_response_info.h" | 17 #include "net/http/http_response_info.h" |
| 20 #include "net/http/http_util.h" | 18 #include "net/http/http_util.h" |
| 19 #include "net/spdy/platform/api/spdy_string.h" |
| 21 #include "net/spdy/platform/api/spdy_string_piece.h" | 20 #include "net/spdy/platform/api/spdy_string_piece.h" |
| 22 | 21 |
| 23 namespace net { | 22 namespace net { |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 void AddSpdyHeader(const std::string& name, | 26 void AddSpdyHeader(const SpdyString& name, |
| 28 const std::string& value, | 27 const SpdyString& value, |
| 29 SpdyHeaderBlock* headers) { | 28 SpdyHeaderBlock* headers) { |
| 30 if (headers->find(name) == headers->end()) { | 29 if (headers->find(name) == headers->end()) { |
| 31 (*headers)[name] = value; | 30 (*headers)[name] = value; |
| 32 } else { | 31 } else { |
| 33 std::string joint_value = (*headers)[name].as_string(); | 32 SpdyString joint_value = (*headers)[name].as_string(); |
| 34 joint_value.append(1, '\0'); | 33 joint_value.append(1, '\0'); |
| 35 joint_value.append(value); | 34 joint_value.append(value); |
| 36 (*headers)[name] = joint_value; | 35 (*headers)[name] = joint_value; |
| 37 } | 36 } |
| 38 } | 37 } |
| 39 | 38 |
| 40 } // namespace | 39 } // namespace |
| 41 | 40 |
| 42 bool SpdyHeadersToHttpResponse(const SpdyHeaderBlock& headers, | 41 bool SpdyHeadersToHttpResponse(const SpdyHeaderBlock& headers, |
| 43 HttpResponseInfo* response) { | 42 HttpResponseInfo* response) { |
| 44 // The ":status" header is required. | 43 // The ":status" header is required. |
| 45 SpdyHeaderBlock::const_iterator it = headers.find(":status"); | 44 SpdyHeaderBlock::const_iterator it = headers.find(":status"); |
| 46 if (it == headers.end()) | 45 if (it == headers.end()) |
| 47 return false; | 46 return false; |
| 48 std::string status = it->second.as_string(); | 47 SpdyString status = it->second.as_string(); |
| 49 std::string raw_headers("HTTP/1.1 "); | 48 SpdyString raw_headers("HTTP/1.1 "); |
| 50 raw_headers.append(status); | 49 raw_headers.append(status); |
| 51 raw_headers.push_back('\0'); | 50 raw_headers.push_back('\0'); |
| 52 for (it = headers.begin(); it != headers.end(); ++it) { | 51 for (it = headers.begin(); it != headers.end(); ++it) { |
| 53 // For each value, if the server sends a NUL-separated | 52 // For each value, if the server sends a NUL-separated |
| 54 // list of values, we separate that back out into | 53 // list of values, we separate that back out into |
| 55 // individual headers for each value in the list. | 54 // individual headers for each value in the list. |
| 56 // e.g. | 55 // e.g. |
| 57 // Set-Cookie "foo\0bar" | 56 // Set-Cookie "foo\0bar" |
| 58 // becomes | 57 // becomes |
| 59 // Set-Cookie: foo\0 | 58 // Set-Cookie: foo\0 |
| 60 // Set-Cookie: bar\0 | 59 // Set-Cookie: bar\0 |
| 61 std::string value = it->second.as_string(); | 60 SpdyString value = it->second.as_string(); |
| 62 size_t start = 0; | 61 size_t start = 0; |
| 63 size_t end = 0; | 62 size_t end = 0; |
| 64 do { | 63 do { |
| 65 end = value.find('\0', start); | 64 end = value.find('\0', start); |
| 66 std::string tval; | 65 SpdyString tval; |
| 67 if (end != value.npos) | 66 if (end != value.npos) |
| 68 tval = value.substr(start, (end - start)); | 67 tval = value.substr(start, (end - start)); |
| 69 else | 68 else |
| 70 tval = value.substr(start); | 69 tval = value.substr(start); |
| 71 if (it->first[0] == ':') | 70 if (it->first[0] == ':') |
| 72 raw_headers.append(it->first.as_string().substr(1)); | 71 raw_headers.append(it->first.as_string().substr(1)); |
| 73 else | 72 else |
| 74 raw_headers.append(it->first.as_string()); | 73 raw_headers.append(it->first.as_string()); |
| 75 raw_headers.push_back(':'); | 74 raw_headers.push_back(':'); |
| 76 raw_headers.append(tval); | 75 raw_headers.append(tval); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 92 if (info.method == "CONNECT") { | 91 if (info.method == "CONNECT") { |
| 93 (*headers)[":authority"] = GetHostAndPort(info.url); | 92 (*headers)[":authority"] = GetHostAndPort(info.url); |
| 94 } else { | 93 } else { |
| 95 (*headers)[":authority"] = GetHostAndOptionalPort(info.url); | 94 (*headers)[":authority"] = GetHostAndOptionalPort(info.url); |
| 96 (*headers)[":scheme"] = info.url.scheme(); | 95 (*headers)[":scheme"] = info.url.scheme(); |
| 97 (*headers)[":path"] = info.url.PathForRequest(); | 96 (*headers)[":path"] = info.url.PathForRequest(); |
| 98 } | 97 } |
| 99 | 98 |
| 100 HttpRequestHeaders::Iterator it(request_headers); | 99 HttpRequestHeaders::Iterator it(request_headers); |
| 101 while (it.GetNext()) { | 100 while (it.GetNext()) { |
| 102 std::string name = base::ToLowerASCII(it.name()); | 101 SpdyString name = base::ToLowerASCII(it.name()); |
| 103 if (name.empty() || name[0] == ':' || name == "connection" || | 102 if (name.empty() || name[0] == ':' || name == "connection" || |
| 104 name == "proxy-connection" || name == "transfer-encoding" || | 103 name == "proxy-connection" || name == "transfer-encoding" || |
| 105 name == "host") { | 104 name == "host") { |
| 106 continue; | 105 continue; |
| 107 } | 106 } |
| 108 AddSpdyHeader(name, it.value(), headers); | 107 AddSpdyHeader(name, it.value(), headers); |
| 109 } | 108 } |
| 110 } | 109 } |
| 111 | 110 |
| 112 static_assert(HIGHEST - LOWEST < 4 && HIGHEST - MINIMUM_PRIORITY < 6, | 111 static_assert(HIGHEST - LOWEST < 4 && HIGHEST - MINIMUM_PRIORITY < 6, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 143 for (const auto& value : values) { | 142 for (const auto& value : values) { |
| 144 http_headers->SetHeader(key, value); | 143 http_headers->SetHeader(key, value); |
| 145 } | 144 } |
| 146 } | 145 } |
| 147 } | 146 } |
| 148 | 147 |
| 149 GURL GetUrlFromHeaderBlock(const SpdyHeaderBlock& headers) { | 148 GURL GetUrlFromHeaderBlock(const SpdyHeaderBlock& headers) { |
| 150 SpdyHeaderBlock::const_iterator it = headers.find(":scheme"); | 149 SpdyHeaderBlock::const_iterator it = headers.find(":scheme"); |
| 151 if (it == headers.end()) | 150 if (it == headers.end()) |
| 152 return GURL(); | 151 return GURL(); |
| 153 std::string url = it->second.as_string(); | 152 SpdyString url = it->second.as_string(); |
| 154 url.append("://"); | 153 url.append("://"); |
| 155 | 154 |
| 156 it = headers.find(":authority"); | 155 it = headers.find(":authority"); |
| 157 if (it == headers.end()) | 156 if (it == headers.end()) |
| 158 return GURL(); | 157 return GURL(); |
| 159 url.append(it->second.as_string()); | 158 url.append(it->second.as_string()); |
| 160 | 159 |
| 161 it = headers.find(":path"); | 160 it = headers.find(":path"); |
| 162 if (it == headers.end()) | 161 if (it == headers.end()) |
| 163 return GURL(); | 162 return GURL(); |
| 164 url.append(it->second.as_string()); | 163 url.append(it->second.as_string()); |
| 165 return GURL(url); | 164 return GURL(url); |
| 166 } | 165 } |
| 167 | 166 |
| 168 } // namespace net | 167 } // namespace net |
| OLD | NEW |