Skip to content

Commit

Permalink
Merge pull request #18 from jubako/adapt_jbk_api
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr authored Jul 24, 2024
2 parents 19a8376 + 0ba539e commit 4a49f74
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 39 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions libwaj/src/common/builder.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use super::entry::*;
use super::entry_type::EntryType;
use super::{AllProperties, Reader};
use super::AllProperties;
use jbk::reader::builder::PropertyBuilderTrait;
use jbk::reader::ByteSlice;

pub trait Builder {
type Entry;

fn new(properties: &AllProperties) -> Self;
fn create_entry(&self, idx: jbk::EntryIdx, reader: &Reader) -> jbk::Result<Self::Entry>;
fn create_entry(&self, idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<Self::Entry>;
}

impl Builder for () {
type Entry = ();
fn new(_properties: &AllProperties) -> Self {}
fn create_entry(&self, _idx: jbk::EntryIdx, _reader: &Reader) -> jbk::Result<Self::Entry> {
fn create_entry(&self, _idx: jbk::EntryIdx, _reader: &ByteSlice) -> jbk::Result<Self::Entry> {
Ok(())
}
}
Expand All @@ -25,12 +26,12 @@ pub trait FullBuilderTrait {
fn create_content(
&self,
idx: jbk::EntryIdx,
reader: &Reader,
reader: &ByteSlice,
) -> jbk::Result<<Self::Entry as EntryDef>::Content>;
fn create_redirect(
&self,
idx: jbk::EntryIdx,
reader: &Reader,
reader: &ByteSlice,
) -> jbk::Result<<Self::Entry as EntryDef>::Redirect>;
}

Expand All @@ -50,15 +51,15 @@ where
fn create_content(
&self,
idx: jbk::EntryIdx,
reader: &Reader,
reader: &ByteSlice,
) -> jbk::Result<<Self::Entry as EntryDef>::Content> {
self.0.create_entry(idx, reader)
}

fn create_redirect(
&self,
idx: jbk::EntryIdx,
reader: &Reader,
reader: &ByteSlice,
) -> jbk::Result<<Self::Entry as EntryDef>::Redirect> {
self.1.create_entry(idx, reader)
}
Expand Down
2 changes: 0 additions & 2 deletions libwaj/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use jbk::reader::builder::{BuilderTrait, PropertyBuilderTrait};
use jbk::reader::Range;
pub use properties::{AllProperties, Property};

pub use jbk::SubReader as Reader;

pub const VENDOR_ID: jbk::VendorId = jbk::VendorId::new([0x77, 0x61, 0x6a, 0x00]);

pub struct Comparator {
Expand Down
9 changes: 5 additions & 4 deletions libwaj/src/entry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::common::{AllProperties, Builder, Reader};
use crate::common::{AllProperties, Builder};
use jbk::reader::builder::PropertyBuilderTrait;
use jbk::reader::ByteSlice;

pub struct CommonPart {
idx: jbk::EntryIdx,
Expand Down Expand Up @@ -68,7 +69,7 @@ mod private {
}
}

fn create_entry(&self, idx: jbk::EntryIdx, reader: &Reader) -> jbk::Result<CommonPart> {
fn create_entry(&self, idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<CommonPart> {
let path_prop = self.path_property.create(reader)?;
let mut path = vec![];
path_prop.resolve_to_vec(&mut path)?;
Expand All @@ -93,7 +94,7 @@ mod private {
}
}

fn create_entry(&self, idx: jbk::EntryIdx, reader: &Reader) -> jbk::Result<Self::Entry> {
fn create_entry(&self, idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<Self::Entry> {
let mimetype_prop = self.mimetype_property.create(reader)?;
let mut mimetype = vec![];
mimetype_prop.resolve_to_vec(&mut mimetype)?;
Expand All @@ -120,7 +121,7 @@ mod private {
}
}

fn create_entry(&self, idx: jbk::EntryIdx, reader: &Reader) -> jbk::Result<Self::Entry> {
fn create_entry(&self, idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<Self::Entry> {
let common = self.common.create_entry(idx, reader)?;
let target_prop = self.link_property.create(reader)?;
let mut target = vec![];
Expand Down
2 changes: 1 addition & 1 deletion libwaj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod serve;
mod waj;
pub mod walk;

pub use common::{AllProperties, Builder, Entry, FullBuilderTrait, Reader, VENDOR_ID};
pub use common::{AllProperties, Builder, Entry, FullBuilderTrait, VENDOR_ID};
pub use entry::*;
pub use serve::Server;
pub use waj::Waj;
Expand Down
34 changes: 16 additions & 18 deletions libwaj/src/serve.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::common::{AllProperties, Builder, Entry, Reader};
use crate::common::{AllProperties, Builder, Entry};
use crate::Waj;
use ascii::IntoAsciiString;
use jbk::reader::builder::PropertyBuilderTrait;
use jbk::reader::{ByteRegion, ByteSlice};
use log::{debug, error, trace, warn};
use percent_encoding::{percent_decode, percent_encode, CONTROLS};
use std::borrow::Cow;
Expand Down Expand Up @@ -50,7 +51,7 @@ impl Builder for ContentBuilder {
}
}

fn create_entry(&self, _idx: jbk::EntryIdx, reader: &Reader) -> jbk::Result<Self::Entry> {
fn create_entry(&self, _idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<Self::Entry> {
let content_address = self.content_address_property.create(reader)?;
let mut mimetype = Default::default();
self.content_mimetype_property
Expand All @@ -76,7 +77,7 @@ impl Builder for RedirectBuilder {
}
}

fn create_entry(&self, _idx: jbk::EntryIdx, reader: &Reader) -> jbk::Result<Self::Entry> {
fn create_entry(&self, _idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<Self::Entry> {
let target_prop = self.target_property.create(reader)?;
let mut target = vec![];
target_prop.resolve_to_vec(&mut target)?;
Expand Down Expand Up @@ -119,21 +120,21 @@ impl RequestHandler {
///
/// No tricky part.
/// We set cache header as content will never change without waj change.
fn build_response_from_reader(
fn build_response_from_bytes(
&self,
reader: jbk::Reader,
bytes: ByteRegion,
with_content: bool,
status_code: u16,
) -> ResponseBox {
let mut response = Self::build_response_from_read(
reader.create_flux_all().to_owned(),
Some(reader.size().into_usize()),
bytes.stream(),
Some(bytes.size().into_usize()),
with_content,
status_code,
);
response.add_header(Header {
field: "Content-Length".parse().unwrap(),
value: reader.size().into_usize().to_string().parse().unwrap(),
value: bytes.size().into_usize().to_string().parse().unwrap(),
});
response.add_header(Header {
field: "Cache-Control".parse().unwrap(),
Expand All @@ -155,12 +156,12 @@ impl RequestHandler {
/// If not, we have to generate a dummy content (and no cache, as it may change if server change)
fn build_content_response(
&self,
reader: jbk::reader::MayMissPack<jbk::Reader>,
bytes: jbk::reader::MayMissPack<ByteRegion>,
with_content: bool,
status_code: u16,
mimetype: &str,
) -> jbk::Result<ResponseBox> {
match reader {
match bytes {
jbk::reader::MayMissPack::MISSING(pack_info) => {
let (msg, mimetype, status_code) = match mimetype {
"text/html" | "text/css" | "application/javascript" => {
Expand Down Expand Up @@ -199,9 +200,8 @@ impl RequestHandler {
});
Ok(response)
}
jbk::reader::MayMissPack::FOUND(reader) => {
let mut response =
self.build_response_from_reader(reader, with_content, status_code);
jbk::reader::MayMissPack::FOUND(bytes) => {
let mut response = self.build_response_from_bytes(bytes, with_content, status_code);
response.add_header(Header {
field: "Content-Type".parse().unwrap(),
value: mimetype.parse().unwrap(),
Expand All @@ -223,7 +223,7 @@ impl RequestHandler {
match e {
Entry::Content(e) => {
return self.build_content_response(
self.waj.get_reader(e.content_address)?,
self.waj.get_bytes(e.content_address)?,
with_content,
200,
&String::from_utf8_lossy(&e.mimetype),
Expand All @@ -245,10 +245,8 @@ impl RequestHandler {
// No entry found. Return 404. If we have one in the Waj use it, else return empty 404.
warn!("{url} not found");
if let Ok(Entry::Content(e)) = self.waj.get_entry::<FullBuilder>("404.html") {
if let jbk::reader::MayMissPack::FOUND(reader) =
self.waj.get_reader(e.content_address)?
{
let mut response = self.build_response_from_reader(reader, with_content, 404);
if let jbk::reader::MayMissPack::FOUND(bytes) = self.waj.get_bytes(e.content_address)? {
let mut response = self.build_response_from_bytes(bytes, with_content, 404);
response.add_header(Header {
field: "Content-Type".parse().unwrap(),
value: String::from_utf8_lossy(&e.mimetype).parse().unwrap(),
Expand Down
6 changes: 2 additions & 4 deletions libwaj/src/waj.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use super::common::{AllProperties, Builder, Comparator, Entry, FullBuilderTrait, RealBuilder};
use jbk::reader::builder::PropertyBuilderTrait;
use jbk::reader::Range;
use jbk::reader::{ByteSlice, Range};
use std::path::Path;

pub use jbk::SubReader as Reader;

pub struct Waj {
container: jbk::reader::Container,
pub(crate) root_index: jbk::reader::Index,
Expand Down Expand Up @@ -41,7 +39,7 @@ impl Builder for PathBuilder {
}
}

fn create_entry(&self, _idx: jbk::EntryIdx, reader: &Reader) -> jbk::Result<Self::Entry> {
fn create_entry(&self, _idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<Self::Entry> {
let path_prop = self.path_property.create(reader)?;
let mut path = vec![];
path_prop.resolve_to_vec(&mut path)?;
Expand Down

0 comments on commit 4a49f74

Please sign in to comment.