Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Add support for specifying ParrotRequest body as a ByteArrayInputStream #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/main/scala/com/twitter/parrot/server/FinagleTransport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have request.bodyInputStream.isDefined -- what does it do that request.body.length > 0 doesn't? Is it that we want to support zero length messages?

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)] =
Expand Down