Merge branch 'master' of https://github.com/gedoor/legado
commit
f4751e6ba9
@ -1,20 +0,0 @@ |
||||
package io.legado.app.help.http |
||||
|
||||
import okhttp3.ResponseBody |
||||
import retrofit2.Converter |
||||
import retrofit2.Retrofit |
||||
import java.lang.reflect.Type |
||||
|
||||
class ByteConverter : Converter.Factory() { |
||||
|
||||
override fun responseBodyConverter( |
||||
type: Type?, |
||||
annotations: Array<Annotation>?, |
||||
retrofit: Retrofit? |
||||
): Converter<ResponseBody, ByteArray>? { |
||||
return Converter { value -> |
||||
value.bytes() |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,40 +0,0 @@ |
||||
package io.legado.app.help.http |
||||
|
||||
import io.legado.app.utils.EncodingDetect |
||||
import io.legado.app.utils.UTF8BOMFighter |
||||
import okhttp3.ResponseBody |
||||
import retrofit2.Converter |
||||
import retrofit2.Retrofit |
||||
import java.lang.reflect.Type |
||||
import java.nio.charset.Charset |
||||
|
||||
class EncodeConverter(private val encode: String? = null) : Converter.Factory() { |
||||
|
||||
override fun responseBodyConverter( |
||||
type: Type?, |
||||
annotations: Array<Annotation>?, |
||||
retrofit: Retrofit? |
||||
): Converter<ResponseBody, String>? { |
||||
return Converter { value -> |
||||
val responseBytes = UTF8BOMFighter.removeUTF8BOM(value.bytes()) |
||||
var charsetName: String? = encode |
||||
|
||||
charsetName?.let { |
||||
try { |
||||
return@Converter String(responseBytes, Charset.forName(charsetName)) |
||||
} catch (e: Exception) { |
||||
} |
||||
} |
||||
|
||||
//根据http头判断 |
||||
value.contentType()?.charset()?.let { |
||||
return@Converter String(responseBytes, it) |
||||
} |
||||
|
||||
//根据内容判断 |
||||
charsetName = EncodingDetect.getHtmlEncode(responseBytes) |
||||
String(responseBytes, Charset.forName(charsetName)) |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,6 +0,0 @@ |
||||
package io.legado.app.help.http |
||||
|
||||
data class Res( |
||||
val url: String, |
||||
val body: String?, |
||||
) |
@ -0,0 +1,124 @@ |
||||
package io.legado.app.help.http |
||||
|
||||
import okhttp3.* |
||||
import okhttp3.Response.Builder |
||||
import java.util.* |
||||
|
||||
/** |
||||
* An HTTP response. |
||||
*/ |
||||
@Suppress("unused", "MemberVisibilityCanBePrivate") |
||||
class StrResponse private constructor( |
||||
val rawResponse: Response, |
||||
val body: String?, |
||||
val errorBody: ResponseBody? |
||||
) { |
||||
val raw get() = rawResponse |
||||
|
||||
val url: String |
||||
get() { |
||||
raw.networkResponse?.let { |
||||
return it.request.url.toString() |
||||
} |
||||
return raw.request.url.toString() |
||||
} |
||||
|
||||
fun code(): Int { |
||||
return rawResponse.code |
||||
} |
||||
|
||||
fun message(): String { |
||||
return rawResponse.message |
||||
} |
||||
|
||||
fun headers(): Headers { |
||||
return rawResponse.headers |
||||
} |
||||
|
||||
val isSuccessful: Boolean |
||||
get() = rawResponse.isSuccessful |
||||
|
||||
fun errorBody(): ResponseBody? { |
||||
return errorBody |
||||
} |
||||
|
||||
override fun toString(): String { |
||||
return rawResponse.toString() |
||||
} |
||||
|
||||
companion object { |
||||
fun success(code: Int, body: String): StrResponse { |
||||
require(!(code < 200 || code >= 300)) { "code < 200 or >= 300: $code" } |
||||
return success( |
||||
body, |
||||
Builder() // |
||||
.code(code) |
||||
.message("Response.success()") |
||||
.protocol(Protocol.HTTP_1_1) |
||||
.request(Request.Builder().url("http://localhost/").build()) |
||||
.build() |
||||
) |
||||
} |
||||
|
||||
fun success(body: String, headers: Headers): StrResponse { |
||||
return success( |
||||
body, |
||||
Builder() // |
||||
.code(200) |
||||
.message("OK") |
||||
.protocol(Protocol.HTTP_1_1) |
||||
.headers(headers) |
||||
.request(Request.Builder().url("http://localhost/").build()) |
||||
.build() |
||||
) |
||||
} |
||||
|
||||
fun success(body: String, url: String): StrResponse { |
||||
return success( |
||||
body, |
||||
Builder() // |
||||
.code(200) |
||||
.message("OK") |
||||
.protocol(Protocol.HTTP_1_1) |
||||
.request(Request.Builder().url(url).build()) |
||||
.build() |
||||
) |
||||
} |
||||
|
||||
@JvmOverloads |
||||
fun success( |
||||
body: String?, rawResponse: Response = |
||||
Builder() // |
||||
.code(200) |
||||
.message("OK") |
||||
.protocol(Protocol.HTTP_1_1) |
||||
.request(Request.Builder().url("http://localhost/").build()) |
||||
.build() |
||||
): StrResponse { |
||||
Objects.requireNonNull(rawResponse, "rawResponse == null") |
||||
require(rawResponse.isSuccessful) { "rawResponse must be successful response" } |
||||
return StrResponse(rawResponse, body, null) |
||||
} |
||||
|
||||
fun error(code: Int, body: ResponseBody?): StrResponse { |
||||
Objects.requireNonNull(body, "body == null") |
||||
require(code >= 400) { "code < 400: $code" } |
||||
return error( |
||||
body, |
||||
Builder() // |
||||
.code(code) |
||||
.message("Response.error()") |
||||
.protocol(Protocol.HTTP_1_1) |
||||
.request(Request.Builder().url("http://localhost/").build()) |
||||
.build() |
||||
) |
||||
} |
||||
|
||||
fun error(body: ResponseBody?, rawResponse: Response): StrResponse { |
||||
Objects.requireNonNull(body, "body == null") |
||||
Objects.requireNonNull(rawResponse, "rawResponse == null") |
||||
require(!rawResponse.isSuccessful) { "rawResponse should not be successful response" } |
||||
return StrResponse(rawResponse, null, body) |
||||
} |
||||
} |
||||
} |
@ -1,67 +0,0 @@ |
||||
package io.legado.app.help.http.api |
||||
|
||||
import retrofit2.Call |
||||
import retrofit2.Response |
||||
import retrofit2.http.GET |
||||
import retrofit2.http.HeaderMap |
||||
import retrofit2.http.QueryMap |
||||
import retrofit2.http.Url |
||||
|
||||
/** |
||||
* Created by GKF on 2018/1/21. |
||||
* get web content |
||||
*/ |
||||
@Suppress("unused") |
||||
interface HttpGetApi { |
||||
@GET |
||||
suspend fun getAsync( |
||||
@Url url: String, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Response<String> |
||||
|
||||
@GET |
||||
suspend fun getMapAsync( |
||||
@Url url: String, |
||||
@QueryMap(encoded = true) queryMap: Map<String, String>, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Response<String> |
||||
|
||||
@GET |
||||
fun get( |
||||
@Url url: String, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Call<String> |
||||
|
||||
@GET |
||||
fun getByte( |
||||
@Url url: String, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Call<ByteArray> |
||||
|
||||
@GET |
||||
fun getMap( |
||||
@Url url: String, |
||||
@QueryMap(encoded = true) queryMap: Map<String, String>, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Call<String> |
||||
|
||||
@GET |
||||
fun getMapByte( |
||||
@Url url: String, |
||||
@QueryMap(encoded = true) queryMap: Map<String, String>, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Call<ByteArray> |
||||
|
||||
@GET |
||||
suspend fun getByteAsync( |
||||
@Url url: String, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Response<ByteArray> |
||||
|
||||
@GET |
||||
suspend fun getMapByteAsync( |
||||
@Url url: String, |
||||
@QueryMap(encoded = true) queryMap: Map<String, String>, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Response<ByteArray> |
||||
} |
@ -1,60 +0,0 @@ |
||||
package io.legado.app.help.http.api |
||||
|
||||
import okhttp3.RequestBody |
||||
import retrofit2.Call |
||||
import retrofit2.Response |
||||
import retrofit2.http.* |
||||
|
||||
/** |
||||
* Created by GKF on 2018/1/29. |
||||
* post |
||||
*/ |
||||
@Suppress("unused") |
||||
interface HttpPostApi { |
||||
|
||||
@FormUrlEncoded |
||||
@POST |
||||
suspend fun postMapAsync( |
||||
@Url url: String, |
||||
@FieldMap(encoded = true) fieldMap: Map<String, String>, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Response<String> |
||||
|
||||
@POST |
||||
suspend fun postBodyAsync( |
||||
@Url url: String, |
||||
@Body body: RequestBody, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Response<String> |
||||
|
||||
@FormUrlEncoded |
||||
@POST |
||||
fun postMap( |
||||
@Url url: String, |
||||
@FieldMap(encoded = true) fieldMap: Map<String, String>, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Call<String> |
||||
|
||||
@POST |
||||
fun postBody( |
||||
@Url url: String, |
||||
@Body body: RequestBody, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Call<String> |
||||
|
||||
@FormUrlEncoded |
||||
@POST |
||||
suspend fun postMapByteAsync( |
||||
@Url url: String, |
||||
@FieldMap(encoded = true) fieldMap: Map<String, String>, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Response<ByteArray> |
||||
|
||||
@POST |
||||
suspend fun postBodyByteAsync( |
||||
@Url url: String, |
||||
@Body body: RequestBody, |
||||
@HeaderMap headers: Map<String, String> |
||||
): Response<ByteArray> |
||||
|
||||
} |
@ -0,0 +1,39 @@ |
||||
package io.legado.app.help.http.parser |
||||
|
||||
import io.legado.app.help.http.StrResponse |
||||
import io.legado.app.utils.EncodingDetect |
||||
import io.legado.app.utils.UTF8BOMFighter |
||||
import okhttp3.Response |
||||
import rxhttp.wrapper.annotation.Parser |
||||
import rxhttp.wrapper.exception.HttpStatusCodeException |
||||
import java.nio.charset.Charset |
||||
|
||||
@Parser(name = "StrResponse") |
||||
class StrResponseParser(val encode: String? = null) : rxhttp.wrapper.parse.Parser<StrResponse> { |
||||
|
||||
override fun onParse(response: Response): StrResponse { |
||||
val body = getString(response) |
||||
return StrResponse.success(body, response) |
||||
} |
||||
|
||||
private fun getString(response: Response): String { |
||||
|
||||
val responseBody = response.body ?: throw HttpStatusCodeException(response, "内容为空") |
||||
val responseBytes = UTF8BOMFighter.removeUTF8BOM(responseBody.bytes()) |
||||
var charsetName: String? = encode |
||||
|
||||
charsetName?.let { |
||||
return String(responseBytes, Charset.forName(charsetName)) |
||||
} |
||||
|
||||
//根据http头判断 |
||||
responseBody.contentType()?.charset()?.let { |
||||
return String(responseBytes, it) |
||||
} |
||||
|
||||
//根据内容判断 |
||||
charsetName = EncodingDetect.getHtmlEncode(responseBytes) |
||||
return String(responseBytes, Charset.forName(charsetName)) |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue