NET-A-PORTER / scala-uri

Simple scala library for building and parsing URIs
Other
261 stars 33 forks source link

Spaces encoded as '+' characters don't decode #126

Open subourbonite opened 7 years ago

subourbonite commented 7 years ago

We're running into issues where some upstream systems and browsers are encoding space characters as a '+' in the URL query string. UriConfig can be configured to encode spaces as '+', but I cannot find any way (short of writing my own implementation of UriDecoder) to support parsing those values back out. Am I just missing something obvious that isn't covered in the documentation, or is this an enhancement that could be made?

theon commented 7 years ago

This is a missing feature. For the meantime you can get the desired behaviour like so:

import com.netaporter.uri.decoding.UriDecoder
import com.netaporter.uri.config.UriConfig

object PlusToSpaceDecoder extends UriDecoder { 
  def decode(s: String) = PercentDecoder.decode(s).replace('+', ' ')
}

implicit val c = UriConfig(decoder = PlusToSpaceDecoder)

val uri = Uri.parse("http://example.com?a=b+c")
uri.toString // http://example.com?a=b%20c
uri.toStringRaw // http://example.com?a=b c

I not longer have write access to this repo, so I am going to move scala-uri over to a new home. Once I have done that, I will make this easier and update here.

subourbonite commented 7 years ago

Thanks! I actually wrote almost that exact same piece of code as a workaround, but wanted to bring it up as something the framework could address more elegantly.