Skip to content

Instantly share code, notes, and snippets.

View hamnis's full-sized avatar

Erlend Hamnaberg hamnis

View GitHub Profile
interface Session {
String getId();
}
sealed trait Score {
def isEmpty = this match {
case SetScore(_, _) => false
case NoScore => true
}
def isDefined = !isEmpty
def toOption: Option[(Int, Int)] = this match {
case NoScore => None
public class Main {
public static void main(String[] args) {
HTTPClientResponseResolver resolver = HTTPClientResponseResolver.createMultithreadedInstance();
HttpClient client = resolver.getClient();
HttpConnectionManagerParams params = client.getConnectionManager().getParams();
params.setDefaultMaxConnectionsPerHost(5);
params.setMaxTotalConnections(20);
}
@hamnis
hamnis / SecureKit.scala
Last active December 10, 2015 21:09
Unfiltered kit for detecting Forwarded Proto and setting secure flag
package unfilteredx.kit
import unfiltered.request._
import unfiltered.response._
object Secure {
def secure[A,B](intent: unfiltered.Cycle.Intent[A,B]): unfiltered.Cycle.Intent[A,B] = {
case req@ForwardedProto(proto) if (intent.isDefinedAt(req)) => intent(wrap(req, proto.toLowerCase))
case req if (intent.isDefinedAt(req)) => intent(req)
}
@hamnis
hamnis / Extractor.java
Created January 11, 2013 15:00
Extract RPM Files with redline
package rpm;
import org.freecompany.redline.ReadableChannelWrapper;
import org.freecompany.redline.Scanner;
import org.freecompany.redline.header.Format;
import org.freecompany.redline.payload.CpioHeader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
{
"template": {
"data": [
{"name": "title", "value": "FOOO"},
{"name": "level", "value": "advanced"},
{"name": "format", "value": "presentation"},
{"name": "body", "value": "Abstract ...."},
{"name": "summary", "value": "Something something"},
{"name": "audience", "value": "FOOO"},
{"name": "outline", "value": "FOOO"},
trait JsonParser extends BodyParserModule {
def parse[String, O <: Parser[O]](a: String)(implicit Parser: O): O = a match {
case Parser(o) => o
case _ => ???
}
}
sealed trait Parser[A] {
def parse(a: String): A
@hamnis
hamnis / Main.scala
Last active December 14, 2015 01:49
Unzip4 List
import java.net.URI
object Main extends App {
val list = List((1, URI.create("hello"), "hello", 23.2), (2, URI.create("bye"), "bye", 45.2))
val (ints, uris, strings, doubles) = list.unzip4
println(ints)
println(uris)
println(strings)
println(doubles)
@hamnis
hamnis / gist:5055314
Created February 28, 2013 08:57
Collection + json error response
GET /foo?input=invalid HTTP/1.1
Host: example.com
Accept: application/vnd.collection+json,*/*
HTTP/1.1 400 Bad Request
Content-Type: application/vnd.collection+json
{
"error": {
"title": "Expected int in input",
{
"default": {
"server": {
"binary": "binary",
"mongo": "mongodb://localhost:27017/ems",
"root": "/server/"
},
"crypt": {
"password": "changeme",
"type": "DES"