Skip to content

Instantly share code, notes, and snippets.

View jroper's full-sized avatar

James Roper jroper

View GitHub Profile
@jroper
jroper / HttpPipeliningChannel.java
Last active December 15, 2015 06:59
Demonstration of HTTP pipelining support in Netty.
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.*;
import org.jboss.netty.handler.codec.http.*;
import java.net.SocketAddress;
import java.util.LinkedList;
import java.util.concurrent.Callable;
public class HttpPipeliningHandler implements ChannelUpstreamHandler, ChannelDownstreamHandler {
@jroper
jroper / MyManagedResource.scala
Last active November 4, 2021 10:20
Play resource routing
class MyManagedResource extends ResourceController[String] {
def index = Action(Ok("index"))
def newScreen = Action(Ok("new"))
def create = Action {
Redirect(MyInjectableResource.reverseRoutes.index())
}
def show(id: String) = Action(Ok("Show " + id))
@jroper
jroper / Global.scala
Last active December 20, 2015 19:38
Transparent HEAD request handling in Play 2.1. Note that it may be more efficient to implement HEAD support in an action directly, as this may avoid unnecessary opening of resources or rendering things.
import play.api.libs.iteratee.{Done, Iteratee, Enumerator}
import play.api.mvc._
import play.api._
import play.api.libs.concurrent.Execution.Implicits._
object Global extends GlobalSettings {
override def onRouteRequest(req: RequestHeader) = {
// Lookup handler
super.onRouteRequest(req) match {
@jroper
jroper / With Grzegorz's improvements
Last active December 21, 2015 22:19
Scala incremental compiler improvements by Grzegorz Kossakowski. Actual runtimes are not so important here, since the two benchmarks were run on different machines, and also they make no attempt to ensure that the scala compiler is fully JITed, which during normal Play development offers significant improvements over these times as the compiler …
Initial clean compile of entire project:
[info] [info] Compiling 29 Scala sources and 1 Java source to /tmp/inc-compile/target/scala-2.10/classes...
[info] [success] Total time: 15 s, completed Aug 28, 2013 9:43:38 PM
Now simulate adding a method to a controller:
[info] [info] Compiling 1 Scala source to /tmp/inc-compile/target/scala-2.10/classes...
[info] [success] Total time: 2 s, completed Aug 28, 2013 9:43:40 PM
Now simulate adding a new route to the routes file:
[info] [info] Compiling 2 Scala sources and 1 Java source to /tmp/inc-compile/target/scala-2.10/classes...
@jroper
jroper / gist:7019306
Created October 17, 2013 04:44
Reads for recursive search paths
// Reads for recursive path
def recursiveSearchReads[T : Reads](path: JsPath) = Reads[Seq[T]] { json =>
path.apply(json).map(_.validate[T]).foldLeft[JsResult[Seq[T]]](JsSuccess(Nil)) {
case (JsError(a), JsError(b)) => JsError(a ++ b)
case (err: JsError, _) => err
case (_, err: JsError) => err
case (JsSuccess(ts, _), JsSuccess(t, _)) => JsSuccess(ts :+ t)
}.repath(path)
}
@jroper
jroper / gist:7019715
Created October 17, 2013 05:54
Oracle JDK TLS renegotiation bug when want_auth is added and session caching is used

There is a bug in the Oracle JDK TLS implementation.

A server might have an existing SSL session that has no client authentication associated with it. For example, it might be a web server, and a client has accessed the index page. Authentication is not required for the index page, so the server hasn't requested it.

At some point in future, the user might attempt to access a secured area of the site. A typical use case might be that they click an authenticate button. If the server wants to use SSL client certificates to authenticate the user, then at this point the server can send a renegotiation request, asking for a client certificate.

Java SSL negotiation and renogotiation has two modes when it comes to requesting a client certificate, one is "want auth", and the other is "need auth". Want auth means that if the client doesn't provide a certificate, the session will continue, without any client certificates. Need auth means that if the client doesn't provide a certificate, the session will be termi

import play.api._
import play.api.mvc._
object Global extends GlobalSettings {
override def onRequestReceived(header: RequestHeader): (RequestHeader, Handler) = {
val rewritten = if (header.uri.startsWith("//")) {
val newPath = header.uri.substring(1).replaceAll("\\?.*$", "")
header.copy(path = newPath)
} else {
@jroper
jroper / json.md
Last active August 29, 2015 14:02
RFC7159 Encoding

RFC7159 introduced a change to the RFC4627, as mentioned in Appendix A:

Changed the definition of "JSON text" so that it can be any JSON
  value, removing the constraint that it be an object or array.

This meant that the heuristic in Section 3 of RFC4627 was no longer valid:

Since the first two characters of a JSON text will always be ASCII
characters [RFC0020], it is possible to determine whether an octet

stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking

@jroper
jroper / Router.scala
Created July 2, 2014 07:50
Simple Play routing DSL with string interpolation
import java.util.regex.Pattern
import play.core.Routes
import play.api.mvc._
object Router extends Routes {
def routes = {
// Static paths
case Route("GET", p"") => controllers.Application.index
case Route("GET", p"/items") => controllers.Items.list
@jroper
jroper / GlobalStaticVar.scala
Last active September 25, 2017 21:12
Taking global static variables to a new level on the JVM...
/**
* Provides a global (cross classloader) static var.
*
* Where might you use this? Anywhere where you want to be evil ;)
*
* But, my use case was in a dynamic classloading environment where you want to load a library that
* depends on a native library. Only one classloader can ever load and link the classes associated
* with the native library, so if a second classloader (for example if a dynamic reload was done)
* wanted to use it to, it couldn't. This provided a cross classloader mechanism for storing the
* classloader that loaded the native library.