zio/zio-http

"Frowarded" Header renders incorrectly which causes it to loose information

Closed this issue · 0 comments

Describe the bug
The Forwarded header incorrectly renders optional values when those values are None

For example:

Header.Forwarded(forValues = List("""127.0.0.1"""))

Renders as:

None; for=127.0.0.1; None; None

This causes the header to loose data when pulled out of a Request.

To Reproduce

 val header      = Header.Forwarded(forValues = List("""127.0.0.1"""))
// This renders as `None; for=127.0.0.1; None; None` which is not valid

val r = Request(headers = Headers(header))
println(r.header(Header.Forwarded)) // Output: Some(Forwarded(None,List(),None,None))

As can be seen above, the Forwarded is loosing information when pulled back out of the request because it fails to correctly parse the incorrectly rendered header string.

Expected behaviour

  • Forwarded should correctly render unset values as an empty string, "" and not as None; .
  • A round trip of rendering and parsing the Forwarded should NOT loose data.

Additional context
I should have a fix for this forth coming, it will look something like this:

   def render(forwarded: Forwarded): String = {
      def formatDirective(directive: Option[ String ]) = directive.map(_+"; ").getOrElse("")

      s"${formatDirective(forwarded.by)}${forwarded.forValues.map(v => s"for=$v").mkString(",")};${formatDirective(forwarded.host)}${formatDirective( forwarded.proto )}"
    }