diff options
Diffstat (limited to 'src/lib/NetCSP.js')
-rw-r--r-- | src/lib/NetCSP.js | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/lib/NetCSP.js b/src/lib/NetCSP.js new file mode 100644 index 0000000..cb79a80 --- /dev/null +++ b/src/lib/NetCSP.js @@ -0,0 +1,30 @@ +"use strict"; + +class NetCSP extends CSP { + constructor(start, end) { + super(); + this.start = start; + this.end = end; + } + + isMine(header) { + let {name, value} = header; + if (name.toLowerCase() !== CSP.headerName) return false; + let startIdx = value.indexOf(this.start); + return startIdx > -1 && startIdx < value.lastIndexOf(this.end); + } + + inject(headerValue, mine) { + let startIdx = headerValue.indexOf(this.start); + if (startIdx < 0) return `${headerValue};${mine}`; + let endIdx = headerValue.lastIndexOf(this.end); + let retValue = `${headerValue.substring(0, startIdx)}${mine}`; + + return endIdx < 0 ? retValue : `${retValue}${headerValue.substring(endIdx + this.end.length + 1)}`; + } + + build(...directives) { + return `${this.start}${super.build(...directives)}${this.end}`; + } + +} |