aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala
blob: 8d0024a3925f47498a41cc20d86a3a6e3e5f814e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package com.softwaremill.sttp

import java.net.URLDecoder

import scala.annotation.tailrec

object UriInterpolator {

  def interpolate(sc: StringContext, args: Any*): Uri = {
    val tokens = tokenize(sc, args: _*)

    val builders = List(
      UriBuilder.Scheme,
      UriBuilder.UserInfo,
      UriBuilder.HostPort,
      UriBuilder.Path,
      UriBuilder.Query,
      UriBuilder.Fragment
    )

    val startingUri = Uri("-")

    val (uri, leftTokens) =
      builders.foldLeft((startingUri, tokens)) {
        case ((u, t), builder) =>
          builder.fromTokens(u, t)
      }

    if (leftTokens.nonEmpty) {
      throw new IllegalStateException(
        s"Tokens left after building the whole uri: $leftTokens, result so far: $uri")
    }

    uri
  }

  private def tokenize(sc: StringContext, args: Any*): Vector[Token] = {
    val strings = sc.parts.iterator
    val expressions = args.iterator

    var (tokenizer, tokens) = Tokenizer.Scheme.tokenize(strings.next())

    while (strings.hasNext) {
      val nextExpression = expressions.next()
      val nextExpressionStr = nextExpression.toString

      // special case: the interpolation starts with an expression, which
      // contains a whole URI. In this case, parsing the expression as if
      // its string value was embedded in the interpolated string. This
      // way it's possible to extend existing URIs. Without special-casing
      // the embedded URI would be escaped and become part of the host
      // as a whole.
      if (tokens == Vector(StringToken("")) && nextExpressionStr.contains(
            "://")) {
        def tokenizeExpressionAsString(): Unit = {
          val (nextTokenizer, nextTokens) =
            tokenizer.tokenize(nextExpression.toString)
          tokenizer = nextTokenizer
          tokens = tokens ++ nextTokens
        }

        def tokenizeStringRemoveEmptyPrefix(): Unit = {
          val (nextTokenizer, nextTokens) = tokenizer.tokenize(strings.next())
          tokenizer = nextTokenizer

          // we need to remove empty tokens around exp as well - however here
          // by hand, as the expression token is unwrapped, so removeEmptyTokensAroundExp
          // won't handle this.
          val nextTokensWithoutEmptyPrefix = nextTokens match {
            case StringToken("") +: tail => tail
            case x                       => x
          }

          tokens = tokens ++ nextTokensWithoutEmptyPrefix
        }

        tokenizeExpressionAsString()
        tokenizeStringRemoveEmptyPrefix()
      } else {
        tokens = tokens :+ ExpressionToken(nextExpression)

        val (nextTokenizer, nextTokens) = tokenizer.tokenize(strings.next())
        tokenizer = nextTokenizer
        tokens = tokens ++ nextTokens
      }

    }

    removeEmptyTokensAroundExp(tokens)
  }

  sealed trait Token
  case class StringToken(s: String) extends Token
  case class ExpressionToken(e: Any) extends Token
  case object SchemeEnd extends Token
  case object ColonInAuthority extends Token
  case object AtInAuthority extends Token
  case object DotInAuthority extends Token
  case object PathStart extends Token
  case object SlashInPath extends Token
  case object QueryStart extends Token
  case object AmpInQuery extends Token
  case object EqInQuery extends Token
  case object FragmentStart extends Token

  trait Tokenizer {
    def tokenize(s: String): (Tokenizer, Vector[Token])
  }

  object Tokenizer {
    object Scheme extends Tokenizer {
      override def tokenize(s: String): (Tokenizer, Vector[Token]) = {
        s.split("://", 2) match {
          case Array(scheme, rest) =>
            val (next, authorityTokens) = Authority.tokenize(rest)
            (next, Vector(StringToken(scheme), SchemeEnd) ++ authorityTokens)

          case Array(x) =>
            if (!x.matches("[a-zA-Z0-9+\\.\\-]*")) {
              // anything else than the allowed characters in scheme suggest that
              // there is no scheme; tokenizing using the next tokenizer in chain
              // https://stackoverflow.com/questions/3641722/valid-characters-for-uri-schemes
              Authority.tokenize(x)
            } else {
              (this, Vector(StringToken(x)))
            }
        }
      }
    }

