var
// configure address and port
IF = '0.0.0.0',
PORT = 8888,
g_http = require('http'),
g_url = require('url'),
g_fs = require('fs'),
main = function(request, response) {
var
strTimestamp = new Date().toISOString(),
strPath = g_url.parse(request.url).pathname;
if (request.method === 'POST') {
console.log('POST');
request.on('data', function(chunk) {
console.log('BEGIN');
var
strChunk = chunk.toString(),
strPartHeader = strChunk.substring(1, strChunk.indexOf('\r\n\r\n')),
strContent =
'' +
'
' +
'reload' +
'Request Headers ' + strTimestamp + '
' +
'';
for (header in request.headers) {
strContent += (header + ': ' + request.headers[header] + '\n');
}
strContent += '\n\nDATA\n';
strContent += strPartHeader;
strContent += '\n...
';
if (strPartHeader.indexOf('"application/pdf"') !== -1)
strContent += 'ERROR: "application/pdf" with surrounding double quotes is not a valid MIME type.
';
strContent +=
'' +
'';
response.writeHead(200, 'OK', {
'Content-Type': 'text/html; charset=UTF-8',
'Content-Length': strContent.length,
'Cache-Control': 'private, max-age=0'
});
response.end(strContent, 'UTF-8');
});
} else if (strPath === '/' || strPath === '/index.html') {
var strContent =
'' +
'' +
'reload' +
'Request Headers ' + strTimestamp + '
' +
'';
for (header in request.headers) {
strContent += (header + ': ' + request.headers[header] + '\n');
}
strContent +=
'
' +
'Download
' +
'Download RFC 2045 PDF
' +
'Download RFC 2045 PDF with invalid MIME type
' +
'Upload
' +
'' +
'' +
'reload' +
'';
response.writeHead(200, 'OK', {
'Content-Type': 'text/html; charset=UTF-8',
'Content-Length': strContent.length,
'Cache-Control': 'private, max-age=0'
});
response.end(strContent, 'UTF-8');
} else if (strPath.indexOf(".pdf") !== -1 && strPath.indexOf("/buggy/") !== -1) {
g_fs.stat('test.pdf', function(err, stats) {
response.writeHead(200, 'OK', {
'Server': 'node',
'Cache-Control': 'private, max-age=0',
'Content-Disposition': 'inline; filename="test.pdf"',
'Content-Type': '"application/pdf"',
'Content-Length': stats.size
});
g_fs.createReadStream('test.pdf').pipe(response);
});
} else if (strPath.indexOf(".pdf") !== -1) {
g_fs.stat('test.pdf', function(err, stats) {
response.writeHead(200, 'OK', {
'Server': 'node',
'Cache-Control': 'private, max-age=0',
'Content-Disposition': 'inline; filename="test.pdf"',
'Content-Type': 'application/pdf',
'Content-Length': stats.size
});
g_fs.createReadStream('test.pdf').pipe(response);
});
} else {
response.writeHead(404, 'Not found', {
'Content-Type': 'text/plain; charset=UTF-8',
'Content-Length': '0',
'Cache-Control': 'private, max-age=0'
});
response.end();
}
},
g_server = g_http.createServer(main);
process.on('uncaughtException', function(err) {
console.log(new Date().toISOString() + ' Uncaught exception: ' + err);
});
process.on('SIGINT', function() {
g_server.close();
process.exit(0);
});
process.on('exit', function() {
console.log(new Date().toISOString() + ' EXIT');
});
g_server.listen(PORT, IF);
console.log(new Date().toISOString() + ' Server running at http://' + IF + ':' + PORT + '/');