I have to confess this is copy-pasted from Spray's code as they don't expose a function doing this. I'll expand it and place it in `SprayUtils` so that it doesn't compromise the visibility of the rest.