@@ -2,22 +2,46 @@ package server.http.headers
22
33import scala .collection .mutable .HashMap
44
5- // TODO: has to be case insensitive!
6- class Headers extends HashMap [ String , String ]
7- object Headers {
8- val CONTENT_LENGTH = " Content-Length "
9- val CONTENT_TYPE = " Content-Type "
10- val TRANSFER_ENCODING = " Transfer-Encoding "
11- val ACCEPT_ENCODING = " Accept-Encoding "
12- val CONNECTION = " Connection "
13- }
5+ class HeaderKey ( private val key : String ) {
6+ protected lazy val keyLower = if (key != null ) key.toLowerCase else null
7+
8+ override def equals ( other : scala. Any ) : Boolean = {
9+ if (other == null || ! other. isInstanceOf [ HeaderKey ]) {
10+ return false
11+ }
12+
13+ val otherKeyLower = other. asInstanceOf [ HeaderKey ].keyLower
1414
15- /*
16- class HeaderName(name: String) {
17- override def hashCode(): Int = name.toLowerCase.hashCode
15+ if (keyLower == null ) {
16+ return otherKeyLower == null
17+ }
1818
19- override def equals(obj: scala.Any): Boolean = obj match {
20- case other: HeaderName
19+ keyLower.equals(otherKeyLower)
2120 }
21+
22+ override def hashCode (): Int = {
23+ if (keyLower != null ) keyLower.hashCode() else 0
24+ }
25+
26+ override def toString : String = key
2227}
23- */
28+
29+ class Headers extends HashMap [HeaderKey , String ] {
30+ def put (key : String , value : String ): Option [String ] = put(new HeaderKey (key), value)
31+
32+ def get (key : String ): Option [String ] = get(new HeaderKey (key))
33+
34+ def getOrElse [B1 >: String ](key : String , default : => B1 ): B1 = getOrElse(new HeaderKey (key), default)
35+
36+ def contains (key : String ): Boolean = contains(new HeaderKey (key))
37+
38+ def += (kv : (String , String )): Headers = += (new HeaderKey (kv._1) -> kv._2)
39+ }
40+
41+ object Headers {
42+ val CONTENT_LENGTH = new HeaderKey (" Content-Length" )
43+ val CONTENT_TYPE = new HeaderKey (" Content-Type" )
44+ val TRANSFER_ENCODING = new HeaderKey (" Transfer-Encoding" )
45+ val ACCEPT_ENCODING = new HeaderKey (" Accept-Encoding" )
46+ val CONNECTION = new HeaderKey (" Connection" )
47+ }
0 commit comments