Skip to content

Commit

Permalink
finagle-base-http: -> scala 2.13
Browse files Browse the repository at this point in the history
use strategy common extends versionspecific
  • Loading branch information
Martijn Hoekstra committed Oct 20, 2019
1 parent de48cd9 commit 92d7d3e
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 68 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,8 @@ lazy val finagleBaseHttp = Project(
id = "finagle-base-http",
base = file("finagle-base-http")
).settings(
sharedSettings
sharedSettings,
withTwoThirteen
).settings(
name := "finagle-base-http",
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

package com.twitter.finagle.http

import scala.collection.mutable.Builder
import scala.collection.mutable.MapLike

protected abstract class CookieMapVersionSpecific(message: Message, cookieCodec: CookieCodec) {
def this(message: Message) = this(message, CookieMap.cookieCodec)

def addCookie(cookie: (String, Cookie)): this.type
def addCookies(cookies: TraversableOnce[(String, Cookie)]): this.type
def removeCookie(name: String): this.type
def removeCookies(names: TraversableOnce[String]): this.type

def +=(cookie: (String, Cookie)): this.type = addCookie(cookie)
def -=(name: String): this.type = removeCookie(name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

package com.twitter.finagle.http

import scala.collection.mutable.MapLike

protected trait ParamMapVersionSpecific {
def setParam[B >: String](kv: (String, B)): ParamMap
def +[V1 >: String](kv: (String, V1)): ParamMap = setParam(kv._1, kv._2)

def clearParam(name: String): ParamMap
def -(name: String): ParamMap = clearParam(name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ private final class HashBackedHeaderMap extends http.HeaderMap {
}

// Validates key and value.
def set(key: String, value: String): HeaderMap = {
def set(key: String, value: String): this.type = {
validateName(key)
setUnsafe(key, foldReplacingValidateValue(key, value))
}

// Does not validate key and value.
def setUnsafe(key: String, value: String): HeaderMap = underlying.synchronized {
def setUnsafe(key: String, value: String): this.type = underlying.synchronized {
underlying.set(key, value)
this
}
Expand All @@ -62,16 +62,10 @@ private final class HashBackedHeaderMap extends http.HeaderMap {
underlying.flattenIterator
}

def +=(kv: (String, String)): this.type = {
set(kv._1, kv._2)
this
}

def -=(key: String): this.type = underlying.synchronized {
def removeHeader(key: String): this.type = underlying.synchronized {
underlying.removeAll(key)
this
}

override def keysIterator: Iterator[String] = underlying.synchronized {
underlying.uniqueNamesIterator
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.twitter.finagle.http

import com.twitter.finagle.http.headers.Header
import scala.collection.mutable

protected abstract class HeaderMapVersionSpecific {
def set(name: String, header: String): this.type
def removeHeader(name: String): this.type

def -=(key: String): this.type = removeHeader(key)
def +=(kv: (String, String)): this.type = set(kv._1, kv._2)

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import scala.collection.mutable
// - iteration by gaining access to `entriesIterator` (protected method).
// - get/add functions by providing custom hashCode and equals methods for a key
private[http] final class HeadersHash extends mutable.HashMap[String, Header.Root] {
import HeadersHash._
import HeaderMap.hashChar

// Adopted from Netty 3 HttpHeaders.
override protected def elemHashCode(key: String): Int = {
Expand Down Expand Up @@ -120,9 +120,3 @@ private[http] final class HeadersHash extends mutable.HashMap[String, Header.Roo
def removeAll(key: String): Unit = remove(key)

}

object HeadersHash {
final def hashChar(c: Char): Int =
if (c >= 'A' && c <= 'Z') c + 32
else c
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.twitter.finagle.http

import scala.collection.IterableOnce

protected abstract class CookieMapVersionSpecific(message: Message, cookieCodec: CookieCodec) {
def this(message: Message) = this(message, CookieMap.cookieCodec)

def addCookie(cookie: (String, Cookie)): this.type
def addCookies(cookies: IterableOnce[(String, Cookie)]): this.type
def removeCookie(name: String): this.type
def removeCookies(names: IterableOnce[String]): this.type

def addOne(cookie: (String, Cookie)): this.type = addCookie(cookie)
def subtractOne(name: String): this.type = removeCookie(name)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

package com.twitter.finagle.http

protected abstract class ParamMapVersionSpecific {
def setParam[B >: String](kv: (String, B)): ParamMap
def updated[V1 >: String](key: String, value: V1): ParamMap = setParam(key, value)
def +[V1 >: String](key: String, value: V1): ParamMap = setParam(key, value)

def clearParam(name: String): ParamMap
def removed(name: String): ParamMap = clearParam(name)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.twitter.finagle.http

import com.twitter.finagle.http.headers.Header
import scala.collection.mutable

protected abstract class HeaderMapVersionSpecific {
def set(name: String, header: String): this.type
def removeHeader(name: String): this.type

def subtractOne(key: String): this.type = removeHeader(key)
def addOne(kv: (String, String)): this.type = set(kv._1, kv._2)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import scala.collection.mutable

private[finagle] object CookieMap {

private def cookieCodec = Netty4CookieCodec
def cookieCodec = Netty4CookieCodec

// Note that this is a def to allow it to be toggled for unit tests.
private[finagle] def includeSameSite: Boolean = supportSameSiteCodec()
Expand All @@ -23,9 +23,9 @@ private[finagle] object CookieMap {
* the ''message''
*/
class CookieMap private[finagle] (message: Message, cookieCodec: CookieCodec)
extends mutable.Map[String, Cookie]
with mutable.MapLike[String, Cookie, CookieMap] {

extends CookieMapVersionSpecific(message, cookieCodec) with mutable.Map[String, Cookie]
{
def this(message: Message) =
this(message, CookieMap.cookieCodec)

Expand Down Expand Up @@ -113,28 +113,28 @@ class CookieMap private[finagle] (message: Message, cookieCodec: CookieCodec)
* and Cookie` itself) into this map. If there are already cookies
* with the given `name` in the map, they will be removed.
*/
def +=(cookie: (String, Cookie)): this.type = {
def addCookie(cookie: (String, Cookie)): this.type = {
val (n, c) = cookie
setNoRewrite(n, c)
rewriteCookieHeaders()
this
}

/**
/**
* Adds the given `cookie` into this map. If there are already cookies
* with the given `name` in the map, they will be removed.
*/
def +=(cookie: Cookie): CookieMap = {
this += ((cookie.name, cookie))
}

override def ++=(xs: TraversableOnce[(String, Cookie)]): this.type = {
xs.foreach { case (n, c) => setNoRewrite(n, c) }
def addCookies(cookies: scala.collection.TraversableOnce[(String, Cookie)]): this.type = {
cookies.foreach { case (n, c) => setNoRewrite(n, c) }
rewriteCookieHeaders()
this
}

override def --=(xs: TraversableOnce[String]): this.type = {
def removeCookies(xs: TraversableOnce[String]): this.type = {
xs.foreach { n =>
underlying -= n
}
Expand All @@ -145,7 +145,7 @@ class CookieMap private[finagle] (message: Message, cookieCodec: CookieCodec)
/**
* Deletes all cookies with the given `name` from this map.
*/
def -=(name: String): this.type = {
def removeCookie(name: String): this.type = {
underlying -= name
rewriteCookieHeaders()
this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import scala.collection.mutable
* The map is a multi-map. Use [[getAll]] to get all values for a key. Use [[add]]
* to append a key-value.
*/
abstract class HeaderMap
extends mutable.Map[String, String]
with mutable.MapLike[String, String, HeaderMap] {
abstract class HeaderMap extends HeaderMapVersionSpecific with mutable.Map[String, String] {

/**
* Retrieves all values for a given header name.
Expand Down Expand Up @@ -61,12 +59,12 @@ abstract class HeaderMap
/**
* Set a header. If an entry already exists, it is replaced.
*/
def set(k: String, v: String): HeaderMap
def set(k: String, v: String): this.type

/**
* Set or replace a header without validating the key and value.
*/
def setUnsafe(k: String, v: String): HeaderMap
def setUnsafe(k: String, v: String): this.type

/**
* Set or replace a header, as in [[set(String, String)]],
Expand Down Expand Up @@ -108,6 +106,10 @@ object HeaderMap {
/** Create a new, empty HeaderMap. */
def newHeaderMap: HeaderMap = apply()

def hashChar(c: Char): Int =
if (c >= 'A' && c <= 'Z') c + 32
else c

private[this] val formatter = new ThreadLocal[SimpleDateFormat] {
override protected def initialValue(): SimpleDateFormat = {
val f = TwitterDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ import scala.collection.JavaConverters._
* Use `getAll()` to get all values for a key.
*/
abstract class ParamMap
extends immutable.Map[String, String]
with immutable.MapLike[String, String, ParamMap] {
extends ParamMapVersionSpecific with immutable.Map[String, String] {

/**
* Add a key/value pair to the map, returning a new map.
* Overwrites all values if the key exists.
*/
def +[B >: String](kv: (String, B)): ParamMap = {
def setParam[B >: String](kv: (String, B)): ParamMap = {
val (key, value) = (kv._1, kv._2.toString)
val map = MapParamMap.tuplesToMultiMap(iterator.toSeq)
val mapWithKey = map.updated(key, Seq(value))
Expand All @@ -31,7 +30,7 @@ abstract class ParamMap
* Removes a key from this map, returning a new map.
* All values for the key are removed.
*/
def -(name: String): ParamMap = {
def clearParam(name: String): ParamMap = {
val map = MapParamMap.tuplesToMultiMap(iterator.toSeq)
new MapParamMap(map - name, isValid)
}
Expand Down Expand Up @@ -141,14 +140,14 @@ object MapParamMap {
new MapParamMap(MapParamMap.tuplesToMultiMap(params))

def apply(map: Map[String, String]): MapParamMap =
new MapParamMap(map.mapValues { value =>
new MapParamMap(map.transform { case (_, value) =>
Seq(value)
})

private[http] def tuplesToMultiMap(tuples: Seq[(String, String)]): Map[String, Seq[String]] = {
tuples
.groupBy { case (k, v) => k }
.mapValues { values =>
.transform { case (_, values) =>
values.map { _._2 }
}
}
Expand All @@ -160,7 +159,8 @@ object EmptyParamMap extends ParamMap {
def get(name: String): Option[String] = None
def getAll(name: String): Iterable[String] = Nil
def iterator: Iterator[(String, String)] = Iterator.empty
override def -(name: String): ParamMap = this
override def clearParam(name: String): ParamMap = this
override def +[B >: String](kv: (String, B)): ParamMap = MapParamMap(kv._1 -> kv._2.toString)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ final class Uri private (host: Option[String], path: String, query: Option[Strin
case Some(q) => QueryParamDecoder.decodeParams(q)
case None => Collections.emptyMap[String, JList[String]]
}
val map: Map[String, Seq[String]] = decoded.asScala.toMap.mapValues(_.asScala)
val map: Map[String, Seq[String]] = decoded.asScala.toMap.transform {
case (_, v) => v.asScala.toSeq
}
new MapParamMap(map)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import com.twitter.finagle.http.{HeaderMap, Message}
import com.twitter.util.{Return, Throw, Try}
import java.nio.charset.StandardCharsets.{US_ASCII, UTF_8}
import java.util.Base64
import scala.collection.mutable.ArrayBuffer
import scala.collection.compat.immutable.ArraySeq
import scala.collection.mutable.Builder
import scala.util.control.NonFatal

/**
Expand Down Expand Up @@ -91,19 +92,20 @@ object HttpDtab {
* @return a Seq[(String, String)] containing the dtab header entries found.
*/
private[finagle] def strip(msg: Message): Seq[(String, String)] = {
var headerArr: ArrayBuffer[(String, String)] = null
var builder: Builder[(String, String), ArraySeq[(String, String)]] = null
var x = ArraySeq.newBuilder[(String, String)]
val nameValueIt = msg.headerMap.nameValueIterator
while (nameValueIt.hasNext) {
val nameValue = nameValueIt.next()
if (isDtabHeader(nameValue)) {
if (headerArr == null)
headerArr = new ArrayBuffer[(String, String)]()
headerArr += ((nameValue.name, nameValue.value))
if (builder == null)
builder = ArraySeq.newBuilder[(String, String)]
builder += ((nameValue.name, nameValue.value))
msg.headerMap -= nameValue.name
}
}
if (headerArr == null) Nil
else headerArr
if (builder == null) Nil
else builder.result
}

/**
Expand Down Expand Up @@ -170,24 +172,24 @@ object HttpDtab {
*/
private def readXDtabPairs(msg: Message): Try[Dtab] = {
// Common case: no actual overrides.
var keys: ArrayBuffer[String] = null
var builder: Builder[String, ArraySeq[String]] = null
val headers = msg.headerMap.nameValueIterator
while (headers.hasNext) {
val key = headers.next().name.toLowerCase
if (key.startsWith(Prefix)) {
if (keys == null) keys = ArrayBuffer[String]()
keys += key
if (builder == null) builder = ArraySeq.newBuilder[String]
builder += key
}
}

if (keys == null)
if (builder == null)
return EmptyReturn

val keys = builder.mapResult(as => as.sorted).result
if (keys.size % 2 != 0)
return Throw(unmatchedFailure)

keys = keys.sorted
val n = keys.size / 2

val n = keys.size / 2

val dentries = new Array[Dentry](n)
var i = 0
Expand Down

0 comments on commit 92d7d3e

Please sign in to comment.