From 5362d834c97b694c33803cd2701462b443838f6d Mon Sep 17 00:00:00 2001 From: Christoffer Ahlbin Date: Mon, 23 Feb 2015 23:37:12 +0100 Subject: [PATCH] Add support for specifying ParrotRequest body as a ByteArrayInputStream This allows sending parrot requests with a body that are to be interpreted as Content-Type: application/octet-stream. --- .../parrot/server/FinagleTransport.scala | 17 +++++++++++++---- .../twitter/parrot/server/ParrotRequest.scala | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/scala/com/twitter/parrot/server/FinagleTransport.scala b/src/main/scala/com/twitter/parrot/server/FinagleTransport.scala index a68d29a..08cabe2 100644 --- a/src/main/scala/com/twitter/parrot/server/FinagleTransport.scala +++ b/src/main/scala/com/twitter/parrot/server/FinagleTransport.scala @@ -101,10 +101,19 @@ class FinagleTransport(service: FinagleServiceAbstraction, config: ParrotServerC httpRequest.setHeader("X-Forwarded-For", randomIp) } - if ((request.method == "POST" || request.method == "PUT") && request.body.length > 0) { - val buffer = ChannelBuffers.copiedBuffer(BIG_ENDIAN, request.body, UTF_8) - httpRequest.setHeader(CONTENT_LENGTH, buffer.readableBytes) - httpRequest.setContent(buffer) + if ((request.method == "POST" || request.method == "PUT") && + (request.bodyInputStream.isDefined || request.body.length > 0)) { + if (request.bodyInputStream.isDefined) { + val inputStream = request.bodyInputStream.get + val buffer = ChannelBuffers.buffer(inputStream.available) + buffer.writeBytes(inputStream, inputStream.available) + httpRequest.setHeader(CONTENT_LENGTH, buffer.readableBytes) + httpRequest.setContent(buffer) + } else { + val buffer = ChannelBuffers.copiedBuffer(BIG_ENDIAN, request.body, UTF_8) + httpRequest.setHeader(CONTENT_LENGTH, buffer.readableBytes) + httpRequest.setContent(buffer) + } } allRequests += 1 diff --git a/src/main/scala/com/twitter/parrot/server/ParrotRequest.scala b/src/main/scala/com/twitter/parrot/server/ParrotRequest.scala index d823972..bdc12b9 100644 --- a/src/main/scala/com/twitter/parrot/server/ParrotRequest.scala +++ b/src/main/scala/com/twitter/parrot/server/ParrotRequest.scala @@ -30,6 +30,7 @@ class ParrotRequest( val cookies: Seq[(String, String)] = Seq(), val method: String = "GET", val body: String = "", + val bodyInputStream: Option[java.io.ByteArrayInputStream] = None, val weight: Int = 1 ) { val headers: Seq[(String, String)] =