vibe.http.log 0/160(0%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
/** A HTTP 1.1/1.0 server implementation. Copyright: © 2012-2013 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig, Jan Krüger */ module vibe.http.log; import vibe.core.file; import vibe.core.log; import vibe.core.sync : InterruptibleTaskMutex, performLocked; import vibe.http.server; import vibe.utils.array : FixedAppender; import std.array; import std.conv; import std.exception; import std.string; class HTTPLogger { @safe: private { string m_format; const(HTTPServerSettings) m_settings; InterruptibleTaskMutex m_mutex; Appender!(char[]) m_lineAppender; } this(in HTTPServerSettings settings, string format) { m_format = format; m_settings = settings; m_mutex = new InterruptibleTaskMutex; m_lineAppender.reserve(2048); } void close() {} final void log(scope HTTPServerRequest req, scope HTTPServerResponse res) { m_mutex.performLocked!(() @safe { m_lineAppender.clear(); formatApacheLog(m_lineAppender, m_format, req, res, m_settings); writeLine(m_lineAppender.data); }); } protected abstract void writeLine(const(char)[] ln); } final class HTTPConsoleLogger : HTTPLogger { @safe: this(HTTPServerSettings settings, string format) { super(settings, format); } protected override void writeLine(const(char)[] ln) { logInfo("%s", ln); } } final class HTTPFileLogger : HTTPLogger { @safe: private { FileStream m_stream; } this(HTTPServerSettings settings, string format, string filename) { m_stream = openFile(filename, FileMode.append); super(settings, format); } override void close() { m_stream.close(); m_stream = FileStream.init; } protected override void writeLine(const(char)[] ln) { assert(!!m_stream); m_stream.write(ln); m_stream.write("\n"); m_stream.flush(); } } void formatApacheLog(R)(ref R ln, string format, scope HTTPServerRequest req, scope HTTPServerResponse res, in HTTPServerSettings settings) @safe { import std.format : formattedWrite; enum State {Init, Directive, Status, Key, Command} State state = State.Init; bool conditional = false; bool negate = false; bool match = false; string statusStr; string key = ""; while( format.length > 0 ) { final switch(state) { case State.Init: auto idx = format.indexOf('%'); if( idx < 0 ) { ln.put( format ); format = ""; } else { ln.put( format[0 .. idx] ); format = format[idx+1 .. $]; state = State.Directive; } break; case State.Directive: if( format[0] == '!' ) { conditional = true; negate = true; format = format[1 .. $]; state = State.Status; } else if( format[0] == '%' ) { ln.put("%"); format = format[1 .. $]; state = State.Init; } else if( format[0] == '{' ) { format = format[1 .. $]; state = State.Key; } else if( format[0] >= '0' && format[0] <= '9' ) { conditional = true; state = State.Status; } else { state = State.Command; } break; case State.Status: if( format[0] >= '0' && format[0] <= '9' ) { statusStr ~= format[0]; format = format[1 .. $]; } else if( format[0] == ',' ) { statusStr = ""; format = format[1 .. $]; } else if( format[0] == '{' ) { format = format[1 .. $]; state = State.Key; } else { state = State.Command; } if (statusStr.length == 3 && !match) { auto status = parse!int(statusStr); match = status == res.statusCode; } break; case State.Key: auto idx = format.indexOf('}'); enforce(idx > -1, "Missing '}'"); key = format[0 .. idx]; format = format[idx+1 .. $]; state = State.Command; break; case State.Command: if( conditional && negate == match ) { ln.put('-'); format = format[1 .. $]; state = State.Init; break; } switch(format[0]) { case 'a': //Remote IP-address ln.put(req.peer); break; //TODO case 'A': //Local IP-address //case 'B': //Size of Response in bytes, excluding headers case 'b': //same as 'B' but a '-' is written if no bytes where sent if (!res.bytesWritten) ln.put('-'); else formattedWrite(() @trusted { return &ln; } (), "%s", res.bytesWritten); break; case 'C': //Cookie content {cookie} import std.algorithm : joiner; enforce(key != "", "cookie name missing"); auto values = req.cookies.getAll(key); if (values.length) ln.formattedWrite("%s", values.joiner(";")); else ln.put("-"); break; case 'D': //The time taken to serve the request auto d = res.timeFinalized - req.timeCreated; formattedWrite(() @trusted { return &ln; } (), "%s", d.total!"msecs"()); break; //case 'e': //Environment variable {variable} //case 'f': //Filename case 'h': //Remote host ln.put(req.peer); break; case 'H': //The request protocol ln.put("HTTP"); break; case 'i': //Request header {header} enforce(key != "", "header name missing"); if (auto pv = key in req.headers) ln.put(*pv); else ln.put("-"); break; case 'm': //Request method ln.put(httpMethodString(req.method)); break; case 'o': //Response header {header} enforce(key != "", "header name missing"); if( auto pv = key in res.headers ) ln.put(*pv); else ln.put("-"); break; case 'p': //port formattedWrite(() @trusted { return &ln; } (), "%s", settings.port); break; //case 'P': //Process ID case 'q': //query string (with prepending '?') ln.put("?"); ln.put(req.queryString); break; case 'r': //First line of Request ln.put(httpMethodString(req.method)); ln.put(' '); ln.put(req.requestURL); ln.put(' '); ln.put(getHTTPVersionString(req.httpVersion)); break; case 's': //Status formattedWrite(() @trusted { return &ln; } (), "%s", res.statusCode); break; case 't': //Time the request was received {format} ln.put(req.timeCreated.toSimpleString()); break; case 'T': //Time taken to server the request in seconds auto d = res.timeFinalized - req.timeCreated; formattedWrite(() @trusted { return &ln; } (), "%s", d.total!"seconds"); break; case 'u': //Remote user ln.put(req.username.length ? req.username : "-"); break; case 'U': //The URL path without query string ln.put(req.path); break; case 'v': //Server name ln.put(req.host.length ? req.host : "-"); break; default: throw new Exception("Unknown directive '" ~ format[0] ~ "' in log format string"); } state = State.Init; format = format[1 .. $]; break; } } }