Skip to content

Commit a6b03ea

Browse files
authored
shared character toLowerCase function (#809)
Update CharUtils.scala Update CharUtils.scala use byteChar Remove unused import from HttpHeaderParser.scala revert change to use byteChar fn Update HttpHeaderParser.scala
1 parent 2eec9c6 commit a6b03ea

File tree

5 files changed

+55
-33
lines changed

5 files changed

+55
-33
lines changed

http-core/src/main/scala/org/apache/pekko/http/impl/engine/parsing/HttpHeaderParser.scala

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ import java.lang.{ StringBuilder => JStringBuilder }
2020
import org.apache.pekko
2121
import pekko.annotation.InternalApi
2222
import pekko.event.LoggingAdapter
23-
import pekko.http.scaladsl.settings.ParserSettings
23+
import pekko.http.impl.model.parser.CharacterClasses._
24+
import pekko.http.impl.model.parser.HeaderParser
25+
import pekko.http.impl.util._
26+
import pekko.http.impl.util.CharUtils.toLowerCase
27+
import pekko.http.impl.util.HttpConstants._
28+
import pekko.http.scaladsl.model.{ ErrorInfo, HttpHeader, MediaTypes, StatusCode, StatusCodes }
29+
import pekko.http.scaladsl.model.headers.{ EmptyHeader, RawHeader }
2430
import pekko.http.scaladsl.settings.ParserSettings.{
2531
ErrorLoggingVerbosity,
2632
IllegalResponseHeaderNameProcessingMode,
2733
IllegalResponseHeaderValueProcessingMode
2834
}
29-
import pekko.http.impl.util._
30-
import pekko.http.impl.util.HttpConstants._
31-
import pekko.http.scaladsl.model.{ ErrorInfo, HttpHeader, MediaTypes, StatusCode, StatusCodes }
32-
import pekko.http.scaladsl.model.headers.{ EmptyHeader, RawHeader }
33-
import pekko.http.impl.model.parser.HeaderParser
34-
import pekko.http.impl.model.parser.CharacterClasses._
3535
import pekko.util.ByteString
3636

3737
import scala.annotation.tailrec
@@ -643,13 +643,13 @@ private[http] object HttpHeaderParser {
643643
}
644644
} else {
645645
mode match {
646-
case ParserSettings.IllegalResponseHeaderValueProcessingMode.Error =>
646+
case IllegalResponseHeaderValueProcessingMode.Error =>
647647
fail(s"Illegal character '${escape(c)}' in header value")
648-
case ParserSettings.IllegalResponseHeaderValueProcessingMode.Warn =>
648+
case IllegalResponseHeaderValueProcessingMode.Warn =>
649649
// ignore the illegal character and log a warning message
650650
log.warning(s"Illegal character '${escape(c)}' in header value")
651651
sb
652-
case ParserSettings.IllegalResponseHeaderValueProcessingMode.Ignore =>
652+
case IllegalResponseHeaderValueProcessingMode.Ignore =>
653653
// just ignore the illegal character
654654
sb
655655
}
@@ -682,11 +682,4 @@ private[http] object HttpHeaderParser {
682682
def withValueCountIncreased = copy(valueCount = valueCount + 1)
683683
def spaceLeft = valueCount < parser.maxValueCount
684684
}
685-
686-
/**
687-
* Efficiently lower-cases the given character.
688-
* Note: only works for 7-bit ASCII letters (which is enough for header names)
689-
*/
690-
private[HttpHeaderParser] def toLowerCase(c: Char): Char =
691-
if (c >= 'A' && c <= 'Z') (c + 0x20 /* - 'A' + 'a' */ ).toChar else c
692685
}

http-core/src/main/scala/org/apache/pekko/http/impl/model/parser/CommonActions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private[parser] trait CommonActions {
8080
val char2 = str2.charAt(at)
8181

8282
(char1 | char2) < 0x80 &&
83-
Character.toLowerCase(char1) == Character.toLowerCase(char2) &&
83+
CharUtils.toLowerCase(char1) == CharUtils.toLowerCase(char2) &&
8484
stringEquals(at + 1, length)
8585
} else true
8686

