Skip to content

Instantly share code, notes, and snippets.

@ponkotuy
Created March 24, 2015 16:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ponkotuy/90ff3b6880e509bdef02 to your computer and use it in GitHub Desktop.
Save ponkotuy/90ff3b6880e509bdef02 to your computer and use it in GitHub Desktop.
LittleProxyによる艦これProxyサンプル
package com.ponkotuy
import java.nio.charset.Charset
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.http._
import org.littleshoot.proxy.impl.DefaultHttpProxyServer
import org.littleshoot.proxy.{HttpFilters, HttpFiltersAdapter, HttpFiltersSourceAdapter}
object Main extends App {
val server = DefaultHttpProxyServer.bootstrap()
.withPort(8080)
.withFiltersSource(new PrintFilter)
server.start()
}
class PrintFilter extends HttpFiltersSourceAdapter {
override def filterRequest(orig: HttpRequest, ctx: ChannelHandlerContext): HttpFilters = {
println(orig.getUri)
val builder = new StringBuilder
new HttpFiltersAdapter(orig, ctx) {
override def responsePre(obj: HttpObject): HttpObject = {
ContentResult.fromObject(obj).foreach { result =>
builder.append(result.content)
if(result.last) println(builder.toString()) // 好きな処理を噛ます
}
obj
}
}
}
}
case class ContentResult(last: Boolean, content: String)
object ContentResult {
def fromObject(obj: HttpObject): Option[ContentResult] = {
try {
obj match {
case res: LastHttpContent => Some(ContentResult(true, toString(res)))
case res: HttpContent => Some(ContentResult(false, toString(res)))
case _ => None
}
} catch {
case e: Throwable => e.printStackTrace(); None
}
}
private[this] def toString(cont: HttpContent): String = {
cont.content().toString(Charset.forName("UTF-8"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment