Skip to content

Commit

Permalink
Prepare for next release/refactor a bit of code
Browse files Browse the repository at this point in the history
  • Loading branch information
kittinunf committed Mar 29, 2016
1 parent 42d0534 commit 828ad1d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 85 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ repositories {
}
dependencies {
compile 'com.github.kittinunf.fuel:fuel:1.1.2' //for JVM
compile 'com.github.kittinunf.fuel:fuel-android:1.1.2' //for Android
compile 'com.github.kittinunf.fuel:fuel-rxjava:1.1.2' //for RxJava support
compile 'com.github.kittinunf.fuel:fuel:1.1.3' //for JVM
compile 'com.github.kittinunf.fuel:fuel-android:1.1.3' //for Android
compile 'com.github.kittinunf.fuel:fuel-rxjava:1.1.3' //for RxJava support
}
```

Expand Down Expand Up @@ -454,7 +454,7 @@ Fuel.get("/get").response { request, response, result ->
* `socketFactory` can be supplied by user. If `keyStore` is not null, `socketFactory` will be derived from it.
* `hostnameVerifier` is configurable by user. By default, it is just ignore it by returning `true` to all hostnames. If this is not what you want, please consider provide it.
* `hostnameVerifier` is configurable by user. By default, it uses `HttpsURLConnection.getDefaultHostnameVerifier()`.
### Test mode
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buildscript {

allprojects {
ext {
publish_version = '1.1.2'
publish_version = '1.1.3'

//dependencies version
kotlin_version = '1.0.1'
Expand Down

This file was deleted.

34 changes: 3 additions & 31 deletions fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Manager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import com.github.kittinunf.fuel.toolbox.HttpClient
import com.github.kittinunf.fuel.util.SameThreadExecutorService
import com.github.kittinunf.fuel.util.readWriteLazy
import java.security.KeyStore
import java.security.SecureRandom
import java.security.cert.X509Certificate
import java.util.*
import java.util.concurrent.Executor
import java.util.concurrent.ExecutorService
Expand All @@ -28,11 +26,11 @@ class Manager {
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustFactory.trustManagers, null)
sslContext.socketFactory
} ?: unsecuredSocketFactory()
} ?: HttpsURLConnection.getDefaultSSLSocketFactory()
}

var hostnameVerifier: HostnameVerifier by readWriteLazy {
unsecuredHostnameVerifier()
HttpsURLConnection.getDefaultHostnameVerifier()
}

//background executor
Expand All @@ -45,9 +43,7 @@ class Manager {
}
}

val testExecutor: ExecutorService by lazy { SameThreadExecutorService() }

fun createExecutor() = if (Fuel.testConfiguration.blocking) testExecutor else executor
fun createExecutor() = if (Fuel.testConfiguration.blocking) SameThreadExecutorService() else executor

//callback executor
var callbackExecutor: Executor by readWriteLazy { createEnvironment().callbackExecutor }
Expand Down Expand Up @@ -162,30 +158,6 @@ class Manager {
return request
}

private fun unsecuredSocketFactory(): SSLSocketFactory {
val trustAllCerts = arrayOf(object : X509TrustManager {

override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String) {
}

override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String) {
}

override fun getAcceptedIssuers(): Array<out X509Certificate>? {
return null
}

})

val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCerts, SecureRandom())
return sslContext.socketFactory
}

private fun unsecuredHostnameVerifier(): HostnameVerifier {
return HostnameVerifier { hostname, session -> true }
}

companion object {
//manager
var instance by readWriteLazy { Manager() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import java.util.concurrent.Executor
import java.util.concurrent.ExecutorService
import java.util.concurrent.Future
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLSocketFactory

class Request {
Expand Down Expand Up @@ -47,8 +46,8 @@ class Request {
var taskFuture: Future<*>? = null

//configuration
var socketFactory: SSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory()
var hostnameVerifier: HostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier()
var socketFactory: SSLSocketFactory? = null
var hostnameVerifier: HostnameVerifier? = null

//callers
lateinit var executor: ExecutorService
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.kittinunf.fuel.util

import java.util.concurrent.AbstractExecutorService
import java.util.concurrent.TimeUnit

/**
* Executes all submitted tasks directly in the same thread as the caller.
*/
class SameThreadExecutorService : AbstractExecutorService() {

//volatile because can be viewed by other threads
@Volatile private var terminated: Boolean = false

override fun shutdown() {
terminated = true
}

override fun isShutdown(): Boolean {
return terminated
}

override fun isTerminated(): Boolean {
return terminated
}

@Throws(InterruptedException::class)
override fun awaitTermination(theTimeout: Long, theUnit: TimeUnit): Boolean {
shutdown()
return terminated
}

override fun shutdownNow(): List<Runnable> {
return emptyList()
}

override fun execute(theCommand: Runnable) {
theCommand.run()
}
}

0 comments on commit 828ad1d

Please sign in to comment.