Skip to content

Commit

Permalink
Removing memory leak.
Browse files Browse the repository at this point in the history
  • Loading branch information
luksow committed Jun 26, 2017
1 parent a1f5f8c commit 72b348e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ It's in a very early stage, so please report bugs through GitHub issues or via e
cors-buster requires Java 8 to work properly.

```
wget https://bintray.com/artifact/download/iterators/cors-buster/cors-buster.jar
java -jar cors-buster.jar proxyHost proxyPort serverHost serverPort
wget https://bintray.com/artifact/download/iterators/cors-buster/cors-buster-1.0.1.jar
java -jar cors-buster-1.0.1.jar proxyHost proxyPort serverHost serverPort
```

For example
```
java -jar cors-buster.jar 0.0.0.0 8080 localhost 9000
java -jar cors-buster-1.0.1.jar 0.0.0.0 8080 localhost 9000
```
will set up a proxy listening on 0.0.0.0:8080 and will forward all requests to server running on localhost:9000.

Expand Down
9 changes: 5 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
name := "cors-buster"
organization := "pl.iterators"
version := "1.0.0"
scalaVersion := "2.11.7"
version := "1.0.1"
scalaVersion := "2.12.2"

scalacOptions := Seq("-target:jvm-1.8", "-unchecked", "-deprecation", "-encoding", "utf8")

libraryDependencies ++= {
val akkaV = "2.4.2"
val akkaV = "2.5.2"
val akkaHttpV = "10.0.7"
Seq(
"com.typesafe.akka" %% "akka-actor" % akkaV,
"com.typesafe.akka" %% "akka-stream" % akkaV,
"com.typesafe.akka" %% "akka-http-core" % akkaV
"com.typesafe.akka" %% "akka-http" % akkaHttpV
)
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/scala/CorsBuster.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import akka.http.scaladsl.Http
import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.model.{HttpHeader, HttpMethods, HttpResponse, HttpRequest}
import akka.http.scaladsl.model.{HttpResponse, HttpRequest}
import akka.stream._
import akka.stream.scaladsl.{Merge, Broadcast, GraphDSL, Flow}

Expand All @@ -26,10 +26,8 @@ object CorsBuster extends App {
implicit val materializer = ActorMaterializer()

val requestFlow = Flow.fromFunction[HttpRequest, HttpRequest] { request =>
request.entity.discardBytes()
request
.withUri(request.uri.withAuthority(config.serverHost, config.serverPort))
.mapHeaders(headers => headers.filterNot(_.lowercaseName() == Host.lowercaseName))
.addHeader(Host(config.serverHost, config.serverPort))
}

val optionRequestFlow = Flow.fromFunction[HttpRequest, HttpResponse] { request =>
Expand All @@ -55,15 +53,17 @@ object CorsBuster extends App {
val requestResponseFlow = Flow.fromGraph(GraphDSL.create() { implicit b =>
import GraphDSL.Implicits._

val request = b.add(requestFlow)
val broadcast = b.add(Broadcast[HttpRequest](2))
val merge = b.add(Merge[HttpResponse](2))
val outFlow = b.add(Flow[HttpResponse])

request ~> broadcast
broadcast.filter(_.method == OPTIONS) ~> optionRequestFlow ~> merge ~> responseFlow ~> outFlow
broadcast.filter(_.method != OPTIONS) ~> standardRequestFlow ~> merge

FlowShape(broadcast.in, outFlow.out)
FlowShape(request.in, outFlow.out)
})

Http().bindAndHandle(requestResponseFlow, config.proxyHost, config.proxyPort)
}
}

0 comments on commit 72b348e

Please sign in to comment.