Use gurl to parse url

Change-Id: I50794206ece70efcdfb5cf8bd720e6f877d99462
diff --git a/mojo/services/network/BUILD.gn b/mojo/services/network/BUILD.gn
index 1c6667e..400dd1d 100644
--- a/mojo/services/network/BUILD.gn
+++ b/mojo/services/network/BUILD.gn
@@ -34,8 +34,6 @@
     "network_service_impl.h",
     "upload_element_reader.cc",
     "upload_element_reader.h",
-    "url.cc",
-    "url.h",
     "url_loader_impl.cc",
     "url_loader_impl.h",
   ]
@@ -59,5 +57,6 @@
     "//third_party/mojo/src/mojo/public/cpp/system",
     "//third_party/mojo/src/mojo/public/cpp/utility",
     "//third_party/mojo/src/mojo/public/interfaces/network",
+    "//url",
   ]
 }
diff --git a/mojo/services/network/url.cc b/mojo/services/network/url.cc
deleted file mode 100644
index 9f55708..0000000
--- a/mojo/services/network/url.cc
+++ /dev/null
@@ -1,76 +0,0 @@
-// Copyright 2016 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "base/logging.h"
-
-#include "mojo/services/network/url.h"
-
-#include <algorithm>
-
-namespace mojo {
-
-URL::URL(const std::string& str) : str_(str) {
-  parsed_ = Parse();
-}
-
-URL::~URL() {}
-
-bool URL::Parse() {
-  std::string delim("://");
-  std::string::const_iterator proto_end =
-    std::search(str_.begin(), str_.end(), delim.begin(), delim.end());
-  if (proto_end == str_.end()) {
-    return false;
-  }
-  proto_.assign(str_.begin(), proto_end);
-
-  std::string::const_iterator host_start = proto_end + delim.length();
-  std::string::const_iterator path_start = std::find(host_start, str_.end(),
-                                                     '/');
-  std::string::const_iterator host_end = std::find(host_start, path_start,
-                                                   ':');
-  host_.assign(host_start, host_end);
-
-  if (host_end != path_start)
-    port_.assign(host_end + 1, path_start);
-  else
-    port_ = proto_;
-
-  if (path_start != str_.end())
-    path_.assign(path_start, str_.end());
-  else
-    path_.assign("/");
-
-  if (proto_.length() == 0 || host_.length() == 0 || port_.length() == 0 ||
-      path_.length() == 0)
-    return false;
-
-  return true;
-}
-
-bool URL::IsParsed() {
-  return parsed_;
-}
-
-std::string& URL::Proto() {
-  DCHECK(parsed_);
-  return proto_;
-}
-
-std::string& URL::Host() {
-  DCHECK(parsed_);
-  return host_;
-}
-
-std::string& URL::Port() {
-  DCHECK(parsed_);
-  return port_;
-}
-
-std::string& URL::Path() {
-  DCHECK(parsed_);
-  return path_;
-}
-
-} // namespace mojo
diff --git a/mojo/services/network/url.h b/mojo/services/network/url.h
deleted file mode 100644
index 683bc43..0000000
--- a/mojo/services/network/url.h
+++ /dev/null
@@ -1,38 +0,0 @@
-// Copyright 2016 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef MOJO_SERVICES_NETWORK_URL_H_
-#define MOJO_SERVICES_NETWORK_URL_H_
-
-#include <string>
-
-namespace mojo {
-
-class URL {
- public:
-  URL(const std::string& str);
-  ~URL();
-
-  bool IsParsed();
-
-  std::string& Proto();
-  std::string& Host();
-  std::string& Port();
-  std::string& Path();
-
- private:
-  const std::string str_;
-
-  bool Parse();
-
-  bool parsed_;
-  std::string proto_;
-  std::string host_;
-  std::string port_;
-  std::string path_;
-};
-
-}  // namespace mojo
-
-#endif  // MOJO_SERVICES_NETWORK_URL_H_
diff --git a/mojo/services/network/url_loader_impl.cc b/mojo/services/network/url_loader_impl.cc
index 5c0917c..ded1a21 100644
--- a/mojo/services/network/url_loader_impl.cc
+++ b/mojo/services/network/url_loader_impl.cc
@@ -9,6 +9,8 @@
 #include "mojo/services/network/net_errors.h"
 #include "mojo/services/network/net_adapters.h"
 
