-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
182 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...c/main/scala/io/finch/iteratee/Main.scala → .../main/scala/io/finch/streaming/Main.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.finch.iteratee | ||
package io.finch.streaming | ||
|
||
import java.util.concurrent.atomic.AtomicLong | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...ala/io/finch/iteratee/StreamingSpec.scala → ...la/io/finch/streaming/StreamingSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.finch.iteratee | ||
package io.finch.streaming | ||
|
||
import com.twitter.util.Await | ||
import io.finch.Input | ||
|
40 changes: 40 additions & 0 deletions
40
iteratee/src/main/scala/io/finch/iteratee/AsyncDecode.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.finch.iteratee | ||
|
||
import java.nio.charset.Charset | ||
|
||
import com.twitter.io.Buf | ||
import com.twitter.util.Future | ||
import io.catbird.util._ | ||
import io.finch.Application | ||
import io.finch.internal._ | ||
import io.iteratee.Enumerator | ||
|
||
/** | ||
* Decodes an HTTP streamed payload represented as [[Enumerator]] (encoded with [[Charset]]) into | ||
* an [[Enumerator]] of arbitrary type `A`. | ||
*/ | ||
trait AsyncDecode[A] { | ||
|
||
type ContentType <: String | ||
|
||
def apply(enumerator: Enumerator[Future, Buf], cs: Charset): Enumerator[Future, A] | ||
} | ||
|
||
object AsyncDecode extends DecodeInstances { | ||
|
||
type Aux[A, CT <: String] = AsyncDecode[A] {type ContentType = CT} | ||
|
||
type Json[A] = Aux[A, Application.Json] | ||
} | ||
|
||
trait DecodeInstances { | ||
def instance[A, CT <: String] | ||
(f: (Enumerator[Future, Buf], Charset) => Enumerator[Future, A]): AsyncDecode.Aux[A, CT] = new AsyncDecode[A] { | ||
override type ContentType = CT | ||
|
||
override def apply(enumerator: Enumerator[Future, Buf], cs: Charset): Enumerator[Future, A] = f(enumerator, cs) | ||
} | ||
|
||
implicit def buf2bufDecode[CT <: String]: AsyncDecode.Aux[Buf, CT] = | ||
instance[Buf, CT]((enum, _) => enum) | ||
} |
37 changes: 37 additions & 0 deletions
37
iteratee/src/main/scala/io/finch/iteratee/AsyncEncode.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.finch.iteratee | ||
|
||
import java.nio.charset.Charset | ||
|
||
import com.twitter.io.Buf | ||
import com.twitter.util.Future | ||
import io.finch.Application | ||
import io.iteratee.Enumerator | ||
|
||
trait AsyncEncode[A] { | ||
|
||
type ContentType <: String | ||
|
||
def apply(enumerator: Enumerator[Future, A], cs: Charset): Enumerator[Future, Buf] | ||
} | ||
|
||
object AsyncEncode extends EncodeInstances { | ||
|
||
type Aux[A, CT <: String] = AsyncEncode[A] { type ContentType = CT } | ||
|
||
type Json[A] = Aux[A, Application.Json] | ||
} | ||
|
||
trait EncodeInstances { | ||
|
||
def instance[A, CT <: String] | ||
(f: (Enumerator[Future, A], Charset) => Enumerator[Future, Buf]): AsyncEncode.Aux[A, CT] = { | ||
new AsyncEncode[A] { | ||
|
||
override type ContentType = CT | ||
|
||
override def apply(enumerator: Enumerator[Future, A], cs: Charset): Enumerator[Future, Buf] = f(enumerator, cs) | ||
} | ||
} | ||
|
||
implicit def buf2bufEncode[CT <: String]: AsyncEncode.Aux[Buf, CT] = instance((enum, _) => enum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.finch | ||
|
||
import com.twitter.finagle.http.Response | ||
import com.twitter.io.{Buf, Reader} | ||
import com.twitter.util.Future | ||
import io.catbird.util._ | ||
import io.finch.internal._ | ||
import io.finch.items.RequestItem | ||
import io.finch.iteratee.AsyncEncode | ||
import io.iteratee.Enumerator | ||
import io.iteratee.twitter.FutureModule | ||
import shapeless.Witness | ||
|
||
/** | ||
* Iteratee module | ||
*/ | ||
package object iteratee extends IterateeInstances { | ||
|
||
import syntax._ | ||
|
||
private[finch] def enumeratorFromReader(reader: Reader): Enumerator[Future, Buf] = { | ||
reader.read(Int.MaxValue).intoEnumerator.flatMap({ | ||
case None => empty[Buf] | ||
case Some(buf) => enumOne(buf).append(enumeratorFromReader(reader)) | ||
}) | ||
} | ||
|
||
/** | ||
* An evaluating [[Endpoint]] that reads a required chunked streaming binary body, interpreted as | ||
* an `Enumerator[Future, A]`. The returned [[Endpoint]] only matches chunked (streamed) requests. | ||
*/ | ||
def enumeratorBody[A, CT <: String](implicit decode: AsyncDecode.Aux[A, CT]): Endpoint[Enumerator[Future, A]] = { | ||
new Endpoint[Enumerator[Future, A]] { | ||
final def apply(input: Input): Endpoint.Result[Enumerator[Future, A]] = { | ||
if (!input.request.isChunked) { | ||
EndpointResult.Skipped | ||
} else { | ||
val req = input.request | ||
EndpointResult.Matched( | ||
input, | ||
Rerunnable(Output.payload(decode(enumeratorFromReader(req.reader), req.charsetOrUtf8))) | ||
) | ||
} | ||
} | ||
|
||
final override def item: RequestItem = items.BodyItem | ||
final override def toString: String = "asyncJsonBody" | ||
} | ||
} | ||
|
||
/** | ||
* An evaluating [[Endpoint]] that reads a required chunked streaming JSON body, interpreted as | ||
* an `Enumerator[Future, A]`. The returned [[Endpoint]] only matches chunked (streamed) requests. | ||
*/ | ||
def enumeratorJsonBody[A](implicit ad: AsyncDecode.Aux[A, Application.Json]): Endpoint[Enumerator[Future, A]] = | ||
enumeratorBody[A, Application.Json] | ||
|
||
} | ||
|
||
trait IterateeInstances extends FutureModule { | ||
|
||
implicit def enumeratorToResponse[A, CT <: String](implicit | ||
ae: AsyncEncode.Aux[A, CT], | ||
w: Witness.Aux[CT] | ||
): ToResponse.Aux[Enumerator[Future, A], CT] = { | ||
ToResponse.instance[Enumerator[Future, A], CT]((enum, cs) => { | ||
val response = Response() | ||
response.setChunked(true) | ||
response.contentType = w.value | ||
val writer = response.writer | ||
val iteratee = foreachM((buf: Buf) => writer.write(buf)) | ||
ae(enum, cs).into(iteratee).ensure(writer.close()) | ||
response | ||
}) | ||
} | ||
|
||
} |
39 changes: 0 additions & 39 deletions
39
streaming/src/main/scala/io/finch/iteratee/AsyncDecode.scala
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.