Permalink
Browse files

Make jvalue header lenient if request body is empty.

HttpClientXLightWeb sends text/plain for the Content-Type header if the request
body is empty, even if some other content-type is specified. This changes the
jvalue combinator to be lenient with respect to content-type if the request
body is in fact empty.
  • Loading branch information...
1 parent d2db832 commit 952abf71e915961f08c391c79f178ae50fd82d37 @nuttycom nuttycom committed May 14, 2013
@@ -228,6 +228,8 @@ trait HttpRequestHandlerCombinators {
produce(mimeType) { h }
}
+ def ifRequest[A, B](p: HttpRequest[A] => Boolean)(h: HttpService[A, B]): HttpService[A, B] = new IfRequestService(p, h)
+
/** The aggregate combinator creates a handler that stitches together chunks
* to make a bigger chunk, up to the specified size.
*/
@@ -246,8 +248,14 @@ trait HttpRequestHandlerCombinators {
/** The jvalue combinator creates a handler that accepts and produces JSON.
* Requires an implicit bijection used for transcoding.
*/
- def jvalue[A](h: HttpService[Future[JValue], Future[HttpResponse[JValue]]])(implicit inj: A => Future[JValue], surj: JValue => A, M: Monad[Future]): HttpService[A, Future[HttpResponse[A]]] =
- contentType(MimeTypes.application/MimeTypes.json) { h.contramap(inj) } map { _ map { _ map surj } }
+ def jvalue[A](h: HttpService[Future[JValue], Future[HttpResponse[JValue]]])(implicit inj: A => Future[JValue], surj: JValue => A, M: Monad[Future]): HttpService[A, Future[HttpResponse[A]]] = {
+ ifRequest((_: HttpRequest[A]).content.isDefined) {
+ contentType(MimeTypes.application/MimeTypes.json) { h.contramap(inj) } map { _ map { _ map surj } }
+ } ~
+ ifRequest((_: HttpRequest[A]).content.isEmpty) {
+ produce(MimeTypes.application/MimeTypes.json) { h.contramap(inj) } map { _ map { _ map surj } }
+ }
+ }
/** The xml combinator creates a handler that accepts and produces XML.
* Requires an implicit bijection used for transcoding.
@@ -156,6 +156,15 @@ class DebugService[A, B](logger: Logger, val delegate: HttpService[A, B]) extend
val metadata = NoMetadata
}
+class IfRequestService[A, B](p: HttpRequest[A] => Boolean, val delegate: HttpService[A, B]) extends DelegatingService[A, B, A, B] {
+ val service = (req: HttpRequest[A]) => {
+ if (p(req)) delegate.service(req) else Failure(inapplicable)
+ }
+
+ val metadata = NoMetadata
+}
+
+
class HealthMonitorService[A, B](context: ServiceContext, monitor: HealthMonitor, startTime: Long)(implicit jv2b: JValue => B) extends CustomHttpService[A, Future[HttpResponse[B]]] {
val service = (req: HttpRequest[A]) => Success {
import scalaz.syntax.show._
@@ -109,6 +109,12 @@ class HttpClientXLightWeb(implicit val executor: ExecutionContext) extends HttpC
}
}
+ logger.trace("Executing request of type " + xlrequest.getClass())
+ logger.trace("xlRequest content type: " + xlrequest.getContentType())
+ for (name <- xlrequest.getHeaderNames.asScala) {
+ logger.trace("Header %s has values %s".format(name, xlrequest.getHeaderList(name.asInstanceOf[String]).asScala.mkString("[", ", ", "]")))
+ }
+
xlrequest match {
case e: IHttpRequest =>
clientInstance.send(e, handler)

0 comments on commit 952abf7

Please sign in to comment.