Skip to content

Commit

Permalink
Adding support for inspect dumped http interceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanishka Dilshan committed Dec 28, 2018
1 parent 7b94d5d commit 8daca45
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 20 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const receptionRoute = require('./routes/reception');
const receptionsRoute = require('./routes/receptions');
const interceptionsRoute = require('./routes/interceptions');
const interceptionRoute = require('./routes/interception');
const interceptionDumpsRoute = require('./routes/interceptiondumps');

//db connection
const dbConfig = require('./config/database')
Expand Down Expand Up @@ -94,6 +95,7 @@ app.use('/reception', receptionRoute);
app.use('/receptions', receptionsRoute);
app.use('/interceptions', interceptionsRoute);
app.use('/interception', interceptionRoute);
app.use('/interceptiondumps', interceptionDumpsRoute);

//end of http handlers

Expand Down
34 changes: 24 additions & 10 deletions routes/interception.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,18 @@ function processRequest(req, res, method, httpInterception) {
.then(savedOriginalHttpRequestDump => {
//Time to forward request
const forwadedRequestHeaderMap = {};
const forwardedRequestHeaderArr = [];
var idx = 0;
for (; idx < originalRequestHeaderMap.length; idx++) {
//drop the header 'host'
const hdrName = originalRequestHeaderMap[idx].key;
const hdrVal = originalRequestHeaderMap[idx].value;
if (hdrName.toUpperCase() != "HOST") {
forwadedRequestHeaderMap[hdrName] = hdrVal;
forwardedRequestHeaderArr.push({
key: hdrName,
value: hdrVal
});
}
}

Expand Down Expand Up @@ -142,7 +147,7 @@ function processRequest(req, res, method, httpInterception) {
protocol: req.protocol, //TODO review
method: method,
body: finalBuffer,
headers: forwadedRequestHeaderMap,
headers: forwardedRequestHeaderArr,
responseBody: body,
responseHeaders: forwadedResponseHeadersMap //TODO add response headers here
}
Expand All @@ -164,15 +169,24 @@ function processRequest(req, res, method, httpInterception) {
})
.save()
.then(savedHttpInterceptionDump => {
//Time to perform STEP 4 : Responding to the original requester
var idx = 0;
for (; idx < forwadedResponseHeadersMap.length; idx++) {
const headerKey = forwadedResponseHeadersMap[idx].key;
const headerVal = forwadedResponseHeadersMap[idx].value;
res.setHeader(headerKey, headerVal);
}
res.writeHead(response.statusCode, response.statusMessage);
res.end(body);
savedOriginalHttpRequestDump.responseHeaders = forwadedResponseHeadersMap;
savedOriginalHttpRequestDump.responseBody = body;
savedOriginalHttpRequestDump.save()
.then(savedOriginalHttpRequestDump => {
//Time to perform STEP 4 : Responding to the original requester
var idx = 0;
for (; idx < forwadedResponseHeadersMap.length; idx++) {
const headerKey = forwadedResponseHeadersMap[idx].key;
const headerVal = forwadedResponseHeadersMap[idx].value;
res.setHeader(headerKey, headerVal);
}
res.writeHead(response.statusCode, response.statusMessage);
res.end(body);
})
.catch(err => {
console.log(err);
res.send("Unable to save original http request dump" + err);
})
})
.catch(err => {
console.log(err);
Expand Down
45 changes: 45 additions & 0 deletions routes/interceptiondumps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const express = require('express');
const mongoose = require('mongoose');

const router = express.Router();

//import models
require('../models/HttpInterception');
require('../models/HttpInterceptionDump');
require('../models/HttpDump');

const HttpDump = mongoose.model('httdump');
const HttpInterceptionDump = mongoose.model('httpinterceptiondump');

router.get('/:id', (req, res) => {
HttpInterceptionDump.findById(req.params.id)
.then(httpImterceptionDump => {
//Find associated http dumps with this http interception
HttpDump.find({
'_id': {
$in: [
mongoose.Types.ObjectId(httpImterceptionDump.originalRequestDumpId),
mongoose.Types.ObjectId(httpImterceptionDump.forwardedRequestDumpId)
]
}
}, function(err, docs){
if(err){
console.log(err)
res.send("Unable to load http inspection dumps " + err)
}else{
//render view
res.render('httpinterceptionsdumps/inspect', {
httpImterceptionDump : httpImterceptionDump,
originalRequestDump : docs[0],
forwardedRequestDump : docs[1]
});
}
});
})
.catch(err => {
console.log(err)
res.send("Unable to retrieve http interception dump" + err)
})
});

module.exports = router;
154 changes: 154 additions & 0 deletions views/httpinterceptionsdumps/inspect.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<div class="card mb-3">
<div class="card-header container">
<div class="row">
<div class="col-sm-6">
<h4><span class="badge badge-secondary">{{httpImterceptionDump.method}}</span>
({{httpImterceptionDump.fromProtocol}} -> {{httpImterceptionDump.toProtocol}}) </h4>
</div>
<div class="col-sm-6">
<p class="text-right"><small>{{httpImterceptionDump.payloadSize}} bytes</small></p>
</div>
</div>
<p><small>{{httpImterceptionDump.fromIp}} -> {{httpImterceptionDump.toHost}}</small></p>
<p class="text-left text-secondary"><small>{{httpImterceptionDump.date}}</small></p>
</div>

<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" role="tab" data-toggle="tab" aria-controls="originalRequest" aria-selected="true"
href="#originalRequest">Original Request</a>
</li>
<li class="nav-item">
<a class="nav-link" role="tab" data-toggle="tab" aria-controls="forwadedRequest" aria-selected="false" href="#forwadedRequest">Forwaded
Request</a>
</li>
</ul>

<div class="tab-content">
<div id="originalRequest" class="tab-pane fade show active">
<!-- Display area for original request -->
<div class="card-body">
<table class="table table-hover">
<thead>
<tr>
<th>HTTP Request Header</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{{#each originalRequestDump.headers}}
<tr>
<td><small><mark>{{key}}</mark></small></td>
<td><small>{{value}}</small></td>
</tr>
{{/each}}
</tbody>
</table>
</div>
{{#if originalRequestDump.body.length}}
<div class="card-body">
<div class="d-inline-flex p-2">
<a href="/reception/download/requestpayload/{{originalRequestDump._id}}" target="_blank" class="btn btn-primary">
<span class="fas fa-download" aria-hidden="true"></span> Download</a>
</div>
<div class="d-inline-flex p-2">
<p class="h6 card-title">Request payload size : {{originalRequestDump.body.length}} bytes</p>
</div>
</div>
{{/if}}
<div class="card-body">
<table class="table table-hover">
<thead>
<tr>
<th>HTTP Response Header</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{{#each originalRequestDump.responseHeaders}}
<tr>
<td><small><mark>{{key}}</mark></small></td>
<td><small>{{value}}</small></td>
</tr>
{{/each}}
</tbody>
</table>
</div>
{{#if originalRequestDump.responseBody.length}}
<div class="card-body">
<div class="d-inline-flex p-2">
<a href="/reception/download/responsepayload/{{originalRequestDump._id}}" target="_blank" class="btn btn-primary">
<span class="fas fa-download" aria-hidden="true"></span> Download</a>
</div>
<div class="d-inline-flex p-2">
<p class="h6 card-title">Response payload size : {{originalRequestDump.responseBody.length}} bytes</p>
</div>
</div>
{{/if}}
<!-- End of the display area for original request-->
</div>
<div id="forwadedRequest" class="tab-pane fade">
<!-- Area for displaying forwaded http request-->
<div class="card-body">
<table class="table table-hover">
<thead>
<tr>
<th>HTTP Request Header</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{{#each forwardedRequestDump.headers}}
<tr>
<td><small><mark>{{key}}</mark></small></td>
<td><small>{{value}}</small></td>
</tr>
{{/each}}
</tbody>
</table>
</div>
{{#if forwardedRequestDump.body.length}}
<div class="card-body">
<div class="d-inline-flex p-2">
<a href="/reception/download/requestpayload/{{forwardedRequestDump._id}}" target="_blank" class="btn btn-primary">
<span class="fas fa-download" aria-hidden="true"></span> Download</a>
</div>
<div class="d-inline-flex p-2">
<p class="h6 card-title">Request payload size : {{forwardedRequestDump.body.length}} bytes</p>
</div>
</div>
{{/if}}
<div class="card-body">
<table class="table table-hover">
<thead>
<tr>
<th>HTTP Response Header</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{{#each forwardedRequestDump.responseHeaders}}
<tr>
<td><small><mark>{{key}}</mark></small></td>
<td><small>{{value}}</small></td>
</tr>
{{/each}}
</tbody>
</table>
</div>
{{#if forwardedRequestDump.responseBody.length}}
<div class="card-body">
<div class="d-inline-flex p-2">
<a href="/reception/download/responsepayload/{{forwardedRequestDump._id}}" target="_blank" class="btn btn-primary">
<span class="fas fa-download" aria-hidden="true"></span> Download</a>
</div>
<div class="d-inline-flex p-2">
<p class="h6 card-title">Response payload size : {{forwardedRequestDump.responseBody.length}} bytes</p>
</div>
</div>
{{/if}}
<!-- End of the area for displaying forwaded http request-->
</div>
</div>

</div>
12 changes: 2 additions & 10 deletions views/interceptions/index.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,8 @@
<p class="text-left text-secondary"><small>{{date}}</small></p>
</div>
<div class="card-body">

</div>

<hr />
<div class="d-inline-flex p-1">
<a href="/receptions/dump/{{_id}}/delete" class="btn btn-danger" style="margin:5px;" data-toggle="confirmation"
data-title="Are you sure?">
<span class="fas fa-trash" aria-hidden="true"></span> Delete</a>
<a href="/receptions/save" class="btn btn-info" style="margin:5px;">
<span class="fas fa-save" aria-hidden="true"></span> Save</a>
<a href="/interceptiondumps/{{_id}}" class="btn btn-primary" >
<span class="fas fa-project-diagram" aria-hidden="true"></span> Inspect</a>
</div>
</div>
{{else}}
Expand Down
2 changes: 2 additions & 0 deletions views/receptions/index.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
</table>
</div>

{{#if responseBody.length}}
<div class="card-body">
<div class="d-inline-flex p-2">
<a href="/reception/download/responsepayload/{{_id}}" target="_blank" class="btn btn-primary">
Expand All @@ -108,6 +109,7 @@
<p class="h6 card-title">Response payload size : {{responseBody.length}} bytes</p>
</div>
</div>
{{/if}}
<hr />
<div class="d-inline-flex p-1">
<a href="/receptions/dump/{{_id}}/delete" class="btn btn-danger" style="margin:5px;" data-toggle="confirmation"
Expand Down

0 comments on commit 8daca45

Please sign in to comment.