michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsSecurityHeaderParser_h__ michael@0: #define nsSecurityHeaderParser_h__ michael@0: michael@0: #include "nsString.h" michael@0: #include "mozilla/LinkedList.h" michael@0: #include "nsCOMPtr.h" michael@0: michael@0: // Utility class for handing back parsed directives and (optional) values michael@0: class nsSecurityHeaderDirective : public mozilla::LinkedListElement { michael@0: public: michael@0: nsAutoCString mName; michael@0: nsAutoCString mValue; michael@0: }; michael@0: michael@0: // This class parses security-related HTTP headers like michael@0: // Strict-Transport-Security. The Augmented Backus-Naur Form syntax for this michael@0: // header is reproduced below, for reference: michael@0: // michael@0: // Strict-Transport-Security = "Strict-Transport-Security" ":" michael@0: // [ directive ] *( ";" [ directive ] ) michael@0: // michael@0: // directive = directive-name [ "=" directive-value ] michael@0: // directive-name = token michael@0: // directive-value = token | quoted-string michael@0: // michael@0: // where: michael@0: // michael@0: // token = michael@0: // quoted-string = / michael@0: // michael@0: // For further reference, see [RFC6797], Section 6.1 michael@0: michael@0: class nsSecurityHeaderParser { michael@0: public: michael@0: explicit nsSecurityHeaderParser(const char *aHeader); michael@0: ~nsSecurityHeaderParser(); michael@0: michael@0: // Only call Parse once. michael@0: nsresult Parse(); michael@0: // The caller does not take ownership of the memory returned here. michael@0: mozilla::LinkedList *GetDirectives(); michael@0: michael@0: private: michael@0: bool Accept(char aChr); michael@0: bool Accept(bool (*aClassifier) (signed char)); michael@0: void Expect(char aChr); michael@0: void Advance(); michael@0: void Header(); // header = [ directive ] *( ";" [ directive ] ) michael@0: void Directive(); // directive = directive-name [ "=" directive-value ] michael@0: void DirectiveName(); // directive-name = token michael@0: void DirectiveValue(); // directive-value = token | quoted-string michael@0: void Token(); // token = 1* michael@0: void QuotedString(); // quoted-string = (<"> *( qdtext | quoted-pair ) <">) michael@0: void QuotedText(); // qdtext = and "\"> michael@0: void QuotedPair(); // quoted-pair = "\" CHAR michael@0: michael@0: // LWS = [CRLF] 1*( SP | HT ) michael@0: void LWSMultiple(); // Handles *( LWS ) michael@0: void LWSCRLF(); // Handles the [CRLF] part of LWS michael@0: void LWS(); // Handles the 1*( SP | HT ) part of LWS michael@0: michael@0: mozilla::LinkedList mDirectives; michael@0: const char *mCursor; michael@0: nsSecurityHeaderDirective *mDirective; michael@0: michael@0: nsAutoCString mOutput; michael@0: bool mError; michael@0: }; michael@0: michael@0: #endif /* nsSecurityHeaderParser_h__ */