]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/url.cpp
Security-54.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / url.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // url - URL object with decomposition
21 //
22 #include "url.h"
23 #include <CoreFoundation/CFURL.h>
24 #include <CoreFoundation/CFString.h>
25 #include <errno.h>
26
27
28 namespace Security {
29 namespace Network {
30
31
32 //
33 // Turn a CFStringRef into an STL string and release the incoming CFStringRef
34 //
35 static string mkstr(CFStringRef str)
36 {
37 if (!str)
38 return "";
39 char buffer[2048];
40 if (CFStringGetCString(str, buffer, sizeof(buffer), kCFStringEncodingUTF8))
41 return buffer;
42 else
43 UnixError::throwMe(EINVAL);
44 }
45
46
47 //
48 // Construction
49 //
50 URL::URL()
51 {
52 ref = NULL;
53 }
54
55 URL::URL(const char *s)
56 {
57 ref = CFURLCreateWithBytes(NULL, (const UInt8 *)s, strlen(s), kCFStringEncodingUTF8, NULL);
58 if (!ref)
59 UnixError::throwMe(EINVAL);
60 }
61
62 URL::URL(const char *s, const URL &base)
63 {
64 ref = CFURLCreateWithBytes(NULL, (const UInt8 *)s, strlen(s), kCFStringEncodingUTF8, base.ref);
65 if (!ref)
66 UnixError::throwMe(EINVAL);
67 }
68
69 URL::~URL()
70 {
71 if (ref)
72 CFRelease(ref);
73 }
74
75
76 //
77 // Extraction: These methods produce UTF8 strings
78 //
79 URL::operator string() const
80 {
81 return mkstr(CFURLGetString(ref));
82 }
83
84 string URL::scheme() const
85 {
86 return mkstr(CFURLCopyScheme(ref));
87 }
88
89 string URL::host() const
90 {
91 return mkstr(CFURLCopyHostName(ref));
92 }
93
94 IPPort URL::port(IPPort defaultPort) const
95 {
96 SInt32 port = CFURLGetPortNumber(ref);
97 return (port == -1) ? defaultPort : port;
98 }
99
100 string URL::username() const
101 {
102 return mkstr(CFURLCopyUserName(ref));
103 }
104
105 string URL::password() const
106 {
107 return mkstr(CFURLCopyPassword(ref));
108 }
109
110 string URL::path() const
111 {
112 Boolean isAbsolute;
113 return "/" + mkstr(CFURLCopyStrictPath(ref, &isAbsolute));
114 }
115
116 string URL::resourceSpec() const
117 {
118 return mkstr(CFURLCopyResourceSpecifier(ref));
119 }
120
121 string URL::fullPath() const
122 {
123 return path() + resourceSpec();
124 }
125
126 string URL::basename() const
127 {
128 return mkstr(CFURLCopyLastPathComponent(ref));
129 }
130
131 string URL::extension() const
132 {
133 return mkstr(CFURLCopyPathExtension(ref));
134 }
135
136 void URL::recreateURL(const char* url)
137 {
138 if(ref)
139 CFRelease(ref);
140 ref = CFURLCreateWithBytes(NULL, (const UInt8 *)url, strlen(url), kCFStringEncodingUTF8, NULL);
141 }
142
143 } // end namespace Network
144 } // end namespace Security