forked from popomore/koa-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (125 loc) · 3.98 KB
/
index.js
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
'use strict';
var join = require('url').resolve;
var iconv = require('iconv-lite');
var coRequest = require('co-request');
module.exports = function(options) {
options || (options = {});
var request = coRequest.defaults({ jar: options.jar === true });
if (!(options.host || options.map || options.url)) {
throw new Error('miss options');
}
return function* proxy(next) {
var url = resolve(this.path, options);
if(typeof options.suppressRequestHeaders === 'object'){
options.suppressRequestHeaders.forEach(function(h, i){
options.suppressRequestHeaders[i] = h.toLowerCase();
});
}
var suppressResponseHeaders = []; // We should not be overwriting the options object!
if(typeof options.suppressResponseHeaders === 'object'){
options.suppressResponseHeaders.forEach(function(h, i){
suppressResponseHeaders.push(h.toLowerCase());
});
}
// don't match
if (!url) {
return yield* next;
}
// if match option supplied, restrict proxy to that match
if (options.match) {
if (!this.path.match(options.match)) {
return yield* next;
}
}
var parsedBody = getParsedBody(this);
var opt = {
url: url + (this.querystring ? '?' + this.querystring : ''),
headers: this.header,
encoding: null,
followRedirect: options.followRedirect === false ? false : true,
method: this.method,
body: parsedBody,
};
// set 'Host' header to options.host (without protocol prefix), strip trailing slash
if (options.host) opt.headers.host = options.host.slice(options.host.indexOf('://')+3).replace(/\/$/,'');
if (options.requestOptions) {
if (typeof options.requestOptions === 'function') {
opt = options.requestOptions(this.request, opt);
} else {
Object.keys(options.requestOptions).forEach(function (option) { opt[option] = options.requestOptions[option]; });
}
}
for(name in opt.headers){
if(options.suppressRequestHeaders && options.suppressRequestHeaders.indexOf(name.toLowerCase()) >= 0){
delete opt.headers[name];
}
}
var requestThunk = request(opt);
if (parsedBody) {
var res = yield requestThunk;
} else {
// Is there a better way?
// https://github.com/leukhin/co-request/issues/11
var res = yield pipeRequest(this.req, requestThunk);
}
this.status = res.statusCode;
for (var name in res.headers) {
// http://stackoverflow.com/questions/35525715/http-get-parse-error-code-hpe-unexpected-content-length
if(suppressResponseHeaders.indexOf(name.toLowerCase())>=0){
continue;
}
if (name === 'transfer-encoding') {
continue;
}
this.set(name, res.headers[name]);
}
if (options.encoding === 'gbk') {
this.body = iconv.decode(res.body, 'gbk');
return;
}
this.body = res.body;
if (options.yieldNext) {
yield next;
}
};
};
function resolve(path, options) {
var url = options.url;
if (url) {
if (!/^http/.test(url)) {
url = options.host ? join(options.host, url) : null;
}
return ignoreQuery(url);
}
if (typeof options.map === 'object') {
if (options.map && options.map[path]) {
path = ignoreQuery(options.map[path]);
}
} else if (typeof options.map === 'function') {
path = options.map(path);
}
return options.host ? join(options.host, path) : null;
}
function ignoreQuery(url) {
return url ? url.split('?')[0] : null;
}
function getParsedBody(ctx){
var body = ctx.request.body;
if (body === undefined || body === null){
return undefined;
}
var contentType = ctx.request.header['content-type'];
if (!Buffer.isBuffer(body) && typeof body !== 'string'){
if (contentType && contentType.indexOf('json') !== -1){
body = JSON.stringify(body);
} else {
body = body + '';
}
}
return body;
}
function pipeRequest(readable, requestThunk){
return function(cb){
readable.pipe(requestThunk(cb));
}
}