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