    object Authority extends Tokenizer {
      private val IpV6InAuthorityPattern = "\\[[0-9a-fA-F:]+\\]".r

      private def ipv6parser(a: String): Option[Vector[Token]] = {
        a match {
          case IpV6InAuthorityPattern() =>
            // removing the [] which are used to surround ipv6 adresses in URLs
            Some(Vector(StringToken(a.substring(1, a.length - 1))))
          case _ => None
        }
      }

      override def tokenize(s: String): (Tokenizer, Vector[Token]) =
        tokenizeTerminatedFragment(
          s,
          this,
          Set('/', '?', '#'),
          Map(':' -> ColonInAuthority,
              '@' -> AtInAuthority,
              '.' -> DotInAuthority),
          ipv6parser
        )
    }

    object Path extends Tokenizer {
      override def tokenize(s: String): (Tokenizer, Vector[Token]) =
        tokenizeTerminatedFragment(
          s,
          this,
          Set('?', '#'),
          Map('/' -> SlashInPath)
        )
    }

    object Query extends Tokenizer {
      override def tokenize(s: String): (Tokenizer, Vector[Token]) =
        tokenizeTerminatedFragment(
          s,
          this,
          Set('#'),
          Map('&' -> AmpInQuery, '=' -> EqInQuery)
        )
    }

    object Fragment extends Tokenizer {
      override def tokenize(s: String): (Tokenizer, Vector[Token]) =
        (this, Vector(StringToken(s)))
    }

    /**
      * Tokenize the given string up to any of the given terminator characters
      * by splitting it using the given separators and translating each
      * separator to a token.
      *
      * The rest of the string, after the terminators, is tokenized using
      * a tokenizer determined by the type of the terminator.
      *
      * @param extraFragmentParser A context-specific parser which is given the
      * option to tokenize a fragment (without terminators).
      */
    private def tokenizeTerminatedFragment(
        s: String,
        current: Tokenizer,
        terminators: Set[Char],
        separatorsToTokens: Map[Char, Token],
        extraFragmentParser: String => Option[Vector[Token]] = _ => None)
      : (Tokenizer, Vector[Token]) = {

      def tokenizeFragment(f: String): Vector[Token] = {
        extraFragmentParser(f) match {
          case None =>
            splitPreserveSeparators(f, separatorsToTokens.keySet).map { t =>
              t.headOption.flatMap(separatorsToTokens.get) match {
                case Some(token) => token
                case None        => StringToken(t)
              }
            }
          case Some(tt) => tt
        }
      }

      // first checking if the fragment doesn't end; e.g. the authority is
      // terminated by /, ?, # or end of string (there might be other /, ?,
      // # later on e.g. in the query).
      // See: https://tools.ietf.org/html/rfc3986#section-3.2
      split(s, terminators) match {
        case Right((fragment, separator, rest)) =>
          tokenizeAfterSeparator(tokenizeFragment(fragment), separator, rest)

        case Left(fragment) =>
          (current, tokenizeFragment(fragment))
      }
    }

    private def tokenizeAfterSeparator(
        beforeSeparatorTokens: Vector[Token],
        separator: Char,
        s: String): (Tokenizer, Vector[Token]) = {

      val (next, separatorToken) = separatorTokenizerAndToken(separator)
      val (nextNext, nextTokens) = next.tokenize(s)
      (nextNext, beforeSeparatorTokens ++ Vector(separatorToken) ++ nextTokens)
    }

    private def separatorTokenizerAndToken(
        separator: Char): (Tokenizer, Token) =
      separator match {
        case '/' => (Path, PathStart)
        case '?' => (Query, QueryStart)
        case '#' => (Fragment, FragmentStart)
      }

