From db4f555efb8831ac18dca06f986e7d36c562f812 Mon Sep 17 00:00:00 2001 From: "rch@chromium.org" Date: Wed, 4 Apr 2012 02:47:49 +0000 Subject: Remove crazy c string contortions in spdy_test_util_spdy[23].cc. Review URL: https://chromiumcodereview.appspot.com/9958122 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@130545 0039d316-1c4b-4281-b951-d872f2087c98 --- net/spdy/spdy_test_util_spdy2.cc | 55 ++++++------ net/spdy/spdy_test_util_spdy3.cc | 179 ++++++++++----------------------------- 2 files changed, 68 insertions(+), 166 deletions(-) (limited to 'net/spdy') diff --git a/net/spdy/spdy_test_util_spdy2.cc b/net/spdy/spdy_test_util_spdy2.cc index 30d2f1b..4f2411a 100644 --- a/net/spdy/spdy_test_util_spdy2.cc +++ b/net/spdy/spdy_test_util_spdy2.cc @@ -21,6 +21,24 @@ namespace net { namespace test_spdy2 { +namespace { + +// Parses a URL into the scheme, host, and path components required for a +// SPDY request. +void ParseUrl(const char* const url, std::string* scheme, std::string* host, + std::string* path) { + GURL gurl(url); + path->assign(gurl.PathForRequest()); + scheme->assign(gurl.scheme()); + host->assign(gurl.host()); + if (gurl.has_port()) { + host->append(":"); + host->append(gurl.port()); + } +} + +} // namespace + // Chop a frame into an array of MockWrites. // |data| is the frame to chop. // |length| is the length of the frame to chop. @@ -353,37 +371,14 @@ SpdyFrame* ConstructSpdyGet(const char* const url, DATA_FLAG_NONE // Data Flags }; - GURL gurl(url); - - // This is so ugly. Why are we using char* in here again? - std::string str_path = gurl.PathForRequest(); - std::string str_scheme = gurl.scheme(); - std::string str_host = gurl.host(); - if (gurl.has_port()) { - str_host += ":"; - str_host += gurl.port(); - } - scoped_array req(new char[str_path.size() + 1]); - scoped_array scheme(new char[str_scheme.size() + 1]); - scoped_array host(new char[str_host.size() + 1]); - memcpy(req.get(), str_path.c_str(), str_path.size()); - memcpy(scheme.get(), str_scheme.c_str(), str_scheme.size()); - memcpy(host.get(), str_host.c_str(), str_host.size()); - req.get()[str_path.size()] = '\0'; - scheme.get()[str_scheme.size()] = '\0'; - host.get()[str_host.size()] = '\0'; - + std::string scheme, host, path; + ParseUrl(url, &scheme, &host, &path); const char* const headers[] = { - "method", - "GET", - "url", - req.get(), - "host", - host.get(), - "scheme", - scheme.get(), - "version", - "HTTP/1.1" + "method", "GET", + "url", path.c_str(), + "host", host.c_str(), + "scheme", scheme.c_str(), + "version", "HTTP/1.1" }; return ConstructSpdyPacket( kSynStartHeader, diff --git a/net/spdy/spdy_test_util_spdy3.cc b/net/spdy/spdy_test_util_spdy3.cc index 5646166..fc58945 100644 --- a/net/spdy/spdy_test_util_spdy3.cc +++ b/net/spdy/spdy_test_util_spdy3.cc @@ -21,6 +21,24 @@ namespace net { namespace test_spdy3 { +namespace { + +// Parses a URL into the scheme, host, and path components required for a +// SPDY request. +void ParseUrl(const char* const url, std::string* scheme, std::string* host, + std::string* path) { + GURL gurl(url); + path->assign(gurl.PathForRequest()); + scheme->assign(gurl.scheme()); + host->assign(gurl.host()); + if (gurl.has_port()) { + host->append(":"); + host->append(gurl.port()); + } +} + +} // namespace + // Chop a frame into an array of MockWrites. // |data| is the frame to chop. // |length| is the length of the frame to chop. @@ -356,37 +374,14 @@ SpdyFrame* ConstructSpdyGet(const char* const url, DATA_FLAG_NONE // Data Flags }; - GURL gurl(url); - - // This is so ugly. Why are we using char* in here again? - std::string str_path = gurl.PathForRequest(); - std::string str_scheme = gurl.scheme(); - std::string str_host = gurl.host(); - if (gurl.has_port()) { - str_host += ":"; - str_host += gurl.port(); - } - scoped_array req(new char[str_path.size() + 1]); - scoped_array scheme(new char[str_scheme.size() + 1]); - scoped_array host(new char[str_host.size() + 1]); - memcpy(req.get(), str_path.c_str(), str_path.size()); - memcpy(scheme.get(), str_scheme.c_str(), str_scheme.size()); - memcpy(host.get(), str_host.c_str(), str_host.size()); - req.get()[str_path.size()] = '\0'; - scheme.get()[str_scheme.size()] = '\0'; - host.get()[str_host.size()] = '\0'; - + std::string scheme, host, path; + ParseUrl(url, &scheme, &host, &path); const char* const headers[] = { - ":method", - "GET", - ":path", - req.get(), - ":host", - host.get(), - ":scheme", - scheme.get(), - ":version", - "HTTP/1.1" + ":method", "GET", + ":path", path.c_str(), + ":host", host.c_str(), + ":scheme", scheme.c_str(), + ":version", "HTTP/1.1" }; return ConstructSpdyPacket( kSynStartHeader, @@ -493,38 +488,15 @@ SpdyFrame* ConstructSpdyPush(const char* const extra_headers[], int stream_id, int associated_stream_id, const char* url) { - GURL gurl(url); - - std::string str_path = gurl.PathForRequest(); - std::string str_scheme = gurl.scheme(); - std::string str_host = gurl.host(); - if (gurl.has_port()) { - str_host += ":"; - str_host += gurl.port(); - } - scoped_array req(new char[str_path.size() + 1]); - scoped_array scheme(new char[str_scheme.size() + 1]); - scoped_array host(new char[str_host.size() + 1]); - memcpy(req.get(), str_path.c_str(), str_path.size()); - memcpy(scheme.get(), str_scheme.c_str(), str_scheme.size()); - memcpy(host.get(), str_host.c_str(), str_host.size()); - req.get()[str_path.size()] = '\0'; - scheme.get()[str_scheme.size()] = '\0'; - host.get()[str_host.size()] = '\0'; - + std::string scheme, host, path; + ParseUrl(url, &scheme, &host, &path); const char* const headers[] = { - "hello", - "bye", - ":status", - "200 OK", - ":version", - "HTTP/1.1", - ":path", - req.get(), - ":host", - host.get(), - ":scheme", - scheme.get(), + "hello", "bye", + ":status", "200 OK", + ":version", "HTTP/1.1", + ":path", path.c_str(), + ":host", host.c_str(), + ":scheme", scheme.c_str(), }; return ConstructSpdyControlFrame(extra_headers, extra_header_count, @@ -545,40 +517,16 @@ SpdyFrame* ConstructSpdyPush(const char* const extra_headers[], const char* url, const char* status, const char* location) { - GURL gurl(url); - - std::string str_path = gurl.PathForRequest(); - std::string str_scheme = gurl.scheme(); - std::string str_host = gurl.host(); - if (gurl.has_port()) { - str_host += ":"; - str_host += gurl.port(); - } - scoped_array req(new char[str_path.size() + 1]); - scoped_array scheme(new char[str_scheme.size() + 1]); - scoped_array host(new char[str_host.size() + 1]); - memcpy(req.get(), str_path.c_str(), str_path.size()); - memcpy(scheme.get(), str_scheme.c_str(), str_scheme.size()); - memcpy(host.get(), str_host.c_str(), str_host.size()); - req.get()[str_path.size()] = '\0'; - scheme.get()[str_scheme.size()] = '\0'; - host.get()[str_host.size()] = '\0'; - - const char* const kStandardGetHeaders[] = { - "hello", - "bye", - ":status", - status, - "location", - location, - ":path", - req.get(), - ":host", - host.get(), - ":scheme", - scheme.get(), - ":version", - "HTTP/1.1" + std::string scheme, host, path; + ParseUrl(url, &scheme, &host, &path); + const char* const headers[] = { + "hello", "bye", + ":status", status, + "location", location, + ":path", path.c_str(), + ":host", host.c_str(), + ":scheme", scheme.c_str(), + ":version", "HTTP/1.1" }; return ConstructSpdyControlFrame(extra_headers, extra_header_count, @@ -587,47 +535,6 @@ SpdyFrame* ConstructSpdyPush(const char* const extra_headers[], LOWEST, SYN_STREAM, CONTROL_FLAG_NONE, - kStandardGetHeaders, - arraysize(kStandardGetHeaders), - associated_stream_id); -} - -SpdyFrame* ConstructSpdyPush(int stream_id, - int associated_stream_id, - const char* url) { - GURL gurl(url); - - std::string str_path = gurl.PathForRequest(); - std::string str_scheme = gurl.scheme(); - std::string str_host = gurl.host(); - if (gurl.has_port()) { - str_host += ":"; - str_host += gurl.port(); - } - scoped_array req(new char[str_path.size() + 1]); - scoped_array scheme(new char[str_scheme.size() + 1]); - scoped_array host(new char[str_host.size() + 1]); - memcpy(req.get(), str_path.c_str(), str_path.size()); - memcpy(scheme.get(), str_scheme.c_str(), str_scheme.size()); - memcpy(host.get(), str_host.c_str(), str_host.size()); - req.get()[str_path.size()] = '\0'; - scheme.get()[str_scheme.size()] = '\0'; - host.get()[str_host.size()] = '\0'; - - const char* const headers[] = { - req.get(), - ":host", - host.get(), - ":scheme", - scheme.get(), - }; - return ConstructSpdyControlFrame(0, - 0, - false, - stream_id, - LOWEST, - SYN_STREAM, - CONTROL_FLAG_NONE, headers, arraysize(headers), associated_stream_id); -- cgit v1.1