2 * Copyright (c) 2000-2002,2004,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // url - URL object with decomposition
29 #include <CoreFoundation/CFURL.h>
30 #include <CoreFoundation/CFString.h>
32 #include <utilities/SecCFRelease.h>
40 // Turn a CFStringRef into an STL string and release the incoming CFStringRef
42 static string
mkstr(CFStringRef CF_CONSUMED str
)
47 if (CFStringGetCString(str
, buffer
, sizeof(buffer
), kCFStringEncodingUTF8
)) {
51 UnixError::throwMe(EINVAL
);
63 URL::URL(const char *s
)
65 ref
= CFURLCreateWithBytes(NULL
, (const UInt8
*)s
, strlen(s
), kCFStringEncodingUTF8
, NULL
);
67 UnixError::throwMe(EINVAL
);
70 URL::URL(const char *s
, const URL
&base
)
72 ref
= CFURLCreateWithBytes(NULL
, (const UInt8
*)s
, strlen(s
), kCFStringEncodingUTF8
, base
.ref
);
74 UnixError::throwMe(EINVAL
);
85 // Extraction: These methods produce UTF8 strings
87 URL::operator string() const
89 return mkstr(CFRetainSafe(CFURLGetString(ref
)));
92 string
URL::scheme() const
94 return mkstr(CFURLCopyScheme(ref
));
97 string
URL::host() const
99 return mkstr(CFURLCopyHostName(ref
));
102 IPPort
URL::port(IPPort defaultPort
) const
104 SInt32 port
= CFURLGetPortNumber(ref
);
105 return (port
== -1) ? defaultPort
: port
;
108 string
URL::username() const
110 return mkstr(CFURLCopyUserName(ref
));
113 string
URL::password() const
115 return mkstr(CFURLCopyPassword(ref
));
118 string
URL::path() const
121 return "/" + mkstr(CFURLCopyStrictPath(ref
, &isAbsolute
));
124 string
URL::resourceSpec() const
126 return mkstr(CFURLCopyResourceSpecifier(ref
));
129 string
URL::fullPath() const
131 return path() + resourceSpec();
134 string
URL::basename() const
136 return mkstr(CFURLCopyLastPathComponent(ref
));
139 string
URL::extension() const
141 return mkstr(CFURLCopyPathExtension(ref
));
144 void URL::recreateURL(const char* url
)
148 ref
= CFURLCreateWithBytes(NULL
, (const UInt8
*)url
, strlen(url
), kCFStringEncodingUTF8
, NULL
);
151 } // end namespace Network
152 } // end namespace Security