Created
January 31, 2015 16:29
-
-
Save rklaehn/3aa3215046df2c0a7795 to your computer and use it in GitHub Desktop.
Minimal akka http proxy
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
package akkahttptest | |
import akka.actor.ActorSystem | |
import akka.http.Http | |
import akka.stream.FlowMaterializer | |
import akka.http.server._ | |
import akka.http.marshalling.PredefinedToResponseMarshallers._ | |
import akka.stream.scaladsl.{HeadSink, Source} | |
object Proxy extends App { | |
implicit val system = ActorSystem("Proxy") | |
implicit val materializer = FlowMaterializer() | |
implicit val ec = system.dispatcher | |
val proxy: Route = Route { context => | |
val request = context.request | |
println("Opening connection to " + request.uri.authority.host.address) | |
val flow = Http(system).outgoingConnection(request.uri.authority.host.address, 80).flow | |
Source.single(context.request) | |
.via(flow) | |
.runWith(HeadSink()) | |
.flatMap(r => context.complete(r)) | |
} | |
val binding = Http(system).bind(interface = "localhost", port = 1080) | |
binding.startHandlingWith(proxy) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @adomasGithub,
Request HttpEntity has 3 forms : HttpEntity.Strict, HttpEntity.Default or HttpEntity.Chunked
Only Strict can be consumed multiple times
This is a little tricky because nothing prevents you to build a route using more than ONE Directives that consume this Entity stream.
If you do so you fail with this message without any compilation warning. ( Sad story !! )
Everything works fine, you add some kind of logger and everything fails !!!?
Worse : you test with chrome everything works fine but it won't with firefox => because each browser/version/os might have a different entity size limit to choose if they send it in Strict mode or Default
Depending on your use case one solution could be to make all your requests Strict like in this exemple code :
This solution isn't perfect :
AFAIK there's no way to have garanties that you won't use more than one Directive that consumes the Entity.
One more thing : Don't forget to drain your Entities if you don't need them
Fred