Skip to content

Commit df5f380

Browse files
authored
Merge pull request #43 from webpack/bugfix/no-fragment
add support for escaping in requests
2 parents fbbfb66 + a32c111 commit df5f380

File tree

4 files changed

+1961
-5
lines changed

4 files changed

+1961
-5
lines changed

lib/LoaderRunner.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function utf8BufferToString(buf) {
1515
}
1616
}
1717

18-
var PATH_QUERY_FRAGMENT_REGEXP = /^([^?#]*)(\?[^#]*)?(#.*)?$/;
18+
const PATH_QUERY_FRAGMENT_REGEXP = /^((?:\0.|[^?#\0])*)(\?(?:\0.|[^#\0])*)?(#.*)?$/;
1919

2020
/**
2121
* @param {string} str the path with query and fragment
@@ -24,8 +24,8 @@ var PATH_QUERY_FRAGMENT_REGEXP = /^([^?#]*)(\?[^#]*)?(#.*)?$/;
2424
function parsePathQueryFragment(str) {
2525
var match = PATH_QUERY_FRAGMENT_REGEXP.exec(str);
2626
return {
27-
path: match[1],
28-
query: match[2] || "",
27+
path: match[1].replace(/\0(.)/g, "$1"),
28+
query: match[2] ? match[2].replace(/\0(.)/g, "$1") : "",
2929
fragment: match[3] || ""
3030
};
3131
}
@@ -60,7 +60,7 @@ function createLoaderObject(loader) {
6060
Object.defineProperty(obj, "request", {
6161
enumerable: true,
6262
get: function() {
63-
return obj.path + obj.query;
63+
return obj.path.replace(/#/g, "\0#") + obj.query.replace(/#/g, "\0#") + obj.fragment;
6464
},
6565
set: function(value) {
6666
if(typeof value === "string") {
@@ -326,7 +326,7 @@ exports.runLoaders = function runLoaders(options, callback) {
326326
get: function() {
327327
if(loaderContext.resourcePath === undefined)
328328
return undefined;
329-
return loaderContext.resourcePath + loaderContext.resourceQuery + loaderContext.resourceFragment;
329+
return loaderContext.resourcePath.replace(/#/g, "\0#") + loaderContext.resourceQuery.replace(/#/g, "\0#") + loaderContext.resourceFragment;
330330
},
331331
set: function(value) {
332332
var splittedResource = value && parsePathQueryFragment(value);

test/fixtures/res#ource.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
resource

test/runLoaders.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,16 +629,79 @@ describe("runLoaders", function() {
629629
});
630630
});
631631
}
632+
it("should support escaping in resource", function(done) {
633+
runLoaders({
634+
resource: path.resolve(fixtures, "res\0#ource.bin")
635+
}, function(err, result) {
636+
if(err) return done(err);
637+
result.result.should.be.eql([Buffer.from("resource", "utf-8")]);
638+
result.cacheable.should.be.eql(true);
639+
result.fileDependencies.should.be.eql([
640+
path.resolve(fixtures, "res#ource.bin")
641+
]);
642+
result.contextDependencies.should.be.eql([]);
643+
done();
644+
});
645+
});
646+
it("should have to correct keys in context when using escaping", function(done) {
647+
runLoaders({
648+
resource: path.resolve(fixtures, "res\0#ource.bin") + "?query\0#frag",
649+
loaders: [
650+
path.resolve(fixtures, "keys-loader.js") + "?loader\0#query"
651+
]
652+
}, function(err, result) {
653+
if(err) return done(err);
654+
try {
655+
JSON.parse(result.result[0]).should.be.eql({
656+
context: fixtures,
657+
resource: path.resolve(fixtures, "res\0#ource.bin") + "?query\0#frag",
658+
resourcePath: path.resolve(fixtures, "res#ource.bin"),
659+
resourceQuery: "?query#frag",
660+
resourceFragment: "",
661+
loaderIndex: 0,
662+
query: "?loader#query",
663+
currentRequest: path.resolve(fixtures, "keys-loader.js") + "?loader\0#query!" +
664+
path.resolve(fixtures, "res\0#ource.bin") + "?query\0#frag",
665+
remainingRequest: path.resolve(fixtures, "res\0#ource.bin") + "?query\0#frag",
666+
previousRequest: "",
667+
request: path.resolve(fixtures, "keys-loader.js") + "?loader\0#query!" +
668+
path.resolve(fixtures, "res\0#ource.bin") + "?query\0#frag",
669+
data: null,
670+
loaders: [{
671+
request: path.resolve(fixtures, "keys-loader.js") + "?loader\0#query",
672+
path: path.resolve(fixtures, "keys-loader.js"),
673+
query: "?loader#query",
674+
fragment: "",
675+
data: null,
676+
pitchExecuted: true,
677+
normalExecuted: true
678+
}]
679+
});
680+
} catch(e) {
681+
return done(e);
682+
}
683+
done();
684+
});
685+
});
686+
632687
describe("getContext", function() {
633688
var TESTS = [
634689
["/", "/"],
635690
["/path/file.js", "/path"],
691+
["/path/file.js#fragment", "/path"],
692+
["/path/file.js?query", "/path"],
693+
["/path/file.js?query#fragment", "/path"],
694+
["/path/\0#/file.js", "/path/#"],
636695
["/some/longer/path/file.js", "/some/longer/path"],
637696
["/file.js", "/"],
638697
["C:\\", "C:\\"],
639698
["C:\\file.js", "C:\\"],
640699
["C:\\some\\path\\file.js", "C:\\some\\path"],
641700
["C:\\path\\file.js", "C:\\path"],
701+
["C:\\path\\file.js#fragment", "C:\\path"],
702+
["C:\\path\\file.js?query", "C:\\path"],
703+
["C:\\path\\file.js?query#fragment", "C:\\path"],
704+
["C:\\path\\\0#\\file.js", "C:\\path\\#"],
642705
];
643706
TESTS.forEach(function(testCase) {
644707
it("should get the context of '" + testCase[0] + "'", function() {

0 commit comments

Comments
 (0)