| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | import utils from '../utils.js';
|
|---|
| 4 | import parseHeaders from '../helpers/parseHeaders.js';
|
|---|
| 5 | import { sanitizeHeaderValue } from '../helpers/sanitizeHeaderValue.js';
|
|---|
| 6 |
|
|---|
| 7 | const $internals = Symbol('internals');
|
|---|
| 8 |
|
|---|
| 9 | function normalizeHeader(header) {
|
|---|
| 10 | return header && String(header).trim().toLowerCase();
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | function normalizeValue(value) {
|
|---|
| 14 | if (value === false || value == null) {
|
|---|
| 15 | return value;
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | return utils.isArray(value) ? value.map(normalizeValue) : sanitizeHeaderValue(String(value));
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | function parseTokens(str) {
|
|---|
| 22 | const tokens = Object.create(null);
|
|---|
| 23 | const tokensRE = /([^\s,;=]+)\s*(?:=\s*([^,;]+))?/g;
|
|---|
| 24 | let match;
|
|---|
| 25 |
|
|---|
| 26 | while ((match = tokensRE.exec(str))) {
|
|---|
| 27 | tokens[match[1]] = match[2];
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | return tokens;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | const isValidHeaderName = (str) => /^[-_a-zA-Z0-9^`|~,!#$%&'*+.]+$/.test(str.trim());
|
|---|
| 34 |
|
|---|
| 35 | function matchHeaderValue(context, value, header, filter, isHeaderNameFilter) {
|
|---|
| 36 | if (utils.isFunction(filter)) {
|
|---|
| 37 | return filter.call(this, value, header);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | if (isHeaderNameFilter) {
|
|---|
| 41 | value = header;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | if (!utils.isString(value)) return;
|
|---|
| 45 |
|
|---|
| 46 | if (utils.isString(filter)) {
|
|---|
| 47 | return value.indexOf(filter) !== -1;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | if (utils.isRegExp(filter)) {
|
|---|
| 51 | return filter.test(value);
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | function formatHeader(header) {
|
|---|
| 56 | return header
|
|---|
| 57 | .trim()
|
|---|
| 58 | .toLowerCase()
|
|---|
| 59 | .replace(/([a-z\d])(\w*)/g, (w, char, str) => {
|
|---|
| 60 | return char.toUpperCase() + str;
|
|---|
| 61 | });
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | function buildAccessors(obj, header) {
|
|---|
| 65 | const accessorName = utils.toCamelCase(' ' + header);
|
|---|
| 66 |
|
|---|
| 67 | ['get', 'set', 'has'].forEach((methodName) => {
|
|---|
| 68 | Object.defineProperty(obj, methodName + accessorName, {
|
|---|
| 69 | // Null-proto descriptor so a polluted Object.prototype.get cannot turn
|
|---|
| 70 | // this data descriptor into an accessor descriptor on the way in.
|
|---|
| 71 | __proto__: null,
|
|---|
| 72 | value: function (arg1, arg2, arg3) {
|
|---|
| 73 | return this[methodName].call(this, header, arg1, arg2, arg3);
|
|---|
| 74 | },
|
|---|
| 75 | configurable: true,
|
|---|
| 76 | });
|
|---|
| 77 | });
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | class AxiosHeaders {
|
|---|
| 81 | constructor(headers) {
|
|---|
| 82 | headers && this.set(headers);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | set(header, valueOrRewrite, rewrite) {
|
|---|
| 86 | const self = this;
|
|---|
| 87 |
|
|---|
| 88 | function setHeader(_value, _header, _rewrite) {
|
|---|
| 89 | const lHeader = normalizeHeader(_header);
|
|---|
| 90 |
|
|---|
| 91 | if (!lHeader) {
|
|---|
| 92 | throw new Error('header name must be a non-empty string');
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | const key = utils.findKey(self, lHeader);
|
|---|
| 96 |
|
|---|
| 97 | if (
|
|---|
| 98 | !key ||
|
|---|
| 99 | self[key] === undefined ||
|
|---|
| 100 | _rewrite === true ||
|
|---|
| 101 | (_rewrite === undefined && self[key] !== false)
|
|---|
| 102 | ) {
|
|---|
| 103 | self[key || _header] = normalizeValue(_value);
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | const setHeaders = (headers, _rewrite) =>
|
|---|
| 108 | utils.forEach(headers, (_value, _header) => setHeader(_value, _header, _rewrite));
|
|---|
| 109 |
|
|---|
| 110 | if (utils.isPlainObject(header) || header instanceof this.constructor) {
|
|---|
| 111 | setHeaders(header, valueOrRewrite);
|
|---|
| 112 | } else if (utils.isString(header) && (header = header.trim()) && !isValidHeaderName(header)) {
|
|---|
| 113 | setHeaders(parseHeaders(header), valueOrRewrite);
|
|---|
| 114 | } else if (utils.isObject(header) && utils.isIterable(header)) {
|
|---|
| 115 | let obj = {},
|
|---|
| 116 | dest,
|
|---|
| 117 | key;
|
|---|
| 118 | for (const entry of header) {
|
|---|
| 119 | if (!utils.isArray(entry)) {
|
|---|
| 120 | throw TypeError('Object iterator must return a key-value pair');
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | obj[(key = entry[0])] = (dest = obj[key])
|
|---|
| 124 | ? utils.isArray(dest)
|
|---|
| 125 | ? [...dest, entry[1]]
|
|---|
| 126 | : [dest, entry[1]]
|
|---|
| 127 | : entry[1];
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | setHeaders(obj, valueOrRewrite);
|
|---|
| 131 | } else {
|
|---|
| 132 | header != null && setHeader(valueOrRewrite, header, rewrite);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | return this;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | get(header, parser) {
|
|---|
| 139 | header = normalizeHeader(header);
|
|---|
| 140 |
|
|---|
| 141 | if (header) {
|
|---|
| 142 | const key = utils.findKey(this, header);
|
|---|
| 143 |
|
|---|
| 144 | if (key) {
|
|---|
| 145 | const value = this[key];
|
|---|
| 146 |
|
|---|
| 147 | if (!parser) {
|
|---|
| 148 | return value;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | if (parser === true) {
|
|---|
| 152 | return parseTokens(value);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | if (utils.isFunction(parser)) {
|
|---|
| 156 | return parser.call(this, value, key);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | if (utils.isRegExp(parser)) {
|
|---|
| 160 | return parser.exec(value);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | throw new TypeError('parser must be boolean|regexp|function');
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | has(header, matcher) {
|
|---|
| 169 | header = normalizeHeader(header);
|
|---|
| 170 |
|
|---|
| 171 | if (header) {
|
|---|
| 172 | const key = utils.findKey(this, header);
|
|---|
| 173 |
|
|---|
| 174 | return !!(
|
|---|
| 175 | key &&
|
|---|
| 176 | this[key] !== undefined &&
|
|---|
| 177 | (!matcher || matchHeaderValue(this, this[key], key, matcher))
|
|---|
| 178 | );
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | return false;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | delete(header, matcher) {
|
|---|
| 185 | const self = this;
|
|---|
| 186 | let deleted = false;
|
|---|
| 187 |
|
|---|
| 188 | function deleteHeader(_header) {
|
|---|
| 189 | _header = normalizeHeader(_header);
|
|---|
| 190 |
|
|---|
| 191 | if (_header) {
|
|---|
| 192 | const key = utils.findKey(self, _header);
|
|---|
| 193 |
|
|---|
| 194 | if (key && (!matcher || matchHeaderValue(self, self[key], key, matcher))) {
|
|---|
| 195 | delete self[key];
|
|---|
| 196 |
|
|---|
| 197 | deleted = true;
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | if (utils.isArray(header)) {
|
|---|
| 203 | header.forEach(deleteHeader);
|
|---|
| 204 | } else {
|
|---|
| 205 | deleteHeader(header);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | return deleted;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | clear(matcher) {
|
|---|
| 212 | const keys = Object.keys(this);
|
|---|
| 213 | let i = keys.length;
|
|---|
| 214 | let deleted = false;
|
|---|
| 215 |
|
|---|
| 216 | while (i--) {
|
|---|
| 217 | const key = keys[i];
|
|---|
| 218 | if (!matcher || matchHeaderValue(this, this[key], key, matcher, true)) {
|
|---|
| 219 | delete this[key];
|
|---|
| 220 | deleted = true;
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | return deleted;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | normalize(format) {
|
|---|
| 228 | const self = this;
|
|---|
| 229 | const headers = {};
|
|---|
| 230 |
|
|---|
| 231 | utils.forEach(this, (value, header) => {
|
|---|
| 232 | const key = utils.findKey(headers, header);
|
|---|
| 233 |
|
|---|
| 234 | if (key) {
|
|---|
| 235 | self[key] = normalizeValue(value);
|
|---|
| 236 | delete self[header];
|
|---|
| 237 | return;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | const normalized = format ? formatHeader(header) : String(header).trim();
|
|---|
| 241 |
|
|---|
| 242 | if (normalized !== header) {
|
|---|
| 243 | delete self[header];
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | self[normalized] = normalizeValue(value);
|
|---|
| 247 |
|
|---|
| 248 | headers[normalized] = true;
|
|---|
| 249 | });
|
|---|
| 250 |
|
|---|
| 251 | return this;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | concat(...targets) {
|
|---|
| 255 | return this.constructor.concat(this, ...targets);
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | toJSON(asStrings) {
|
|---|
| 259 | const obj = Object.create(null);
|
|---|
| 260 |
|
|---|
| 261 | utils.forEach(this, (value, header) => {
|
|---|
| 262 | value != null &&
|
|---|
| 263 | value !== false &&
|
|---|
| 264 | (obj[header] = asStrings && utils.isArray(value) ? value.join(', ') : value);
|
|---|
| 265 | });
|
|---|
| 266 |
|
|---|
| 267 | return obj;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | [Symbol.iterator]() {
|
|---|
| 271 | return Object.entries(this.toJSON())[Symbol.iterator]();
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | toString() {
|
|---|
| 275 | return Object.entries(this.toJSON())
|
|---|
| 276 | .map(([header, value]) => header + ': ' + value)
|
|---|
| 277 | .join('\n');
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | getSetCookie() {
|
|---|
| 281 | return this.get('set-cookie') || [];
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | get [Symbol.toStringTag]() {
|
|---|
| 285 | return 'AxiosHeaders';
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | static from(thing) {
|
|---|
| 289 | return thing instanceof this ? thing : new this(thing);
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | static concat(first, ...targets) {
|
|---|
| 293 | const computed = new this(first);
|
|---|
| 294 |
|
|---|
| 295 | targets.forEach((target) => computed.set(target));
|
|---|
| 296 |
|
|---|
| 297 | return computed;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | static accessor(header) {
|
|---|
| 301 | const internals =
|
|---|
| 302 | (this[$internals] =
|
|---|
| 303 | this[$internals] =
|
|---|
| 304 | {
|
|---|
| 305 | accessors: {},
|
|---|
| 306 | });
|
|---|
| 307 |
|
|---|
| 308 | const accessors = internals.accessors;
|
|---|
| 309 | const prototype = this.prototype;
|
|---|
| 310 |
|
|---|
| 311 | function defineAccessor(_header) {
|
|---|
| 312 | const lHeader = normalizeHeader(_header);
|
|---|
| 313 |
|
|---|
| 314 | if (!accessors[lHeader]) {
|
|---|
| 315 | buildAccessors(prototype, _header);
|
|---|
| 316 | accessors[lHeader] = true;
|
|---|
| 317 | }
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | utils.isArray(header) ? header.forEach(defineAccessor) : defineAccessor(header);
|
|---|
| 321 |
|
|---|
| 322 | return this;
|
|---|
| 323 | }
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | AxiosHeaders.accessor([
|
|---|
| 327 | 'Content-Type',
|
|---|
| 328 | 'Content-Length',
|
|---|
| 329 | 'Accept',
|
|---|
| 330 | 'Accept-Encoding',
|
|---|
| 331 | 'User-Agent',
|
|---|
| 332 | 'Authorization',
|
|---|
| 333 | ]);
|
|---|
| 334 |
|
|---|
| 335 | // reserved names hotfix
|
|---|
| 336 | utils.reduceDescriptors(AxiosHeaders.prototype, ({ value }, key) => {
|
|---|
| 337 | let mapped = key[0].toUpperCase() + key.slice(1); // map `set` => `Set`
|
|---|
| 338 | return {
|
|---|
| 339 | get: () => value,
|
|---|
| 340 | set(headerValue) {
|
|---|
| 341 | this[mapped] = headerValue;
|
|---|
| 342 | },
|
|---|
| 343 | };
|
|---|
| 344 | });
|
|---|
| 345 |
|
|---|
| 346 | utils.freezeMethods(AxiosHeaders);
|
|---|
| 347 |
|
|---|
| 348 | export default AxiosHeaders;
|
|---|