nsSecurityHeaderParser.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef nsSecurityHeaderParser_h__
  5. #define nsSecurityHeaderParser_h__
  6. #include "nsString.h"
  7. #include "mozilla/LinkedList.h"
  8. #include "nsCOMPtr.h"
  9. // Utility class for handing back parsed directives and (optional) values
  10. class nsSecurityHeaderDirective : public mozilla::LinkedListElement<nsSecurityHeaderDirective> {
  11. public:
  12. nsCString mName;
  13. nsCString mValue;
  14. };
  15. // This class parses security-related HTTP headers like
  16. // Strict-Transport-Security. The Augmented Backus-Naur Form syntax for this
  17. // header is reproduced below, for reference:
  18. //
  19. // Strict-Transport-Security = "Strict-Transport-Security" ":"
  20. // [ directive ] *( ";" [ directive ] )
  21. //
  22. // directive = directive-name [ "=" directive-value ]
  23. // directive-name = token
  24. // directive-value = token | quoted-string
  25. //
  26. // where:
  27. //
  28. // token = <token, defined in [RFC2616], Section 2.2>
  29. // quoted-string = <quoted-string, defined in [RFC2616], Section 2.2>/
  30. //
  31. // For further reference, see [RFC6797], Section 6.1
  32. class nsSecurityHeaderParser {
  33. public:
  34. explicit nsSecurityHeaderParser(const char *aHeader);
  35. ~nsSecurityHeaderParser();
  36. // Only call Parse once.
  37. nsresult Parse();
  38. // The caller does not take ownership of the memory returned here.
  39. mozilla::LinkedList<nsSecurityHeaderDirective> *GetDirectives();
  40. private:
  41. bool Accept(char aChr);
  42. bool Accept(bool (*aClassifier) (signed char));
  43. void Expect(char aChr);
  44. void Advance();
  45. void Header(); // header = [ directive ] *( ";" [ directive ] )
  46. void Directive(); // directive = directive-name [ "=" directive-value ]
  47. void DirectiveName(); // directive-name = token
  48. void DirectiveValue(); // directive-value = token | quoted-string
  49. void Token(); // token = 1*<any CHAR except CTLs or separators>
  50. void QuotedString(); // quoted-string = (<"> *( qdtext | quoted-pair ) <">)
  51. void QuotedText(); // qdtext = <any TEXT except <"> and "\">
  52. void QuotedPair(); // quoted-pair = "\" CHAR
  53. // LWS = [CRLF] 1*( SP | HT )
  54. void LWSMultiple(); // Handles *( LWS )
  55. void LWSCRLF(); // Handles the [CRLF] part of LWS
  56. void LWS(); // Handles the 1*( SP | HT ) part of LWS
  57. mozilla::LinkedList<nsSecurityHeaderDirective> mDirectives;
  58. const char *mCursor;
  59. nsSecurityHeaderDirective *mDirective;
  60. nsCString mOutput;
  61. bool mError;
  62. };
  63. #endif /* nsSecurityHeaderParser_h__ */