add Host header to requests submitted by Client.fromHttpService if the request URI is absolute #1874
Conversation
private def addHostHeaderIfUriIsAbsolute[F[_]](req: Request[F]): Request[F] = { | ||
val maybeHost = for { | ||
host <- req.uri.host | ||
if req.headers.get(Host).isEmpty |
ChristopherDavenport
May 25, 2018
Member
_ <- Alternative[Option].guard(req.headers.get(Host).isEmpty)
_ <- Alternative[Option].guard(req.headers.get(Host).isEmpty)
bpholt
May 25, 2018
Author
Contributor
@ChristopherDavenport I don't understand the difference here. Can you explain why Alternative[Option].guard
is preferred?
@ChristopherDavenport I don't understand the difference here. Can you explain why Alternative[Option].guard
is preferred?
ChristopherDavenport
May 25, 2018
•
Member
One is implemented in terms of flatMap
the other in terms of withFilter
. I just see this as utilizing non-functional language features which has the possibility for issues in the future.
I'm sure people may disagree here.
One is implemented in terms of flatMap
the other in terms of withFilter
. I just see this as utilizing non-functional language features which has the possibility for issues in the future.
I'm sure people may disagree here.
jmcardon
May 25, 2018
•
Member
No comment on cats fp guard syntax (I don't care for it but I don't hate it), however, instead of the withfilter guard, you can always just
req.uri.host match {
case Some(host) if req.headers.get(Host).isEmpty =>
req.withHeaders(req.headers.put((Host(host.value, req.uri.port)))
case _ => req
}
That's microscopically more efficient (no fold call + no withFilter)
No comment on cats fp guard syntax (I don't care for it but I don't hate it), however, instead of the withfilter guard, you can always just
req.uri.host match {
case Some(host) if req.headers.get(Host).isEmpty =>
req.withHeaders(req.headers.put((Host(host.value, req.uri.port)))
case _ => req
}
That's microscopically more efficient (no fold call + no withFilter)
Did you also want to set the secure flag if the scheme is https? That was mentioned on Gitter, and sounded like a smart idea. That's another thing that a real client backend does for free. |
I'm not sure I have a solid objection to withFilter on option other than a lack of trust in withFilter. As such this is an improvement, but if we want the secure flag I agree with that change as well. |
…e request URI is absolute
Updated with @jmcardon's pattern match suggestion. I'm going to leave the secure flag alone, as it's not easily changed for this usage. |
Addresses #1872 in the easy case where the request URI is absolute.