Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ZXURIParsedResult.m 2.8KB

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