title: Scala: How to parse a URL with custom protocols
body: <p>I need to parse URLs that might contain protocols different than <code>http</code> or <code>https</code>... and since if try to create a <code>java.net.URL</code> object with a URL like <code>nio://localhost:61616</code> the constructor crashes, I've implemented something like this:</p>
  
  <pre><code>def parseURL(spec: String): (String, String, Int, String) = {
    import java.net.URL
  
    var protocol: String = null
  
    val url = spec.split("://") match {
      case parts if parts.length &gt; 1 =&gt;
        protocol = parts(0)
        new URL(if (protocol == "http" || protocol == "https" ) spec else "http://" + parts(1))
      case _ =&gt; new URL("http" + spec.dropWhile(_ == '/'))
    } 
  
    var port = url.getPort; if (port &lt; 0) port = url.getDefaultPort
    (protocol, url.getHost, port, url.getFile)
  }
  </code></pre>
  
  <p>In case a given URL contains a protocol different than <code>http</code> or <code>https</code>, I save it in a variable and then I force <code>http</code> to let <code>java.net.URL</code> parse it without crashing.</p>
  
  <p>Is there a more elegant way to solve this problem?</p>