Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lenient URI parser #2

Open
hamnis opened this issue Mar 11, 2015 · 0 comments
Open

Lenient URI parser #2

hamnis opened this issue Mar 11, 2015 · 0 comments

Comments

@hamnis
Copy link
Member

hamnis commented Mar 11, 2015

This is scala code. We should generalize this and make it as part of the builder.

import java.net.URI

import org.codehaus.httpcache4j.uri.{QueryParams, URIBuilder}
import scalaz.\/

object URIFactory {
  val Extractor = "(http|https)://(\\w+\\.\\w+)(:\\d+)?(/[^\\?#]*)?(\\?.*)?#?(.*)?".r
  def parse(s: String): String \/ URI = {
    \/.fromTryCatch{ URI.create(s) }.orElse(\/.fromTryCatch {
      s match {
        case Extractor(scheme, host, port, path, query, fragment) => {
          URIBuilder.empty().
            withScheme(scheme).
            withHost(host).
            withPort(Option(port).map(_.stripPrefix(":").toInt).getOrElse(-1)).
            withRawPath(path).
            withParameters(QueryParams.parse(Option(query).map(_.stripPrefix("?")).orNull)).
            withFragment(Option(fragment).filterNot(_.isEmpty).orNull).
            toURI
        }
      }
    }).leftMap(_ => s)
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant