-
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.
- Loading branch information
1 parent
6fe91d7
commit 63e13d5
Showing
11 changed files
with
127 additions
and
107 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
This file was deleted.
Oops, something went wrong.
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
.../main/scala/io/finch/streaming/Main.scala → ...c/main/scala/io/finch/iteratee/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.streaming | ||
package io.finch.iteratee | ||
|
||
import java.util.concurrent.atomic.AtomicLong | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...la/io/finch/streaming/StreamingSpec.scala → ...ala/io/finch/iteratee/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.streaming | ||
package io.finch.iteratee | ||
|
||
import com.twitter.util.Await | ||
import io.finch.Input | ||
|
39 changes: 39 additions & 0 deletions
39
streaming/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,39 @@ | ||
package io.finch.iteratee | ||
|
||
import java.nio.charset.Charset | ||
|
||
import com.twitter.io.Buf | ||
import com.twitter.util.Future | ||
import io.finch.{Application, Text} | ||
import io.finch.internal._ | ||
import io.iteratee.Enumerator | ||
|
||
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] | ||
type Text[A] = Aux[A, Text.Plain] | ||
} | ||
|
||
trait DecodeInstances { | ||
def instance[A, CT](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) | ||
|
||
implicit def buf2stringDecode[CT <: String]: AsyncDecode.Aux[String, CT] = | ||
instance[String, CT]((enum,cs) => enum.map(_.asString(cs))) | ||
} |
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,68 @@ | ||
package io.finch | ||
|
||
import java.nio.charset.Charset | ||
|
||
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.iteratee.{Enumeratee, Enumerator, Iteratee} | ||
import shapeless.Witness | ||
|
||
/** | ||
* Streaming module | ||
*/ | ||
package object iteratee extends IterateeInstances { | ||
|
||
private[finch] def enumeratorFromReader(reader: Reader, cs: Charset): Enumerator[Future, String] = { | ||
Enumerator.liftM(reader.read(Int.MaxValue)).flatMap({ | ||
case None => Enumerator.empty[Future, String] | ||
case Some(buf) => Enumerator.enumOne[Future, String](buf.asString(cs)).append(enumeratorFromReader(reader, cs)) | ||
}) | ||
} | ||
|
||
/** | ||
* 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](implicit ee: Enumeratee[Future, String, A]): 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(enumeratorFromReader(req.reader, req.charsetOrUtf8).through(ee))) | ||
) | ||
} | ||
} | ||
|
||
final override def item: RequestItem = items.BodyItem | ||
final override def toString: String = "asyncJsonBody" | ||
} | ||
} | ||
|
||
} | ||
|
||
trait IterateeInstances { | ||
|
||
implicit def enumeratorToResponse[A, CT <: String](implicit | ||
encode: Encode.Aux[A, CT], | ||
w: Witness.Aux[CT] | ||
): ToResponse[Enumerator[Future, A]] = { | ||
ToResponse.instance[Enumerator[Future, A], CT]((enum, cs) => { | ||
val response = Response() | ||
response.setChunked(true) | ||
response.contentType = w.value | ||
val writer = response.writer | ||
val iteratee = Iteratee.foldM[Future, Buf, Unit](())((_, buf) => writer.write(buf)).ensure(writer.close()) | ||
enum.map(encode.apply(_, cs)).into(iteratee) | ||
response | ||
}) | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
streaming/src/test/scala/io/finch/streaming/EndpointStreamingSpec.scala
This file was deleted.
Oops, something went wrong.