source: trunk/ICeCoffEE/ICeCoffEE/ICeCoffEEParser.m@ 384

Last change on this file since 384 was 384, checked in by Nicholas Riley, 16 years ago

ICeCoffEEParser.m: No longer need left/right delimiter sets to both
include >< because the parser handles it properly. Remove an
unnecessary debug log and one which generated exceptions on an
otherwise correct URL parse.

urls.plist: Test < not at beginning of string; failed before above
change to delimiter sets.

File size: 9.6 KB
Line 
1//
2// ICeCoffEEParser.m
3// ICeCoffEE
4//
5// Created by Nicholas Riley on 6/21/07.
6// Copyright 2007 Nicholas Riley. All rights reserved.
7//
8
9#import "ICeCoffEEParser.h"
10#import "ICeCoffEE.h"
11
12void ICCF_Delimiters(NSCharacterSet **leftPtr, NSCharacterSet **rightPtr) {
13 static NSCharacterSet *urlLeftDelimiters = nil, *urlRightDelimiters = nil;
14
15 if (urlLeftDelimiters == nil || urlRightDelimiters == nil) {
16 NSMutableCharacterSet *set = [[NSCharacterSet whitespaceAndNewlineCharacterSet] mutableCopy];
17 NSMutableCharacterSet *tmpSet;
18 [urlLeftDelimiters release];
19 [urlRightDelimiters release];
20
21 [set autorelease];
22 [set formUnionWithCharacterSet: [[NSCharacterSet characterSetWithRange: NSMakeRange(0x21, 0x5e)] invertedSet]]; // nonprintable and non-ASCII characters
23 [set formUnionWithCharacterSet: [NSCharacterSet punctuationCharacterSet]];
24 // XXX obsoleted by RFC 3986 now... use §2.1, 2.2, 2.3
25 [set removeCharactersInString: @";/?:@&=+$,-_.!~*'(){}[]%#"]; // RFC 2396 §2.2, 2.3, 2.4, plus % and # from "delims" set and {}, []
26
27 tmpSet = [[set mutableCopy] autorelease];
28 [tmpSet formUnionWithCharacterSet: [NSCharacterSet characterSetWithCharactersInString: @"<(["]];
29 urlLeftDelimiters = [tmpSet copy]; // make immutable again - for efficiency
30
31 tmpSet = [[set mutableCopy] autorelease];
32 [tmpSet formUnionWithCharacterSet: [NSCharacterSet characterSetWithCharactersInString: @">)]"]];
33 urlRightDelimiters = [tmpSet copy]; // make immutable again - for efficiency
34 }
35
36 *leftPtr = urlLeftDelimiters; *rightPtr = urlRightDelimiters;
37}
38
39static ICInstance ICCF_icInst = NULL;
40
41void ICCF_StartIC() {
42 OSStatus err;
43
44 if (ICCF_icInst != NULL) {
45 ICLog(@"ICCF_StartIC: Internet Config is already running!");
46 ICCF_StopIC();
47 }
48 err = ICStart(&ICCF_icInst, kICCFCreator);
49 NSCAssert1(err == noErr, ICCF_LocalizedString(@"Unable to start Internet Config (error %d)"), err);
50}
51
52void ICCF_StopIC() {
53 if (ICCF_icInst == NULL) {
54 ICLog(@"ICCF_StopIC: Internet Config is not running!");
55 } else {
56 ICStop(ICCF_icInst);
57 ICCF_icInst = NULL;
58 }
59}
60
61ICInstance ICCF_GetInst() {
62 NSCAssert(ICCF_icInst != NULL, @"Internal error: Called ICCF_GetInst without ICCF_StartIC");
63 return ICCF_icInst;
64}
65
66// input/output 'range' is the range of source document which contains 'string'
67void ICCF_ParseURL(NSString *string, NSRange *range) {
68 OSStatus err;
69 Handle h;
70 long selStart = 0, selEnd = range->length; // local offsets within 'string'
71 char *urlData = NULL;
72
73 NSCAssert(selEnd == [string length], @"Internal error: URL string is wrong length");
74
75 NS_DURING
76 if ([[NSCharacterSet characterSetWithCharactersInString: @";,."] characterIsMember:
77 [string characterAtIndex: selEnd - 1]]) {
78 selEnd--;
79 }
80 NSCharacterSet *alphanumericCharacterSet = [NSCharacterSet alphanumericCharacterSet];
81 while (![alphanumericCharacterSet characterIsMember: [string characterAtIndex: selStart]]) {
82 selStart++;
83 NSCAssert(selStart < selEnd, @"No URL is selected");
84 }
85
86 string = [string substringWithRange: NSMakeRange(selStart, selEnd - selStart)];
87
88 ICLog(@"Parsing URL |%@|", string);
89
90 NSCAssert([string canBeConvertedToEncoding: NSASCIIStringEncoding], @"No URL is selected");
91
92 urlData = (char *)malloc( (range->length + 1) * sizeof(char));
93 NSCAssert(urlData != NULL, @"Internal error: can't allocate memory for URL string");
94
95 // XXX getCString: is deprecated in 10.4, but this is safe and shouldn't assert because we've already verified the string can be converted to ASCII, which should be a subset of any possible system encoding. The replacement (getCString:maxLength:encoding:) is not available until 10.4, so we leave this until we dump Internet Config and gain IDN friendliness.
96 [string getCString: urlData];
97
98 h = NewHandle(0);
99 NSCAssert(h != NULL, @"Internal error: can't allocate URL handle");
100
101 err = ICParseURL(ICCF_GetInst(), "\pmailto", urlData, range->length, &selStart, &selEnd, h);
102 DisposeHandle(h);
103
104 ICCF_OSErrCAssert(err, @"ICParseURL");
105
106 range->length = selEnd - selStart;
107 range->location += selStart;
108 NS_HANDLER
109 free(urlData);
110 [localException raise];
111 NS_ENDHANDLER
112
113 free(urlData);
114}
115
116static BOOL ICCF_StringIncludesCharacter(NSString *s, unichar character, NSRange range) {
117 return ([s rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString:
118 [NSString stringWithCharacters: &character length: 1]]
119 options: NSLiteralSearch range: range].location != NSNotFound);
120}
121
122static BOOL ICCF_IsLikelyURI(NSString *s, NSRange range) {
123 return ([s rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString: @":/.@"]
124 options: NSLiteralSearch range: range].location != NSNotFound);
125}
126
127static BOOL ICCF_IsLikelyIPv6Address(NSString *s, NSRange range) {
128 return ([s rangeOfCharacterFromSet:
129 [[NSCharacterSet characterSetWithCharactersInString: @"ABCDEFabcdef0123456789:"] invertedSet]
130 options: NSLiteralSearch range: range].location == NSNotFound);
131}
132
133NSRange ICCF_URLEnclosingRange(NSString *s, NSRange range) {
134 NSCharacterSet *urlLeftDelimiters = nil, *urlRightDelimiters = nil;
135 NSRange delimiterRange;
136 unsigned extraLen;
137
138 ICCF_CheckRange(range);
139
140 ICCF_Delimiters(&urlLeftDelimiters, &urlRightDelimiters);
141
142 // right delimiter selected? Yes, this can break with ...)URL(.... Oh well.
143 if (range.location > 0 && [urlRightDelimiters characterIsMember: [s characterAtIndex: range.location]]) {
144 --range.location;
145 ++range.length;
146 ICLog(@"expanding past initial %c, now |%@|", [s characterAtIndex: range.location + 1],
147 [s substringWithRange: range]);
148 }
149
150expandFront:
151 // XXX instead of 0, make this stop at the max URL length to prevent protracted searches
152 // XXX backport to ICeCoffEETerminal
153 // add 1 to range to trap delimiters that are on the edge of the selection (i.e., <...)
154 delimiterRange = [s rangeOfCharacterFromSet: urlLeftDelimiters
155 options: NSLiteralSearch | NSBackwardsSearch
156 range: NSMakeRange(0, range.location + (range.location != [s length]))];
157 if (delimiterRange.location == NSNotFound) {
158 // extend to beginning of string
159 range.length += range.location;
160 range.location = 0;
161 } else {
162 NSCAssert(delimiterRange.length == 1, @"Internal error: delimiter matched range is not of length 1");
163 range.length += range.location - delimiterRange.location - 1;
164 range.location = delimiterRange.location + 1;
165 }
166
167expandBack:
168 // XXX instead of length of string, make this stop at the max URL length to prevent protracted searches
169 // add 1 to range to trap delimiters that are on the edge of the selection (i.e., ...>)
170 extraLen = [s length] - range.location - range.length;
171 delimiterRange = [s rangeOfCharacterFromSet: urlRightDelimiters
172 options: NSLiteralSearch
173 range: NSMakeRange(range.location + range.length - (range.length != 0),
174 extraLen + (range.length != 0))];
175 if (delimiterRange.location == NSNotFound) {
176 // extend to end of string
177 range.length += extraLen;
178 extraLen = 0;
179 } else {
180 NSCAssert(delimiterRange.length == 1, @"Internal error: delimiter matched range is not of length 1");
181 range.length += delimiterRange.location - range.location - range.length;
182 extraLen = [s length] - NSMaxRange(range);
183
184 // grow URL past closing paren/brace/bracket if we've seen an open paren/brace/bracket
185 unichar opening, closing = [s characterAtIndex: delimiterRange.location];
186 if (closing == ')') opening = '(';
187 else if (closing == '}') opening = '{';
188 else if (closing == ']') opening = '[';
189 else goto expandBoth;
190 if (!ICCF_StringIncludesCharacter(s, opening, range))
191 goto expandBoth;
192
193 if (extraLen == 1) {
194 range.length += 1;
195 --extraLen;
196 ICLog(@"expanding past %c, now |%@|", closing, [s substringWithRange: range]);
197 } else {
198 range.length += 2;
199 ICLog(@"expanding past %c, now |%@|", closing, [s substringWithRange: range]);
200 goto expandBack;
201 }
202 }
203
204expandBoth:
205 if (range.location <= 1)
206 goto checkRange; // nowhere to expand
207 unichar opening = [s characterAtIndex: range.location - 1], closing;
208 if (opening == '(') closing = ')';
209 else if (opening == '{') closing = '}';
210 else if (opening == '[') closing = ']';
211 else goto checkRange;
212
213 ICLog(@"extraLen = %d", extraLen);
214 // check if we're inside a partial delimited URL: not foolproof, but handles (foo), {UUID} and [IPv6]
215 if (delimiterRange.location != NSNotFound && [s characterAtIndex: delimiterRange.location] == closing &&
216 ((opening == '[' && ICCF_IsLikelyIPv6Address(s, range)) || !ICCF_IsLikelyURI(s, range))) {
217 ICLog(@"expanding past %c...%c, was |%@|", opening, closing, [s substringWithRange: range]);
218 range.location -= 2;
219 if (extraLen > 1)
220 range.length += 4;
221 else
222 range.length += 2 + extraLen;
223 ICLog(@"expanding past %c...%c, now |%@|", opening, closing, [s substringWithRange: range]);
224 goto expandFront;
225 }
226
227 if (ICCF_StringIncludesCharacter(s, closing, range) &&
228 ((opening == '[' &&
229 ICCF_IsLikelyIPv6Address(s, NSMakeRange(range.location,
230 [s rangeOfString: @"]"].location - range.location)))
231 || !ICCF_IsLikelyURI(s, range))) {
232 range.location -= 2;
233 range.length += 2;
234 ICLog(@"expanding past %c, now |%@|", opening, [s substringWithRange: range]);
235 goto expandFront;
236 }
237
238checkRange:
239 ICCF_CheckRange(range);
240
241 ICCF_ParseURL([s substringWithRange: range], &range);
242
243 return range;
244}
Note: See TracBrowser for help on using the repository browser.