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 "base/strings/string_piece.h"
8 #include "base/strings/string_tokenizer.h"
9 #include "base/strings/string_util.h"
10
11 namespace net {
12
HttpAuthChallengeTokenizer(std::string::const_iterator begin,std::string::const_iterator end)13 HttpAuthChallengeTokenizer::HttpAuthChallengeTokenizer(
14 std::string::const_iterator begin,
15 std::string::const_iterator end)
16 : begin_(begin),
17 end_(end),
18 params_begin_(end),
19 params_end_(end) {
20 Init(begin, end);
21 }
22
23 HttpAuthChallengeTokenizer::~HttpAuthChallengeTokenizer() = default;
24
param_pairs() const25 HttpUtil::NameValuePairsIterator HttpAuthChallengeTokenizer::param_pairs()
26 const {
27 return HttpUtil::NameValuePairsIterator(params_begin_, params_end_, ',');
28 }
29
base64_param() const30 std::string HttpAuthChallengeTokenizer::base64_param() const {
31 // Strip off any padding.
32 // (See https://bugzilla.mozilla.org/show_bug.cgi?id=230351.)
33 //
34 // Our base64 decoder requires that the length be a multiple of 4.
35 auto encoded_length = params_end_ - params_begin_;
36 while (encoded_length > 0 && encoded_length % 4 != 0 &&
37 params_begin_[encoded_length - 1] == '=') {
38 --encoded_length;
39 }
40 return std::string(params_begin_, params_begin_ + encoded_length);
41 }
42
Init(std::string::const_iterator begin,std::string::const_iterator end)43 void HttpAuthChallengeTokenizer::Init(std::string::const_iterator begin,
44 std::string::const_iterator end) {
45 // The first space-separated token is the auth-scheme.
46 // NOTE: we are more permissive than RFC 2617 which says auth-scheme
47 // is separated by 1*SP.
48 base::StringTokenizer tok(begin, end, HTTP_LWS);
49 if (!tok.GetNext()) {
50 // Default param and scheme iterators provide empty strings
51 return;
52 }
53
54 // Save the scheme's position.
55 lower_case_scheme_ = base::ToLowerASCII(
56 base::MakeStringPiece(tok.token_begin(), tok.token_end()));
57
58 params_begin_ = tok.token_end();
59 params_end_ = end;
60 HttpUtil::TrimLWS(¶ms_begin_, ¶ms_end_);
61 }
62
63 } // namespace net
64