blob: 2130667e41b197b98ccbc1761606dbe2c1763eec [file] [log] [blame]
Upstreamd8543bb1970-01-12 13:46:40 +00001/*
The Android Open Source Project9364f222008-10-21 07:00:00 -07002 * Copyright (C) 2004 Apple Computer, Inc. All rights reserved.
Upstreamd8543bb1970-01-12 13:46:40 +00003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "KURL.h"
28
29#include <wtf/RetainPtr.h>
The Android Open Source Project9364f222008-10-21 07:00:00 -070030#include <wtf/Vector.h>
Upstreamd8543bb1970-01-12 13:46:40 +000031#include <CoreFoundation/CFURL.h>
32
33using namespace std;
34
35namespace WebCore {
36
37KURL::KURL(CFURLRef url)
38{
The Android Open Source Project9364f222008-10-21 07:00:00 -070039 if (url) {
40 CFIndex bytesLength = CFURLGetBytes(url, 0, 0);
41 Vector<char, 2048> buffer(bytesLength + 6); // 6 for "file:", 1 for NUL terminator
42 char* bytes = &buffer[5];
43 CFURLGetBytes(url, (UInt8*)bytes, bytesLength);
44 bytes[bytesLength] = '\0';
45 if (bytes[0] == '/') {
46 buffer[0] = 'f';
47 buffer[1] = 'i';
48 buffer[2] = 'l';
49 buffer[3] = 'e';
50 buffer[4] = ':';
51 parse(buffer.data(), 0);
52 } else
53 parse(bytes, 0);
54 } else
55 parse("", 0);
Upstreamd8543bb1970-01-12 13:46:40 +000056}
57
58CFURLRef KURL::createCFURL() const
59{
The Android Open Source Project9364f222008-10-21 07:00:00 -070060 const UInt8 *bytes = (const UInt8 *)urlString.latin1();
Upstreamd8543bb1970-01-12 13:46:40 +000061 // NOTE: We use UTF-8 here since this encoding is used when computing strings when returning URL components
62 // (e.g calls to NSURL -path). However, this function is not tolerant of illegal UTF-8 sequences, which
63 // could either be a malformed string or bytes in a different encoding, like Shift-JIS, so we fall back
64 // onto using ISO Latin-1 in those cases.
The Android Open Source Project9364f222008-10-21 07:00:00 -070065 CFURLRef result = CFURLCreateAbsoluteURLWithBytes(0, bytes, urlString.length(), kCFStringEncodingUTF8, 0, true);
Upstreamd8543bb1970-01-12 13:46:40 +000066 if (!result)
The Android Open Source Project9364f222008-10-21 07:00:00 -070067 result = CFURLCreateAbsoluteURLWithBytes(0, bytes, urlString.length(), kCFStringEncodingISOLatin1, 0, true);
Upstreamd8543bb1970-01-12 13:46:40 +000068 return result;
69}
70
71String KURL::fileSystemPath() const
72{
73 RetainPtr<CFURLRef> cfURL(AdoptCF, createCFURL());
74 if (!cfURL)
75 return String();
76
77#if PLATFORM(WIN)
78 CFURLPathStyle pathStyle = kCFURLWindowsPathStyle;
79#else
80 CFURLPathStyle pathStyle = kCFURLPOSIXPathStyle;
81#endif
The Android Open Source Project9364f222008-10-21 07:00:00 -070082
83 RetainPtr<CFStringRef> path(AdoptCF, CFURLCopyFileSystemPath(cfURL.get(), pathStyle));
84 return path.get();
Upstreamd8543bb1970-01-12 13:46:40 +000085}
86
87}