From 45133d97bf1d345e5266b70f5e179cab834493ff Mon Sep 17 00:00:00 2001 From: Neuman Vong Date: Wed, 16 Sep 2015 01:50:28 +0000 Subject: [PATCH] Small cleanup: Twitter Server and Finatra Problem Converting Twitter Server to use Finagle HTTPx made some code redundant. It also introduced a bug, where stats in the Metrics section of the admin handler were doubled. Solution Eliminate redundancies and fix the stat doubling bug. RB_ID=740731 TBR=true --- CHANGES | 7 ++++ bin/travisci | 1 - .../com/twitter/server/AdminHttpServer.scala | 21 +++++----- .../twitter/server/handler/AbortHandler.scala | 3 +- .../server/handler/AnnouncerHandler.scala | 3 +- .../handler/ClientRegistryHandler.scala | 5 ++- .../server/handler/ContentionHandler.scala | 3 +- .../twitter/server/handler/DtabHandler.scala | 3 +- .../handler/EventRecordingHandler.scala | 3 +- .../server/handler/EventsHandler.scala | 6 +-- .../server/handler/HeapResourceHandler.scala | 4 +- .../twitter/server/handler/IndexHandler.scala | 6 +-- .../server/handler/LoggingHandler.scala | 3 +- .../server/handler/MetricQueryHandler.scala | 5 ++- .../handler/ProfileResourceHandler.scala | 4 +- .../server/handler/RegistryHandler.scala | 7 ++-- .../twitter/server/handler/ReplyHandler.scala | 5 ++- .../server/handler/ResourceHandler.scala | 6 +-- .../server/handler/ServerInfoHandler.scala | 3 +- .../handler/ServerRegistryHandler.scala | 5 ++- .../server/handler/ShutdownHandler.scala | 4 +- .../server/handler/SummaryHandler.scala | 5 ++- .../server/handler/ThreadsHandler.scala | 3 +- .../server/handler/TracingHandler.scala | 4 +- .../server/handler/TwitterHandler.scala | 4 +- .../com/twitter/server/util/HttpUtils.scala | 22 +++++------ .../com/twitter/server/view/IndexView.scala | 4 +- .../twitter/server/view/NotFoundView.scala | 4 +- .../twitter/server/view/TextBlockView.scala | 3 +- .../server/ShadowAdminServerTest.scala | 2 +- .../handler/ClientRegistryHandlerTest.scala | 13 ++++--- .../server/handler/LoggingHandlerTest.scala | 18 ++++----- .../twitter/server/util/HttpUtilsTest.scala | 39 +++++++++---------- .../twitter/server/view/IndexViewTest.scala | 10 ++--- .../server/view/NotFoundViewTest.scala | 10 ++--- .../server/view/TextBlockViewTest.scala | 6 +-- 36 files changed, 135 insertions(+), 119 deletions(-) diff --git a/CHANGES b/CHANGES index 065357d0..70c722a0 100644 --- a/CHANGES +++ b/CHANGES @@ -7,6 +7,13 @@ Note that ``RB_ID=#`` correspond to associated messages in commits. 1.x ----- +Dependencies: +------------- + + * Converted to finagle-httpx. Projects that depend transitively on + finagle-http through twitter-server will need to switch to finagle-httpx. + RB_ID=741454 RB_ID=740731 + 1.13.0 ~~~~~~~ diff --git a/bin/travisci b/bin/travisci index 17a1b972..893f75df 100755 --- a/bin/travisci +++ b/bin/travisci @@ -25,7 +25,6 @@ if [ "$TWITTER_SERVER_BRANCH" != "master" ]; then git clone https://github.com/twitter/finagle.git --branch develop cd finagle $TWITTER_SERVER_SBT ++$TRAVIS_SCALA_VERSION finagle-core/publishLocal - $TWITTER_SERVER_SBT ++$TRAVIS_SCALA_VERSION finagle-http/publishLocal $TWITTER_SERVER_SBT ++$TRAVIS_SCALA_VERSION finagle-httpx/publishLocal $TWITTER_SERVER_SBT ++$TRAVIS_SCALA_VERSION finagle-httpx-compat/publishLocal $TWITTER_SERVER_SBT ++$TRAVIS_SCALA_VERSION finagle-thrift/publishLocal diff --git a/src/main/scala/com/twitter/server/AdminHttpServer.scala b/src/main/scala/com/twitter/server/AdminHttpServer.scala index fe34d8ae..774cf2af 100644 --- a/src/main/scala/com/twitter/server/AdminHttpServer.scala +++ b/src/main/scala/com/twitter/server/AdminHttpServer.scala @@ -3,7 +3,6 @@ package com.twitter.server import com.twitter.app.App import com.twitter.finagle.client.ClientRegistry import com.twitter.finagle.httpx.{Response, Request, HttpMuxer} -import com.twitter.finagle.httpx import com.twitter.finagle.server.ServerRegistry import com.twitter.finagle.stats.NullStatsReceiver import com.twitter.finagle.tracing.NullTracer @@ -36,7 +35,7 @@ object AdminHttpServer { */ case class Route( path: String, - handler: Service[HttpUtils.Request, HttpUtils.Response], + handler: Service[Request, Response], alias: String, group: Option[String], includeInIndex: Boolean) @@ -59,7 +58,7 @@ object AdminHttpServer { */ def mkRoutex( path: String, - handler: Service[httpx.Request, httpx.Response], + handler: Service[Request, Response], alias: String, group: Option[String], includeInIndex: Boolean @@ -77,12 +76,13 @@ trait AdminHttpServer { self: App => def defaultHttpPort: Int = 9990 val adminPort = flag("admin.port", new InetSocketAddress(defaultHttpPort), "Admin http server port") - private[this] val adminHttpMuxer = new Service[HttpUtils.Request, HttpUtils.Response] { - override def apply(request: HttpUtils.Request): Future[HttpUtils.Response] = underlying(request) + private[this] val adminHttpMuxer = new Service[Request, Response] { + override def apply(request: Request): Future[Response] = underlying(request) - @volatile var underlying: Service[HttpUtils.Request, HttpUtils.Response] = - new Service[HttpUtils.Request, HttpUtils.Response] { - def apply(request: HttpUtils.Request): Future[HttpUtils.Response] = HttpUtils.new404("no admin server initialized") + @volatile var underlying: Service[Request, Response] = + new Service[Request, Response] { + def apply(request: Request): Future[Response] = + HttpUtils.new404("no admin server initialized") } } @@ -111,7 +111,7 @@ trait AdminHttpServer { self: App => // Stat libraries join the global muxer namespace. // Special case and group them here. val (metricLinks, otherLinks) = { - val links = (HttpMuxer.patterns ++ httpx.HttpMuxer.patterns).map { + val links = HttpMuxer.patterns.map { case path@"/admin/metrics.json" => IndexView.Link(path, s"$path?pretty=true") case path => IndexView.Link(path, path) } @@ -158,14 +158,13 @@ trait AdminHttpServer { self: App => } // create a service which multiplexes across all endpoints. - val httpxMuxer: Service[Request, Response] = httpx.HttpMuxer val localMuxer = allRoutes.foldLeft(new HttpMuxer) { case (muxer, route) => log.info(s"${route.path} => ${route.handler.getClass.getName}") val service = new IndexView(route.alias, route.path, index) andThen route.handler muxer.withHandler(route.path, service) } - adminHttpMuxer.underlying = HttpUtils.combine(Seq(localMuxer, httpxMuxer, HttpMuxer)) + adminHttpMuxer.underlying = HttpUtils.combine(Seq(localMuxer, HttpMuxer)) } private[this] def startServer() = { diff --git a/src/main/scala/com/twitter/server/handler/AbortHandler.scala b/src/main/scala/com/twitter/server/handler/AbortHandler.scala index 6dea1218..fbbf792b 100644 --- a/src/main/scala/com/twitter/server/handler/AbortHandler.scala +++ b/src/main/scala/com/twitter/server/handler/AbortHandler.scala @@ -1,7 +1,8 @@ package com.twitter.server.handler import com.twitter.finagle.Service -import com.twitter.server.util.HttpUtils._ +import com.twitter.finagle.httpx.{Request, Response} +import com.twitter.server.util.HttpUtils.newOk import com.twitter.util.Future import java.util.logging.Logger diff --git a/src/main/scala/com/twitter/server/handler/AnnouncerHandler.scala b/src/main/scala/com/twitter/server/handler/AnnouncerHandler.scala index 75b99bda..39ee7694 100644 --- a/src/main/scala/com/twitter/server/handler/AnnouncerHandler.scala +++ b/src/main/scala/com/twitter/server/handler/AnnouncerHandler.scala @@ -1,8 +1,9 @@ package com.twitter.server.handler import com.twitter.finagle.{Announcer, Service} +import com.twitter.finagle.httpx.{Request, Response} import com.twitter.util.Future -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.newOk class AnnouncerHandler extends Service[Request, Response] { def apply(req: Request): Future[Response] = { diff --git a/src/main/scala/com/twitter/server/handler/ClientRegistryHandler.scala b/src/main/scala/com/twitter/server/handler/ClientRegistryHandler.scala index d4a48037..e1748183 100644 --- a/src/main/scala/com/twitter/server/handler/ClientRegistryHandler.scala +++ b/src/main/scala/com/twitter/server/handler/ClientRegistryHandler.scala @@ -1,10 +1,11 @@ package com.twitter.server.handler -import com.twitter.finagle.client.ClientRegistry import com.twitter.finagle.Service +import com.twitter.finagle.client.ClientRegistry +import com.twitter.finagle.httpx.{Request, Response} import com.twitter.finagle.util.StackRegistry import com.twitter.io.Buf -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{parse, new404, newResponse} import com.twitter.server.util.MetricSource import com.twitter.server.view.StackRegistryView import com.twitter.util.Future diff --git a/src/main/scala/com/twitter/server/handler/ContentionHandler.scala b/src/main/scala/com/twitter/server/handler/ContentionHandler.scala index 225a3c17..4bdd1e80 100644 --- a/src/main/scala/com/twitter/server/handler/ContentionHandler.scala +++ b/src/main/scala/com/twitter/server/handler/ContentionHandler.scala @@ -1,8 +1,9 @@ package com.twitter.server.handler import com.twitter.finagle.Service +import com.twitter.finagle.httpx.{Request, Response} import com.twitter.jvm.ContentionSnapshot -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.newOk import com.twitter.util.Future class ContentionHandler extends Service[Request, Response] { diff --git a/src/main/scala/com/twitter/server/handler/DtabHandler.scala b/src/main/scala/com/twitter/server/handler/DtabHandler.scala index 49f7694d..b98427b2 100644 --- a/src/main/scala/com/twitter/server/handler/DtabHandler.scala +++ b/src/main/scala/com/twitter/server/handler/DtabHandler.scala @@ -1,7 +1,8 @@ package com.twitter.server.handler import com.twitter.finagle.{Dtab, Service} -import com.twitter.server.util.HttpUtils._ +import com.twitter.finagle.httpx.{Request, Response} +import com.twitter.server.util.HttpUtils.newOk import com.twitter.util.Future /** diff --git a/src/main/scala/com/twitter/server/handler/EventRecordingHandler.scala b/src/main/scala/com/twitter/server/handler/EventRecordingHandler.scala index 372f937a..0e875e06 100644 --- a/src/main/scala/com/twitter/server/handler/EventRecordingHandler.scala +++ b/src/main/scala/com/twitter/server/handler/EventRecordingHandler.scala @@ -1,10 +1,11 @@ package com.twitter.server.handler import com.twitter.finagle.Service +import com.twitter.finagle.httpx.{Request, Response} import com.twitter.io.Buf import com.twitter.util.Future import com.twitter.util.events.Sink -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.newResponse import java.util.logging.Logger private[server] object EventRecordingHandler { diff --git a/src/main/scala/com/twitter/server/handler/EventsHandler.scala b/src/main/scala/com/twitter/server/handler/EventsHandler.scala index e7ca3ef4..41cbb0ba 100644 --- a/src/main/scala/com/twitter/server/handler/EventsHandler.scala +++ b/src/main/scala/com/twitter/server/handler/EventsHandler.scala @@ -2,11 +2,11 @@ package com.twitter.server.handler import com.twitter.concurrent.exp.AsyncStream import com.twitter.finagle.Service -import com.twitter.finagle.httpx +import com.twitter.finagle.httpx.{Request, Response} import com.twitter.finagle.tracing.SpanId import com.twitter.io.{Reader, Buf} import com.twitter.server.handler.EventRecordingHandler._ -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{accepts, expectsJson} import com.twitter.server.util.{JsonSink, TraceEventSink} import com.twitter.util.events.{Sink, Event} import com.twitter.util.{Future, Throw, Try} @@ -29,7 +29,7 @@ private[server] class EventsHandler(sink: Sink) extends Service[Request, Respons else respond(Html, htmlSerialize(sink)) private[this] def respond(contentType: String, reader: Reader): Future[Response] = { - val response = httpx.Response() + val response = Response() response.contentType = contentType response.setChunked(true) Reader.copy(reader, response.writer).onFailure { e => diff --git a/src/main/scala/com/twitter/server/handler/HeapResourceHandler.scala b/src/main/scala/com/twitter/server/handler/HeapResourceHandler.scala index 8c1c109a..598d1fce 100644 --- a/src/main/scala/com/twitter/server/handler/HeapResourceHandler.scala +++ b/src/main/scala/com/twitter/server/handler/HeapResourceHandler.scala @@ -1,11 +1,11 @@ package com.twitter.server.handler import com.twitter.conversions.time._ -import com.twitter.finagle.httpx.Status +import com.twitter.finagle.httpx.{Request, Response, Status} import com.twitter.finagle.Service import com.twitter.io.Buf import com.twitter.jvm.Heapster -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{newResponse, parse} import com.twitter.util.{Duration, Future} import java.util.logging.Logger diff --git a/src/main/scala/com/twitter/server/handler/IndexHandler.scala b/src/main/scala/com/twitter/server/handler/IndexHandler.scala index 9761bf08..811d238d 100644 --- a/src/main/scala/com/twitter/server/handler/IndexHandler.scala +++ b/src/main/scala/com/twitter/server/handler/IndexHandler.scala @@ -1,9 +1,9 @@ package com.twitter.server.handler -import com.twitter.finagle.httpx.HttpMuxer import com.twitter.finagle.{Service, httpx} +import com.twitter.finagle.httpx.{HttpMuxer, Request, Response} import com.twitter.io.Buf -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{expectsHtml, newOk, newResponse} import com.twitter.util.Future /** @@ -24,4 +24,4 @@ class IndexHandler( content = Buf.Utf8(links.mkString("
\n")) ) } -} \ No newline at end of file +} diff --git a/src/main/scala/com/twitter/server/handler/LoggingHandler.scala b/src/main/scala/com/twitter/server/handler/LoggingHandler.scala index 89c24e4a..151ec2f1 100644 --- a/src/main/scala/com/twitter/server/handler/LoggingHandler.scala +++ b/src/main/scala/com/twitter/server/handler/LoggingHandler.scala @@ -1,9 +1,10 @@ package com.twitter.server.handler import com.twitter.finagle.Service +import com.twitter.finagle.httpx.{Request, Response} import com.twitter.io.Buf import com.twitter.logging.{Level, Logger} -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{expectsHtml, newResponse, parse} import com.twitter.util.Future import java.net.URLEncoder import java.util.{logging => javalog} diff --git a/src/main/scala/com/twitter/server/handler/MetricQueryHandler.scala b/src/main/scala/com/twitter/server/handler/MetricQueryHandler.scala index 4ee89bae..7613d2f3 100644 --- a/src/main/scala/com/twitter/server/handler/MetricQueryHandler.scala +++ b/src/main/scala/com/twitter/server/handler/MetricQueryHandler.scala @@ -1,9 +1,10 @@ package com.twitter.server.handler import com.twitter.finagle.Service +import com.twitter.finagle.httpx.{Request, Response} import com.twitter.io.Buf import com.twitter.io.Charsets -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{newResponse, parse} import com.twitter.server.util.{JsonConverter, MetricSource} import com.twitter.util.{Future, Time} @@ -54,4 +55,4 @@ class MetricQueryHandler(source: MetricSource = new MetricSource) ) } } -} \ No newline at end of file +} diff --git a/src/main/scala/com/twitter/server/handler/ProfileResourceHandler.scala b/src/main/scala/com/twitter/server/handler/ProfileResourceHandler.scala index e88aa92a..aafa73f8 100644 --- a/src/main/scala/com/twitter/server/handler/ProfileResourceHandler.scala +++ b/src/main/scala/com/twitter/server/handler/ProfileResourceHandler.scala @@ -1,11 +1,11 @@ package com.twitter.server.handler import com.twitter.conversions.time._ -import com.twitter.finagle.httpx.Status +import com.twitter.finagle.httpx.{Request, Response, Status} import com.twitter.finagle.Service import com.twitter.io.Buf import com.twitter.jvm.CpuProfile -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{newResponse, parse} import com.twitter.util.{Duration, Future, Return, Throw} import java.io.ByteArrayOutputStream import java.util.logging.Logger diff --git a/src/main/scala/com/twitter/server/handler/RegistryHandler.scala b/src/main/scala/com/twitter/server/handler/RegistryHandler.scala index 3ba80b0c..d43216e8 100644 --- a/src/main/scala/com/twitter/server/handler/RegistryHandler.scala +++ b/src/main/scala/com/twitter/server/handler/RegistryHandler.scala @@ -1,10 +1,11 @@ package com.twitter.server.handler import com.twitter.finagle.Service -import com.twitter.util.registry.{Formatter, GlobalRegistry} -import com.twitter.util.Future -import com.twitter.server.util.HttpUtils._ +import com.twitter.finagle.httpx.{Request, Response} +import com.twitter.server.util.HttpUtils.newOk import com.twitter.server.util.JsonConverter +import com.twitter.util.Future +import com.twitter.util.registry.{Formatter, GlobalRegistry} /** * A [[com.twitter.finagle.Service]] for displaying the current state of the diff --git a/src/main/scala/com/twitter/server/handler/ReplyHandler.scala b/src/main/scala/com/twitter/server/handler/ReplyHandler.scala index 036b30cc..c5373410 100644 --- a/src/main/scala/com/twitter/server/handler/ReplyHandler.scala +++ b/src/main/scala/com/twitter/server/handler/ReplyHandler.scala @@ -1,8 +1,9 @@ package com.twitter.server.handler import com.twitter.finagle.Service -import com.twitter.server.util.HttpUtils._ +import com.twitter.finagle.httpx.{Request, Response} +import com.twitter.server.util.HttpUtils.newOk class ReplyHandler(msg: String) extends Service[Request, Response] { def apply(req: Request) = newOk(msg) -} \ No newline at end of file +} diff --git a/src/main/scala/com/twitter/server/handler/ResourceHandler.scala b/src/main/scala/com/twitter/server/handler/ResourceHandler.scala index 67d4b31a..58e3be97 100644 --- a/src/main/scala/com/twitter/server/handler/ResourceHandler.scala +++ b/src/main/scala/com/twitter/server/handler/ResourceHandler.scala @@ -1,9 +1,9 @@ package com.twitter.server.handler -import com.twitter.finagle.httpx.Status import com.twitter.finagle.Service +import com.twitter.finagle.httpx.{Request, Response, Status} import com.twitter.io.{Buf, Charsets} -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{new404, newResponse, parse} import com.twitter.util.{Future, FuturePool, JavaSingleton} import java.io.{File, FileInputStream, InputStream} import java.nio.charset.Charset @@ -133,4 +133,4 @@ object ResourceHandler extends JavaSingleton { baseDirectory: String ): PartialFunction[String, InputStream] = directoryResolver(baseDirectory) orElse jarResolver(baseResourcePath) -} \ No newline at end of file +} diff --git a/src/main/scala/com/twitter/server/handler/ServerInfoHandler.scala b/src/main/scala/com/twitter/server/handler/ServerInfoHandler.scala index 1622b208..dbfc3513 100644 --- a/src/main/scala/com/twitter/server/handler/ServerInfoHandler.scala +++ b/src/main/scala/com/twitter/server/handler/ServerInfoHandler.scala @@ -2,8 +2,9 @@ package com.twitter.server.handler import com.twitter.finagle.Service import com.twitter.finagle.stats.LoadedStatsReceiver +import com.twitter.finagle.httpx.{Request, Response} import com.twitter.io.Buf -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.newResponse import com.twitter.server.util.JsonConverter import com.twitter.util.Future import com.twitter.util.registry.GlobalRegistry diff --git a/src/main/scala/com/twitter/server/handler/ServerRegistryHandler.scala b/src/main/scala/com/twitter/server/handler/ServerRegistryHandler.scala index 65907198..2163d8c2 100644 --- a/src/main/scala/com/twitter/server/handler/ServerRegistryHandler.scala +++ b/src/main/scala/com/twitter/server/handler/ServerRegistryHandler.scala @@ -1,10 +1,11 @@ package com.twitter.server.handler -import com.twitter.finagle.server.ServerRegistry import com.twitter.finagle.Service +import com.twitter.finagle.httpx.{Request, Response, Status} +import com.twitter.finagle.server.ServerRegistry import com.twitter.finagle.util.StackRegistry import com.twitter.io.Buf -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{new404, newResponse, parse} import com.twitter.server.util.MetricSource import com.twitter.server.view.StackRegistryView import com.twitter.util.Future diff --git a/src/main/scala/com/twitter/server/handler/ShutdownHandler.scala b/src/main/scala/com/twitter/server/handler/ShutdownHandler.scala index 823e5bf5..6788e00d 100644 --- a/src/main/scala/com/twitter/server/handler/ShutdownHandler.scala +++ b/src/main/scala/com/twitter/server/handler/ShutdownHandler.scala @@ -1,10 +1,10 @@ package com.twitter.server.handler import com.twitter.app.App -import com.twitter.finagle.httpx.Status +import com.twitter.finagle.httpx.{Request, Response, Status} import com.twitter.finagle.Service import com.twitter.io.Buf -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{parse, newOk, newResponse} import com.twitter.util.{Duration, Future} import java.util.logging.Logger diff --git a/src/main/scala/com/twitter/server/handler/SummaryHandler.scala b/src/main/scala/com/twitter/server/handler/SummaryHandler.scala index f44883c0..594d3d73 100644 --- a/src/main/scala/com/twitter/server/handler/SummaryHandler.scala +++ b/src/main/scala/com/twitter/server/handler/SummaryHandler.scala @@ -1,8 +1,9 @@ package com.twitter.server.handler import com.twitter.finagle.Service +import com.twitter.finagle.httpx.{Request, Response} import com.twitter.io.Buf -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{expectsHtml, newOk, newResponse} import com.twitter.util.Future private object SummaryHandler { @@ -52,4 +53,4 @@ class SummaryHandler extends Service[Request, Response] { content = Buf.Utf8(html) ) } -} \ No newline at end of file +} diff --git a/src/main/scala/com/twitter/server/handler/ThreadsHandler.scala b/src/main/scala/com/twitter/server/handler/ThreadsHandler.scala index e071bbcc..807e13c6 100644 --- a/src/main/scala/com/twitter/server/handler/ThreadsHandler.scala +++ b/src/main/scala/com/twitter/server/handler/ThreadsHandler.scala @@ -1,8 +1,9 @@ package com.twitter.server.handler import com.twitter.finagle.Service +import com.twitter.finagle.httpx.{Request, Response} import com.twitter.io.Buf -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{expectsHtml, newOk, newResponse} import com.twitter.server.util.JsonConverter import com.twitter.server.view.ThreadsView import com.twitter.util.Future diff --git a/src/main/scala/com/twitter/server/handler/TracingHandler.scala b/src/main/scala/com/twitter/server/handler/TracingHandler.scala index 49359c3b..d5f2624f 100644 --- a/src/main/scala/com/twitter/server/handler/TracingHandler.scala +++ b/src/main/scala/com/twitter/server/handler/TracingHandler.scala @@ -1,9 +1,9 @@ package com.twitter.server.handler -import com.twitter.finagle.httpx.Status import com.twitter.finagle.Service +import com.twitter.finagle.httpx.{Request, Response, Status} import com.twitter.io.Buf -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{newResponse, parse} import com.twitter.util.Future import java.util.logging.Logger diff --git a/src/main/scala/com/twitter/server/handler/TwitterHandler.scala b/src/main/scala/com/twitter/server/handler/TwitterHandler.scala index be9d2210..06cbee74 100644 --- a/src/main/scala/com/twitter/server/handler/TwitterHandler.scala +++ b/src/main/scala/com/twitter/server/handler/TwitterHandler.scala @@ -1,9 +1,9 @@ package com.twitter.server.handler -import com.twitter.finagle.httpx.Status import com.twitter.finagle.Service +import com.twitter.finagle.httpx.{Request, Response, Status} import com.twitter.io.Buf -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{newResponse, parse} import com.twitter.util.Future import java.util.logging.Logger import scala.collection.{Map, Seq} diff --git a/src/main/scala/com/twitter/server/util/HttpUtils.scala b/src/main/scala/com/twitter/server/util/HttpUtils.scala index 8837dfb2..36afeb64 100644 --- a/src/main/scala/com/twitter/server/util/HttpUtils.scala +++ b/src/main/scala/com/twitter/server/util/HttpUtils.scala @@ -1,7 +1,7 @@ package com.twitter.server.util import com.twitter.finagle.Service -import com.twitter.finagle.httpx +import com.twitter.finagle.httpx.{MediaType, Request, Response, Status, Version} import com.twitter.io.Buf import com.twitter.util.Future import org.jboss.netty.handler.codec.http.QueryStringDecoder @@ -9,9 +9,6 @@ import scala.collection.JavaConverters._ import scala.collection.{Map, Seq} private[server] object HttpUtils { - type Request = httpx.Request - type Response = httpx.Response - /** * Creates a http [[com.twitter.finagle.Service]] which attempts a * request on the given `services`, in order, until a service returns @@ -26,14 +23,13 @@ private[server] object HttpUtils { case service +: Nil => service(req) case service +: tail => service(req).flatMap { rep => - if (rep.status == httpx.Status.NotFound) + if (rep.status == Status.NotFound) loop(tail) else Future.value(rep) } case Nil => - Future.value(httpx.Response(req.version, httpx.Status.NotFound) - ) + Future.value(Response(req.version, Status.NotFound)) } loop(services) @@ -53,7 +49,7 @@ private[server] object HttpUtils { */ def expectsHtml(req: Request): Boolean = { val decoder = new QueryStringDecoder(req.uri) - decoder.getPath.endsWith(".html") || accepts(req, httpx.MediaType.Html) + decoder.getPath.endsWith(".html") || accepts(req, MediaType.Html) } /** @@ -61,7 +57,7 @@ private[server] object HttpUtils { */ def expectsJson(req: Request): Boolean = { val decoder = new QueryStringDecoder(req.uri) - decoder.getPath.endsWith(".json") || accepts(req, httpx.MediaType.Json) + decoder.getPath.endsWith(".json") || accepts(req, MediaType.Json) } /** @@ -75,13 +71,13 @@ private[server] object HttpUtils { * @param content The content body of the HTTP response. */ def newResponse( - version: httpx.Version = httpx.Version.Http11, - status: httpx.Status = httpx.Status.Ok, + version: Version = Version.Http11, + status: Status = Status.Ok, headers: Iterable[(String, Object)] = Seq(), contentType: String, content: Buf ): Future[Response] = { - val response = httpx.Response(version, status) + val response = Response(version, status) response.content = content for ((k, v) <- headers) response.headerMap.add(k, v.toString) response.headerMap.add("Content-Language", "en") @@ -100,7 +96,7 @@ private[server] object HttpUtils { /** Returns a new 404 with contents set to `msg` */ def new404(msg: String): Future[Response] = newResponse( - status = httpx.Status.NotFound, + status = Status.NotFound, contentType = "text/plain;charset=UTF-8", content = Buf.Utf8(msg) ) diff --git a/src/main/scala/com/twitter/server/view/IndexView.scala b/src/main/scala/com/twitter/server/view/IndexView.scala index 909ca8e9..d567979a 100644 --- a/src/main/scala/com/twitter/server/view/IndexView.scala +++ b/src/main/scala/com/twitter/server/view/IndexView.scala @@ -2,9 +2,9 @@ package com.twitter.server.view import com.twitter.concurrent.exp.AsyncStream import com.twitter.finagle.{Service, SimpleFilter} -import com.twitter.finagle.httpx.Response +import com.twitter.finagle.httpx.{Request, Response, Status} import com.twitter.io.{Reader, Buf, Charsets} -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{expectsHtml, newResponse} import com.twitter.util.Future object IndexView { diff --git a/src/main/scala/com/twitter/server/view/NotFoundView.scala b/src/main/scala/com/twitter/server/view/NotFoundView.scala index 1bcce900..dce29fe0 100644 --- a/src/main/scala/com/twitter/server/view/NotFoundView.scala +++ b/src/main/scala/com/twitter/server/view/NotFoundView.scala @@ -1,9 +1,9 @@ package com.twitter.server.view -import com.twitter.finagle.httpx.Status +import com.twitter.finagle.httpx.{Request, Response, Status} import com.twitter.finagle.{Service, SimpleFilter} import com.twitter.io.Buf -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{expectsHtml, newResponse} import com.twitter.util.Future object NotFoundView { diff --git a/src/main/scala/com/twitter/server/view/TextBlockView.scala b/src/main/scala/com/twitter/server/view/TextBlockView.scala index 878921fb..0f9e47fd 100644 --- a/src/main/scala/com/twitter/server/view/TextBlockView.scala +++ b/src/main/scala/com/twitter/server/view/TextBlockView.scala @@ -1,8 +1,9 @@ package com.twitter.server.view import com.twitter.finagle.{Service, SimpleFilter} +import com.twitter.finagle.httpx.{Request, Response} import com.twitter.io.{Buf, Charsets} -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{expectsHtml, newResponse} class TextBlockView extends SimpleFilter[Request, Response] { def apply(req: Request, svc: Service[Request, Response]) = { diff --git a/src/test/scala/com/twitter/server/ShadowAdminServerTest.scala b/src/test/scala/com/twitter/server/ShadowAdminServerTest.scala index a6254cba..3d160c5c 100644 --- a/src/test/scala/com/twitter/server/ShadowAdminServerTest.scala +++ b/src/test/scala/com/twitter/server/ShadowAdminServerTest.scala @@ -1,7 +1,7 @@ package com.twitter.server import com.twitter.finagle.Httpx -import com.twitter.finagle.httpx.{HttpMuxHandler, Request} +import com.twitter.finagle.httpx.{HttpMuxHandler, Request, Response} import com.twitter.finagle.{ListeningServer, NullServer} import com.twitter.io.Charsets import com.twitter.server.util.HttpUtils._ diff --git a/src/test/scala/com/twitter/server/handler/ClientRegistryHandlerTest.scala b/src/test/scala/com/twitter/server/handler/ClientRegistryHandlerTest.scala index fe4335c4..bf96fe72 100644 --- a/src/test/scala/com/twitter/server/handler/ClientRegistryHandlerTest.scala +++ b/src/test/scala/com/twitter/server/handler/ClientRegistryHandlerTest.scala @@ -3,7 +3,8 @@ package com.twitter.server.handler import com.twitter.conversions.time._ import com.twitter.finagle.client.StackClient import com.twitter.finagle.util.StackRegistry -import com.twitter.finagle.{httpx, Stack, param} +import com.twitter.finagle.{Stack, param} +import com.twitter.finagle.httpx.{Request, Status} import com.twitter.io.Charsets import com.twitter.server.util.HttpUtils._ import com.twitter.server.util.MetricSourceTest @@ -23,14 +24,14 @@ class ClientRegistryHandlerTest extends FunSuite { registry.register("localhost:8080", StackClient.newStack, Stack.Params.empty + param.Label("client0")) val handler = new ClientRegistryHandler(source, registry) - val res = Await.result(handler(httpx.Request("/client0"))) - assert(res.status === httpx.Status.Ok) + val res = Await.result(handler(Request("/client0"))) + assert(res.status === Status.Ok) val content = res.contentString assert(content.contains("client0")) assert(content.contains("localhost:8080")) - val res1 = Await.result(handler(httpx.Request("/client1"))) - assert(res1.status === httpx.Status.NotFound) + val res1 = Await.result(handler(Request("/client1"))) + assert(res1.status === Status.NotFound) } test("client profile") { @@ -44,7 +45,7 @@ class ClientRegistryHandlerTest extends FunSuite { val handler = new ClientRegistryHandler(source, registry) tc.advance(1.second) - val req = httpx.Request("/index.html") + val req = Request("/index.html") assert(Await.result(handler(req)).contentString === "") underlying = Map( diff --git a/src/test/scala/com/twitter/server/handler/LoggingHandlerTest.scala b/src/test/scala/com/twitter/server/handler/LoggingHandlerTest.scala index c88148ce..5249d0f6 100644 --- a/src/test/scala/com/twitter/server/handler/LoggingHandlerTest.scala +++ b/src/test/scala/com/twitter/server/handler/LoggingHandlerTest.scala @@ -1,6 +1,6 @@ package com.twitter.server.handler -import com.twitter.finagle.httpx +import com.twitter.finagle.httpx.{Request, Response, Status} import com.twitter.io.Charsets import com.twitter.logging.{Level, Logger} import com.twitter.server.util.HttpUtils._ @@ -15,17 +15,17 @@ class LoggingHandlerTest extends FunSuite { val handler = new LoggingHandler val loggers = Logger.iterator - val res = Await.result(handler(httpx.Request("/"))) - assert(res.status === httpx.Status.Ok) + val res = Await.result(handler(Request("/"))) + assert(res.status === Status.Ok) val text = res.contentString for (logger <- loggers) { assert(text.contains(logger.name)) } - val browserReq = httpx.Request("/") + val browserReq = Request("/") browserReq.headerMap.set("User-Agent", "Mozilla") - val browserRes = Await.result(handler(httpx.Request("/"))) - assert(browserRes.status === httpx.Status.Ok) + val browserRes = Await.result(handler(Request("/"))) + assert(browserRes.status === Status.Ok) val html = browserRes.contentString for (logger <- loggers) { assert(html.contains(logger.name)) @@ -37,7 +37,7 @@ class LoggingHandlerTest extends FunSuite { Logger("").setLevel(Level.INFO) assert(Logger("").getLevel == Level.INFO) - Await.result(handler(httpx.Request("/?logger=root&level=DEBUG"))) + Await.result(handler(Request("/?logger=root&level=DEBUG"))) assert(Logger("").getLevel == Level.DEBUG) } @@ -57,9 +57,9 @@ class LoggingHandlerTest extends FunSuite { } Logger.withLoggers(List(l0, l1)) { - val req = httpx.Request("/") + val req = Request("/") val res = Await.result(handler(req)) - assert(res.status === httpx.Status.Ok) + assert(res.status === Status.Ok) val text = res.contentString assert(text === "root OFF\nl0 ALL\nl1 DEBUG") } diff --git a/src/test/scala/com/twitter/server/util/HttpUtilsTest.scala b/src/test/scala/com/twitter/server/util/HttpUtilsTest.scala index a9ca8425..a4a9751b 100644 --- a/src/test/scala/com/twitter/server/util/HttpUtilsTest.scala +++ b/src/test/scala/com/twitter/server/util/HttpUtilsTest.scala @@ -1,7 +1,7 @@ package com.twitter.server.util -import com.twitter.finagle.{Service, httpx} -import com.twitter.finagle.httpx.Status +import com.twitter.finagle.Service +import com.twitter.finagle.httpx.{HttpMuxer, Request, Response, Status, Version} import com.twitter.io.{Buf, Charsets} import com.twitter.server.util.HttpUtils._ import com.twitter.util.Await @@ -11,7 +11,6 @@ import org.scalatest.junit.JUnitRunner @RunWith(classOf[JUnitRunner]) class HttpUtilsTest extends FunSuite { - test("combine") { val hello = new Service[Request, Response] { def apply(req: Request) = newOk("hello") @@ -21,46 +20,46 @@ class HttpUtilsTest extends FunSuite { def apply(req: Request) = newOk("world") } - val muxer0 = new httpx.HttpMuxer().withHandler("/hello", hello) - val muxer1 = new httpx.HttpMuxer().withHandler("/world", world) + val muxer0 = new HttpMuxer().withHandler("/hello", hello) + val muxer1 = new HttpMuxer().withHandler("/world", world) val svc = combine(Seq(muxer0, muxer1)) - val res0 = Await.result(svc(httpx.Request("/hello"))) + val res0 = Await.result(svc(Request("/hello"))) assert(res0.contentString === "hello") - val res1 = Await.result(svc(httpx.Request("/world"))) + val res1 = Await.result(svc(Request("/world"))) assert(res1.contentString === "world") - val muxer2 = new httpx.HttpMuxer().withHandler("/hello", + val muxer2 = new HttpMuxer().withHandler("/hello", new Service[Request, Response] { def apply(req: Request) = newOk("sup") } ) val svcSeq1 = combine(Seq(muxer0, muxer1, muxer2)) - val res2 = Await.result(svcSeq1(httpx.Request("/hello"))) + val res2 = Await.result(svcSeq1(Request("/hello"))) assert(res2.contentString === "hello") val svcSeq2 = combine(Seq(muxer2, muxer0, muxer1)) - val res3 = Await.result(svcSeq2(httpx.Request("/hello"))) + val res3 = Await.result(svcSeq2(Request("/hello"))) assert(res3.contentString === "sup") - val res4 = Await.result(svcSeq1(httpx.Request("/an404"))) + val res4 = Await.result(svcSeq1(Request("/an404"))) assert(res4.status == Status.NotFound) } test("expects") { - val req1 = httpx.Request("/") + val req1 = Request("/") req1.headerMap.set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") - val req2 = httpx.Request("/admin/threads.json?k=v") + val req2 = Request("/admin/threads.json?k=v") req2.headerMap.set("Accept", "text/html,application/json;q=0.9,*/*;q=0.8") - val req3 = httpx.Request("/admin/threads.json") + val req3 = Request("/admin/threads.json") req3.headerMap.set("Accept", "*/*") assert(expectsHtml(req1)) @@ -75,28 +74,28 @@ class HttpUtilsTest extends FunSuite { test("newResponse") { val res = Await.result(newResponse( - version = httpx.Version.Http11, - status = httpx.Status.Ok, + version = Version.Http11, + status = Status.Ok, headers = Seq(("host", "localhost")), contentType = "application/json;charset=UTF-8", content = Buf.Utf8("[]") )) - assert(res.version === httpx.Version.Http11) - assert(res.status === httpx.Status.Ok) + assert(res.version === Version.Http11) + assert(res.status === Status.Ok) assert(res.headerMap.get("content-type") === Some("application/json;charset=UTF-8")) assert(res.contentString === "[]") } test("newOk") { val res = Await.result(newOk("hello")) - assert(res.status === httpx.Status.Ok) + assert(res.status === Status.Ok) assert(res.headerMap.get("content-type") === Some("text/plain;charset=UTF-8")) assert(res.contentString === "hello") } test("new404") { val res = Await.result(new404("not found")) - assert(res.status === httpx.Status.NotFound) + assert(res.status === Status.NotFound) assert(res.headerMap.get("content-type") === Some("text/plain;charset=UTF-8")) assert(res.contentString === "not found") } diff --git a/src/test/scala/com/twitter/server/view/IndexViewTest.scala b/src/test/scala/com/twitter/server/view/IndexViewTest.scala index 9a0bd45d..ee69c1a6 100644 --- a/src/test/scala/com/twitter/server/view/IndexViewTest.scala +++ b/src/test/scala/com/twitter/server/view/IndexViewTest.scala @@ -1,9 +1,9 @@ package com.twitter.server.view -import com.twitter.finagle.httpx +import com.twitter.finagle.httpx.{Request, Response, Status} import com.twitter.finagle.Service import com.twitter.io.{Buf, Charsets} -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.{newOk, newResponse} import com.twitter.util.Await import org.junit.runner.RunWith import org.scalatest.FunSuite @@ -23,20 +23,20 @@ class IndexViewTest extends FunSuite { } val idx = new IndexView("test", "", () => Seq()) - val req = httpx.Request("/") + val req = Request("/") req.headerMap.set("Accept", "text/html") val svc0 = idx andThen fragment val res0 = Await.result(svc0(req)) assert(res0.headerMap.get("content-type") === Some("text/html;charset=UTF-8")) - assert(res0.status === httpx.Status.Ok) + assert(res0.status === Status.Ok) assert(res0.contentString.contains("")) val svc1 = idx andThen nofragment req.headerMap.set("Accept", "*/*") val res1 = Await.result(svc1(req)) assert(res1.headerMap.get("content-type") === Some("text/plain;charset=UTF-8")) - assert(res1.status === httpx.Status.Ok) + assert(res1.status === Status.Ok) assert(res1.contentString === "hello") } } diff --git a/src/test/scala/com/twitter/server/view/NotFoundViewTest.scala b/src/test/scala/com/twitter/server/view/NotFoundViewTest.scala index 8396f413..a20915fe 100644 --- a/src/test/scala/com/twitter/server/view/NotFoundViewTest.scala +++ b/src/test/scala/com/twitter/server/view/NotFoundViewTest.scala @@ -1,9 +1,9 @@ package com.twitter.server.view -import com.twitter.finagle.httpx +import com.twitter.finagle.httpx.{Request, Response, Status} import com.twitter.finagle.Service import com.twitter.io.{Buf, Charsets} -import com.twitter.server.util.HttpUtils._ +import com.twitter.server.util.HttpUtils.newResponse import com.twitter.util.Await import org.junit.runner.RunWith import org.scalatest.FunSuite @@ -15,17 +15,17 @@ class NotFoundViewTest extends FunSuite { val handler = new Service[Request, Response] { def apply(req: Request) = newResponse( contentType = "text/plain;charset=UTF-8", - status = httpx.Status.NotFound, + status = Status.NotFound, content = Buf.Utf8("hello")) } val svc = new NotFoundView andThen handler - val req0 = httpx.Request("/") + val req0 = Request("/") req0.headerMap.add("Accept", "text/html") val res0 = Await.result(svc(req0)) assert(res0.headerMap.get("content-type") === Some("text/html;charset=UTF-8")) - assert(res0.status === httpx.Status.NotFound) + assert(res0.status === Status.NotFound) assert(res0.contentString.contains("")) } } diff --git a/src/test/scala/com/twitter/server/view/TextBlockViewTest.scala b/src/test/scala/com/twitter/server/view/TextBlockViewTest.scala index 366d867c..71092548 100644 --- a/src/test/scala/com/twitter/server/view/TextBlockViewTest.scala +++ b/src/test/scala/com/twitter/server/view/TextBlockViewTest.scala @@ -1,6 +1,6 @@ package com.twitter.server.view -import com.twitter.finagle.httpx +import com.twitter.finagle.httpx.{Request, Response} import com.twitter.finagle.Service import com.twitter.io.{Buf, Charsets} import com.twitter.server.util.HttpUtils._ @@ -20,13 +20,13 @@ class TextBlockViewTest extends FunSuite { val svc = new TextBlockView andThen handler - val req0 = httpx.Request("/") + val req0 = Request("/") req0.headerMap.set("Accept", "text/html") val res0 = Await.result(svc(req0)) assert(res0.headerMap.get("content-type") === Some("text/html;charset=UTF-8")) assert(res0.contentString === "
hello
") - val req = httpx.Request("/") + val req = Request("/") val res = Await.result(svc(req)) assert(res.headerMap.get("content-type") === Some("text/plain;charset=UTF-8")) assert(res.contentString === "hello")