    private def splitPreserveSeparators(s: String,
                                        sep: Set[Char]): Vector[String] = {
      @tailrec
      def doSplit(s: String, acc: Vector[String]): Vector[String] = {
        split(s, sep) match {
          case Left(x) => acc :+ x
          case Right((before, separator, after)) =>
            doSplit(after, acc ++ Vector(before, separator.toString))
        }
      }

      doSplit(s, Vector.empty)
    }

    private def split(
        s: String,
        sep: Set[Char]): Either[String, (String, Char, String)] = {
      val i = s.indexWhere(sep.contains)
      if (i == -1) Left(s)
      else Right((s.substring(0, i), s.charAt(i), s.substring(i + 1)))
    }
  }

  sealed trait UriBuilder {
    def fromTokens(u: Uri, t: Vector[Token]): (Uri, Vector[Token])
  }

  object UriBuilder {

    case object Scheme extends UriBuilder {
      override def fromTokens(u: Uri,
                              t: Vector[Token]): (Uri, Vector[Token]) = {
        split(t, Set[Token](SchemeEnd)) match {
          case Left(tt) => (u.scheme("http"), tt)
          case Right((schemeTokens, _, otherTokens)) =>
            val scheme = tokensToString(schemeTokens)
            (u.scheme(scheme), otherTokens)
        }
      }
    }

    case object UserInfo extends UriBuilder {
      override def fromTokens(u: Uri,
                              t: Vector[Token]): (Uri, Vector[Token]) = {
        split(t, Set[Token](AtInAuthority)) match {
          case Left(tt) => (u, tt)
          case Right((uiTokens, _, otherTokens)) =>
            (uiFromTokens(u, uiTokens), otherTokens)
        }
      }

      private def uiFromTokens(u: Uri, uiTokens: Vector[Token]): Uri = {
        val uiTokensWithDots = uiTokens.map {
          case DotInAuthority => StringToken(".")
          case x              => x
        }
        split(uiTokensWithDots, Set[Token](ColonInAuthority)) match {
          case Left(tt) => uiFromTokens(u, tt, Vector.empty)
          case Right((usernameTokens, _, passwordTokens)) =>
            uiFromTokens(u, usernameTokens, passwordTokens)
        }
      }

      private def uiFromTokens(u: Uri,
                               usernameTokens: Vector[Token],
                               passwordTokens: Vector[Token]): Uri = {

        (tokensToStringOpt(usernameTokens), tokensToStringOpt(passwordTokens)) match {
          case (Some(un), Some(p)) => u.userInfo(un, p)
          case (Some(un), None)    => u.userInfo(un)
          case (None, Some(p))     => u.userInfo("", p)
          case (None, None)        => u
        }
      }
    }

    case object HostPort extends UriBuilder {
      override def fromTokens(u: Uri,
                              t: Vector[Token]): (Uri, Vector[Token]) = {
        split(t, Set[Token](PathStart, QueryStart, FragmentStart)) match {
          case Left(tt) =>
            (hostPortFromTokens(u, tt), Vector.empty)
          case Right((hpTokens, sep, otherTokens)) =>
            (hostPortFromTokens(u, hpTokens), sep +: otherTokens)
        }
      }

      private def hostPortFromTokens(u: Uri,
                                     rawHpTokens: Vector[Token]): Uri = {
        // Special case: if the host/port part contains an expression token,
        // which has a string representation which contains a colon (:), then
        // we assume that the intention was to embed the port and host separately,
        // not to escape the colon in the host name.
        val hpTokens = rawHpTokens.flatMap {
          case e: ExpressionToken =>
            val es = tokensToString(Vector(e))
            es.split(":", 2) match {
              case Array(h, p) if p.matches("\\d+") =>
                Vector(StringToken(h), ColonInAuthority, StringToken(p))
              case _ => Vector(e)
            }
          case t => Vector(t)
        }

        if (hpTokens.count(_ == ColonInAuthority) > 1) {
          throw new IllegalArgumentException("port specified multiple times")
        }

        split(hpTokens, Set[Token](ColonInAuthority)) match {
          case Left(tt) => hostFromTokens(u, tt)
          case Right((hostTokens, _, portTokens)) =>
            portFromTokens(hostFromTokens(u, hostTokens), portTokens)
        }
      }

      private def hostFromTokens(u: Uri, tokens: Vector[Token]): Uri = {
        val hostFragments = tokensToStringSeq(tokens)
        u.host(hostFragments.mkString("."))
      }

      private def portFromTokens(u: Uri, tokens: Vector[Token]): Uri = {
        u.port(tokensToStringOpt(tokens).map(_.toInt))
      }
    }

