Advertisement
didkoslawow

Untitled

Apr 6th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function requestValidator(object) {
  2.      const validMethods = ['GET', 'POST', 'DELETE', 'CONNECT'];
  3.      const validURI = /[^A-Za-z0-9\.\*]+/gm;
  4.      const validVersions = ['HTTP/0.9', 'HTTP/1.0', 'HTTP/1.1', 'HTTP/2.0'];
  5.      const validMessage = /[<>\\&'"]/mg;
  6.  
  7.     if (!object.hasOwnProperty('method') || !validMethods.includes(object['method']) || !object['method']) {
  8.         throw new Error('Invalid request header: Invalid Method')
  9.     }
  10.     if (!object.hasOwnProperty('uri') || validURI.test(object['uri']) || !object['uri']) {
  11.         throw new Error('Invalid request header: Invalid URI');
  12.     }
  13.     if (!object.hasOwnProperty('version') || !validVersions.includes(object['version']) || !object['version']) {
  14.         throw new Error('Invalid request header: Invalid Version');
  15.     }
  16.     if (!object.hasOwnProperty('message') || validMessage.test(object['message'])) {
  17.         throw new Error('Invalid request header: Invalid Message');
  18.     }
  19.     return object;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement