Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 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 // NOTE: based loosely on mozilla's nsDataChannel.cpp
      6 
      7 #include <algorithm>
      8 
      9 #include "net/base/data_url.h"
     10 
     11 #include "base/base64.h"
     12 #include "base/basictypes.h"
     13 #include "base/strings/string_split.h"
     14 #include "base/strings/string_util.h"
     15 #include "net/base/escape.h"
     16 #include "net/base/mime_util.h"
     17 #include "url/gurl.h"
     18 
     19 namespace net {
     20 
     21 // static
     22 bool DataURL::Parse(const GURL& url, std::string* mime_type,
     23                     std::string* charset, std::string* data) {
     24   DCHECK(mime_type->empty());
     25   DCHECK(charset->empty());
     26   std::string::const_iterator begin = url.spec().begin();
     27   std::string::const_iterator end = url.spec().end();
     28 
     29   std::string::const_iterator after_colon = std::find(begin, end, ':');
     30   if (after_colon == end)
     31     return false;
     32   ++after_colon;
     33 
     34   std::string::const_iterator comma = std::find(after_colon, end, ',');
     35   if (comma == end)
     36     return false;
     37 
     38   std::vector<std::string> meta_data;
     39   std::string unparsed_meta_data(after_colon, comma);
     40   base::SplitString(unparsed_meta_data, ';', &meta_data);
     41 
     42   std::vector<std::string>::iterator iter = meta_data.begin();
     43   if (iter != meta_data.end()) {
     44     mime_type->swap(*iter);
     45     StringToLowerASCII(mime_type);
     46     ++iter;
     47   }
     48 
     49   static const char kBase64Tag[] = "base64";
     50   static const char kCharsetTag[] = "charset=";
     51   const size_t kCharsetTagLength = arraysize(kCharsetTag) - 1;
     52 
     53   bool base64_encoded = false;
     54   for (; iter != meta_data.end(); ++iter) {
     55     if (!base64_encoded && *iter == kBase64Tag) {
     56       base64_encoded = true;
     57     } else if (charset->empty() &&
     58                iter->compare(0, kCharsetTagLength, kCharsetTag) == 0) {
     59       charset->assign(iter->substr(kCharsetTagLength));
     60     }
     61   }
     62 
     63   if (mime_type->empty()) {
     64     // fallback to defaults if nothing specified in the URL:
     65     mime_type->assign("text/plain");
     66   } else if (!ParseMimeTypeWithoutParameter(*mime_type, NULL, NULL)) {
     67     return false;
     68   }
     69   if (charset->empty())
     70     charset->assign("US-ASCII");
     71 
     72   // The caller may not be interested in receiving the data.
     73   if (!data)
     74     return true;
     75 
     76   // Preserve spaces if dealing with text or xml input, same as mozilla:
     77   //   https://bugzilla.mozilla.org/show_bug.cgi?id=138052
     78   // but strip them otherwise:
     79   //   https://bugzilla.mozilla.org/show_bug.cgi?id=37200
     80   // (Spaces in a data URL should be escaped, which is handled below, so any
     81   // spaces now are wrong. People expect to be able to enter them in the URL
     82   // bar for text, and it can't hurt, so we allow it.)
     83   std::string temp_data = std::string(comma + 1, end);
     84 
     85   // For base64, we may have url-escaped whitespace which is not part
     86   // of the data, and should be stripped. Otherwise, the escaped whitespace
     87   // could be part of the payload, so don't strip it.
     88   if (base64_encoded) {
     89     temp_data = UnescapeURLComponent(temp_data,
     90         UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS |
     91         UnescapeRule::CONTROL_CHARS);
     92   }
     93 
     94   // Strip whitespace.
     95   if (base64_encoded || !(mime_type->compare(0, 5, "text/") == 0 ||
     96                           mime_type->find("xml") != std::string::npos)) {
     97     temp_data.erase(std::remove_if(temp_data.begin(), temp_data.end(),
     98                                    IsAsciiWhitespace<wchar_t>),
     99                     temp_data.end());
    100   }
    101 
    102   if (!base64_encoded) {
    103     temp_data = UnescapeURLComponent(temp_data,
    104         UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS |
    105         UnescapeRule::CONTROL_CHARS);
    106   }
    107 
    108   if (base64_encoded) {
    109     size_t length = temp_data.length();
    110     size_t padding_needed = 4 - (length % 4);
    111     // If the input wasn't padded, then we pad it as necessary until we have a
    112     // length that is a multiple of 4 as required by our decoder. We don't
    113     // correct if the input was incorrectly padded. If |padding_needed| == 3,
    114     // then the input isn't well formed and decoding will fail with or without
    115     // padding.
    116     if ((padding_needed == 1 || padding_needed == 2) &&
    117         temp_data[length - 1] != '=') {
    118       temp_data.resize(length + padding_needed, '=');
    119     }
    120     return base::Base64Decode(temp_data, data);
    121   }
    122 
    123   temp_data.swap(*data);
    124   return true;
    125 }
    126 
    127 }  // namespace net
    128