1
0
Fork 0

For #9420 - relaxed custom engine rules (#9967)

Allow websites that return 404 to be added
Change long query param with one with higher changes of being found
master
Mihai Branescu 2020-04-15 23:20:06 +03:00 committed by GitHub
parent f0464b9e72
commit bded28a017
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 2 deletions

View File

@ -10,10 +10,13 @@ import mozilla.components.concept.fetch.Request
import mozilla.components.concept.fetch.isSuccess
import mozilla.components.support.ktx.kotlin.toNormalizedUrl
import java.io.IOException
import java.net.HttpURLConnection
object SearchStringValidator {
enum class Result { Success, CannotReach }
private const val QUERY_PARAM = "1"
fun isSearchStringValid(client: Client, searchString: String): Result {
val request = createRequest(searchString)
val response = try {
@ -26,15 +29,23 @@ object SearchStringValidator {
// read the response stream to ensure the body is closed correctly. workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=1603114
response.body.string()
return if (response.isSuccess) Result.Success else Result.CannotReach
return if (response.isSuccess ||
isTestQueryParamNotFound(response.status)) Result.Success else Result.CannotReach
}
private fun createRequest(searchString: String): Request {
// we should share the code to substitute and normalize the search string (see SearchEngine.buildSearchUrl).
val encodedTestQuery = Uri.encode("testSearchEngineValidation")
val encodedTestQuery = Uri.encode(QUERY_PARAM)
val normalizedHttpsSearchUrlStr = searchString.toNormalizedUrl()
val searchUrl = normalizedHttpsSearchUrlStr.replace("%s".toRegex(), encodedTestQuery)
return Request(searchUrl)
}
/**
* There is no universal query param, so a site returning 404 doesn't mean the user input is wrong,
* it means that the website's search functionality doesn't find our test param, but it's reachable.
*/
private fun isTestQueryParamNotFound(status: Int): Boolean =
status == HttpURLConnection.HTTP_NOT_FOUND
}