summaryrefslogtreecommitdiff
path: root/chromium/net/base/mac/url_conversions.mm
blob: 3171c6fde6538a6051f218c3e585478dcd82f7db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "net/base/mac/url_conversions.h"

#import <Foundation/Foundation.h>

#include "base/mac/scoped_nsobject.h"
#include "net/base/escape.h"
#include "url/gurl.h"
#include "url/url_canon.h"

namespace net {

NSURL* NSURLWithGURL(const GURL& url) {
  if (!url.is_valid())
    return nil;

  // NSURL strictly enforces RFC 1738 which requires that certain characters
  // are always encoded. These characters are: "<", ">", """, "#", "%", "{",
  // "}", "|", "\", "^", "~", "[", "]", and "`".
  //
  // GURL leaves some of these characters unencoded in the path, query, and
  // ref. This function manually encodes those components, and then passes the
  // result to NSURL.
  GURL::Replacements replacements;
  std::string escaped_path = EscapeNSURLPrecursor(url.path());
  std::string escaped_query = EscapeNSURLPrecursor(url.query());
  std::string escaped_ref = EscapeNSURLPrecursor(url.ref());
  if (!escaped_path.empty()) {
    replacements.SetPath(escaped_path.c_str(),
                         url::Component(0, escaped_path.size()));
  }
  if (!escaped_query.empty()) {
    replacements.SetQuery(escaped_query.c_str(),
                          url::Component(0, escaped_query.size()));
  }
  if (!escaped_ref.empty()) {
    replacements.SetRef(escaped_ref.c_str(),
                        url::Component(0, escaped_ref.size()));
  }
  GURL escaped_url = url.ReplaceComponents(replacements);

  base::scoped_nsobject<NSString> escaped_url_string(
      [[NSString alloc] initWithUTF8String:escaped_url.spec().c_str()]);
  return [NSURL URLWithString:escaped_url_string];
}

GURL GURLWithNSURL(NSURL* url) {
  if (url)
    return GURL([[url absoluteString] UTF8String]);
  return GURL();
}

}  // namespace net