Skip to content
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

Use nsIContentSniffer for content type detection #60

Merged
merged 1 commit into from
Aug 8, 2018
Merged
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
15 changes: 13 additions & 2 deletions flow-typed/npm/gecko_vx.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -2737,6 +2737,15 @@ declare module "gecko" {
exists(name: AString): boolean;
}

// https://github.com/mozilla/gecko-dev/blob/86897859913403b68829dbf9a154f5a87c4b0638/netwerk/base/nsIContentSniffer.idl
declare export interface nsIContentSniffer {
getMIMETypeFromContent(
nsIRequest,
Uint8Array | ArrayBuffer,
number
): ACString;
}

// -------------------------
declare export type JSM<url: string, jsm> = (url, {}) => jsm

Expand Down Expand Up @@ -3052,7 +3061,8 @@ declare module "gecko" {
nsIListNetworkAddressesListener: nsIJSCID<
nsIListNetworkAddressesListener
>,
nsIEnvironment: nsIJSCID<nsIEnvironment>
nsIEnvironment: nsIJSCID<nsIEnvironment>,
nsIContentSniffer: nsIJSCID<nsIContentSniffer>
},
classes: {
"@mozilla.org/nss_errors_service;1": nsIJSCID<nsINSSErrorsService>,
Expand Down Expand Up @@ -3119,7 +3129,8 @@ declare module "gecko" {
>,
"@mozilla.org/hash-property-bag;1": nsIJSCID<nsIWritablePropertyBag2>,
"@mozilla.org/network-info-service;1": nsIJSCID<nsINetworkInfoService>,
"@mozilla.org/process/environment;1": nsIJSCID<nsIEnvironment>
"@mozilla.org/process/environment;1": nsIJSCID<nsIEnvironment>,
"@mozilla.org/network/content-sniffer;1": nsIJSCID<nsIContentSniffer>
},
utils: {
Sandbox(
Expand Down
33 changes: 26 additions & 7 deletions src/protocol/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ const contentSecManager = Cc[
"@mozilla.org/contentsecuritymanager;1"
].getService(Ci.nsIContentSecurityManager)

const contentSniffer = Cc[
"@mozilla.org/network/content-sniffer;1"
].createInstance(Ci.nsIContentSniffer)

const isParent = appinfo.processType === appinfo.PROCESS_TYPE_DEFAULT
const { ID } = Components

Expand Down Expand Up @@ -110,10 +114,6 @@ const registerProtocol = ({ scheme, uuid }, handler) => {
)
}

const Channel$QueryInterface = XPCOMUtils.generateQI([
Ci.nsIChannel,
Ci.nsIRequest
])
const LOAD_NORMAL = 0

const IDLE = 0
Expand Down Expand Up @@ -184,6 +184,7 @@ class TransportSecurityInfo /*::implements nsITransportSecurityInfo*/ {
}

const MAX_UNKNOWN = 0xffffffffffffffff
const UNKNOWN_CONTENT_TYPE = "application/x-unknown-content-type"

class Channel /*::implements nsIChannel, nsIRequest*/ {
/*::
Expand All @@ -204,7 +205,6 @@ class Channel /*::implements nsIChannel, nsIRequest*/ {
name: string
status: nsresult
readyState: ReadyState
QueryInterface: typeof Channel$QueryInterface
contentDisposition: number
contentDispositionFilename: string
contentDispositionHeader: string
Expand All @@ -228,7 +228,7 @@ class Channel /*::implements nsIChannel, nsIRequest*/ {
this.originalURI = uri
this.contentCharset = "utf-8"
this.contentLength = -1
this.contentType = "application/x-unknown-content-type"
this.contentType = UNKNOWN_CONTENT_TYPE
this.contentDispositionFilename = ""
this.contentDispositionHeader = ""
this.byteOffset = 0
Expand All @@ -241,9 +241,20 @@ class Channel /*::implements nsIChannel, nsIRequest*/ {
this.name = uri.spec
this.status = Cr.NS_ERROR_NOT_INITIALIZED
this.readyState = IDLE
this.QueryInterface = Channel$QueryInterface
this.handler = handler
}
QueryInterface(iid) {
const isSupported =
false ||
iid.equals(Ci.nsISupports) ||
iid.equals(Ci.nsIChannel) ||
iid.equals(Ci.nsIRequest)
if (isSupported) {
return this
} else {
throw Cr.NS_ERROR_NO_INTERFACE
}
}
toJSON() {
return {
scheme: this.URI.scheme,
Expand Down Expand Up @@ -402,6 +413,14 @@ class Channel /*::implements nsIChannel, nsIRequest*/ {
const { byteLength } = content
stream.setData(content, 0, byteLength)

if (this.contentType === UNKNOWN_CONTENT_TYPE) {
this.contentType = contentSniffer.getMIMETypeFromContent(
this,
new Uint8Array(content),
byteLength
)
}

debug &&
console.log(
`body${pid} ${JSON.stringify(
Expand Down