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