1 | 'use strict';
|
---|
2 |
|
---|
3 | import utils from './../utils.js';
|
---|
4 |
|
---|
5 | // RawAxiosHeaders whose duplicates are ignored by node
|
---|
6 | // c.f. https://nodejs.org/api/http.html#http_message_headers
|
---|
7 | const ignoreDuplicateOf = utils.toObjectSet([
|
---|
8 | 'age', 'authorization', 'content-length', 'content-type', 'etag',
|
---|
9 | 'expires', 'from', 'host', 'if-modified-since', 'if-unmodified-since',
|
---|
10 | 'last-modified', 'location', 'max-forwards', 'proxy-authorization',
|
---|
11 | 'referer', 'retry-after', 'user-agent'
|
---|
12 | ]);
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Parse headers into an object
|
---|
16 | *
|
---|
17 | * ```
|
---|
18 | * Date: Wed, 27 Aug 2014 08:58:49 GMT
|
---|
19 | * Content-Type: application/json
|
---|
20 | * Connection: keep-alive
|
---|
21 | * Transfer-Encoding: chunked
|
---|
22 | * ```
|
---|
23 | *
|
---|
24 | * @param {String} rawHeaders Headers needing to be parsed
|
---|
25 | *
|
---|
26 | * @returns {Object} Headers parsed into an object
|
---|
27 | */
|
---|
28 | export default rawHeaders => {
|
---|
29 | const parsed = {};
|
---|
30 | let key;
|
---|
31 | let val;
|
---|
32 | let i;
|
---|
33 |
|
---|
34 | rawHeaders && rawHeaders.split('\n').forEach(function parser(line) {
|
---|
35 | i = line.indexOf(':');
|
---|
36 | key = line.substring(0, i).trim().toLowerCase();
|
---|
37 | val = line.substring(i + 1).trim();
|
---|
38 |
|
---|
39 | if (!key || (parsed[key] && ignoreDuplicateOf[key])) {
|
---|
40 | return;
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (key === 'set-cookie') {
|
---|
44 | if (parsed[key]) {
|
---|
45 | parsed[key].push(val);
|
---|
46 | } else {
|
---|
47 | parsed[key] = [val];
|
---|
48 | }
|
---|
49 | } else {
|
---|
50 | parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
|
---|
51 | }
|
---|
52 | });
|
---|
53 |
|
---|
54 | return parsed;
|
---|
55 | };
|
---|