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

Improve /add interop #380

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ warp = { default-features = false, version = "0.2" }

[dev-dependencies]
hex-literal = { default-features = false, version = "0.3" }
rand = { default-features = false, version = "0.7" }
tempfile = { default-features = false, version = "3.1" }
56 changes: 37 additions & 19 deletions http/src/v0/root_files/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,7 @@ where
let content_type = field.content_type().map_err(AddError::Header)?;

let next = match content_type {
"application/octet-stream" => {

// files are of the form "file-{1,2,3,..}"
let _ = if field_name != "file" && !field_name.starts_with("file-") {
Err(AddError::UnsupportedField(field_name.to_string()))
} else {
Ok(())
}?;
Comment on lines -148 to -153
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this is a good idea to be removed. I mean if you think of a html form which is pretty much the basis for http multipart and fields with different names kinda should be different things so I'd like to retain some validation OR targeting here. Or are the go-ipfs and js-ipfs using random field names here? I'd rather see them "enumerated" here.

my comment is a bit off here, I think js-ipfs-http-client does the file-N.


"application/octet-stream" | "text/plain" => {
let mut adder = FileAdder::default();
// how many bytes we have stored as blocks
let mut total_written = 0u64;
Expand Down Expand Up @@ -376,10 +368,14 @@ impl<D: fmt::Display> serde::Serialize for Quoted<D> {
#[cfg(test)]
mod tests {
use crate::v0::root_files::add;
use ipfs::Node;
use rand::distributions::Alphanumeric;
use rand::Rng;
use std::iter;

#[tokio::test(max_threads = 1)]
async fn add_single_block_file() {
let ipfs = tokio_ipfs().await;
let ipfs = Node::new("0").await;

// this is from interface-ipfs-core, pretty much simplest add a buffer test case
// but the body content is from the pubsub test case I copied this from
Expand Down Expand Up @@ -408,15 +404,37 @@ mod tests {
);
}

async fn tokio_ipfs() -> ipfs::Ipfs<ipfs::TestTypes> {
let options = ipfs::IpfsOptions::inmemory_with_generated_keys();
let (ipfs, fut) = ipfs::UninitializedIpfs::new(options, None)
.await
.start()
.await
.unwrap();
// copied from js-ipfs/packages/ipfs/test/http-api/inject/files.js
#[tokio::test(max_threads = 1)]
async fn js_multipart_test() {
let ipfs = Node::new("0").await;
let mut rng = rand::thread_rng();

let response = warp::test::request()
.path("/add")
.header(
"Content-Type",
"multipart/form-data; boundary=----------287032381131322",
)
.body(
format!(
"\r\n\
------------287032381131322\r\n\
Content-Disposition: form-data; name=\"test\"; filename=\"test.txt\"\r\n\
Content-Type: text/plain\r\n\
\r\n\
{}\r\n\
------------287032381131322--",
iter::repeat(())
.map(|_| rng.sample(Alphanumeric))
.take(1024 * 1024 * 2)
.collect::<String>()
)
.into_bytes(),
)
.reply(&add(&ipfs))
.await;

tokio::spawn(fut);
ipfs
assert_eq!(response.status(), 200);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this test case is just to accept the text/plain as I guess js-ipfs accepts it?

accepting text/plain sounds really strange be honest ... wouldn't that require some charset wrangling as well? I guess there's no issues if you assume ascii or utf8 but ... Why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was the way js-ipfs did it, I just followed suit; since it's random alphanumeric bytes there's no encoding issues

}
}