forked from mozilla-mobile/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mozilla-mobile#86 from thebnich/search-default-fal…
…lback Bug 1120776 - Implement URIFixup with search fallback
- Loading branch information
Showing
4 changed files
with
89 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import Foundation | ||
|
||
class URIFixup { | ||
func getURL(entry: String) -> NSURL? { | ||
let trimmed = entry.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) | ||
var url = NSURL(string: trimmed) | ||
|
||
// First check if the URL includes a scheme. This will handle | ||
// all valid requests starting with "http://", "about:", etc. | ||
if url?.scheme != nil { | ||
return url | ||
} | ||
|
||
// If there's no scheme, we're going to prepend "http://". First, | ||
// make sure there's at least one "." in the host. This means | ||
// we'll allow single-word searches (e.g., "foo") at the expense | ||
// of breaking single-word hosts without a scheme (e.g., "localhost"). | ||
if trimmed.rangeOfString(".") == nil { | ||
return nil | ||
} | ||
|
||
// If there is a ".", prepend "http://" and try again. Since this | ||
// is strictly an "http://" URL, we also require a host. | ||
url = NSURL(string: "http://\(trimmed)") | ||
if url?.host != nil { | ||
return url | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters