AreWe

Untitled

Nov 5th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. const requestValidator = (obj) => {
  2. if (!['GET', 'POST', 'DELETE', 'CONNECT'].includes(obj.method)) {
  3. throw new Error('Invalid request header: Invalid Method');
  4. }
  5.  
  6. if (!obj.uri || (obj.uri !== '*' && obj.uri.match(/[^a-z0-9\.]/g))) {
  7. throw new Error('Invalid request header: Invalid URI');
  8. }
  9.  
  10. if (!['HTTP/0.9', 'HTTP/1.0', 'HTTP/1.1', 'HTTP/2.0'].includes(obj.version)) {
  11. throw new Error('Invalid request header: Invalid Version');
  12. }
  13.  
  14. if (typeof obj.message !== 'string' || (obj.message !== '' && obj.message.match(/[<>\\'"\&]/g))) {
  15. throw new Error('Invalid request header: Invalid Message');
  16. }
  17.  
  18. return obj;
  19. };
Add Comment
Please, Sign In to add comment