    case object Path extends UriBuilder {
      override def fromTokens(u: Uri, t: Vector[Token]): (Uri, Vector[Token]) =
        fromStartingToken(u,
                          t,
                          PathStart,
                          Set[Token](QueryStart, FragmentStart),
                          pathFromTokens)

      private def pathFromTokens(u: Uri, tokens: Vector[Token]): Uri = {
        u.path(tokensToStringSeq(tokens))
      }
    }

    case object Query extends UriBuilder {

      import com.softwaremill.sttp.Uri.{QueryFragment => QF}

      override def fromTokens(u: Uri, t: Vector[Token]): (Uri, Vector[Token]) =
        fromStartingToken(u,
                          t,
                          QueryStart,
                          Set[Token](FragmentStart),
                          queryFromTokens)

      private def queryFromTokens(u: Uri, tokens: Vector[Token]): Uri = {
        val qfs =
          splitToGroups(tokens, AmpInQuery)
            .flatMap(queryMappingsFromTokens)

        u.copy(queryFragments = qfs)
      }

      private def queryMappingsFromTokens(tokens: Vector[Token]): Vector[QF] = {
        def expressionPairToQueryFragment(ke: Any,
                                          ve: Any): Option[QF.KeyValue] =
          for {
            k <- anyToStringOpt(ke)
            v <- anyToStringOpt(ve)
          } yield QF.KeyValue(k, v)

        def seqToQueryFragments(s: Seq[_]): Vector[QF] = {
          s.flatMap {
            case (ke, ve) => expressionPairToQueryFragment(ke, ve)
            case ve       => anyToStringOpt(ve).map(QF.Value(_))
          }.toVector
        }

        split(tokens, Set[Token](EqInQuery)) match {
          case Left(Vector(ExpressionToken(e: Map[_, _]))) =>
            seqToQueryFragments(e.toSeq)
          case Left(Vector(ExpressionToken(e: Seq[_]))) =>
            seqToQueryFragments(e)
          case Left(t) => tokensToStringOpt(t).map(QF.Value(_)).toVector
          case Right((leftEq, _, rightEq)) =>
            tokensToStringOpt(leftEq) match {
              case Some(k) =>
                tokensToStringSeq(rightEq).map(QF.KeyValue(k, _)).toVector

              case None =>
                Vector.empty
            }
        }
      }
    }

    case object Fragment extends UriBuilder {
      override def fromTokens(u: Uri,
                              t: Vector[Token]): (Uri, Vector[Token]) = {
        t match {
          case FragmentStart +: tt =>
            (u.fragment(tokensToStringOpt(tt)), Vector.empty)

          case _ => (u, t)
        }
      }
    }

    /**
      * Parse a prefix of tokens `t` into a component of a URI. The component
      * is only present in the tokens if there's a `startingToken`; otherwise
      * the component is skipped.
      *
      * The component is terminated by any of `nextComponentTokens`.
      */
    private def fromStartingToken(
        u: Uri,
        t: Vector[Token],
        startingToken: Token,
        nextComponentTokens: Set[Token],
        componentFromTokens: (Uri, Vector[Token]) => Uri)
      : (Uri, Vector[Token]) = {

      t match {
        case `startingToken` +: tt =>
          split(tt, nextComponentTokens) match {
            case Left(ttt) =>
              (componentFromTokens(u, ttt), Vector.empty)
            case Right((componentTokens, sep, otherTokens)) =>
              (componentFromTokens(u, componentTokens), sep +: otherTokens)
          }

        case _ => (u, t)
      }
    }

