@@ -6,8 +6,12 @@ var timeoutReq = require('timed-out');
6
6
7
7
var http = require ( 'http' ) ;
8
8
var https = require ( 'https' ) ;
9
+ var tunnel = require ( 'tunnel-agent' ) ;
9
10
10
- var agentOptions = { keepAlive : true , maxSockets : 100 } ;
11
+ var agentOptions = {
12
+ keepAlive : true ,
13
+ maxSockets : 100
14
+ } ;
11
15
var httpAgent = new http . Agent ( agentOptions ) ;
12
16
var httpsAgent = new https . Agent ( agentOptions ) ;
13
17
@@ -21,7 +25,8 @@ function HTTPTransport(options) {
21
25
this . agent = httpAgent ;
22
26
}
23
27
util . inherits ( HTTPTransport , Transport ) ;
24
- HTTPTransport . prototype . send = function ( client , message , headers , eventId , cb ) {
28
+ HTTPTransport . prototype . send = function ( client , message , headers , eventId , cb ) {
29
+
25
30
var options = {
26
31
hostname : client . dsn . host ,
27
32
path : client . dsn . path + 'api/' + client . dsn . project_id + '/store/' ,
@@ -31,22 +36,39 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
31
36
ca : client . ca ,
32
37
agent : this . agent
33
38
} ;
39
+
40
+ // set path apprpriately when using http endpoint + proxy, set proxy headers appropriately when using https endpoint + proxy
41
+ if ( this . options . hasOwnProperty ( 'proxyHost' ) ) {
42
+ if ( client . dsn . protocol === 'http' ) {
43
+ options . path = 'http://' + client . dsn . host + ':' + client . dsn . port + client . dsn . path + 'api/' + client . dsn . project_id + '/store/' ;
44
+ delete options . hostname ; // only 'host' should be set when using proxy
45
+ } else {
46
+ this . options . agent . proxyOptions . headers = {
47
+ 'Content-Type' : 'application/octet-stream' ,
48
+ host : client . dsn . host + ':' + client . dsn . port
49
+ }
50
+ }
51
+ }
52
+
34
53
for ( var key in this . options ) {
35
54
if ( this . options . hasOwnProperty ( key ) ) {
36
55
options [ key ] = this . options [ key ] ;
37
56
}
38
57
}
39
58
40
59
// prevent off heap memory explosion
41
- var _name = this . agent . getName ( { host : client . dsn . host , port : client . dsn . port } ) ;
60
+ var _name = this . agent . getName ( {
61
+ host : client . dsn . host ,
62
+ port : client . dsn . port
63
+ } ) ;
42
64
var _requests = this . agent . requests [ _name ] ;
43
65
if ( _requests && Object . keys ( _requests ) . length > client . maxReqQueueCount ) {
44
66
// other feedback strategy
45
67
client . emit ( 'error' , new Error ( 'client req queue is full..' ) ) ;
46
68
return ;
47
69
}
48
70
49
- var req = this . transport . request ( options , function ( res ) {
71
+ var req = this . transport . request ( options , function ( res ) {
50
72
res . setEncoding ( 'utf8' ) ;
51
73
if ( res . statusCode >= 200 && res . statusCode < 300 ) {
52
74
client . emit ( 'logged' , eventId ) ;
@@ -63,17 +85,16 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
63
85
client . emit ( 'error' , e ) ;
64
86
cb && cb ( e ) ;
65
87
}
66
-
67
88
// force the socket to drain
68
- var noop = function ( ) { } ;
89
+ var noop = function ( ) { } ;
69
90
res . on ( 'data' , noop ) ;
70
91
res . on ( 'end' , noop ) ;
71
92
} ) ;
72
93
73
94
timeoutReq ( req , client . sendTimeout * 1000 ) ;
74
95
75
96
var cbFired = false ;
76
- req . on ( 'error' , function ( e ) {
97
+ req . on ( 'error' , function ( e ) {
77
98
client . emit ( 'error' , e ) ;
78
99
if ( ! cbFired ) {
79
100
cb && cb ( e ) ;
@@ -89,10 +110,40 @@ function HTTPSTransport(options) {
89
110
this . options = options || { } ;
90
111
this . agent = httpsAgent ;
91
112
}
113
+
114
+ function HTTPProxyTransport ( options ) {
115
+ this . defaultPort = 80 ;
116
+ this . transport = http ;
117
+ this . options = options || { } ;
118
+ this . agent = httpAgent ;
119
+ this . options . host = options . proxyHost ;
120
+ this . options . port = options . proxyPort ;
121
+ }
122
+
123
+ function HTTPSProxyTransport ( options ) {
124
+ this . transport = https ;
125
+ this . options = options || { } ;
126
+ this . agent = httpsAgent ;
127
+ this . options . agent = tunnel [ 'httpsOverHttp' ] ( {
128
+ proxy : {
129
+ host : options . proxyHost ,
130
+ port : options . proxyPort ,
131
+ proxyAuth : null // TODO: Add ability to specify creds/auth
132
+ } ,
133
+ keepAlive : agentOptions . keepAlive ,
134
+ maxSockets : agentOptions . maxSockets
135
+ } ) ;
136
+ }
137
+
92
138
util . inherits ( HTTPSTransport , HTTPTransport ) ;
139
+ util . inherits ( HTTPProxyTransport , HTTPTransport ) ;
140
+ util . inherits ( HTTPSProxyTransport , HTTPTransport ) ;
93
141
94
142
module . exports . http = new HTTPTransport ( ) ;
95
143
module . exports . https = new HTTPSTransport ( ) ;
96
144
module . exports . Transport = Transport ;
97
145
module . exports . HTTPTransport = HTTPTransport ;
98
146
module . exports . HTTPSTransport = HTTPSTransport ;
147
+
148
+ module . exports . HTTPProxyTransport = HTTPProxyTransport ;
149
+ module . exports . HTTPSProxyTransport = HTTPSProxyTransport ;
0 commit comments