Crates.io | gdhttp |
lib.rs | gdhttp |
version | 1.0.3 |
source | src |
created_at | 2023-05-15 12:19:12.233265 |
updated_at | 2023-05-15 20:21:50.927104 |
description | As low-level as you can do HTTP server for rust lang. |
homepage | |
repository | https://github.com/itedya/gdhttp-rs |
max_upload_size | |
id | 864970 |
size | 12,951 |
GDHttp is as low-level as you can make HTTP server for rust lang.
Only supports HTTP 1.1 at the moment
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.
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(),
};
});
}
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.
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:
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:
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: