From 61fe057cd3651773b1ac353d33ea60d6626d4ec3 Mon Sep 17 00:00:00 2001 From: Stewart Stewart Date: Tue, 21 Mar 2017 21:22:18 -0400 Subject: slight refactor of escapeScriptTags --- src/main/scala/xyz/driver/core/rest.scala | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'src/main') diff --git a/src/main/scala/xyz/driver/core/rest.scala b/src/main/scala/xyz/driver/core/rest.scala index 498ba33..fd86b33 100644 --- a/src/main/scala/xyz/driver/core/rest.scala +++ b/src/main/scala/xyz/driver/core/rest.scala @@ -70,19 +70,21 @@ package rest { } } - val firstSlash = byteString.indexOf('/') - if (firstSlash === -1) byteString - else { - val indices = dirtyIndices(firstSlash, Nil) :+ byteString.length - val builder = ByteString.newBuilder - builder ++= byteString.take(firstSlash) - indices.sliding(2).foreach { - case Seq(start, end) => - builder += ' ' - builder ++= byteString.slice(start, end) - } - builder.result - } + val indices = dirtyIndices(0, Nil) + + indices.headOption.fold(byteString){head => + val builder = ByteString.newBuilder + builder ++= byteString.take(head) + + (indices :+ byteString.length).sliding(2).foreach { + case Seq(start, end) => + builder += ' ' + builder ++= byteString.slice(start, end) + case Seq(byteStringLength) => // Should not match; sliding on at least 2 elements + assert(indices.nonEmpty, s"Indices should have been nonEmpty: $indices") + } + builder.result + } } val sanitizeRequestEntity: Directive0 = { -- cgit v1.2.3