Skip to content
This repository was archived by the owner on Oct 23, 2022. It is now read-only.

add /cat endpoint #184

Merged
merged 45 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
4bdce1d
add initial version of /cat endpoint
Jun 9, 2020
4359e66
refactor: remove ipfs-unixfs::pb::UnixFsReadFailed from api
Jun 9, 2020
719163c
add: missing Error::source impl
Jun 9, 2020
fb91f51
add: try_parse to recover pbnode either way
Jun 9, 2020
86957ed
add: dagpb/unixfs path segment resolving
Jun 9, 2020
3097c16
add: caching via Option parameter
Jun 10, 2020
3872aae
refactor: simplify most the partitoning and err cases
Jun 10, 2020
2f07dd6
refactor: align errors, cleanup
Jun 10, 2020
588ff5a
refactor: inline refactored methods
Jun 10, 2020
301011d
chore: cargo fmt
Jun 10, 2020
74ac580
refactor: simplify conversions to cid and unify error type
Jun 10, 2020
92b782e
chore: doc and fmt
Jun 10, 2020
4a800c4
add: re-export of ipfs-unixfs under ipfs::unixfs::ll
Jun 10, 2020
da23a23
add: StdError impl for ResolveError, helpers
Jun 10, 2020
b26332a
add: UnexpectedNodeType for specific error messages
Jun 10, 2020
ffa747d
refactor: resolve first block before streaming bodies
Jun 10, 2020
819d8eb
fix: resolve ipfspaths with ipfs-unixfs on dag-pb
Jun 10, 2020
0aca449
refactor: move test case over to ipfs-unixfs
Jun 10, 2020
7043fe2
fix returning outer pbnode when empty inner
Jun 10, 2020
392840c
chore: cargo fmt
Jun 10, 2020
d78a50c
chore: update unixfs readme
Jun 10, 2020
da7f2b4
fix: get dag tests passing again
Jun 11, 2020
ba98835
add: dagpb getters to ipfs-unixfs
Jun 11, 2020
b65f6b7
fix: handle corner case gracefully in walk_path
Jun 11, 2020
5fed9c5
chore: cargo fmt
Jun 11, 2020
b1f635a
chore: cargo fmt again
Jun 11, 2020
808a1da
refactor: simplify error types, less matched cases
Jun 11, 2020
6c88e96
refactor: split shard stuff away from dir
Jun 11, 2020
d0070f0
add minor test case
Jun 11, 2020
ec9607f
add test cases for sharded lookup
Jun 11, 2020
3f0d9ac
add: err when resolving through unsupported type
Jun 11, 2020
17812a2
add: more test cases
Jun 11, 2020
2766857
add: more locking down tests
Jun 11, 2020
bcc7a0c
chore: remove extra TODO
Jun 11, 2020
b1575f2
refactor: remove unused conversion
Jun 11, 2020
2d99d1a
fix: adjust default log levels with the addition of a crate
Jun 11, 2020
31b30cd
fix: load the block always on walk_paths
Jun 11, 2020
6102488
chore: cargo fmt
Jun 11, 2020
38b7e4b
doc: add comments for the long function
Jun 11, 2020
d5be602
Apply suggestions from code review
koivunej Jun 11, 2020
38e41ff
Apply suggestions from code review
koivunej Jun 11, 2020
f935c7b
refactor: use unwrap_or_else
Jun 11, 2020
42fd182
refactor: use map_err
Jun 11, 2020
e43008f
doc: fix comment
Jun 11, 2020
169be7b
refactor: extract special dagpb handling out
Jun 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion examples/examples/fetch_and_cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ fn main() {

eprintln!();

let stream = ipfs.cat_unixfs(cid, None);
let stream = match ipfs.cat_unixfs(cid, None).await {
Ok(stream) => stream,
Err(e) => {
eprintln!("Error: {}", e);
exit(1);
}
};
// The stream needs to be pinned on the stack to be used with StreamExt::next
pin_mut!(stream);
let mut stdout = async_std::io::stdout();
Expand Down
5 changes: 4 additions & 1 deletion http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ fn main() {
if std::env::var_os("RUST_LOG").is_none() {
// FIXME: see if tracing could be used as the frontend for log macros
// FIXME: use log macros here as well
std::env::set_var("RUST_LOG", "ipfs-http=trace,ipfs=trace,bitswap=trace");
std::env::set_var(
"RUST_LOG",
"ipfs_http=trace,ipfs=trace,bitswap=trace,ipfs_unixfs=trace",
);
}

env_logger::init();
Expand Down
2 changes: 2 additions & 0 deletions http/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod dag;
pub mod id;
pub mod pubsub;
pub mod refs;
pub mod root_files;
pub mod swarm;
pub mod version;

Expand Down Expand Up @@ -70,6 +71,7 @@ pub fn routes<T: IpfsTypes>(
dag::resolve(ipfs),
warp::path!("dht" / ..).and_then(not_implemented),
warp::path!("get").and_then(not_implemented),
root_files::cat(ipfs),
warp::path!("key" / ..).and_then(not_implemented),
warp::path!("name" / ..).and_then(not_implemented),
warp::path!("object" / ..).and_then(not_implemented),
Expand Down
49 changes: 9 additions & 40 deletions http/src/v0/dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,56 +92,25 @@ async fn inner_resolve<T: IpfsTypes>(
ipfs: Ipfs<T>,
opts: ResolveOptions,
) -> Result<impl Reply, Rejection> {
use crate::v0::refs::{IpfsPath, WalkSuccess};
use ipfs::Block;
use libipld::block::decode_ipld;
use std::collections::VecDeque;
use crate::v0::refs::{walk_path, IpfsPath};
use std::convert::TryFrom;

let mut path = IpfsPath::try_from(opts.arg.as_str()).map_err(StringError::from)?;
let path = IpfsPath::try_from(opts.arg.as_str()).map_err(StringError::from)?;

let root = path.take_root().unwrap();

// the walk api and path owning the strings doesn't really work so nicely here
let mut current = root;
let mut remaining = path
.remaining_path()
.iter()
.cloned()
.collect::<VecDeque<_>>();

loop {
let Block { data, .. } = ipfs.get_block(&current).await.map_err(StringError::from)?;
let ipld = decode_ipld(&current, &data).map_err(StringError::from)?;
let res = path.walk(&current, ipld).map_err(StringError::from)?;

match res {
WalkSuccess::EmptyPath(_) | WalkSuccess::AtDestination(_) => {
break;
}
WalkSuccess::Link(_key, next_cid) => {
// the return value of RemPath needs to be the path inside the last document
// but our walk will eventually exhaust the items of IpfsPath. update the backup
// in `remaining` only while traversing links
while remaining.len() > path.len() {
remaining.pop_front();
}

current = next_cid;
}
};
}
let (current, _, remaining) = walk_path(&ipfs, path).await.map_err(StringError::from)?;

let remaining = {
let mut buf = String::with_capacity(
remaining.iter().map(|s| s.len()).sum::<usize>() + remaining.len(),
);
while let Some(piece) = remaining.pop_front() {
let slashes = remaining.len();
let mut buf =
String::with_capacity(remaining.iter().map(|s| s.len()).sum::<usize>() + slashes);

for piece in remaining.into_iter().rev() {
if !buf.is_empty() {
buf.push('/');
}
buf.push_str(&piece);
}

buf
};

Expand Down
Loading