]>
Commit | Line | Data |
---|---|---|
bac41a7b A |
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 | Boolean isAbsolute; | |
119 | return mkstr(CFURLCopyResourceSpecifier(ref)); | |
120 | } | |
121 | ||
122 | string URL::fullPath() const | |
123 | { | |
124 | return path() + resourceSpec(); | |
125 | } | |
126 | ||
127 | string URL::basename() const | |
128 | { | |
129 | return mkstr(CFURLCopyLastPathComponent(ref)); | |
130 | } | |
131 | ||
132 | string URL::extension() const | |
133 | { | |
134 | return mkstr(CFURLCopyPathExtension(ref)); | |
135 | } | |
136 | ||
137 | void URL::recreateURL(const char* url) | |
138 | { | |
139 | if(ref) | |
140 | CFRelease(ref); | |
141 | ref = CFURLCreateWithBytes(NULL, (const UInt8 *)url, strlen(url), kCFStringEncodingUTF8, NULL); | |
142 | } | |
143 | ||
144 | } // end namespace Network | |
145 | } // end namespace Security |