// Create server
const server = http.createServer((req, res): void => {
if (req.socket.remoteAddress == null) {
- res.writeHead(401, { 'Content-Type': 'text/plain' })
- res.end('Unauthorized')
+ res.writeHead(401).end('Unauthorized')
return
}
const client = requests.get(req.socket.remoteAddress)
} else {
if (--client.tokens <= 0) {
log(`${req.socket.remoteAddress} potential abuse`)
- res.writeHead(429, { 'Content-Type': 'text/plain' })
- res.end('Too Many Requests')
+ res.writeHead(429).end('Too Many Requests')
return
}
}
let data: Buffer[] = []
let reqSize = 0
if (req.method === 'POST') {
- if (+(req.headers['content-length'] ?? 0) > MAX_BODY_SIZE) {
- res.writeHead(413, { 'Content-Type': 'text/plain' })
- res.end('Content Too Large')
+ const contentLength = +(req.headers['content-length'] ?? 0)
+ if (contentLength == 0 || contentLength > MAX_BODY_SIZE) {
+ res.writeHead(413).end('Content Too Large')
req.socket.destroy()
return
}
req.on('data', (chunk: Buffer): void => {
reqSize += chunk.byteLength
if (reqSize > MAX_REQUEST_SIZE) {
- res.writeHead(413, { 'Content-Type': 'text/plain' })
- res.end('Content Too Large')
+ res.writeHead(413).end('Content Too Large')
req.socket.destroy()
return
}