    private def anyToString(a: Any): String = anyToStringOpt(a).getOrElse("")

    private def anyToStringOpt(a: Any): Option[String] = a match {
      case None    => None
      case null    => None
      case Some(x) => Some(x.toString)
      case x       => Some(x.toString)
    }

    private def tokensToStringSeq(tokens: Vector[Token]): Seq[String] = {
      /*
      #40: when converting tokens to a string sequence, we have to look at
      groups of string/expression (value) tokens separated by others. If there
      are multiple tokens in each such group, their string representations
      should be concatenated (corresponds to e.g. $x$y). A single
      collection-valued token should be expanded.
       */

      def isValueToken(t: Token) = t match {
        case ExpressionToken(_) => true
        case StringToken(_)     => true
        case _                  => false
      }

      @tailrec
      def doToSeq(ts: Vector[Token], acc: Vector[String]): Seq[String] = {
        val tsWithValuesPrefix = ts.dropWhile(to => !isValueToken(to))
        val (valueTs, tailTs) = tsWithValuesPrefix.span(isValueToken)

        valueTs match {
          case Vector() => acc // tailTs must be empty then as well
          case Vector(ExpressionToken(s: Iterable[_])) =>
            doToSeq(tailTs, acc ++ s.flatMap(anyToStringOpt).toVector)
          case Vector(ExpressionToken(s: Array[_])) =>
            doToSeq(tailTs, acc ++ s.flatMap(anyToStringOpt).toVector)
          case _ =>
            val values = valueTs
              .flatMap {
                case ExpressionToken(e) => anyToStringOpt(e)
                case StringToken(s)     => Some(decode(s))
                case _                  => None
              }

            val strToAdd =
              if (values.isEmpty) None else Some(values.mkString(""))

            doToSeq(tailTs, acc ++ strToAdd)
        }
      }

      doToSeq(tokens, Vector.empty)
    }

    private def tokensToStringOpt(t: Vector[Token]): Option[String] = t match {
      case Vector()                   => None
      case Vector(ExpressionToken(e)) => anyToStringOpt(e)
      case _                          => Some(tokensToString(t))
    }

    private def tokensToString(t: Vector[Token]): String =
      t.collect {
          case StringToken(s)     => decode(s)
          case ExpressionToken(e) => anyToString(e)
        }
        .mkString("")

    private def split[T](
        v: Vector[T],
        sep: Set[T]): Either[Vector[T], (Vector[T], T, Vector[T])] = {
      val i = v.indexWhere(sep.contains)
      if (i == -1) Left(v) else Right((v.take(i), v(i), v.drop(i + 1)))
    }

    private def splitToGroups[T](v: Vector[T], sep: T): Vector[Vector[T]] = {
      def doSplit(vv: Vector[T], acc: Vector[Vector[T]]): Vector[Vector[T]] = {
        vv.indexOf(sep) match {
          case -1 => acc :+ vv
          case i  => doSplit(vv.drop(i + 1), acc :+ vv.take(i))
        }
      }

      doSplit(v, Vector.empty)
    }

    private def decode(s: String): String = URLDecoder.decode(s, Utf8)
  }

  /**
    * After tokenizing, there might be extra empty string tokens
    * (`StringToken("")`) before and after expressions. For example,
    * `key=$value` will tokenize to:
    *
    * `Vector(StringToken("key"), EqInQuery, StringToken(""), ExpressionToken(value))`
    *
    * These empty string tokens need to be removed so that e.g. extra key-value
    * mappings are not generated.
    */
  private def removeEmptyTokensAroundExp(
      tokens: Vector[Token]): Vector[Token] = {
    def doRemove(t: Vector[Token], acc: Vector[Token]): Vector[Token] =
      t match {
        case StringToken("") +: (e: ExpressionToken) +: tail =>
          doRemove(e +: tail, acc)
        case (e: ExpressionToken) +: StringToken("") +: tail =>
          doRemove(tail, acc :+ e)
        case v +: tail => doRemove(tail, acc :+ v)
        case Vector()  => acc
      }

    doRemove(tokens, Vector.empty)
  }
}