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/. */
5 #ifndef nsSecurityHeaderParser_h
6 #define nsSecurityHeaderParser_h
8 #include "mozilla/LinkedList.h"
9 #include "mozilla/Maybe.h"
13 // Utility class for handing back parsed directives and (optional) values
14 class nsSecurityHeaderDirective
15 : public mozilla::LinkedListElement
<nsSecurityHeaderDirective
> {
17 // The name of the directive.
19 // The value of the directive, if any. Will be Some if and only if a '='
20 // followed the directive name (the value itself may be the empty string).
21 mozilla::Maybe
<nsCString
> mValue
;
24 // This class parses security-related HTTP headers like
25 // Strict-Transport-Security. The Augmented Backus-Naur Form syntax for this
26 // header is reproduced below, for reference:
28 // Strict-Transport-Security = "Strict-Transport-Security" ":"
29 // [ directive ] *( ";" [ directive ] )
31 // directive = directive-name [ "=" directive-value ]
32 // directive-name = token
33 // directive-value = token | quoted-string
37 // token = <token, defined in [RFC2616], Section 2.2>
38 // quoted-string = <quoted-string, defined in [RFC2616], Section 2.2>/
40 // For further reference, see [RFC6797], Section 6.1
42 class nsSecurityHeaderParser
{
44 // The input to this class must be null-terminated, and must have a lifetime
45 // greater than or equal to the lifetime of the created
46 // nsSecurityHeaderParser.
47 explicit nsSecurityHeaderParser(const nsCString
& aHeader
);
48 ~nsSecurityHeaderParser();
50 // Only call Parse once.
52 // The caller does not take ownership of the memory returned here.
53 mozilla::LinkedList
<nsSecurityHeaderDirective
>* GetDirectives();
56 bool Accept(char aChr
);
57 bool Accept(bool (*aClassifier
)(signed char));
58 void Expect(char aChr
);
60 void Header(); // header = [ directive ] *( ";" [ directive ] )
61 void Directive(); // directive = directive-name [ "=" directive-value ]
62 void DirectiveName(); // directive-name = token
63 void DirectiveValue(); // directive-value = token | quoted-string
64 void Token(); // token = 1*<any CHAR except CTLs or separators>
65 void QuotedString(); // quoted-string = (<"> *( qdtext | quoted-pair ) <">)
66 void QuotedText(); // qdtext = <any TEXT except <"> and "\">
67 void QuotedPair(); // quoted-pair = "\" CHAR
69 // LWS = [CRLF] 1*( SP | HT )
70 void LWSMultiple(); // Handles *( LWS )
71 void LWSCRLF(); // Handles the [CRLF] part of LWS
72 void LWS(); // Handles the 1*( SP | HT ) part of LWS
74 mozilla::LinkedList
<nsSecurityHeaderDirective
> mDirectives
;
76 nsSecurityHeaderDirective
* mDirective
;
82 #endif // nsSecurityHeaderParser_h