Skip to content

Commit

Permalink
Added proxying support to the server.
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Oct 10, 2016
1 parent 680a1a8 commit 46d5d4b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 55 deletions.
53 changes: 53 additions & 0 deletions core/jvm/src/main/scala/org/hyperscala/Handler.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.hyperscala

import io.undertow.server.{HttpHandler, HttpServerExchange}

trait Handler extends Ordered[Handler] {
/**
* Returns true if this URL should be handled by this Handler. The handleRequest method will be invoked following
* a true response.
*
* @param url the current URL being handled
* @return true if this handler is equipped to handle it
*/
def isURLMatch(url: URL): Boolean

/**
* Handles the exchange.
*
* @param url the current URL
* @param exchange the HTTP request
*/
def handleRequest(url: URL, exchange: HttpServerExchange): Unit

/**
* The priority of this handler. A higher priority will be considered before lower priority.
*/
def priority: Priority

override def compare(that: Handler): Int = priority.compare(that.priority)
}

object Handler {
def path(paths: Set[String], handler: HttpHandler, priority: Priority = Priority.Normal): Handler = {
val p = priority
new Handler {
def isURLMatch(url: URL): Boolean = paths.contains(url.path)

override def handleRequest(url: URL, exchange: HttpServerExchange): Unit = handler.handleRequest(exchange)

override def priority: Priority = p
}
}

def apply(handler: HttpHandler, priority: Priority = Priority.Normal): Handler = {
val p = priority
new Handler {
def isURLMatch(url: URL): Boolean = true

override def handleRequest(url: URL, exchange: HttpServerExchange): Unit = handler.handleRequest(exchange)

override def priority: Priority = p
}
}
}
31 changes: 31 additions & 0 deletions core/jvm/src/main/scala/org/hyperscala/ProxyHandler.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.hyperscala
import java.net.URI

import io.undertow.Handlers
import io.undertow.server.HttpServerExchange
import io.undertow.server.handlers.proxy.SimpleProxyClientProvider

trait ProxyHandler extends Handler {
def uri: URI

private lazy val proxyClient = new SimpleProxyClientProvider(uri)
private lazy val proxyHandler = Handlers.proxyHandler(proxyClient)

override def handleRequest(url: URL, exchange: HttpServerExchange): Unit = {
proxyHandler.handleRequest(exchange)
}
}

object ProxyHandler {
def apply(matcher: URL => Boolean, uri: String, priority: Priority = Priority.Normal): ProxyHandler = {
val u = new URI(uri)
val p = priority
new ProxyHandler {
override def uri: URI = u

override def isURLMatch(url: URL): Boolean = matcher(url)

override def priority: Priority = p
}
}
}
58 changes: 3 additions & 55 deletions core/jvm/src/main/scala/org/hyperscala/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package org.hyperscala

import com.outr.scribe.Logging
import io.undertow.server.session.{SessionAttachmentHandler, SessionConfig, SessionCookieConfig, Session => UndertowSession}
import io.undertow.server.{DefaultResponseListener, HttpHandler, HttpServerExchange}
import io.undertow.server.{HttpHandler, HttpServerExchange}
import io.undertow.util.{Headers, Sessions, StatusCodes}
import io.undertow.websockets.WebSocketConnectionCallback
import io.undertow.{Handlers, Undertow}
import io.undertow.{Handlers, Undertow, UndertowOptions}

import scala.collection.immutable.SortedSet
import scala.language.experimental.macros
import scala.language.implicitConversions
import scala.util.matching.Regex

class Server(host: String, port: Int, sessionDomain: Option[String] = None) extends Logging {
private val handler = new ServerHandler
Expand All @@ -25,7 +23,6 @@ class Server(host: String, port: Int, sessionDomain: Option[String] = None) exte
private val resourceHandler = new FunctionalResourceHandler(resourceManager)
register(resourceHandler)

// private var defaultHandler: Option[PathHandler] = None
var errorHandler: Handler = new Handler {
override def isURLMatch(url: URL): Boolean = false

Expand All @@ -50,6 +47,7 @@ class Server(host: String, port: Int, sessionDomain: Option[String] = None) exte

def start(): Unit = synchronized {
val server = Undertow.builder()
.setServerOption(UndertowOptions.ENABLE_HTTP2, java.lang.Boolean.TRUE)
.addHttpListener(port, host)
.setHandler(sessionAttachmentHandler)
.build()
Expand Down Expand Up @@ -189,54 +187,4 @@ object Server extends Logging {

server
}
}

trait Handler extends Ordered[Handler] {
/**
* Returns true if this URL should be handled by this Handler. The handleRequest method will be invoked following
* a true response.
*
* @param url the current URL being handled
* @return true if this handler is equipped to handle it
*/
def isURLMatch(url: URL): Boolean

/**
* Handles the exchange.
*
* @param url the current URL
* @param exchange the HTTP request
*/
def handleRequest(url: URL, exchange: HttpServerExchange): Unit

/**
* The priority of this handler. A higher priority will be considered before lower priority.
*/
def priority: Priority

override def compare(that: Handler): Int = priority.compare(that.priority)
}

object Handler {
def path(paths: Set[String], handler: HttpHandler, priority: Priority = Priority.Normal): Handler = {
val p = priority
new Handler {
def isURLMatch(url: URL): Boolean = paths.contains(url.path)

override def handleRequest(url: URL, exchange: HttpServerExchange): Unit = handler.handleRequest(exchange)

override def priority: Priority = p
}
}

def apply(handler: HttpHandler, priority: Priority = Priority.Normal): Handler = {
val p = priority
new Handler {
def isURLMatch(url: URL): Boolean = true

override def handleRequest(url: URL, exchange: HttpServerExchange): Unit = handler.handleRequest(exchange)

override def priority: Priority = p
}
}
}

0 comments on commit 46d5d4b

Please sign in to comment.