http-core/src/main/scala/org/apache/pekko/http/impl/model/parser/UriParser.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ package org.apache.pekko.http.impl.model.parser
1616
import java.nio.charset.Charset
1717

1818
import org.apache.pekko
19-
import org.parboiled2._
20-
import pekko.http.impl.util.{ enhanceString_, StringRendering }
19+
import pekko.annotation.InternalApi
20+
import pekko.http.impl.util.{ enhanceString_, CharUtils => CU, StringRendering }
2121
import pekko.http.scaladsl.model.{ Uri, UriRendering }
2222
import pekko.http.scaladsl.model.headers.HttpOrigin
23-
import Parser.DeliveryScheme.Either
2423
import Uri._
25-
import pekko.annotation.InternalApi
24+
import org.parboiled2._
25+
import org.parboiled2.Parser.DeliveryScheme.Either
2626

2727
/**
2828
* INTERNAL API
@@ -365,7 +365,7 @@ private[http] final class UriParser(
365365

366366
///////////// helpers /////////////
367367

368-
private def appendLowered(): Rule0 = rule { run(sb.append(CharUtils.toLowerCase(lastChar))) }
368+
private def appendLowered(): Rule0 = rule { run(sb.append(CU.toLowerCase(lastChar))) }
369369

370370
private def savePath() = rule { run(setPath(Path(sb.toString, uriParsingCharset))) }
371371

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* license agreements; and to You under the Apache License, version 2.0:
4+
*
5+
* https://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* This file is part of the Apache Pekko project, which was derived from Akka.
8+
*/
9+
10+
/*
11+
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
12+
*/
13+
14+
package org.apache.pekko.http.impl.util
15+
16+
import org.apache.pekko
17+
import pekko.annotation.InternalApi
18+
19+
@InternalApi
20+
private[http] object CharUtils {
21+
22+
/**
23+
* Internal Pekko HTTP Use only.
24+
*
25+
* Efficiently lower-cases the given character.
26+
* Note: only works for 7-bit ASCII letters (which is enough for header names)
27+
*/
28+
final def toLowerCase(c: Char): Char =
29+
if (c >= 'A' && c <= 'Z') (c + 0x20 /* - 'A' + 'a' */ ).toChar else c
30+
31+
}

http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/HttpMessage.scala

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,29 @@
1313

1414
package org.apache.pekko.http.scaladsl.model
1515

16-
import org.apache.pekko
17-
import pekko.stream.scaladsl.Flow
18-
import pekko.stream.{ FlowShape, Graph, Materializer, SystemMaterializer }
1916
import java.io.File
2017
import java.nio.file.Path
2118
import java.lang.{ Iterable => JIterable }
2219
import java.util.Optional
2320
import java.util.concurrent.{ CompletionStage, Executor }
2421

25-
import scala.concurrent.duration.FiniteDuration
26-
import scala.concurrent.{ ExecutionContext, Future }
22+
import scala.annotation.tailrec
2723
import scala.collection.immutable
24+
import scala.concurrent.{ ExecutionContext, Future }
25+
import scala.concurrent.duration._
26+
import scala.jdk.FutureConverters._
2827
import scala.reflect.{ classTag, ClassTag }
28+
29+
import org.apache.pekko
2930
import pekko.Done
3031
import pekko.actor.ClassicActorSystemProvider
31-
import org.parboiled2.CharUtils
3232
import pekko.util.{ ByteString, HashCode, OptionVal }
3333
import pekko.http.impl.util._
3434
import pekko.http.javadsl.{ model => jm }
3535
import pekko.http.scaladsl.util.FastFuture._
3636
import pekko.http.scaladsl.model.headers._
37-
38-
import scala.annotation.tailrec
39-
import scala.concurrent.duration._
40-
import scala.jdk.FutureConverters._
37+
import pekko.stream.scaladsl.Flow
38+
import pekko.stream.{ FlowShape, Graph, Materializer, SystemMaterializer }
4139

4240
/**
4341
* Common base class of HttpRequest and HttpResponse.

0 commit comments

Comments
 (0)