@@ -4,6 +4,7 @@ const throng = require('throng')
4
4
const extractTokenRegEx = / ( [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 12 } ) /
5
5
const tokenFormatRegEx = / [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 12 } /
6
6
const TokenBlacklist = require ( '../../util/token-blacklist.js' )
7
+ const zlib = require ( 'zlib' )
7
8
8
9
class AwsEcs {
9
10
constructor ( config , eventEmitter ) {
@@ -97,7 +98,6 @@ class AwsEcs {
97
98
if ( body . length === 0 ) {
98
99
return
99
100
}
100
-
101
101
const self = this
102
102
const docs = JSON . parse ( body )
103
103
if ( docs && docs . length > 0 ) {
@@ -127,6 +127,8 @@ class AwsEcs {
127
127
const path = req . url . split ( '/' )
128
128
let token = null
129
129
let bodyIn = ''
130
+ const buffer = [ ]
131
+
130
132
if ( self . config . useIndexFromUrlPath === true && path . length > 1 ) {
131
133
if ( path [ 1 ] && path [ 1 ] . length > 31 && tokenFormatRegEx . test ( path [ 1 ] ) ) {
132
134
const match = path [ 1 ] . match ( extractTokenRegEx )
@@ -147,25 +149,56 @@ class AwsEcs {
147
149
res . end ( `invalid logs token in url ${ req . url } ` )
148
150
return
149
151
}
150
- req . on ( 'data' , function ( data ) {
151
- bodyIn += String ( data )
152
- } )
153
- req . on ( 'end' , function endHandler ( ) {
154
- try {
155
- self . parseRes ( req . headers , bodyIn , token )
156
- } catch ( err ) {
157
- if ( self . config . debug ) {
158
- consoleLogger . error ( 'Error in ECS HttpHandler: ' + err )
152
+
153
+ const isGzip = req . headers [ 'content-encoding' ] === 'gzip'
154
+ // if the JSON content is gzipped this will unzip it and parse it into a stringified JSON string
155
+ if ( isGzip ) {
156
+ const gunzip = zlib . createGunzip ( )
157
+ req . pipe ( gunzip )
158
+
159
+ gunzip . on ( 'data' , function onGzipHandler ( data ) {
160
+ buffer . push ( data . toString ( ) )
161
+ } )
162
+ gunzip . on ( 'end' , function endGzipHandler ( ) {
163
+ try {
164
+ const body = buffer . join ( '' )
165
+ self . parseRes ( req . headers , body , token )
166
+ } catch ( err ) {
167
+ if ( self . config . debug ) {
168
+ consoleLogger . error ( 'Error in ECS HttpHandler: ' + err )
169
+ }
170
+
171
+ res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } )
172
+ res . end ( `Invalid gzip input: ${ err } \n` )
173
+ return
159
174
}
175
+ // send response to client
176
+ res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } )
177
+ res . end ( 'OK\n' )
178
+ } )
160
179
161
- res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } )
162
- res . end ( `Invalid json input: ${ err } \n` )
163
- return
164
- }
165
- // send response to client
166
- res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } )
167
- res . end ( 'OK\n' )
168
- } )
180
+ return
181
+ } else {
182
+ req . on ( 'data' , function onHandler ( data ) {
183
+ bodyIn += String ( data )
184
+ } )
185
+ req . on ( 'end' , function endHandler ( ) {
186
+ try {
187
+ self . parseRes ( req . headers , bodyIn , token )
188
+ } catch ( err ) {
189
+ if ( self . config . debug ) {
190
+ consoleLogger . error ( 'Error in ECS HttpHandler: ' + err )
191
+ }
192
+
193
+ res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } )
194
+ res . end ( `Invalid json input: ${ err } \n` )
195
+ return
196
+ }
197
+ // send response to client
198
+ res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } )
199
+ res . end ( 'OK\n' )
200
+ } )
201
+ }
169
202
} catch ( err ) {
170
203
res . statusCode = 500
171
204
res . end ( )
0 commit comments