Skip to content

URL: reimplement URL.createObjectURL()#1150

Open
duonglaiquang wants to merge 2 commits into
HtmlUnit:masterfrom
duonglaiquang:duong_url
Open

URL: reimplement URL.createObjectURL()#1150
duonglaiquang wants to merge 2 commits into
HtmlUnit:masterfrom
duonglaiquang:duong_url

Conversation

@duonglaiquang

@duonglaiquang duonglaiquang commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

This PR does the following

  • Make XMLHttpRequest check blob url's origin properly instead of throwing No permitted "Access-Control-Allow-Origin" header everytime

  • Rework URL.createObjectURL() / URL.revokeObjectURL() to back blob URLs with a user-agent-wide blob URL store on WebClient, replacing the previous per-Document store.
    This matches W3C specification where a blob: URL minted in one document is resolvable from any same-client browsing context.

*/
@JsxStaticFunction
public static String createObjectURL(final Object fileOrBlob) {
if (fileOrBlob instanceof File file) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe i'm blind but looks like the file support is no longer there?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh the support is still there. The previous implementation doing both check was redundant because File itself is a Blob

*
* @author Lai Quang Duong
*/
public class BlobUrlStore {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great to have a separate class for this.

Maybe we should add a clear() method to support testing use cases where we like to remove urls to test error cases. I guess the page for removal is not always at hand.

final Blob fileOrBlob = blobUrlStore_.resolve(webRequest.getUrl().toString());
if (fileOrBlob == null) {
throw JavaScriptEngine.typeError("Cannot load data from " + webRequest.getUrl());
throw new FileNotFoundException(webRequest.getUrl().toString());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i like to have at lease a small hint in the message text that the file is not found in the BlobUrlStore.

*
* @author Lai Quang Duong
*/
public class URL2Test extends SimpleWebTestCase {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

any chance to do this as webdriver tests?

@rbri

rbri commented Jul 9, 2026

Copy link
Copy Markdown
Member

And also thanks for that and all the work you already put into this.
Maybe you can also add your author signature to the files you touched

@sonarqubecloud

sonarqubecloud Bot commented Jul 9, 2026

Copy link
Copy Markdown

@duonglaiquang duonglaiquang requested a review from rbri July 9, 2026 07:32
@duonglaiquang

duonglaiquang commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

@rbri i have updated the tests and add a little fix for XMLHttpRequest when working with blob url

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants