/
Message.scala
437 lines (368 loc) · 14.6 KB
/
Message.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package org.http4s
import java.io.File
import java.net.{InetAddress, InetSocketAddress}
import cats._
import cats.implicits._
import fs2._
import fs2.interop.cats._
import fs2.text._
import org.http4s.headers._
import org.http4s.util.nonEmptyList._
import org.http4s.server.ServerSoftware
import org.http4s.util.CaseInsensitiveString
import org.log4s.getLogger
/**
* Represents a HTTP Message. The interesting subclasses are Request and
* Response while most of the functionality is found in [[MessageSyntax]] and
* [[ResponseOps]]
* @see [[MessageSyntax]], [[ResponseOps]]
*/
sealed trait Message extends MessageOps { self =>
type Self <: Message { type Self = self.Self }
def httpVersion: HttpVersion
def headers: Headers
def body: EntityBody
final def bodyAsText(implicit defaultCharset: Charset = DefaultCharset): Stream[Task, String] = {
(charset getOrElse defaultCharset) match {
case Charset.`UTF-8` =>
// suspect this one is more efficient, though this is superstition
body.through(utf8Decode)
case cs =>
body.through(util.decode(cs))
}
}
/** True if and only if the body is composed solely of Emits and Halt. This
* indicates that the body can be re-run without side-effects. */
// TODO fs2 port need to replace unemit
/*
def isBodyPure: Boolean =
body.unemit._2.isHalt
*/
def attributes: AttributeMap
protected def change(body: EntityBody = body,
headers: Headers = headers,
attributes: AttributeMap = attributes): Self
override def transformHeaders(f: Headers => Headers): Self =
change(headers = f(headers))
override def withAttribute[A](key: AttributeKey[A], value: A): Self =
change(attributes = attributes.put(key, value))
/** Replace the body of this message with a new body
*
* @param b body to attach to this method
* @param w [[EntityEncoder]] with which to convert the body to an [[EntityBody]]
* @tparam T type of the Body
* @return a new message with the new body
*/
def withBody[T](b: T)(implicit w: EntityEncoder[T]): Task[Self] = {
w.toEntity(b).flatMap { entity =>
val hs = entity.length match {
case Some(l) => `Content-Length`.fromLong(l).fold(_ =>
Task.now {
Message.logger.warn(s"Attempt to provide a negative content length of $l")
w.headers.toList
},
cl => Task.now(cl :: w.headers.toList))
case None => Task.now(w.headers)
}
hs.map(newHeaders => change(body = entity.body, headers = headers ++ newHeaders))
}
}
/** Sets the entity body without affecting headers such as `Transfer-Encoding`
* or `Content-Length`. Most use cases are better served by [[withBody]],
* which uses an [[EntityEncoder]] to maintain the headers.
*/
def withBodyStream(body: EntityBody): Self
/** Set an empty entity body on this message, and remove all payload headers
* that make no sense with an empty body.
*/
def withEmptyBody: Self =
withBodyStream(EmptyBody).transformHeaders(_.removePayloadHeaders)
def contentLength: Option[Long] = headers.get(`Content-Length`).map(_.length)
def contentType: Option[`Content-Type`] = headers.get(`Content-Type`)
/** Returns the charset parameter of the `Content-Type` header, if present. Does
* not introspect the body for media types that define a charset
* internally.
*/
def charset: Option[Charset] = contentType.flatMap(_.charset)
def isChunked: Boolean = headers.get(`Transfer-Encoding`).exists(_.values.contains(TransferCoding.chunked))
/** The trailer headers, as specified in Section 3.6.1 of RFC 2616. The
* resulting task might not complete unless the entire body has been
* consumed.
*/
def trailerHeaders: Task[Headers] = attributes.get(Message.Keys.TrailerHeaders).getOrElse(Task.now(Headers.empty))
/** Decode the [[Message]] to the specified type
*
* @param decoder [[EntityDecoder]] used to decode the [[Message]]
* @tparam T type of the result
* @return the `Task` which will generate the `DecodeResult[T]`
*/
override def attemptAs[T](implicit decoder: EntityDecoder[T]): DecodeResult[T] =
decoder.decode(this, strict = false)
}
object Message {
private[http4s] val logger = getLogger
object Keys {
val TrailerHeaders = AttributeKey[Task[Headers]]
}
}
/** Representation of an incoming HTTP message
*
* A Request encapsulates the entirety of the incoming HTTP request including the
* status line, headers, and a possible request body.
*
* @param method [[Method.GET]], [[Method.POST]], etc.
* @param uri representation of the request URI
* @param httpVersion the HTTP version
* @param headers collection of [[Header]]s
* @param body scalaz.stream.Process[Task,Chunk] defining the body of the request
* @param attributes Immutable Map used for carrying additional information in a type safe fashion
*/
sealed abstract case class Request(
method: Method = Method.GET,
uri: Uri = Uri(path = "/"),
httpVersion: HttpVersion = HttpVersion.`HTTP/1.1`,
headers: Headers = Headers.empty,
body: EntityBody = EmptyBody,
attributes: AttributeMap = AttributeMap.empty
) extends Message with RequestOps {
import Request._
type Self = Request
private def requestCopy(
method: Method = this.method,
uri: Uri = this.uri,
httpVersion: HttpVersion = this.httpVersion,
headers: Headers = this.headers,
body: EntityBody = this.body,
attributes: AttributeMap = this.attributes
): Request =
Request(
method = method,
uri = uri,
httpVersion = httpVersion,
headers = headers,
body = body,
attributes = attributes
)
@deprecated(message = "Copy method is unsafe for setting path info. Use with... methods instead", "0.17.0-M3")
def copy(
method: Method = this.method,
uri: Uri = this.uri,
httpVersion: HttpVersion = this.httpVersion,
headers: Headers = this.headers,
body: EntityBody = this.body,
attributes: AttributeMap = this.attributes
): Request =
requestCopy(
method = method,
uri = uri,
httpVersion = httpVersion,
headers = headers,
body = body,
attributes = attributes
)
def withMethod(method: Method) = requestCopy(method = method)
def withUri(uri: Uri) = requestCopy(uri = uri, attributes = attributes -- Request.Keys.PathInfoCaret)
def withHttpVersion(httpVersion: HttpVersion) = requestCopy(httpVersion = httpVersion)
def withHeaders(headers: Headers) = requestCopy(headers = headers)
def withAttributes(attributes: AttributeMap) = requestCopy(attributes = attributes)
def withBodyStream(body: EntityBody): Request =
requestCopy(body = body)
override protected def change(body: EntityBody, headers: Headers, attributes: AttributeMap): Self =
requestCopy(body = body, headers = headers, attributes = attributes)
lazy val authType: Option[AuthScheme] = headers.get(Authorization).map(_.credentials.authScheme)
lazy val (scriptName, pathInfo) = {
val caret = attributes.get(Request.Keys.PathInfoCaret).getOrElse(0)
uri.path.splitAt(caret)
}
def withPathInfo(pi: String): Request =
withUri(uri.withPath(scriptName + pi))
lazy val pathTranslated: Option[File] = attributes.get(Keys.PathTranslated)
def queryString: String = uri.query.renderString
/**
* Representation of the query string as a map
*
* In case a parameter is available in query string but no value is there the
* sequence will be empty. If the value is empty the the sequence contains an
* empty string.
*
* =====Examples=====
* <table>
* <tr><th>Query String</th><th>Map</th></tr>
* <tr><td><code>?param=v</code></td><td><code>Map("param" -> Seq("v"))</code></td></tr>
* <tr><td><code>?param=</code></td><td><code>Map("param" -> Seq(""))</code></td></tr>
* <tr><td><code>?param</code></td><td><code>Map("param" -> Seq())</code></td></tr>
* <tr><td><code>?=value</code></td><td><code>Map("" -> Seq("value"))</code></td></tr>
* <tr><td><code>?p1=v1&p1=v2&p2=v3&p2=v3</code></td><td><code>Map("p1" -> Seq("v1","v2"), "p2" -> Seq("v3","v4"))</code></td></tr>
* </table>
*
* The query string is lazily parsed. If an error occurs during parsing
* an empty `Map` is returned.
*/
def multiParams: Map[String, Seq[String]] = uri.multiParams
/** View of the head elements of the URI parameters in query string.
*
* In case a parameter has no value the map returns an empty string.
*
* @see multiParams
*/
def params: Map[String, String] = uri.params
private lazy val connectionInfo = attributes.get(Keys.ConnectionInfo)
lazy val remote: Option[InetSocketAddress] = connectionInfo.map(_.remote)
lazy val remoteAddr: Option[String] = remote.map(_.getHostString)
lazy val remoteHost: Option[String] = remote.map(_.getHostName)
lazy val remotePort: Option[Int] = remote.map(_.getPort)
lazy val remoteUser: Option[String] = None
lazy val server: Option[InetSocketAddress] = connectionInfo.map(_.local)
lazy val serverAddr: String = {
server.map(_.getHostString)
.orElse(uri.host.map(_.value))
.orElse(headers.get(Host).map(_.host))
.getOrElse(InetAddress.getLocalHost.getHostName)
}
lazy val serverPort: Int = {
server.map(_.getPort)
.orElse(uri.port)
.orElse(headers.get(Host).flatMap(_.port))
.getOrElse(80) // scalastyle:ignore
}
/** Whether the Request was received over a secure medium */
lazy val isSecure: Option[Boolean] = connectionInfo.map(_.secure)
def serverSoftware: ServerSoftware = attributes.get(Keys.ServerSoftware).getOrElse(ServerSoftware.Unknown)
def decodeWith[A](decoder: EntityDecoder[A], strict: Boolean)(f: A => Task[Response]): Task[Response] =
decoder.decode(this, strict = strict).fold(_.toHttpResponse(httpVersion), f).flatten
override def toString: String = {
val newHeaders = headers.redactSensitive()
s"""Request(method=$method, uri=$uri, headers=$newHeaders)"""
}
// A request is idempotent if and only if its method is idempotent and its body
// is pure. If true, this request can be submitted multipe times.
// TODO fs2 port uncomment when isBodyPure is back
/*
def isIdempotent: Boolean =
method.isIdempotent && isBodyPure
*/
}
object Request {
def apply(
method: Method = Method.GET,
uri: Uri = Uri(path = "/"),
httpVersion: HttpVersion = HttpVersion.`HTTP/1.1`,
headers: Headers = Headers.empty,
body: EntityBody = EmptyBody,
attributes: AttributeMap = AttributeMap.empty
) =
new Request(
method = method,
uri = uri,
httpVersion = httpVersion,
headers = headers,
body = body,
attributes = attributes
) {}
final case class Connection(local: InetSocketAddress, remote: InetSocketAddress, secure: Boolean)
object Keys {
val PathInfoCaret = AttributeKey[Int]
val PathTranslated = AttributeKey[File]
val ConnectionInfo = AttributeKey[Connection]
val ServerSoftware = AttributeKey[ServerSoftware]
}
}
/** Represents that a service either returns a [[Response]] or a [[Pass]] to
* fall through to another service.
*/
sealed trait MaybeResponse {
def cata[A](f: Response => A, a: => A): A =
this match {
case r: Response => f(r)
case Pass => a
}
def orElse[B >: Response](b: => B): B =
this match {
case r: Response => r
case Pass => b
}
def orNotFound: Response =
orElse(Response(Status.NotFound))
def toOption: Option[Response] =
cata(Some(_), None)
}
object MaybeResponse {
/** Intentionally not implicit. Do not use unless you've read and understood the
* deprecation warning on [[instance]].
*/
val monoidInstance: Monoid[MaybeResponse] =
new Monoid[MaybeResponse] {
def empty =
Pass
def combine(a: MaybeResponse, b: MaybeResponse) =
a orElse b
}
@deprecated(
"""We derive `Monoid[Kleisli[Task, Request, MaybeResponse]]`
|from `Monoid[Task[MaybeResponse]]`, which needs to have the
|"orElse"-like semantics of `taskInstance`. The presence of
|this monoid makes it easy to locate a monoid that evaluates
|both effects, which means that when chaining `HttpService`s,
|both services run even if the first service does not return
|`Pass`. To ensure that a correct monoid is resolved for this
|use case, use `taskInstance` instead. If you really need a
|`Monoid[MaybeResponse]` and understand how it can hurt you,
|use `monoidInstance`.""".stripMargin,
"0.16.3")
implicit val instance: Monoid[MaybeResponse] =
monoidInstance
implicit val taskInstance: Monoid[Task[MaybeResponse]] =
new Monoid[Task[MaybeResponse]] {
def empty =
Pass.now
def combine(ta: Task[MaybeResponse], tb: Task[MaybeResponse]): Task[MaybeResponse] =
ta.flatMap(_.cata(Task.now, tb))
}
}
case object Pass extends MaybeResponse {
val now: Task[MaybeResponse] =
Task.now(Pass)
}
/** Representation of the HTTP response to send back to the client
*
* @param status [[Status]] code and message
* @param headers [[Headers]] containing all response headers
* @param body scalaz.stream.Process[Task,Chunk] representing the possible body
* of the response
* @param attributes [[AttributeMap]] containing additional parameters which
* may be used by the http4s backend for additional processing such as
* java.io.File object
*/
final case class Response(
status: Status = Status.Ok,
httpVersion: HttpVersion = HttpVersion.`HTTP/1.1`,
headers: Headers = Headers.empty,
body: EntityBody = EmptyBody,
attributes: AttributeMap = AttributeMap.empty)
extends Message with MaybeResponse with ResponseOps {
type Self = Response
override def withStatus(status: Status): Self =
copy(status = status)
def withBodyStream(body: EntityBody): Response =
copy(body = body)
override protected def change(body: EntityBody, headers: Headers, attributes: AttributeMap): Self =
copy(body = body, headers = headers, attributes = attributes)
override def toString: String = {
val newHeaders = headers.redactSensitive()
s"""Response(status=${status.code}, headers=$newHeaders)"""
}
/** Returns a list of cookies from the [[org.http4s.headers.Set-Cookie]]
* headers. Includes expired cookies, such as those that represent cookie
* deletion. */
def cookies: List[Cookie] =
`Set-Cookie`.from(headers).map(_.cookie)
}
object Response {
@deprecated("Use Pass.now instead", "0.16")
val fallthrough: Task[MaybeResponse] =
Pass.now
def notFound(request: Request): Task[Response] = {
val body = s"${request.pathInfo} not found"
Response(Status.NotFound).withBody(body)
}
}