/lolhttp

A scala HTTP server & client library.

Primary LanguageScalaApache License 2.0Apache-2.0

lolhttp

An HTTP & HTTP/2 server and client library for Scala.

About the library

Both server and client are plain functions accepting an HTTP request and eventually giving back an HTTP response. Requests and responses are just HTTP metadata along with a lazy content body. The content body is a stream of bytes based on fs2, making it easy to handle streaming scenarios if needed. For additional convenience, the library provides content encoders and decoders for the common scala types. SSL is supported on both sides.

Hello World

// Let's start an HTTP server
Server.listen(8888) {
  case GET at "/hello" =>
    Ok("Hello World!")
  case _ =>
    NotFound
}

// Let's connect with an HTTP client
Client.run(Get("http://localhost:8888/hello")) { res =>
  res.readAs[String].map { contentBody =>
    println(s"Received: $contentBody")
  }
}

About HTTP/2 support

HTTP/2 is supported on both server and client side. If SSL is enabled, the protocol negociation is done using ALPN. On plain connections however HTTP/2 is only supported with prior knowledge (clear text upgrade from HTTP/1.1 to HTTP/2 is ignored). Because of ALPN, HTTP/2 support over SSL requires running on Java 9 (Running on Java 8 is still possible but you need to replace the default Java TLS implementation; see http://www.eclipse.org/jetty/documentation/current/alpn-chapter.html#alpn-starting).

Usage

The library is cross-built for Scala 2.11 and Scala 2.12.

The core module to use is "com.criteo.lolhttp" %% "lolhttp" % "0.7.3".

There are also 2 optional companion libraries:

  • "com.criteo.lolhttp" %% "loljson" % "0.7.3", provides integration with the circe JSON library.
  • "com.criteo.lolhttp" %% "lolhtml" % "0.7.3", provides minimal HTML templating.

Documentation

The API documentation is the main reference. If you need to access the underlying content stream, you should first have a look a the fs2 documentation to understand the basics.

For those who prefer documentation by example, you can also follow these hands-on introductions:

License

This project is licensed under the Apache 2.0 license.

Copyright

Copyright © Criteo, 2017.