@@ -85,69 +85,103 @@ public static SIPAuthorisationDigest ParseAuthorisationDigest(SIPAuthorisationHe
8585 {
8686 SIPAuthorisationDigest authRequest = new SIPAuthorisationDigest ( authorisationType ) ;
8787
88- string noDigestHeader = Regex . Replace ( authorisationRequest , $@ "^\s*{ METHOD } \s*", "" , RegexOptions . IgnoreCase ) ;
89- string [ ] headerFields = noDigestHeader . Split ( ',' ) ;
88+ //string noDigestHeader = Regex.Replace(authorisationRequest, $@"^\s*{METHOD}\s*", "", RegexOptions.IgnoreCase);
89+ ReadOnlySpan < char > noDigestHeader = authorisationRequest . AsSpan ( ) . Trim ( ) ;
90+ if ( noDigestHeader . StartsWith ( METHOD . AsSpan ( ) , StringComparison . OrdinalIgnoreCase ) )
91+ {
92+ noDigestHeader = noDigestHeader . Slice ( METHOD . Length + 1 ) ;
93+ }
9094
91- if ( headerFields != null && headerFields . Length > 0 )
95+ while ( noDigestHeader . IsEmpty == false )
9296 {
93- foreach ( string headerField in headerFields )
97+ ReadOnlySpan < char > headerField ;
98+ int commaIndex = noDigestHeader . IndexOf ( ',' ) ;
99+ if ( commaIndex == - 1 )
100+ {
101+ headerField = noDigestHeader . Trim ( ) ;
102+ noDigestHeader = ReadOnlySpan < char > . Empty ;
103+ }
104+ else
105+ {
106+ headerField = noDigestHeader . Slice ( 0 , commaIndex ) . Trim ( ) ;
107+ noDigestHeader = noDigestHeader . Slice ( commaIndex + 1 ) ;
108+ }
109+
110+ int equalsIndex = headerField . IndexOf ( '=' ) ;
111+
112+ if ( equalsIndex != - 1 && equalsIndex < headerField . Length )
94113 {
95- int equalsIndex = headerField . IndexOf ( '=' ) ;
114+ ReadOnlySpan < char > headerName = headerField . Slice ( 0 , equalsIndex ) . Trim ( ) ;
115+ ReadOnlySpan < char > headerValue = headerField . Slice ( equalsIndex + 1 ) . Trim ( m_headerFieldRemoveChars ) ;
96116
97- if ( equalsIndex != - 1 && equalsIndex < headerField . Length )
117+ if ( headerName . Equals ( AuthHeaders . AUTH_REALM_KEY . AsSpan ( ) , StringComparison . OrdinalIgnoreCase ) )
118+ {
119+ authRequest . Realm = headerValue . ToString ( ) ;
120+ }
121+ else if ( headerName . Equals ( AuthHeaders . AUTH_NONCE_KEY . AsSpan ( ) , StringComparison . OrdinalIgnoreCase ) )
122+ {
123+ authRequest . Nonce = headerValue . ToString ( ) ;
124+ }
125+ else if ( headerName . Equals ( AuthHeaders . AUTH_USERNAME_KEY . AsSpan ( ) , StringComparison . OrdinalIgnoreCase ) )
126+ {
127+ authRequest . Username = headerValue . ToString ( ) ;
128+ }
129+ else if ( headerName . Equals ( AuthHeaders . AUTH_RESPONSE_KEY . AsSpan ( ) , StringComparison . OrdinalIgnoreCase ) )
130+ {
131+ authRequest . Response = headerValue . ToString ( ) ;
132+ }
133+ else if ( headerName . Equals ( AuthHeaders . AUTH_URI_KEY . AsSpan ( ) , StringComparison . OrdinalIgnoreCase ) )
134+ {
135+ authRequest . URI = headerValue . ToString ( ) ;
136+ }
137+ else if ( headerName . Equals ( AuthHeaders . AUTH_CNONCE_KEY . AsSpan ( ) , StringComparison . OrdinalIgnoreCase ) )
138+ {
139+ authRequest . Cnonce = headerValue . ToString ( ) ;
140+ }
141+ else if ( headerName . Equals ( AuthHeaders . AUTH_NONCECOUNT_KEY . AsSpan ( ) , StringComparison . OrdinalIgnoreCase ) )
98142 {
99- string headerName = headerField . Substring ( 0 , equalsIndex ) . Trim ( ) ;
100- string headerValue = headerField . Substring ( equalsIndex + 1 ) . Trim ( m_headerFieldRemoveChars ) ;
143+ #if NETCOREAPP2_1_OR_GREATER
144+ Int32 . TryParse ( headerValue , out authRequest . NonceCount ) ;
145+ #else
146+ Int32 . TryParse ( headerValue . ToString ( ) , out authRequest . NonceCount ) ;
147+ #endif
148+ }
149+ else if ( headerName . Equals ( AuthHeaders . AUTH_QOP_KEY . AsSpan ( ) , StringComparison . OrdinalIgnoreCase ) )
150+ {
151+ #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
152+ authRequest . Qop = BuildQop ( headerValue ) ;
101153
102- if ( Regex . Match ( headerName , "^" + AuthHeaders . AUTH_REALM_KEY + "$" , RegexOptions . IgnoreCase ) . Success )
103- {
104- authRequest . Realm = headerValue ;
105- }
106- else if ( Regex . Match ( headerName , "^" + AuthHeaders . AUTH_NONCE_KEY + "$" , RegexOptions . IgnoreCase ) . Success )
107- {
108- authRequest . Nonce = headerValue ;
109- }
110- else if ( Regex . Match ( headerName , "^" + AuthHeaders . AUTH_USERNAME_KEY + "$" , RegexOptions . IgnoreCase ) . Success )
111- {
112- authRequest . Username = headerValue ;
113- }
114- else if ( Regex . Match ( headerName , "^" + AuthHeaders . AUTH_RESPONSE_KEY + "$" , RegexOptions . IgnoreCase ) . Success )
115- {
116- authRequest . Response = headerValue ;
117- }
118- else if ( Regex . Match ( headerName , "^" + AuthHeaders . AUTH_URI_KEY + "$" , RegexOptions . IgnoreCase ) . Success )
154+ static string BuildQop ( ReadOnlySpan < char > headerValue )
119155 {
120- authRequest . URI = headerValue ;
156+ Span < char > chars = stackalloc char [ headerValue . Length ] ;
157+ _ = headerValue . ToLowerInvariant ( chars ) ;
158+ return new string ( ( ReadOnlySpan < char > ) chars ) ;
121159 }
122- else if ( Regex . Match ( headerName , "^" + AuthHeaders . AUTH_CNONCE_KEY + "$" , RegexOptions . IgnoreCase ) . Success )
123- {
124- authRequest . Cnonce = headerValue ;
125- }
126- else if ( Regex . Match ( headerName , "^" + AuthHeaders . AUTH_NONCECOUNT_KEY + "$" , RegexOptions . IgnoreCase ) . Success )
127- {
128- Int32 . TryParse ( headerValue , out authRequest . NonceCount ) ;
129- }
130- else if ( Regex . Match ( headerName , "^" + AuthHeaders . AUTH_QOP_KEY + "$" , RegexOptions . IgnoreCase ) . Success )
131- {
132- authRequest . Qop = headerValue . ToLower ( ) ;
133- }
134- else if ( Regex . Match ( headerName , "^" + AuthHeaders . AUTH_OPAQUE_KEY + "$" , RegexOptions . IgnoreCase ) . Success )
160+ #else
161+ authRequest . Qop = headerValue . ToString ( ) . ToLowerInvariant ( ) ;
162+ #endif
163+ }
164+ else if ( headerName . Equals ( AuthHeaders . AUTH_OPAQUE_KEY . AsSpan ( ) , StringComparison . OrdinalIgnoreCase ) )
165+ {
166+ authRequest . Opaque = headerValue . ToString ( ) ;
167+ }
168+ else if ( headerName . Equals ( AuthHeaders . AUTH_ALGORITHM_KEY . AsSpan ( ) , StringComparison . OrdinalIgnoreCase ) )
169+ {
170+ //authRequest.Algorithm = headerValue;
171+
172+ #if NETCOREAPP6_0_OR_GREATER
173+ var hv = headerValue . Replace ( "-" , "" ) ;
174+ #else
175+ var hv = headerValue . ToString ( ) . Replace ( "-" , "" ) ;
176+ #endif
177+ if ( Enum . TryParse < DigestAlgorithmsEnum > ( hv , true , out var alg ) )
135178 {
136- authRequest . Opaque = headerValue ;
179+ authRequest . DigestAlgorithm = alg ;
137180 }
138- else if ( Regex . Match ( headerName , "^" + AuthHeaders . AUTH_ALGORITHM_KEY + "$" , RegexOptions . IgnoreCase ) . Success )
181+ else
139182 {
140- //authRequest.Algorithhm = headerValue;
141-
142- if ( Enum . TryParse < DigestAlgorithmsEnum > ( headerValue . Replace ( "-" , "" ) , true , out var alg ) )
143- {
144- authRequest . DigestAlgorithm = alg ;
145- }
146- else
147- {
148- logger . LogWarning ( "SIPAuthorisationDigest did not recognised digest algorithm value of {DigestAlgorithms}, defaulting to {DigestAlgorithmsEnumMD5}." , headerValue , DigestAlgorithmsEnum . MD5 ) ;
149- authRequest . DigestAlgorithm = DigestAlgorithmsEnum . MD5 ;
150- }
183+ logger . LogWarning ( "SIPAuthorisationDigest did not recognised digest algorithm value of {DigestAlgorithms}, defaulting to {DigestAlgorithmsEnumMD5}." , headerValue . ToString ( ) , DigestAlgorithmsEnum . MD5 ) ;
184+ authRequest . DigestAlgorithm = DigestAlgorithmsEnum . MD5 ;
151185 }
152186 }
153187 }
0 commit comments