+#include "url/gurl.h"
+
 #include <istream>
 #include <ostream>
 #include <string>
@@ -93,8 +95,8 @@
   asio::io_service io_service;
   bool redirect = false;
 
-  std::unique_ptr<URL> url(new URL(url_str));
-  if (!url->IsParsed()) {
+  GURL url(url_str);
+  if (!url.is_valid()) {
     LOG(ERROR) << "url parse error";
     SendError(net::ERR_INVALID_ARGUMENT);
     return;
@@ -106,25 +108,25 @@
       redirect = false;
     }
 
-    if (url->Proto() == "https") {
+    if (url.SchemeIs("https")) {
 #ifdef NETWORK_SERVICE_USE_HTTPS
       asio::ssl::context ctx(asio::ssl::context::sslv23);
       ctx.set_default_verify_paths();
 
       HTTPClient<asio::ssl::stream<tcp::socket>> c(this, io_service, ctx);
-      MojoResult result = c.CreateRequest(url->Host(), url->Path(), method,
+      MojoResult result = c.CreateRequest(url.host(), url.path(), method,
                                           extra_headers, element_readers);
       if (result != MOJO_RESULT_OK) {
         SendError(net::ERR_INVALID_ARGUMENT);
         break;
       }
-      c.Start(url->Host(), url->Port());
+      c.Start(url.host(), url.has_port() ? url.port() : "https");
       io_service.run();
 
       if (c.status_code_ == 301 || c.status_code_ == 302) {
         redirect = true;
-        url.reset(new URL(c.redirect_location_));
-        if (!url->IsParsed()) {
+        url = GURL(c.redirect_location_);
+        if (!url.is_valid()) {
           LOG(ERROR) << "url parse error";
           SendError(net::ERR_INVALID_RESPONSE);
           break;
@@ -136,21 +138,21 @@
       SendError(net::ERR_INVALID_ARGUMENT);
       break;
 #endif
-    } else if (url->Proto() == "http") {
+    } else if (url.SchemeIs("http")) {
       HTTPClient<tcp::socket> c(this, io_service);
-      MojoResult result = c.CreateRequest(url->Host(), url->Path(), method,
+      MojoResult result = c.CreateRequest(url.host(), url.path(), method,
                                           extra_headers, element_readers);
       if (result != MOJO_RESULT_OK) {
         SendError(net::ERR_INVALID_ARGUMENT);
         break;
       }
-      c.Start(url->Host(), url->Port());
+      c.Start(url.host(), url.has_port() ? url.port() : "http");
       io_service.run();
 
       if (c.status_code_ == 301 || c.status_code_ == 302) {
         redirect = true;
-        url.reset(new URL(c.redirect_location_));
-        if (!url->IsParsed()) {
+        url = GURL(c.redirect_location_);
+        if (!url.is_valid()) {
           LOG(ERROR) << "url parse error";
           SendError(net::ERR_INVALID_RESPONSE);
           break;
diff --git a/mojo/services/network/url_loader_impl.h b/mojo/services/network/url_loader_impl.h
index 222479c..4af9b50 100644
--- a/mojo/services/network/url_loader_impl.h
+++ b/mojo/services/network/url_loader_impl.h
@@ -9,8 +9,6 @@
 #include "mojo/services/network/interfaces/url_loader.mojom.h"
 #include "mojo/public/cpp/bindings/binding.h"
 
-#include "mojo/services/network/url.h"
-
 namespace mojo {
 
 class URLLoaderImpl : public URLLoader {