Skip to content

Commit 67b6d13

Browse files
committed
fix(http): data urls are now shortened
1 parent df6b42a commit 67b6d13

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

lib/instrumentations/core/http/request.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ var debug = require('debug')('risingstack/trace')
22
var url = require('url')
33
var microtime = require('microtime')
44

5+
var util = require('./util')
6+
57
function wrapRequest (originalHttpRequest, agent, mustCollectStore) {
68
var whiteListHosts = agent.getConfig().whiteListHosts
79

@@ -54,7 +56,7 @@ function wrapRequest (originalHttpRequest, agent, mustCollectStore) {
5456
id: requestId,
5557
spanId: spanId,
5658
host: requestParams.host,
57-
url: requestParams.path,
59+
url: util.formatDataUrl(requestParams.path),
5860
time: clientSendTime,
5961
method: requestParams.method,
6062
mustCollect: !!mustCollectStore[requestId]
@@ -76,7 +78,7 @@ function wrapRequest (originalHttpRequest, agent, mustCollectStore) {
7678
id: requestId,
7779
spanId: spanId,
7880
host: requestParams.host,
79-
url: requestParams.path,
81+
url: util.formatDataUrl(requestParams.path),
8082
mustCollect: true,
8183
err: {
8284
type: 'network-error',
@@ -108,7 +110,7 @@ function wrapRequest (originalHttpRequest, agent, mustCollectStore) {
108110
id: requestId,
109111
spanId: spanId,
110112
host: requestParams.host,
111-
url: requestParams.path,
113+
url: util.formatDataUrl(requestParams.path),
112114
statusCode: incomingMessage.statusCode,
113115
mustCollect: mustCollectStore[requestId]
114116
}

lib/instrumentations/core/http/server.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var qs = require('qs')
44
var microtime = require('microtime')
55
var debug = require('debug')('risingstack/trace')
66

7+
var util = require('./util')
8+
79
function wrapListener (listener, agent, mustCollectStore) {
810
var ignoreHeaders = agent.getConfig().ignoreHeaders
911

@@ -18,12 +20,13 @@ function wrapListener (listener, agent, mustCollectStore) {
1820
})
1921

2022
if (skipped) {
21-
debug('trace event (sr); request skipped', headers)
23+
debug('trace event (sr); request skipped because of ignoreHeaders', headers)
2224
return listener.apply(this, arguments)
2325
}
2426

2527
var requestUrl = url.parse(request.url)
2628
var requestQuery = qs.parse(requestUrl.query).requestId
29+
2730
var originalWriteHead = response.writeHead
2831

2932
var requestId = headers['request-id'] || requestQuery || agent.generateId()
@@ -48,7 +51,7 @@ function wrapListener (listener, agent, mustCollectStore) {
4851
id: requestId,
4952
spanId: spanId,
5053
host: headers.host,
51-
url: requestUrl.pathname,
54+
url: util.formatDataUrl(requestUrl.pathname),
5255
time: serverRecieveTime,
5356
method: method,
5457
parentId: headers['x-parent'],
@@ -70,7 +73,7 @@ function wrapListener (listener, agent, mustCollectStore) {
7073
id: requestId,
7174
spanId: headers['x-span-id'],
7275
host: headers.host,
73-
url: requestUrl.pathname,
76+
url: util.formatDataUrl(requestUrl.pathname),
7477
time: serverSendTime,
7578
statusCode: response.statusCode,
7679
responseTime: responseTime
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var DATA_URI_REGEX = /(data:([a-z]+\/[a-z0-9\-\+]+(;[a-z\-]+\=[a-z0-9\-]+)?)?(;base64)?,)([a-z0-9\!\$\&\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*)$/i
2+
3+
function formatDataUrl (text) {
4+
if (!text) {
5+
return
6+
}
7+
8+
return text.replace(DATA_URI_REGEX, function replacer (match, p1) {
9+
return [p1, '{data}'].join('')
10+
})
11+
}
12+
13+
module.exports.formatDataUrl = formatDataUrl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var expect = require('chai').expect
2+
var util = require('./util')
3+
4+
describe('The http util module', function () {
5+
describe('#formatDataUrl', function () {
6+
it('substitutes the data part', function () {
7+
var url = '/image?data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC'
8+
var formatted = util.formatDataUrl(url)
9+
expect(formatted).to.eql('/image?data:image/png;base64,{data}')
10+
})
11+
it('doesn\'t touch non-data urls', function () {
12+
var url = '/image?data=4'
13+
var formatted = util.formatDataUrl(url)
14+
expect(formatted).to.eql(url)
15+
})
16+
})
17+
})

0 commit comments

Comments
 (0)