Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 "nsString.h"
9 #include "mozilla/LinkedList.h"
10 #include "nsCOMPtr.h"
12 // Utility class for handing back parsed directives and (optional) values
13 class nsSecurityHeaderDirective : public mozilla::LinkedListElement<nsSecurityHeaderDirective> {
14 public:
15 nsAutoCString mName;
16 nsAutoCString mValue;
17 };
19 // This class parses security-related HTTP headers like
20 // Strict-Transport-Security. The Augmented Backus-Naur Form syntax for this
21 // header is reproduced below, for reference:
22 //
23 // Strict-Transport-Security = "Strict-Transport-Security" ":"
24 // [ directive ] *( ";" [ directive ] )
25 //
26 // directive = directive-name [ "=" directive-value ]
27 // directive-name = token
28 // directive-value = token | quoted-string
29 //
30 // where:
31 //
32 // token = <token, defined in [RFC2616], Section 2.2>
33 // quoted-string = <quoted-string, defined in [RFC2616], Section 2.2>/
34 //
35 // For further reference, see [RFC6797], Section 6.1
37 class nsSecurityHeaderParser {
38 public:
39 explicit nsSecurityHeaderParser(const char *aHeader);
40 ~nsSecurityHeaderParser();
42 // Only call Parse once.
43 nsresult Parse();
44 // The caller does not take ownership of the memory returned here.
45 mozilla::LinkedList<nsSecurityHeaderDirective> *GetDirectives();
47 private:
48 bool Accept(char aChr);
49 bool Accept(bool (*aClassifier) (signed char));
50 void Expect(char aChr);
51 void Advance();
52 void Header(); // header = [ directive ] *( ";" [ directive ] )
53 void Directive(); // directive = directive-name [ "=" directive-value ]
54 void DirectiveName(); // directive-name = token
55 void DirectiveValue(); // directive-value = token | quoted-string
56 void Token(); // token = 1*<any CHAR except CTLs or separators>
57 void QuotedString(); // quoted-string = (<"> *( qdtext | quoted-pair ) <">)
58 void QuotedText(); // qdtext = <any TEXT except <"> and "\">
59 void QuotedPair(); // quoted-pair = "\" CHAR
61 // LWS = [CRLF] 1*( SP | HT )
62 void LWSMultiple(); // Handles *( LWS )
63 void LWSCRLF(); // Handles the [CRLF] part of LWS
64 void LWS(); // Handles the 1*( SP | HT ) part of LWS
66 mozilla::LinkedList<nsSecurityHeaderDirective> mDirectives;
67 const char *mCursor;
68 nsSecurityHeaderDirective *mDirective;
70 nsAutoCString mOutput;
71 bool mError;
72 };
74 #endif /* nsSecurityHeaderParser_h__ */