1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
| 'use strict';
|
| exports.randomString = function randomString(length, charSet) {
| var result = [];
| length = length || 16;
| charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
| while (length--) {
| result.push(charSet[Math.floor(Math.random() * charSet.length)]);
| }
| return result.join('');
| };
|
| /**
| * split string to array
| * @param {String} str
| * @param {String} [sep] default is ','
| * @return {Array}
| */
| exports.split = function split(str, sep) {
| str = str || '';
| sep = sep || ',';
| var items = str.split(sep);
| var needs = [];
| for (var i = 0; i < items.length; i++) {
| var s = items[i].trim();
| if (s.length > 0) {
| needs.push(s);
| }
| }
| return needs;
| };
| // always optimized
| exports.splitAlwaysOptimized = function splitAlwaysOptimized() {
| var str = '';
| var sep = ',';
| if (arguments.length === 1) {
| str = arguments[0] || '';
| } else if (arguments.length === 2) {
| str = arguments[0] || '';
| sep = arguments[1] || ',';
| }
| var items = str.split(sep);
| var needs = [];
| for (var i = 0; i < items.length; i++) {
| var s = items[i].trim();
| if (s.length > 0) {
| needs.push(s);
| }
| }
| return needs;
| };
|
| /**
| * Replace string
| *
| * @param {String} str
| * @param {String|RegExp} substr
| * @param {String|Function} newSubstr
| * @return {String}
| */
| exports.replace = function replace(str, substr, newSubstr) {
| var replaceFunction = newSubstr;
| if (typeof replaceFunction !== 'function') {
| replaceFunction = function () {
| return newSubstr;
| };
| }
| return str.replace(substr, replaceFunction);
| };
|
| // original source https://github.com/nodejs/node/blob/v7.5.0/lib/_http_common.js#L300
| /**
| * True if val contains an invalid field-vchar
| * field-value = *( field-content / obs-fold )
| * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
| * field-vchar = VCHAR / obs-text
| *
| * checkInvalidHeaderChar() is currently designed to be inlinable by v8,
| * so take care when making changes to the implementation so that the source
| * code size does not exceed v8's default max_inlined_source_size setting.
| **/
| var validHdrChars = [
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, // 0 - 15
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 32 - 47
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 48 - 63
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 80 - 95
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 112 - 127
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 128 ...
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // ... 255
| ];
|
| /**
| * Replace invalid http header characters with replacement
| *
| * @param {String} val
| * @param {String|Function} replacement - can be `function(char)`
| * @return {Object}
| */
| exports.replaceInvalidHttpHeaderChar = function replaceInvalidHttpHeaderChar(val, replacement) {
| replacement = replacement || ' ';
| var invalid = false;
|
| if (!val || typeof val !== 'string') {
| return {
| val: val,
| invalid: invalid,
| };
| }
|
| var replacementType = typeof replacement;
| var chars;
| for (var i = 0; i < val.length; ++i) {
| if (!validHdrChars[val.charCodeAt(i)]) {
| // delay create chars
| chars = chars || val.split('');
| if (replacementType === 'function') {
| chars[i] = replacement(chars[i]);
| } else {
| chars[i] = replacement;
| }
| }
| }
|
| if (chars) {
| val = chars.join('');
| invalid = true;
| }
|
| return {
| val: val,
| invalid: invalid,
| };
| };
|
| /**
| * Detect invalid http header characters in a string
| *
| * @param {String} val
| * @return {Boolean}
| */
| exports.includesInvalidHttpHeaderChar = function includesInvalidHttpHeaderChar(val) {
| if (!val || typeof val !== 'string') {
| return false;
| }
|
| for (var i = 0; i < val.length; ++i) {
| if (!validHdrChars[val.charCodeAt(i)]) {
| return true;
| }
| }
|
| return false;
| };
|
|