gdhttp

Crates.iogdhttp
lib.rsgdhttp
version1.0.3
sourcesrc
created_at2023-05-15 12:19:12.233265
updated_at2023-05-15 20:21:50.927104
descriptionAs low-level as you can do HTTP server for rust lang.
homepage
repositoryhttps://github.com/itedya/gdhttp-rs
max_upload_size
id864970
size12,951
Bartłomiej Domański (itedya)

documentation

https://github.com/itedya/gdhttp-rs

README

Development of this library has been discontinued

GDHttp

GDHttp is as low-level as you can make HTTP server for rust lang.

Only supports HTTP 1.1 at the moment

How to use it?

You can start server with start or start_multithreaded method. The difference is that second one executes callback by thread::spawn (your second argument that you had passed). In callback, you can do whatever you want with the request, but you must return a http response struct.

Documentation

And I know, I know, that status_code_message thing will be done some other way. It's temporary. I just needed this library as fast as I could make it, so that is what I came up with.

Simple example that responds to everything with 200 OK, Hello World:

use std::collections::HashMap;
use gdhttp::HttpResponse;

fn main() {
    let _ = gdhttp::start("0.0.0.0:8080", |_request| {
        return HttpResponse {
            body: "Hello world".to_string(),
            headers: HashMap::new(),
            status_code: 200,
            status_code_message: "OK".to_string(),
        };
    });
}

Example with multithreading:

use std::collections::HashMap;
use gdhttp::HttpResponse;

fn main() {
    let _ = gdhttp::start_multithreaded("0.0.0.0:8080", |_request| {
        return HttpResponse {
            body: "Hello world".to_string(),
            headers: HashMap::new(),
            status_code: 200,
            status_code_message: "OK".to_string(),
        };
    });
}

My app throws empty 400 Bad requests, what am I doing wrong?

This lib throws 400 bad request error on every payload received from client that is invalid. Probably your request is just incorrectly written. If you think that it is not, open an Issue. This lib is very young, so it's normal that it can happen.

Benchmarks

Two benchmarks were done on ASUS Zenbook 14 UX425E i5-1135G7 16GB RAM on Fedora 37 KDE variant. Autocannon has been used to make all the benchmarks.

Autocannon options:

  • Duration: 60s
  • Concurrent connections: 30
  • Workers: 3

1. Start method

Executed code:

use std::collections::HashMap;
use gdhttp::HttpResponse;

fn main() {
    let _ = gdhttp::start("0.0.0.0:8080", |_| {
        return HttpResponse {
            body: "Hello world".to_string(),
            headers: HashMap::new(),
            status_code: 200,
            status_code_message: "OK".to_string(),
        };
    });
}

Results:

  • 19035 request/second (average)
  • 2 284 000 requests in 60.01s
  • CPU usage did not exceed 5,7% (measured by btop)
  • Max RAM usage: 2.00MiB (measured by btop)

2. Start multithreaded method

Executed code:

use std::collections::HashMap;
use gdhttp::HttpResponse;

fn main() {
    let _ = gdhttp::start_multithreaded("0.0.0.0:8080", |_| {
        return HttpResponse {
            body: "Hello world".to_string(),
            headers: HashMap::new(),
            status_code: 200,
            status_code_message: "OK".to_string(),
        };
    });
}

Results:

  • 14897 request/second (average)
  • 1 788 000 requests in 60.03s
  • CPU usage: 22% (measured by btop)
  • Max ram usage: 2.71MiB (measured by btop)
Commit count: 17

cargo fmt