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