1 // Copyright 2014 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "net/http/http_auth_challenge_tokenizer.h" 6 7 #include <string_view> 8 9 #include "base/strings/string_tokenizer.h" 10 #include "base/strings/string_util.h" 11 12 namespace net { 13 HttpAuthChallengeTokenizer(std::string_view challenge)14HttpAuthChallengeTokenizer::HttpAuthChallengeTokenizer( 15 std::string_view challenge) 16 : challenge_(challenge) { 17 Init(challenge); 18 } 19 20 HttpAuthChallengeTokenizer::~HttpAuthChallengeTokenizer() = default; 21 param_pairs() const22HttpUtil::NameValuePairsIterator HttpAuthChallengeTokenizer::param_pairs() 23 const { 24 return HttpUtil::NameValuePairsIterator(params_, /*delimiter=*/','); 25 } 26 base64_param() const27std::string_view HttpAuthChallengeTokenizer::base64_param() const { 28 // Strip off any padding. 29 // (See https://bugzilla.mozilla.org/show_bug.cgi?id=230351.) 30 // 31 // Our base64 decoder requires that the length be a multiple of 4. 32 auto encoded_length = params_.length(); 33 while (encoded_length > 0 && encoded_length % 4 != 0 && 34 params_[encoded_length - 1] == '=') { 35 --encoded_length; 36 } 37 return params_.substr(0, encoded_length); 38 } 39 Init(std::string_view challenge)40void HttpAuthChallengeTokenizer::Init(std::string_view challenge) { 41 // The first space-separated token is the auth-scheme. 42 // NOTE: we are more permissive than RFC 2617 which says auth-scheme 43 // is separated by 1*SP. 44 base::StringViewTokenizer tok(challenge, HTTP_LWS); 45 if (!tok.GetNext()) { 46 // Default param and scheme iterators provide empty strings 47 return; 48 } 49 50 // Save the scheme's position. 51 lower_case_scheme_ = base::ToLowerASCII( 52 base::MakeStringPiece(tok.token_begin(), tok.token_end())); 53 54 params_ = 55 HttpUtil::TrimLWS(std::string_view(tok.token_end(), challenge.end())); 56 } 57 58 } // namespace net 59