]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/url.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // url - URL object with decomposition
23 #include <CoreFoundation/CFURL.h>
24 #include <CoreFoundation/CFString.h>
33 // Turn a CFStringRef into an STL string and release the incoming CFStringRef
35 static string
mkstr(CFStringRef str
)
40 if (CFStringGetCString(str
, buffer
, sizeof(buffer
), kCFStringEncodingUTF8
))
43 UnixError::throwMe(EINVAL
);
55 URL::URL(const char *s
)
57 ref
= CFURLCreateWithBytes(NULL
, (const UInt8
*)s
, strlen(s
), kCFStringEncodingUTF8
, NULL
);
59 UnixError::throwMe(EINVAL
);
62 URL::URL(const char *s
, const URL
&base
)
64 ref
= CFURLCreateWithBytes(NULL
, (const UInt8
*)s
, strlen(s
), kCFStringEncodingUTF8
, base
.ref
);
66 UnixError::throwMe(EINVAL
);
77 // Extraction: These methods produce UTF8 strings
79 URL::operator string() const
81 return mkstr(CFURLGetString(ref
));
84 string
URL::scheme() const
86 return mkstr(CFURLCopyScheme(ref
));
89 string
URL::host() const
91 return mkstr(CFURLCopyHostName(ref
));
94 IPPort
URL::port(IPPort defaultPort
) const
96 SInt32 port
= CFURLGetPortNumber(ref
);
97 return (port
== -1) ? defaultPort
: port
;
100 string
URL::username() const
102 return mkstr(CFURLCopyUserName(ref
));
105 string
URL::password() const
107 return mkstr(CFURLCopyPassword(ref
));
110 string
URL::path() const
113 return "/" + mkstr(CFURLCopyStrictPath(ref
, &isAbsolute
));
116 string
URL::resourceSpec() const
118 return mkstr(CFURLCopyResourceSpecifier(ref
));
121 string
URL::fullPath() const
123 return path() + resourceSpec();
126 string
URL::basename() const
128 return mkstr(CFURLCopyLastPathComponent(ref
));
131 string
URL::extension() const
133 return mkstr(CFURLCopyPathExtension(ref
));
136 void URL::recreateURL(const char* url
)
140 ref
= CFURLCreateWithBytes(NULL
, (const UInt8
*)url
, strlen(url
), kCFStringEncodingUTF8
, NULL
);
143 } // end namespace Network
144 } // end namespace Security