Skip to content

Commit

Permalink
feat: add basic timeout settings
Browse files Browse the repository at this point in the history
jjeffcaii committed Aug 5, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent fb31745 commit 49e2864
Showing 18 changed files with 465 additions and 108 deletions.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -8,3 +8,68 @@
A reverse proxy in Rust, which is inspired from Nginx/OpenResty/Envoy.

> WARNING: still in an active development!!!
## Quick Start

- Prepare Bootstrap YAML

```yaml
loggers:
main:
path: ~/capybara/logs/main.log

providers:
- kind: static_file
props:
path: /your/path/config.yaml
```
- Prepare config YAML
```yaml
listeners:
httpbin:
listen: 0.0.0.0:80
protocol:
name: http
props:
client_header_timeout: 30s
pipelines:
- name: capybara.pipelines.http.lua
props:
# write lua script
content: |
local cnts = 0
function handle_request_line(ctx,request_line)
-- set the upstream here, which links to 'upstreams.httpbin':
ctx:set_upstream('upstream://httpbin')
end
function handle_status_line(ctx,status_line)
ctx:replace_header('X-Powered-By','capybara')
-- set a custom response header which counts the requests:
cnts = cnts + 1
ctx:replace_header('X-Capybara-Requests', tostring(cnts))
end
upstreams:
httpbin:
transport: tcp
resolver: default
balancer: weighted
endpoints:
- addr: httpbin.org:443
weight: 70
- addr: postman-echo.com:443
weight: 30

```

- Run & Test

```shell
$ cargo run --bin capybara -- run -c bootstrap.yaml
$ curl -i http://localhost/get
```
1 change: 1 addition & 0 deletions capybara-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -65,6 +65,7 @@ moka = { version = "0.12", features = ["future", "sync"] }
mlua = { version = "0.9", features = ["luajit", "vendored", "serialize", "async", "macros", "send", "parking_lot"] }
garde = { version = "0.20", features = ["serde", "derive", "pattern", "regex"] }
bytesize = { version = "1.2", features = ["serde"] }
liquid = "0.26"

[[example]]
name = "httpbin"
4 changes: 4 additions & 0 deletions capybara-core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;
use std::io;
use tokio::time::error::Elapsed;

#[derive(thiserror::Error, Debug)]
pub enum CapybaraError {
@@ -58,6 +59,9 @@ pub enum CapybaraError {
#[error("cannot parse upstream from '{0}'")]
InvalidUpstream(Cow<'static, str>),

#[error("operation timeout")]
Timeout,

#[error(transparent)]
Other(#[from] anyhow::Error), // source and Display delegate to anyhow::Error
}
1 change: 0 additions & 1 deletion capybara-core/src/pipeline/http/mod.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ pub use registry::{register, HttpPipelineFactory};

mod noop;
mod pipeline;
mod pipeline_acme;
mod pipeline_lua;
mod pipeline_router;
mod registry;
3 changes: 2 additions & 1 deletion capybara-core/src/pipeline/http/pipeline.rs
Original file line number Diff line number Diff line change
@@ -78,7 +78,8 @@ pub(crate) struct HttpContextFlags(u32);

bitflags! {
impl HttpContextFlags: u32 {
const DOWNSTREAM_EXHAUSTED = 1 << 0;
const HTTPS = 1 << 0;
const DOWNSTREAM_EXHAUSTED = 1 << 1;
}
}

53 changes: 0 additions & 53 deletions capybara-core/src/pipeline/http/pipeline_acme.rs

This file was deleted.

18 changes: 18 additions & 0 deletions capybara-core/src/pipeline/http/pipeline_lua.rs
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use tokio::sync::Mutex;

use crate::pipeline::http::HttpContextFlags;
use crate::pipeline::{HttpContext, HttpPipeline, HttpPipelineFactory, PipelineConf};
use crate::proto::UpstreamKey;
use crate::protocol::http::{Headers, HttpField, Method, RequestLine, Response, StatusLine};
@@ -103,6 +104,15 @@ impl UserData for LuaHttpRequestContext {
Ok(ctx.id())
});

methods.add_method("schema", |lua, this, ()| {
let ctx = unsafe { this.0.as_mut() }.unwrap();
if ctx.flags.contains(HttpContextFlags::HTTPS) {
Ok("https")
} else {
Ok("http")
}
});

methods.add_method("replace_header", |_, this, args: (LuaString, LuaString)| {
let ctx = unsafe { this.0.as_mut() }.unwrap();
let key = args.0.to_string_lossy();
@@ -203,6 +213,14 @@ impl UserData for LuaHttpResponseContext {
let ctx = unsafe { this.0.as_mut() }.unwrap();
Ok(ctx.id())
});
methods.add_method("schema", |lua, this, ()| {
let ctx = unsafe { this.0.as_mut() }.unwrap();
if ctx.flags.contains(HttpContextFlags::HTTPS) {
Ok("https")
} else {
Ok("http")
}
});

methods.add_method("replace_header", |_, this, args: (LuaString, LuaString)| {
let ctx = unsafe { this.0.as_mut() }.unwrap();
7 changes: 7 additions & 0 deletions capybara-core/src/protocol/http/listener/408.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head><title>408 Request Timeout</title></head>
<body>
<center><h1>408 Request Timeout</h1></center>
<hr><center>{{ server }}</center>
</body>
</html>
7 changes: 7 additions & 0 deletions capybara-core/src/protocol/http/listener/500.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>{{ server }}</center>
</body>
</html>
2 changes: 1 addition & 1 deletion capybara-core/src/protocol/http/listener/502.html
Original file line number Diff line number Diff line change
@@ -2,6 +2,6 @@
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>capybara/0.0.1</center>
<hr><center>{{ server }}</center>
</body>
</html>
7 changes: 7 additions & 0 deletions capybara-core/src/protocol/http/listener/504.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head><title>504 Gateway Timeout</title></head>
<body>
<center><h1>504 Gateway Timeout</h1></center>
<hr><center>{{ server }}</center>
</body>
</html>
Loading

0 comments on commit 49e2864

Please sign in to comment.