JLChen
2021-11-04 d3713a9e02760ac9f5c0551ca72be0bdda3ba91c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright 2012 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#import "ZXResultParser.h"
#import "ZXURIParsedResult.h"
 
static NSRegularExpression *ZX_USER_IN_HOST = nil;
 
@implementation ZXURIParsedResult
 
+ (void)initialize {
  if ([self class] != [ZXURIParsedResult class]) return;
 
  ZX_USER_IN_HOST = [[NSRegularExpression alloc] initWithPattern:@":/*([^/@]+)@[^/]+" options:0 error:nil];
}
 
- (id)initWithUri:(NSString *)uri title:(NSString *)title {
  if (self = [super initWithType:kParsedResultTypeURI]) {
    _uri = [self massageURI:uri];
    _title = title;
  }
 
  return self;
}
 
+ (id)uriParsedResultWithUri:(NSString *)uri title:(NSString *)title {
  return [[self alloc] initWithUri:uri title:title];
}
 
- (BOOL)possiblyMaliciousURI {
  return [ZX_USER_IN_HOST numberOfMatchesInString:self.uri options:0 range:NSMakeRange(0, self.uri.length)] > 0;
}
 
- (NSString *)displayResult {
  NSMutableString *result = [NSMutableString stringWithCapacity:30];
  [ZXParsedResult maybeAppend:self.title result:result];
  [ZXParsedResult maybeAppend:self.uri result:result];
  return result;
}
 
/**
 * Transforms a string that represents a URI into something more proper, by adding or canonicalizing
 * the protocol.
 */
- (NSString *)massageURI:(NSString *)uri {
  NSString *massagedUri = [uri stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  NSUInteger protocolEnd = [massagedUri rangeOfString:@":"].location;
  if (protocolEnd == NSNotFound) {
    // No protocol, assume http
    massagedUri = [NSString stringWithFormat:@"http://%@", massagedUri];
  } else if ([self isColonFollowedByPortNumber:massagedUri protocolEnd:(int)protocolEnd]) {
    // Found a colon, but it looks like it is after the host, so the protocol is still missing
    massagedUri = [NSString stringWithFormat:@"http://%@", massagedUri];
  }
  return massagedUri;
}
 
- (BOOL)isColonFollowedByPortNumber:(NSString *)uri protocolEnd:(int)protocolEnd {
  int start = protocolEnd + 1;
  NSUInteger nextSlash = [uri rangeOfString:@"/" options:0 range:NSMakeRange(start, [uri length] - start)].location;
  if (nextSlash == NSNotFound) {
    nextSlash = [uri length];
  }
  return [ZXResultParser isSubstringOfDigits:uri offset:start length:(int)nextSlash - start];
}
 
@end