Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Termack committed May 9, 2024
1 parent 4c254e9 commit fc19e24
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 31 deletions.
3 changes: 2 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
IMPORTANT: Make a better implementation for reading body and http2 and implement tests for both
Add Websocket
HTTP2 and some other things in http1 (like keepalive, chunked, gzip)
Check test coverage
More tests (read body async, http2, some other things)
Reload isn't working correctly when remove or add a new plugin (example: comment static_files_path and uncomment proxy_address)
Add proxy and jequi_go proxy tests
Add Websocket
Add plugin to generate certificate
Add logging
Add metrics
Expand Down
2 changes: 1 addition & 1 deletion jequi/src/http1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<T: AsyncRead + AsyncWrite + Unpin + Send> Http1Conn<T> {
}
pub async fn handle_connection(&mut self, config_map: Arc<ConfigMap>) {
let plugin_list = &config_map.config;
let conf = get_plugin!(plugin_list, jequi);
let conf = get_plugin!(plugin_list, jequi).unwrap();

self.handle_request(conf, config_map.clone()).await;
if let Some(connection) = self.request.headers.get("connection")
Expand Down
13 changes: 11 additions & 2 deletions jequi/src/http2/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,24 @@ impl<T: AsyncRead + AsyncWrite + Unpin + Send> Http2Conn<T> {
);

let config = config_map.get_config_for_request(request.host.as_deref(), Some(&request.uri));
let conf = get_plugin!(config, jequi);
let conf = get_plugin!(config, jequi).unwrap();

self.conn
.write_all(&response_headers.encode())
.await
.unwrap();
self.conn.flush().await.unwrap();

let last = response.body_buffer.len().div_ceil(conf.chunk_size) - 1;
let body_len = response.body_buffer.len();
if body_len == 0 {
let response_body = Http2Frame::new(FrameType::Data, END_STREAM_FLAG, stream_id, b"");

self.conn.write_all(&response_body.encode()).await.unwrap();
self.conn.flush().await.unwrap();
return;
}

let last = body_len.div_ceil(conf.chunk_size) - 1;
for (i, chunk) in response.body_buffer.chunks(conf.chunk_size).enumerate() {
let response_body = Http2Frame::new(
FrameType::Data,
Expand Down
3 changes: 3 additions & 0 deletions jequi/src/http2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ impl Stream {
(self.id, self.request, self.response)
}
}

#[cfg(test)]
mod test;
1 change: 1 addition & 0 deletions jequi/src/http2/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 1 addition & 1 deletion jequi/src/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub async fn ssl_new<T: AsyncRead + AsyncWrite + Unpin + Send>(
let mut ctx_builder = SslContextBuilder::new(SslMethod::tls()).unwrap();
let config =
config_map.get_config_for_request(ssl_ref.servername(NameType::HOST_NAME), None);
let conf = get_plugin!(config, jequi);
let conf = get_plugin!(config, jequi).unwrap();
let http2 = conf.http2;

ctx_builder.set_private_key(&key).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion jequi/src/tcp_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub async fn new_http_conn<T: AsyncRead + AsyncWrite + Unpin + Send>(
config_map: Arc<ConfigMap>,
) -> HttpConn<T> {
let plugin_list = &config_map.config;
let conf = get_plugin!(plugin_list, jequi);
let conf = get_plugin!(plugin_list, jequi).unwrap();

if conf.tls_active {
let (stream, version) = ssl_new(stream, config_map.clone()).await;
Expand Down
31 changes: 16 additions & 15 deletions plugins/jequi_go/go/handle/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package handle

import (
"fmt"
"strings"
// "strings"

"github.com/jequi_go"
)
Expand All @@ -19,18 +19,19 @@ func HandleRequest(req jequi_go.Request, resp jequi_go.Response) {
}

func HandleProxyRequest(req jequi_go.Request, resp jequi_go.Response) *string {
val := strings.SplitN(req.GetUri(), "/", 3)
if len(val) == 1 || (len(val) == 2 && val[1] == "") {
return nil
}

address := val[1]
newUri := "/"

if len(val) == 3 {
newUri = newUri + val[2]
}
req.SetUri(newUri)

return &address
// val := strings.SplitN(req.GetUri(), "/", 3)
// if len(val) == 1 || (len(val) == 2 && val[1] == "") {
// return nil
// }
//
// address := val[1]
// newUri := "/"
//
// if len(val) == 3 {
// newUri = newUri + val[2]
// }
// req.SetUri(newUri)
//
// return &address
return nil
}
1 change: 0 additions & 1 deletion plugins/jequi_go/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ mod tests {

use crate::Config;

#[ignore]
#[tokio::test]
async fn handle_go_request() {
let mut http = Http1Conn::new(RawStream::Normal(Cursor::new(vec![])));
Expand Down
25 changes: 16 additions & 9 deletions plugins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,33 @@ pub fn get_plugin(input: TokenStream) -> TokenStream {
Some(plugin_type) => {
if plugin_type == "Option" {
if input.mutability.is_some() {
quote!(#list[#index].as_mut().map(|plugin| {
quote!(match #list.get_mut(#index) {
Some(config) => config.as_mut().map(|plugin| {
unsafe { Arc::get_mut_unchecked(&mut plugin.config) }
.as_any_mut()
.downcast_mut::<#plugin_name::Config>()
.unwrap()
}))
.as_any_mut()
.downcast_mut::<#plugin_name::Config>()
.unwrap()}),
None => None,
})
} else {
quote!(#list[#index].as_mut().map(|plugin| {
quote!(match #list.get(#index){
Some(config) => config.as_mut().map(|plugin| {
plugin.config
.as_any()
.downcast_ref::<#plugin_name::Config>()
.unwrap()
}))
.unwrap()}),
None => None,
})
}
} else {
return quote! {compile_error!("invalid plugin name")}.into();
}
}
None => {
quote!(#list[#index].config.as_any().downcast_ref::<#plugin_name::Config>().unwrap())
quote!(match #list.get(#index) {
Some(config) => config.config.as_any().downcast_ref::<#plugin_name::Config>(),
None => None,
})
}
};

Expand Down

0 comments on commit fc19e24

Please sign in to comment.