New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read file URL in chunks #21560
Merged
+46
−5
Merged
Read file URL in chunks #21560
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
Loading status checks…
Read file in chunks and send chunks to FetchTaskTarget instead of rea…
…d_to_end call
- Loading branch information
commit f04c965a9bab46ff6943416baab338032bc526c1
| @@ -25,14 +25,16 @@ use servo_url::ServoUrl; | ||
| use std::borrow::Cow; | ||
| use std::fmt; | ||
| use std::fs::File; | ||
| use std::io::Read; | ||
| use std::io::{BufReader, BufRead}; | ||
| use std::mem; | ||
| use std::str; | ||
| use std::sync::{Arc, Mutex}; | ||
| use std::sync::atomic::Ordering; | ||
| use std::sync::mpsc::{Sender, Receiver}; | ||
| use subresource_integrity::is_response_integrity_valid; | ||
|
|
||
| const FILE_CHUNK_SIZE: usize = 32768; //32 KB | ||
|
|
||
| pub type Target<'a> = &'a mut (FetchTaskTarget + Send); | ||
|
|
||
| pub enum Data { | ||
| @@ -486,8 +488,20 @@ fn scheme_fetch(request: &mut Request, | ||
| Ok(file_path) => { | ||
| match File::open(file_path.clone()) { | ||
| Ok(mut file) => { | ||
| let mut bytes = vec![]; | ||
| let _ = file.read_to_end(&mut bytes); | ||
| let mut reader = BufReader::with_capacity(FILE_CHUNK_SIZE, file); | ||
| let mut bytes = Vec::new(); | ||
| loop { | ||
| let length = { | ||
| let mut buffer = reader.fill_buf().unwrap().to_vec(); | ||
| let buffer_len = buffer.len(); | ||
| bytes.append(&mut buffer); | ||
| target.process_response_chunk(buffer); | ||
InquisitivePenguin
Author
Contributor
|
||
| buffer_len | ||
| }; | ||
| if length == 0 { break; } | ||
| reader.consume(length); | ||
| } | ||
|
|
||
| let mime = guess_mime_type(file_path); | ||
|
|
||
| let mut response = Response::new(url); | ||
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
We actually have a system in place that requires less manual work, so let's make use of it.
wait_for_responsehandles both synchronous and asynchronous responses; we can make this one asynchronous by:done_chan(servo/components/net/http_loader.rs
Lines 1091 to 1093 in f04c965
servo/components/net/http_loader.rs
Lines 1127 to 1151 in f04c965
To ensure everything still works, try loading a page from your local machine (not from a web server) that loads an image >32kb using a relative URL.