• Stars
    star
    201
  • Rank 194,491 (Top 4 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTTP library to make it easy to deal with raw HTTP.

RawHTTP

Module Name Latest Version Documentation
rawhttp-core rawhttp-core RawHTTP Core
rawhttp-cli rawhttp-cli RawHTTP CLI
rawhttp-duplex rawhttp-duplex RawHTTP Duplex
rawhttp-cookies rawhttp-cookies RawHTTP Cookies
rawhttp-req-in-edit rawhttp-req-in-edit RawHTTP ReqInEdit (HTTP Tests)

Actions Status

Maven Central

A Java library to make it easy to deal with raw HTTP 1.1, as defined by RFC-7230, and most of HTTP 1.0 (RFC-1945).

For details about using RawHTTP and the motivation for this project, see the blog post I wrote about it!

For testing HTTP servers, check out the blog post I wrote about rawhttp-req-in-edit, which lets you write HTTP files to send requests and assert responses using JS scripts.

For more documentation, visit the website.

Introduction

HTTP is really simple in 99.9% of cases.

For example, the raw HTTP request you would make to fetch a resource from a web server looks like this:

The example below is taken from the HTTP 1.1 RFC 7230.

GET /hello.txt HTTP/1.1
User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
Host: www.example.com
Accept-Language: en, mi

To send that request out to a HTTP server using RawHTTP, you can parse the Request and stream it out via a Socket.

Here's the whole code to do that:

RawHttp rawHttp = new RawHttp();

RawHttpRequest request = rawHttp.parseRequest(
    "GET /hello.txt HTTP/1.1\r\n" +
    "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n" +
    "Host: www.example.com\r\n" +
    "Accept-Language: en, mi");
Socket socket = new Socket("www.example.com", 80);
request.writeTo(socket.getOutputStream());

To read the response, it's just as easy:

RawHttpResponse<?> response = rawHttp.parseResponse(socket.getInputStream());

// call "eagerly()" in order to download the body
System.out.println(response.eagerly());

Which prints the complete response:

HTTP/1.1 404 Not Found
Accept-Ranges: bytes
Cache-Control: max-age=604800
Content-Type: text/html
Date: Mon, 04 Dec 2017 21:19:04 GMT
Expires: Mon, 11 Dec 2017 21:19:04 GMT
Last-Modified: Sat, 02 Dec 2017 02:10:22 GMT
Server: ECS (lga/1389)
Vary: Accept-Encoding
X-Cache: 404-HIT
Content-Length: 1270


<!doctype html>
...

A RawHttpResponse, just like a RawHttpRequest can be written to a File's, ServerSocket's or any other OutpuStream:

try (OutputStream out = Files.newOutputStream(responseFile.toPath())) {
    response.writeTo(out);
}

That simple!

Notice that just with the above, you have everything you need to send and receive HTTP messages.

To illustrate that, here is a simple implementation of a HTTP server that waits for a single request, then responds with a valid response:

RawHttp http = new RawHttp();
ServerSocket server = new ServerSocket(8083);

new Thread(() -> {
    try {
        Socket client = server.accept();
        RawHttpRequest request = http.parseRequest(client.getInputStream());

        if (request.getUri().getPath().equals("/saysomething")) {
            http.parseResponse("HTTP/1.1 200 OK\n" +
                    "Content-Type: text/plain\n" +
                    "Content-Length: 9\n" +
                    "\n" +
                    "something").writeTo(client.getOutputStream());
        } else {
            http.parseResponse("HTTP/1.1 404 Not Found\n" +
                    "Content-Type: text/plain\n" +
                    "Content-Length: 0\n" +
                    "\n").writeTo(client.getOutputStream());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}).start();

HTTP client

Even though it's quite simple to implement your own HTTP client by using the RawHttp class to parse requests and responses (which can then be transmitted via Sockets), RawHTTP offers a simple HTTP client definition (and implementation) that makes it a little bit more convenient to consume HTTP APIs.

Here's the RawHttpClient interface:

public interface RawHttpClient<Response> {
    RawHttpResponse<Response> send(RawHttpRequest request) throws IOException;
}

The Response type parameter allows implementations to expose their own type for HTTP Responses, if needed.

In the core module, a simple implementation is provided: TcpRawHttpClient.

Example usage:

RawHttpClient<?> client = new TcpRawHttpClient();
EagerHttpResponse<?> response = client.send(request).eagerly();

Unless you want to take care of streaming the response body later, always call eagerly() as shown above to consume the full response body (allowing the connection to be re-used).

Other implementations are available in separate modules:

  • RawHttpComponentsClient - based on HttpComponents's HttpClient.

Requires the rawhttp:rawhttp-httpcomponents module.

You can use this if you need support for external specifications, such as cookies (RFC-6265), or basic-auth, for example.

Example usage:

// use a default instance of CloseableHttpClient
RawHttpClient<?> client = new RawHttpComponentsClient();

// or create and configure your own client, then pass it into the constructor
CloseableHttpClient httpClient = HttpClients.createDefault();
RawHttpClient<?> client = new RawHttpComponentsClient(httpClient);

HTTP server

RawHTTP also contains a package defining a few types that describe a simple HTTP server.

The main type is the interface RawHttpServer, which uses a Router to route HTTP requests, returning a HTTP response. Router is a functional interface (i.e. it can be implemented with a Java lambda), so implementing a full server is very simple.

A default implementation of TcpRawHttpServer is provided... it spans a Thread (but re-uses it when possible) for each connected client.

Here's an example, written in Kotlin, of how to use RawHttpServer:

val server = TcpRawHttpServer(8093)

server.start { req ->
    when (req.uri.path) {
        "/hello", "/" ->
            when (req.method) {
                "GET" ->
                    http.parseResponse("HTTP/1.1 200 OK\n" +
                            "Content-Type: text/plain"
                    ).withBody(StringBody("Hello RawHTTP!"))
                else ->
                    http.parseResponse("HTTP/1.1 405 Method Not Allowed\n" +
                            "Content-Type: text/plain"
                    ).withBody(StringBody("Sorry, can't handle this method"))
            }
        else ->
            http.parseResponse("HTTP/1.1 404 Not Found\n" +
                    "Content-Type: text/plain"
            ).withBody(StringBody("Content was not found"))
    }
}

Samples

Several samples showing how to use RawHTTP, including all examples in this page, can be found in the samples project.

Note: to run the samples, execute the tests with the -Prun-samples argument.

The rawhttp-duplex module has its own sample, a chat application.

More Repositories

1

spock-reports

This project creates a global extension to Spock to create test reports.
Groovy
264
star
2

LogFX

LogFX is a simple Log reader supporting color highlighting and able to handle giant files.
Java
193
star
3

go-hash

Small utility to store secret information like passwords.
Go
104
star
4

Automaton

Simple framework which allows the testing of Swing and JavaFX2 applications.
Groovy
96
star
5

kunion

Union types for Kotlin
Kotlin
83
star
6

osgi-run

Osgi-Run - A Gradle plugin to make the development of modular applications using OSGi completely painless
Groovy
53
star
7

actors

Actor Model library for Dart.
Dart
47
star
8

mako-smarthome

Mako Lua Server for managing SmartHome devices based on the deCONZ API
Lua
34
star
9

jvm-alternatives-to-js

Repository comparing JVM alternatives to JS: CheerpJ, GWT, JSweet, TeaVM, Vaadin Flow, bck2brwsr (bonus: React, Dart)
Java
31
star
10

prechelt-phone-number-encoding

Comparison between Java and Common Lisp solutions to a phone-encoding problem described by Prechelt
Java
30
star
11

kotlin-hidden-costs-benchmark

A JMH benchmark of Kotlin hidden costs.
Java
29
star
12

wasm-on-jvm

A Gradle Plugin to compile WASM to JVM easily
Kotlin
24
star
13

ceylon-gradle-plugin

A simple Gradle plugin to manage Ceylon projects.
Groovy
22
star
14

jersey-guice-app

Application demonstrating a web app which provides a RESTful API through Jersey. It uses Guice as its Dependency Injection framework.
Java
18
star
15

osgiaas

OSGiaaS - OSGi as a Service
Java
17
star
16

jgrab

Runs Java code without a build system, grabbing dependencies declared in the Java file itself.
Java
17
star
17

structured_async

Structured concurrency for the Dart Programming Language.
Dart
16
star
18

magnanimous

The simplest and fastest static website generator in the world!!
Go
16
star
19

zig-common-tasks

Zig common tasks (code samples)
Zig
15
star
20

dartle

A simple build system written in Dart.
Dart
14
star
21

javanna

A Java library to create and introspect annotations at runtime.
Java
14
star
22

jbuild

JBuild is a intentionally simple, small build tool for Java.
Java
14
star
23

raw-jse

Vanilla Java: Using Java SE as a Framework
Java
13
star
24

CeylonFX

Ceylon interface for JavaFX
Ceylon
13
star
25

wasmin

A programming language that is a thin layer over pure WebAssembly (WASM).
Dart
13
star
26

specks

Specks enables a different way to check that your Ceylon code works
Ceylon
13
star
27

Grasmin

Groovy AST Transformation to allow writing Jasmin code (JVM bytecode) directly on groovy files
Groovy
13
star
28

parcey

A combinator parser for Ceylon
Ceylon
11
star
29

pony-gradle-plugin

A Gradle plugin to build Pony projects
Groovy
9
star
30

gohash_mobile_app

go-hash official mobile app (Android and iOS)
Dart
8
star
31

eventory

An event sourcing-inspired library targeting offline-first use, with automatic synchronization with remote instances when connection is available.
Dart
8
star
32

emacs.d

Personal emacs.d configuration
Emacs Lisp
7
star
33

gohash_mobile

go-hash Flutter Plugin
Objective-C
7
star
34

jb

The JBuild CLI.
Dart
6
star
35

faster-command-line-tools-kotlin

Faster command line applications with Kotlin (blog post backing repo)
Kotlin
6
star
36

ConcurrenCey

ConcurrenCey is a concurrency framework for the Ceylon language
Ceylon
6
star
37

isolate_current_directory

Support for changing the current working directory only within the scope of a function.
Dart
6
star
38

h_view

A wrk2 histogram viewer
Dart
5
star
39

ansi-color

A Racket library to make it easy to colorize terminal output
Racket
5
star
40

OsgiMonitor

This Project provides a set of bundles to allow users to monitor and control an OSGi instance.
Groovy
4
star
41

flattery

Flattery is a library for building HTML elements using Widgets.
Dart
4
star
42

zig-build-c

Zig Demo building and testing C code
Zig
4
star
43

kanvas

Kotlin and Groovy Canvas DSL based on JavaFX
Kotlin
4
star
44

wasmin-lang

A minimal WASM-focused programming language.
Rust
4
star
45

open_url

Dart lib to open a URL on the user's default application.
Dart
3
star
46

vinegar

A collection of functions and macros to help testing Rust code.
Rust
3
star
47

functions4j

Functions4J attempts to provide high-level functional constructs to Java 8+.
Java
3
star
48

vscode-ponylang

VSCode plugin for the Pony programming language.
TypeScript
3
star
49

spark-ws

Spark-WS is a Java Websockets library inspired by Spark for Java (which is inspired by Sinatra for Ruby).
Java
2
star
50

kootlin

Pure Kotlin OOP
Kotlin
2
star
51

rawhttp-tutorial

Source code for RawHTTP blog post.
Kotlin
2
star
52

kyang

A Yang IntelliJ Plugin
Kotlin
2
star
53

swing-selectors

Swing selectors allows declaratively selecting Swing components anywhere in a component tree - a breakoff from the Automaton testing framework
Groovy
2
star
54

dzipper

A CLI utility and library to extract zip file metadata.
D
2
star
55

mergequick

A web app to help manage and merge Pull Ruquests on popular code hosts.
Dart
1
star
56

simple-fx-app

Very simple JavaFX Application. Used for testing Automaton.
Java
1
star
57

MachineLearning

In this project, I implement some machine learning / data mining algorithms. The preferred language is Groovy. Email: [email protected]
Groovy
1
star
58

rpc-examples

Examples of implementations of services based on RPC frameworks.
Java
1
star
59

pathtrie

A Trie implementation specifically designed for paths
Java
1
star
60

ts-parcel

Basic TypeScript Browser App packaged with Parcel
HTML
1
star
61

dart-bf

brainfuck implementation in Dart
Dart
1
star
62

apps-bouncer

Application that keeps track of running processes, snitching bad behaving ones to the user, who can choose to kill them.
Dart
1
star
63

dart-sass-bulma-example

Example Dart Web Project that uses Sass and Bulma
HTML
1
star
64

checker-maven-demo

This project demonstrates the use of the checker framework with Maven.
Java
1
star
65

FxCodeEditor

A source code editor
Java
1
star
66

easy-jetty

Makes it really easy to embed a Jetty Web Server
Java
1
star
67

keepup

Tiny Java app self-updater designed to work with Java 11+ jlink distributions.
Java
1
star
68

protobuf-tcp-rsa-provider

TCP/Protobuffer implementation of Aries RSA DistributionProvider.
Java
1
star
69

ansi-escapes.zig

ANSI Escapes (colors, styles) for Zig Terminal-based applications
Zig
1
star
70

gradle-multi-java-modules-sample

This project shows how to organize Java modules in a Gradle project.
Java
1
star
71

test_report_parser.dart

Dart Model representing the events emitted by the Dart Tests JSON reporter
Dart
1
star
72

ceylon-medical-web-app

Example application in Ceylon using the type system to create reliable, extensible software
Ceylon
1
star