1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/manager/boot/src/nsSecurityHeaderParser.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,74 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef nsSecurityHeaderParser_h__ 1.9 +#define nsSecurityHeaderParser_h__ 1.10 + 1.11 +#include "nsString.h" 1.12 +#include "mozilla/LinkedList.h" 1.13 +#include "nsCOMPtr.h" 1.14 + 1.15 +// Utility class for handing back parsed directives and (optional) values 1.16 +class nsSecurityHeaderDirective : public mozilla::LinkedListElement<nsSecurityHeaderDirective> { 1.17 +public: 1.18 + nsAutoCString mName; 1.19 + nsAutoCString mValue; 1.20 +}; 1.21 + 1.22 +// This class parses security-related HTTP headers like 1.23 +// Strict-Transport-Security. The Augmented Backus-Naur Form syntax for this 1.24 +// header is reproduced below, for reference: 1.25 +// 1.26 +// Strict-Transport-Security = "Strict-Transport-Security" ":" 1.27 +// [ directive ] *( ";" [ directive ] ) 1.28 +// 1.29 +// directive = directive-name [ "=" directive-value ] 1.30 +// directive-name = token 1.31 +// directive-value = token | quoted-string 1.32 +// 1.33 +// where: 1.34 +// 1.35 +// token = <token, defined in [RFC2616], Section 2.2> 1.36 +// quoted-string = <quoted-string, defined in [RFC2616], Section 2.2>/ 1.37 +// 1.38 +// For further reference, see [RFC6797], Section 6.1 1.39 + 1.40 +class nsSecurityHeaderParser { 1.41 +public: 1.42 + explicit nsSecurityHeaderParser(const char *aHeader); 1.43 + ~nsSecurityHeaderParser(); 1.44 + 1.45 + // Only call Parse once. 1.46 + nsresult Parse(); 1.47 + // The caller does not take ownership of the memory returned here. 1.48 + mozilla::LinkedList<nsSecurityHeaderDirective> *GetDirectives(); 1.49 + 1.50 +private: 1.51 + bool Accept(char aChr); 1.52 + bool Accept(bool (*aClassifier) (signed char)); 1.53 + void Expect(char aChr); 1.54 + void Advance(); 1.55 + void Header(); // header = [ directive ] *( ";" [ directive ] ) 1.56 + void Directive(); // directive = directive-name [ "=" directive-value ] 1.57 + void DirectiveName(); // directive-name = token 1.58 + void DirectiveValue(); // directive-value = token | quoted-string 1.59 + void Token(); // token = 1*<any CHAR except CTLs or separators> 1.60 + void QuotedString(); // quoted-string = (<"> *( qdtext | quoted-pair ) <">) 1.61 + void QuotedText(); // qdtext = <any TEXT except <"> and "\"> 1.62 + void QuotedPair(); // quoted-pair = "\" CHAR 1.63 + 1.64 + // LWS = [CRLF] 1*( SP | HT ) 1.65 + void LWSMultiple(); // Handles *( LWS ) 1.66 + void LWSCRLF(); // Handles the [CRLF] part of LWS 1.67 + void LWS(); // Handles the 1*( SP | HT ) part of LWS 1.68 + 1.69 + mozilla::LinkedList<nsSecurityHeaderDirective> mDirectives; 1.70 + const char *mCursor; 1.71 + nsSecurityHeaderDirective *mDirective; 1.72 + 1.73 + nsAutoCString mOutput; 1.74 + bool mError; 1.75 +}; 1.76 + 1.77 +#endif /* nsSecurityHeaderParser_h__ */