http_auth_challenge_tokenizer.cc 2.0 KB

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