-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.js
137 lines (129 loc) · 4.05 KB
/
http.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// built-in
const http = require('http');
// external modules
const cache = require('memory-cache');
const bind = process.env.SMSMTP_HTTP_BIND || '127.0.0.1';
const port = process.env.SMSMTP_HTTP_PORT || 3000;
const host = process.env.SMSMTP_HTTP_HOST || 'localhost';
const prefix = [
'<!DOCTYPE html><html>',
'<head>',
'<meta charset="utf-8"><title>smsmtp</title>',
'<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0">',
'</head>',
'<style>',
'html,body{width:100%;position:absolute;margin:0;padding:0;}',
'ul,li{list-style:none;display:inline-block;margin:0;padding:0;}',
'th{text-align:right;}',
'iframe{border:0;}',
'#headers,#plaintext{font-family:monospace;background:#eee;overflow:auto;}',
'#htmltext,#plaintext{width:100%;}',
'#headers,#htmltext,#plaintext{max-width:100%;}',
'</style>',
'<body>'
];
const suffix = [
'</body>',
"<script>window.addEventListener('message',function(e){var t=document.getElementById('htmltext');"
+ "if(t&&e.data&&e.data.h){t.style.height=e.data.h+'px';}});</script>",
'</html>'
];
var testrecipient = '[email protected]';
var testmail = {
html: '<!DOCTYPE html><html><head><meta charset="utf-8"><title>testmail</title>'
+ '<style>p{color:green;}</style></head><body><p>Hello, Test!</p></body></html>',
text: 'Hello, Test!',
headers: {
From: '[email protected]',
To: testrecipient
}
};
const pushHeaders = function(content, mail) {
'use strict';
var headers = mail && mail.headers || {};
var keys = Object.keys(headers);
content.push('<div id="headers"><table>');
keys.map(function(key) {
content.push('<tr>');
content.push('<th>', key, '</th>');
content.push('<td><ul><li>', [].concat(headers[key]).join('</li><li>'), '</li></ul></td>');
content.push('</tr>');
});
content.push('</table></div>');
};
const pushHTML = function(content, mail) {
'use strict';
var html = '<base target="_blank" />';
html += mail.html || '';
(mail.attachments || []).forEach(function(attachment) {
var regex = new RegExp('cid:' + attachment.contentId, 'g');
html = html.replace(regex, "data:image/png;base64," + attachment.content);
});
html += '<script>window.parent.postMessage({h:document.body.scrollHeight},"*");</script>';
content.push('<iframe',
' id="htmltext"',
' ref="hc"',
' scrolling="no"',
' src="data:text/html;charset=utf-8,', encodeURIComponent(html), '">',
'</iframe>'
);
};
const pushPlain = function(content, mail) {
'use strict';
content.push('<pre id="plaintext">', mail.text || '', '</pre>');
};
const manglePath = function(path) {
'use strict';
if (!path) {
return path;
}
var mangled = path;
if (mangled.indexOf('?') !== -1) {
mangled = mangled.split('?')[0];
}
if (mangled.indexOf('/') !== 0) {
mangled = '/' + mangled;
}
if (mangled.indexOf('/') !== -1) {
var parts = mangled.split('/');
parts.shift();
mangled = (parts || []).join('/');
}
return mangled;
};
http.createServer(function(request, response) {
'use strict';
var requestPath = manglePath(request.url);
var content = [].concat(prefix);
var mail;
if (testrecipient === requestPath) {
mail = testmail;
} else {
mail = cache.get(requestPath);
}
var statusCode = 404;
var statusMessage = 'not found';
if (mail) {
statusCode = 200;
statusMessage = 'ok';
pushHeaders(content, mail);
pushHTML(content, mail);
pushPlain(content, mail);
} else {
if (requestPath === '') {
requestPath = 'please provide recipient email address '
+ '(<a href="[email protected]">[email protected]</a> to test)';
} else {
requestPath = 'no mail for: ' + requestPath;
}
content.push('<div id="notfound">', requestPath, '</div>');
}
content = content.concat(suffix);
response.writeHead(statusCode, statusMessage, {
'Content-Type': 'text/html;charset=utf-8'
});
response.end(content.join(''));
}).listen(port, bind, function() {
'use strict';
console.log('SMSMTP HTTP frontend listening on: %s:%s', bind, port, ' for: http://' + host + ':' + port);
});