mirror of
https://github.com/bitinflow/localtunnel.git
synced 2026-03-13 13:35:54 +00:00
refactor into files
This commit is contained in:
39
lib/HeaderHostTransformer.js
Normal file
39
lib/HeaderHostTransformer.js
Normal file
@@ -0,0 +1,39 @@
|
||||
var stream = require('stream');
|
||||
var util = require('util');
|
||||
|
||||
var Transform = stream.Transform;
|
||||
|
||||
var HeaderHostTransformer = function(opts) {
|
||||
if (!(this instanceof HeaderHostTransformer)) {
|
||||
return new HeaderHostTransformer(opts);
|
||||
}
|
||||
|
||||
opts = opts || {}
|
||||
Transform.call(this, opts);
|
||||
|
||||
var self = this;
|
||||
self.host = opts.host || 'localhost';
|
||||
self.replaced = false;
|
||||
}
|
||||
util.inherits(HeaderHostTransformer, Transform);
|
||||
|
||||
HeaderHostTransformer.prototype._transform = function (chunk, enc, cb) {
|
||||
var self = this;
|
||||
chunk = chunk.toString();
|
||||
|
||||
// after replacing the first instance of the Host header
|
||||
// we just become a regular passthrough
|
||||
if (!self.replaced) {
|
||||
self.push(chunk.replace(/(\r\nHost: )\S+/, function(match, $1) {
|
||||
self.replaced = true;
|
||||
return $1 + self.host;
|
||||
}));
|
||||
}
|
||||
else {
|
||||
self.push(chunk);
|
||||
}
|
||||
|
||||
cb();
|
||||
};
|
||||
|
||||
module.exports = HeaderHostTransformer;
|
||||
Reference in New Issue
Block a user