Skip to content

Commit

Permalink
Make dumping of request and response body configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
xkr47 committed Mar 27, 2016
1 parent 95f9596 commit 4ad36a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
3 changes: 3 additions & 0 deletions source/org/otherone/vhostproxy/vertx/config.ceylon
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ Integer serverIdleTimeout = 60;
"A file containing 'group:commaSeparatedListOfIps'. Example: 'home:192.168.1.2,127.0.0.1'. IPv6 addresses also allowed in uncompressed format e.g. 0:0:0:0:0:0:0:1 for localhost."
String accessGroupFilename = "accessGroups.txt";

Boolean dumpRequestBody = true;
Boolean dumpResponseBody = false;

class NextHop (
"When the hostname part of the 'Host' header matches, this entry will be used as next hop."
shared String matchHost,
Expand Down
30 changes: 16 additions & 14 deletions source/org/otherone/vhostproxy/vertx/run.ceylon
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,28 @@ Logger log = logger(`package`);

ReentrantLock logLock = ReentrantLock();

class MyPump<T>(AsyncFile logFile, String reqId, LogType logType, String type, ReadStream<T> readStream, WriteStream<T> writeStream, Anything()? firstBufferWrittenHandler = null) given T satisfies Buffer {
class MyPump<T>(AsyncFile logFile, String reqId, LogType logType, String type, ReadStream<T> readStream, WriteStream<T> writeStream, Boolean dumpBody, Anything()? firstBufferWrittenHandler = null) given T satisfies Buffer {
void dataHandler(T? data) {
writeStream.write(data);
if (exists firstBufferWrittenHandler) { firstBufferWrittenHandler(); }

try (logLock) {
if (exists data) {
logFile.write(buffer.buffer("``reqId`` ``logType.str`` ``system.milliseconds`` ``data.length()`` bytes:\n", "UTF-8"));
value prefix = "``reqId`` ``LogType.none.str`` ";
variable value start = 0;
for (i in 0:data.length()) {
if (data.getUnsignedByte(i) == '\n'.integer.byte) {
logFile.write(buffer.buffer(prefix, "UTF-8"));
logFile.write(data.slice(start, i + 1));
start = i+1;
logFile.write(buffer.buffer("``reqId`` ``logType.str`` ``system.milliseconds`` ``data.length()`` bytes``dumpBody then ":" else ""``\n", "UTF-8"));
if (dumpBody) {
value prefix = "``reqId`` ``LogType.none.str`` ";
variable value start = 0;
for (i in 0:data.length()) {
if (data.getUnsignedByte(i) == '\n'.integer.byte) {
logFile.write(buffer.buffer(prefix, "UTF-8"));
logFile.write(data.slice(start, i + 1));
start = i+1;
}
}
logFile.write(buffer.buffer(prefix, "UTF-8"));
logFile.write(data.slice(start, data.length()));
logFile.write(buffer.buffer("\n", "UTF-8"));
}
logFile.write(buffer.buffer(prefix, "UTF-8"));
logFile.write(data.slice(start, data.length()));
logFile.write(buffer.buffer("\n", "UTF-8"));
} else {
logFile.write(buffer.buffer("``reqId`` ``logType.str`` ``system.milliseconds``` null buffer\n", "UTF-8"));
}
Expand Down Expand Up @@ -282,7 +284,7 @@ class ProxyService(HttpClient client, Boolean isTls, Vertx myVertx) {
}
trace(LogType.sres, "Outgoing response initial ``dumpSRes(sres, "")``");

value resPump = MyPump(logFile2, reqId, LogType.resbody, "Response body", cres, sres);
value resPump = MyPump(logFile2, reqId, LogType.resbody, "Response body", cres, sres, dumpResponseBody);
cres.endHandler(() {
trace(LogType.cres, "Incoming response complete");
return sres.end();
Expand Down Expand Up @@ -312,7 +314,7 @@ class ProxyService(HttpClient client, Boolean isTls, Vertx myVertx) {
trace(LogType.creq, "Outgoing request final:``dumpCReq(creq)``");
}
}
value reqPump = MyPump(logFile2, reqId, LogType.reqbody, "Request body", sreq, creq, dumpFinalRequest); // TODO dump contents
value reqPump = MyPump(logFile2, reqId, LogType.reqbody, "Request body", sreq, creq, dumpRequestBody, dumpFinalRequest);
sreq.endHandler(() {
creq.end();
dumpFinalRequest();
Expand Down

0 comments on commit 4ad36a9

Please sign in to comment.