Skip to content

Commit

Permalink
merged with josh branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Lagutko committed Nov 12, 2010
2 parents e88cfd5 + c45e9f5 commit 3828cc4
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion project/plugins/project/build.properties
@@ -1,3 +1,3 @@
#Project properties
#Fri Oct 22 08:50:15 MDT 2010
#Thu Nov 11 17:25:46 MST 2010
plugin.uptodate=true
14 changes: 12 additions & 2 deletions src/main/scala/blueeyes/core/http/HttpIp.scala
Expand Up @@ -19,17 +19,27 @@ sealed trait HttpIp {

object HttpIps {

def ipRegex = """((\d){1,3}\.){3}(\d){1,3}""".r

def parseHttpIps(inString: String): Array[HttpIp] = {

def ipRegex = """((\d){1,3}\.){3}(\d){1,3}""".r

def inetAddresses: Array[InetAddress] = ipRegex.findAllIn(inString).toArray.map(x => InetAddress.getByName(x)).filterNot(_ == null)
def customIps: Array[HttpIp] = inetAddresses.map(x => CustomIP(x))
return customIps

}

def parseSingleIp(inString: String): Option[HttpIp] = {

def httpIp: Option[HttpIp] = ipRegex.findFirstIn(inString).map(x => InetAddress.getByName(x)) match {
case None => None
case Some(null) => None
case Some(d) => Some(CustomIP(d))
}

return httpIp
}

case class CustomIP(ip: InetAddress) extends HttpIp

}
Expand Down
25 changes: 14 additions & 11 deletions src/main/scala/blueeyes/core/service/Converters.scala
Expand Up @@ -2,12 +2,15 @@ package blueeyes.core.service

import org.jboss.netty.handler.codec.http.{QueryStringDecoder, HttpResponseStatus, DefaultHttpResponse, HttpMethod => NettyHttpMethod, HttpResponse => NettyHttpResponse, HttpVersion => NettyHttpVersion, HttpRequest => NettyHttpRequest}
import scala.collection.JavaConversions._
import blueeyes.core.http.HttpIp
import blueeyes.core.http.HttpIps

import org.jboss.netty.util.CharsetUtil
import blueeyes.core.http._
import scala.collection.JavaConversions._

import HttpHeaders._
import HttpVersions._
import blueeyes.core.http.HttpHeaders._
import blueeyes.core.http.HttpVersions._
import org.jboss.netty.buffer.{ChannelBuffer, ChannelBuffers}
import java.net.{SocketAddress, InetSocketAddress}

Expand Down Expand Up @@ -56,15 +59,15 @@ object Converters {
}

implicit def fromNettyRequest[T, S](request: NettyHttpRequest, pathParameters: Map[Symbol, String], remoteAddres: SocketAddress, transcoder: HttpDataTranscoder[T, S]): HttpRequest[T] = {
val queryStringDecoder = new QueryStringDecoder(request.getUri())
val params = pathParameters ++ queryStringDecoder.getParameters().map(param => (Symbol(param._1), if (!param._2.isEmpty) param._2.head else "")).toMap
val headers = buildHeaders(request.getHeaders())
val nettyContent = request.getContent()
val content = if (nettyContent.readable()) Some(fromChannelBuffer(nettyContent, transcoder)) else None
val remoteHost = remoteAddres match {
case x: InetSocketAddress => Some(x.getAddress())
case _ => None
}
val queryStringDecoder = new QueryStringDecoder(request.getUri())
val params = pathParameters ++ queryStringDecoder.getParameters().map(param => (Symbol(param._1), if (!param._2.isEmpty) param._2.head else "")).toMap
val headers = buildHeaders(request.getHeaders())
val nettyContent = request.getContent()
val content = if (nettyContent.readable()) Some(fromChannelBuffer(nettyContent, transcoder)) else None

val xforwarded: Option[HttpHeaders.`X-Forwarded-For`] = (for (`X-Forwarded-For`(value) <- headers) yield `X-Forwarded-For`(value: _*)).headOption

val remoteHost = xforwarded.flatMap(_.ips.headOption.map(_.ip)).orElse(Some(remoteAddres).collect { case x: InetSocketAddress => x.getAddress })

HttpRequest(fromNettyMethod(request.getMethod), request.getUri, params, headers, content, remoteHost, fromNettyVersion(request.getProtocolVersion()))
}
Expand Down
Expand Up @@ -15,8 +15,7 @@ class NettyRequestHandler(hierarchies: List[RestHierarchy[_]]) extends SimpleCha
override def messageReceived(ctx: ChannelHandlerContext, e: MessageEvent) {
val request = e.getMessage().asInstanceOf[NettyHttpRequest]
val method = fromNettyMethod(request.getMethod())

val requestUri = new QueryStringDecoder(request.getUri).getPath
val requestUri = new QueryStringDecoder(request.getUri).getPath

val builders = hierarchies.map(new RequestBuilder(e, _))
val builder = builders.find(_.isDefinedAt((requestUri, method))).getOrElse(new NotFoundBuilder())
Expand Down
24 changes: 23 additions & 1 deletion src/test/scala/blueeyes/core/service/ConvertersSpec.scala
Expand Up @@ -36,7 +36,9 @@ class ConvertersSpec extends Specification {
nettyResponse.getProtocolVersion mustEqual(NettyHttpVersion.HTTP_1_0)
Map(nettyResponse.getHeaders.map(header => (header.getKey(), header.getValue())): _*) mustEqual(Map("retry-after" -> "1", "Content-Type" -> "text/html"))
}

"convert netty NettyHttpRequest to service HttpRequest" in {

val nettyRequest = new DefaultHttpRequest(NettyHttpVersion.HTTP_1_0, NettyHttpMethod.GET, "http://foo/bar?param1=value1")
nettyRequest.setContent(ChannelBuffers.wrappedBuffer("12".getBytes))
nettyRequest.setHeader("retry-after", "1")
Expand All @@ -52,7 +54,27 @@ class ConvertersSpec extends Specification {
request.version mustEqual(`HTTP/1.0`)
request.remoteHost mustEqual(Some(address.getAddress()))
}
"convert netty NettyHttpRequest with multiple jeaders values to service HttpRequest" in {

"convert netty NettyHttpRequest to service NettyHttpRequest, modifying ip if X-Forwarded-For header present" in {
val nettyRequest = new DefaultHttpRequest(NettyHttpVersion.HTTP_1_0, NettyHttpMethod.GET, "http://foo/bar?param1=value1")
nettyRequest.setContent(ChannelBuffers.wrappedBuffer("12".getBytes))
nettyRequest.setHeader("retry-after", "1")
nettyRequest.setHeader("X-Forwarded-For", "111.11.11.1, 121.21.2.2")

val address = new InetSocketAddress("127.0.0.0", 8080)
val forwardedAddress = new InetSocketAddress("111.11.11.1", 8080)
val request = fromNettyRequest(nettyRequest, Map('pathParam1 -> "value"), address, transcoder)

request.method mustEqual(HttpMethods.GET)
request.uri mustEqual("http://foo/bar?param1=value1")
request.parameters mustEqual(Map('param1 -> "value1", 'pathParam1 -> "value"))
request.headers mustEqual(Map("retry-after" -> "1", "X-Forwarded-For" -> "111.11.11.1, 121.21.2.2"))
request.content mustEqual(Some("12"))
request.version mustEqual(`HTTP/1.0`)
request.remoteHost mustEqual(Some(forwardedAddress.getAddress()))
}

"convert netty NettyHttpRequest with multiple headers values to service HttpRequest" in {
val nettyRequest = new DefaultHttpRequest(NettyHttpVersion.HTTP_1_0, NettyHttpMethod.GET, "http://foo/bar?param1=value1")
nettyRequest.setContent(ChannelBuffers.wrappedBuffer("12".getBytes))
nettyRequest.addHeader("retry-after", "1")
Expand Down
Expand Up @@ -51,7 +51,7 @@ class HttpServerNettySpec extends Specification{
val response = future.get
response.getStatusCode mustEqual (HttpStatusCodes.OK.value)
response.getResponseBody mustEqual (Context.context)
}
}

"return not found error by wrong URI" in{
val client = new AsyncHttpClient()
Expand Down
Expand Up @@ -62,7 +62,7 @@ class NettyRequestHandlerSpec extends Specification with MockitoSugar {
}

class TestService extends RestHierarchyBuilder[String]{
path("bar" / 'adId / "adCode.html"){get(handler)}
path("/bar/'adId/adCode.html"){get(handler)}
}

class RequestMatcher(matchingResponce: NettyHttpResponse) extends ArgumentMatcher[NettyHttpResponse] {
Expand Down

0 comments on commit 3828cc4

Please sign in to comment.