-
Notifications
You must be signed in to change notification settings - Fork 787
/
Accept-Charset.scala
88 lines (72 loc) · 2.81 KB
/
Accept-Charset.scala
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
/*
* Copyright 2013 http4s.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.http4s
package headers
import cats.data.NonEmptyList
import cats.parse.Parser
import org.http4s.CharsetRange.*
import org.http4s.CharsetRange.Atom
import org.typelevel.ci._
object `Accept-Charset` {
def apply(head: CharsetRange, tail: CharsetRange*): `Accept-Charset` =
apply(NonEmptyList(head, tail.toList))
def parse(s: String): ParseResult[`Accept-Charset`] =
ParseResult.fromParser(parser, "Invalid Accept-Charset header")(s)
private[http4s] val parser: Parser[`Accept-Charset`] = {
import cats.parse.Parser._
import org.http4s.internal.parsing.CommonRules._
val anyCharset = (char('*') *> QValue.parser)
.map(q => if (q != QValue.One) `*`.withQValue(q) else `*`)
val fromToken = (token ~ QValue.parser).mapFilter { case (s, q) =>
// TODO handle tokens that aren't charsets
Charset
.fromString(s)
.toOption
.map { c =>
if (q != QValue.One) c.withQuality(q) else c.toRange
}
}
val charsetRange = anyCharset.orElse(fromToken)
headerRep1(charsetRange).map(xs => `Accept-Charset`(xs.head, xs.tail: _*))
}
implicit val headerInstance: Header[`Accept-Charset`, Header.Recurring] =
Header.createRendered(
ci"Accept-Charset",
_.values,
parse,
)
implicit val headerSemigroupInstance: cats.Semigroup[`Accept-Charset`] =
(a, b) => `Accept-Charset`(a.values.concatNel(b.values))
}
/** {{{
* The "Accept-Charset" header field can be sent by a user agent to
* indicate what charsets are acceptable in textual response content.
* This field allows user agents capable of understanding more
* }}}
*
* From [https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.3 RFC-7231].
*/
final case class `Accept-Charset`(values: NonEmptyList[CharsetRange]) {
def qValue(charset: Charset): QValue = {
def specific =
values.collectFirst { case cs: Atom if cs.matches(charset) => cs.qValue }
def splatted =
values.collectFirst { case cs: CharsetRange.`*` => cs.qValue }
specific.orElse(splatted).getOrElse(QValue.Zero)
}
def satisfiedBy(charset: Charset): Boolean = qValue(charset) > QValue.Zero
def map(f: CharsetRange => CharsetRange): `Accept-Charset` = `Accept-Charset`(values.map(f))
}