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

Conversation

@csadilek
Copy link
Contributor

With this we can fetch from data URIs and downloads "just work" (we don't require any special handling). So, e.g. this works now (which is also used by uBlock to save backups):

<a href="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" download="backup.txt">Backup</a>

https://github.com/gorhill/uBlock/blob/08d370d32e648c2823e1bc31fcd9f7e1f96d155a/platform/chromium/vapi-common.js#L172

GV didn't want to add API for it so I took a stab at adding this to our fetch libs: #6314 (comment)

@pocmo can you take a look since we looked at download stuff today :). I've time-boxed this and limited to base64 encoded URIs. The device and unit tests pass and the download works / can be tested here:

https://jsfiddle.net/nue9rg0L/2/

@csadilek csadilek requested a review from pocmo March 31, 2020 21:17
@codecov
Copy link

codecov bot commented Mar 31, 2020

Codecov Report

Merging #6475 into master will decrease coverage by 0.13%.
The diff coverage is 20%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #6475      +/-   ##
============================================
- Coverage     78.52%   78.38%   -0.14%     
+ Complexity     4687     4678       -9     
============================================
  Files           621      615       -6     
  Lines         22610    22519      -91     
  Branches       3309     3297      -12     
============================================
- Hits          17754    17652     -102     
- Misses         3496     3512      +16     
+ Partials       1360     1355       -5
Impacted Files Coverage Δ Complexity Δ
.../java/mozilla/components/concept/fetch/Response.kt 92.85% <ø> (ø) 7 <0> (ø) ⬇️
...n/java/mozilla/components/concept/fetch/Request.kt 95.65% <0%> (-4.35%) 10 <0> (ø)
...in/java/mozilla/components/concept/fetch/Client.kt 26.92% <0%> (-73.08%) 2 <0> (ø)
...fetch/httpurlconnection/HttpURLConnectionClient.kt 85.89% <100%> (+0.37%) 3 <2> (+1) ⬆️
...ozilla/components/lib/fetch/okhttp/OkHttpClient.kt 83.63% <100%> (+0.61%) 4 <3> (+1) ⬆️
.../feature/downloads/manager/FetchDownloadManager.kt 80% <100%> (ø) 10 <2> (ø) ⬇️
...components/support/sync/telemetry/SyncTelemetry.kt 91% <0%> (-0.09%) 34% <0%> (ø)
...onents/support/sync/telemetry/BaseGleanSyncPing.kt 100% <0%> (ø) 11% <0%> (ø) ⬇️
...ozilla/components/lib/state/ext/StoreExtensions.kt
...rc/main/java/mozilla/components/lib/state/Store.kt
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ca13af0...facd604. Read the comment docs.

@Amejia481 Amejia481 self-requested a review April 1, 2020 15:53
@Amejia481 Amejia481 self-assigned this Apr 1, 2020
Copy link
Contributor

@Amejia481 Amejia481 left a comment

Choose a reason for hiding this comment

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

* @return The generated [Response] including the decoded bytes as body.
*/
@Suppress("TooGenericExceptionCaught")
fun fetchDataUri(request: Request): Response {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we put this into Client and make it protected (like defaultHeaders) so that it is not used outside of a Client implementation? This could now end up getting called by app code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, makes sense. Updated!

@Test
override fun getDataUri() {
// We can't run this test case because Base64 encoding is not available but we can verify
// that we attempt to fetch the data URI.
Copy link
Contributor

Choose a reason for hiding this comment

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

If we run this with AndroidJUnit4 then we should be able to use the Base64 classes, I think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, good catch! I've upgraded the lib and removed the test override as the base test is running fine now!

val headers = MutableHeaders(
CONTENT_TYPE_HEADER to contentType,
CONTENT_LENGTH_HEADER to bytes.size.toString()
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at RFC 2397 and MDN I wonder if we should be a bit more protective regarding optional elements:

  • If the data uri doesn't specify base64 encoding then bail out early?
  • If the content type is not specified then do not add header for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, added another test case and made this more robust too.

@csadilek
Copy link
Contributor Author

csadilek commented Apr 2, 2020

bors r=Amejia481,pocmo

bors bot pushed a commit that referenced this pull request Apr 2, 2020
6475: Closes #6314: Support fetching / downloading data URIs r=Amejia481,pocmo a=csadilek

With this we can fetch from data URIs and downloads "just work" (we don't require any special handling). So, e.g. this works now (which is also used by uBlock to save backups):

```html
<a href="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" download="backup.txt">Backup</a>
```
https://github.com/gorhill/uBlock/blob/08d370d32e648c2823e1bc31fcd9f7e1f96d155a/platform/chromium/vapi-common.js#L172

GV didn't want to add API for it so I took a stab at adding this to our fetch libs: #6314 (comment)

@pocmo can you take a look since we looked at download stuff today :). I've time-boxed this and limited to base64 encoded URIs. The device and unit tests pass and the download works / can be tested here:

https://jsfiddle.net/nue9rg0L/2/

Co-authored-by: Christian Sadilek <christian.sadilek@gmail.com>
@bors
Copy link

bors bot commented Apr 2, 2020

Timed out

@csadilek
Copy link
Contributor Author

csadilek commented Apr 2, 2020

bors retry

@bors
Copy link

bors bot commented Apr 2, 2020

Build succeeded

@bors bors bot merged commit c24f710 into mozilla-mobile:master Apr 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants