-
Notifications
You must be signed in to change notification settings - Fork 32
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
Protocol Handler: make it possible to set contentType based on content as it arrives #56
Comments
Inline is quote from the MDN regarding how
It is also worth pointing out that currently implementation based on So it appears to me that we should set The reason I'm saying this above is that it could be that turning a handler to an async function may not necessarily map well with how |
My current plan is to make change API as follows (@lidel I think that is what you meant instead of what you wrote in the description): // Sync handler
browser.protocol.registerProtocol("dweb", request => {
return {
contentEncoding: "utf-8"
contentType: "text/html",
content: asyncResponseStreamWithContentType(request.url)
}
})
// Async handler
browser.protocol.registerProtocol("dweb", async request => {
const response = await responseStream(request.url)
const {contentType, contentEncoding} = await contentMetadata(response)
return {
contentType,
contentEncoding,
content: response
}
}) I might have to revisit this decision after I get more insight from networking team on how and when |
This allows protocol implementers to sniff content for contentType detection without blocking. fix mozilla#56
@Gozala yes, that is even better! (for some reason I assumed Got it working in a local branch and so far everything works as expected, thanks! |
Problem
Streaming protocols are unable to set
contentType
, because current Protocol API (@ 99a15b6) requirescontentType
to be provided before we start streaming response:This means we need to skip
contentType
parameter and are required to rely on mime-sniffing present in Firefox itself.This is far from ideal (svg breakage being a known problem: ipfs-inactive/faq#224 (comment)) and may be also insecure, as there will be use cases where specific array of bytes can render as different things, depending on assumed
contentType
.What we want is effectively ability to support
X-Content-Type-Options: nosniff
scenarios and provide own type without sniffing in browser.Solution
Protocol handler should be able to set (optional)
contentType
andcontentEncoding
based on content it read from internal source, something along this pseudocode:Basically, we all should be able to buffer a bit of data and extract contentType from it (be it from a static value in content header, or own heuristics for mime-type sniffing) before passing response to the browser.
@Gozala when we discussed this briefly last week, you mentioned it should be possible, but in case more context is needed below are details from IPFS side of things.
Additional Notes on IPFS Use Case
In case of IPFS we should be able to store media type within IPLD structures, as one of "Extended Attributes" in unixfs-v2 (ipld/legacy-unixfs-v2#11) or just mime-sniff it as soon data starts arriving inside of handler itself (with ability to account for edge cases such as SVG).
Below is a snippet with working stream-based mime-type sniffer ported from our Brave PoC:
This is as generic as it gets, I suspect other protocols could also find this ability to set
contentType
based on beginning of incoming data very useful.The text was updated successfully, but these errors were encountered: