Skip to content

Commit

Permalink
Remove apostrophes in IPv6 addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Mar 10, 2025
1 parent 0c0b5a6 commit 5ee4f18
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 6 additions & 3 deletions core/src/main/scala/sttp/model/headers/Forwarded.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ object Forwarded {

/** Parses a single Forwarded header, which can contain multiple Forwarded values. */
def parse(headerValue: String): Either[String, List[Forwarded]] = {
def removeApostrophes(v: String): String =
if (v.startsWith("\"") && v.endsWith("\"")) v.substring(1, v.length - 1) else v

def parseSingle(headerValue: String): Either[String, Forwarded] = {
val parts = headerValue.split(";").map(_.trim).toList
val kvPairs = parts.map { part =>
Expand All @@ -41,9 +44,9 @@ object Forwarded {

val pairs = kvPairs.collect { case Right(pair) => pair }
if (pairs.size == kvPairs.size) {
val by = pairs.collectFirst { case ("by", v) => v }
val `for` = pairs.collectFirst { case ("for", v) => v }
val host = pairs.collectFirst { case ("host", v) => v }
val by = pairs.collectFirst { case ("by", v) => v }.map(removeApostrophes)
val `for` = pairs.collectFirst { case ("for", v) => v }.map(removeApostrophes)
val host = pairs.collectFirst { case ("host", v) => v }.map(removeApostrophes)
val proto = pairs.collectFirst { case ("proto", v) => v }
Right(Forwarded(by, `for`, host, proto))
} else
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/scala/sttp/model/headers/ForwardedTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ class ForwardedTest extends AnyFlatSpec with Matchers {
actual shouldBe "by=1.2.3.4,by=4.3.2.1"
}

it should "parse ipv6 addresses in For" in {
it should "parse ipv6 addresses in 'for', removing the apostrophes" in {
val actual = Forwarded.parse("""For="[2001:db8:cafe::17]:4711"""")
actual shouldBe Right(List(Forwarded(None, Some("\"[2001:db8:cafe::17]:4711\""), None, None)))
actual shouldBe Right(List(Forwarded(None, Some("[2001:db8:cafe::17]:4711"), None, None)))
}

it should "parse multiple ipv4 addresses" in {
Expand Down

0 comments on commit 5ee4f18

Please sign in to comment.