-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathenvoy3.patch
202 lines (195 loc) · 10.5 KB
/
envoy3.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
diff --git a/envoy/build.gradle b/envoy/build.gradle
index ebb6d30..e39fbf4 100644
--- a/envoy/build.gradle
+++ b/envoy/build.gradle
@@ -41,11 +41,11 @@ android {
//androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
//implementation 'com.google.android.gms:play-services-cronet:17.0.0'
- implementation('com.squareup.okhttp3:okhttp:4.6.0')
- implementation('com.squareup.okhttp3:okhttp-urlconnection:4.4.1')
+ //implementation('com.squareup.okhttp3:okhttp:4.6.0')
+ //implementation('com.squareup.okhttp3:okhttp-urlconnection:4.4.1')
//noinspection GradleDependency
- //implementation('com.squareup.okhttp3:okhttp:3.14.8')
- //implementation('com.squareup.okhttp3:okhttp-urlconnection:3.14.8')
+ implementation('com.squareup.okhttp3:okhttp:3.14.8')
+ implementation('com.squareup.okhttp3:okhttp-urlconnection:3.14.8')
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.72'
//embed project(path: ':cronet', configuration: 'release')
diff --git a/envoy/src/main/kotlin/org/greatfire/envoy/CronetInterceptor.kt b/envoy/src/main/kotlin/org/greatfire/envoy/CronetInterceptor.kt
index fb201e5..fa733c6 100644
--- a/envoy/src/main/kotlin/org/greatfire/envoy/CronetInterceptor.kt
+++ b/envoy/src/main/kotlin/org/greatfire/envoy/CronetInterceptor.kt
@@ -20,16 +20,16 @@ class CronetInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
return when {
mCronetEngine != null -> {
- Log.d(TAG, "hit interceptor for " + chain.request().url)
+ Log.d(TAG, "hit interceptor for " + chain.request().url())
proxyToCronet(chain.request(), chain.call(), mCronetEngine!!)
}
CronetNetworking.cronetEngine() != null -> {
- Log.d(TAG, "hit global interceptor for " + chain.request().url)
+ Log.d(TAG, "hit global interceptor for " + chain.request().url())
// This will stop later interceptors
proxyToCronet(chain.request(), chain.call())
}
else -> {
- Log.d(TAG, "bypass interceptor for " + chain.request().url)
+ Log.d(TAG, "bypass interceptor for " + chain.request().url())
chain.proceed(chain.request())
}
}
diff --git a/envoy/src/main/kotlin/org/greatfire/envoy/CronetNetworking.kt b/envoy/src/main/kotlin/org/greatfire/envoy/CronetNetworking.kt
index 3d9e1b1..5c752ff 100644
--- a/envoy/src/main/kotlin/org/greatfire/envoy/CronetNetworking.kt
+++ b/envoy/src/main/kotlin/org/greatfire/envoy/CronetNetworking.kt
@@ -71,17 +71,21 @@ object CronetNetworking {
@JvmStatic
@Throws(IOException::class)
fun buildRequest(request: Request, callback: UrlRequest.Callback?, cronetEngine: CronetEngine, executorService: ExecutorService): UrlRequest {
- val url = request.url.toString()
+ val url = request.url().toString()
val requestBuilder = cronetEngine.newUrlRequestBuilder(url, callback, executorService)
- requestBuilder.setHttpMethod(request.method)
- request.headers.forEach {
- if (it.first.toLowerCase(Locale.ENGLISH) != "accept-encoding") {
- // Log.d(TAG, "add header for url $url: ${it.first}, ${it.second}")
- requestBuilder.addHeader(it.first, it.second)
- }
+ requestBuilder.setHttpMethod(request.method())
+ request.headers().toMultimap().forEach {
+ val headerKey = it.key
+ val headerValues = it.value
+ headerValues.forEach {value ->
+ if (headerKey.toLowerCase(Locale.ENGLISH) != "accept-encoding") {
+ // Log.d(TAG, "add header for url $url: $headerKey, $value")
+ requestBuilder.addHeader(headerKey, value)
+ }
+ }
}
- val requestBody = request.body
+ val requestBody = request.body()
if (requestBody != null) {
val contentType = requestBody.contentType()
if (contentType != null) {
diff --git a/envoy/src/main/kotlin/org/greatfire/envoy/CronetOkHttpCall.kt b/envoy/src/main/kotlin/org/greatfire/envoy/CronetOkHttpCall.kt
index 1737fd0..e610a37 100644
--- a/envoy/src/main/kotlin/org/greatfire/envoy/CronetOkHttpCall.kt
+++ b/envoy/src/main/kotlin/org/greatfire/envoy/CronetOkHttpCall.kt
@@ -13,7 +13,7 @@ internal class CronetOkHttpCall(
private val client: OkHttpClient,
private val engine: CronetEngine,
private val originalRequest: Request) : Call {
- private val mEventListener: EventListener = client.eventListenerFactory.create(this)
+ private val mEventListener: EventListener = client.eventListenerFactory().create(this)
private var mUrlRequest: UrlRequest? = null
private var mIsExecuted = false
private var mIsCanceled = false
@@ -24,7 +24,7 @@
}
init {
- mTimeout.timeout(client.callTimeoutMillis.toLong(), TimeUnit.MILLISECONDS)
+ mTimeout.timeout(client.callTimeoutMillis().toLong(), TimeUnit.MILLISECONDS)
}
override fun request(): Request {
diff --git a/envoy/src/main/kotlin/org/greatfire/envoy/CronetUrlRequestCallback.kt b/envoy/src/main/kotlin/org/greatfire/envoy/CronetUrlRequestCallback.kt
index 30522b5..b39178e 100644
--- a/envoy/src/main/kotlin/org/greatfire/envoy/CronetUrlRequestCallback.kt
+++ b/envoy/src/main/kotlin/org/greatfire/envoy/CronetUrlRequestCallback.kt
@@ -4,8 +4,7 @@ import android.os.ConditionVariable
import android.util.Log
import okhttp3.*
import okhttp3.EventListener
-import okhttp3.MediaType.Companion.toMediaTypeOrNull
-import okhttp3.ResponseBody.Companion.toResponseBody
+import okhttp3.MediaType
import org.chromium.net.CronetException
import org.chromium.net.UrlRequest
import org.chromium.net.UrlResponseInfo
@@ -53,11 +52,11 @@
}
mRedirectCount += 1
val client = OkHttpClient.Builder().build()
- if (mOriginalRequest.url.isHttps && newLocationUrl.startsWith("http://") && client.followSslRedirects) {
+ if (mOriginalRequest.url().isHttps && newLocationUrl.startsWith("http://") && client.followSslRedirects()) {
request.followRedirect()
- } else if (!mOriginalRequest.url.isHttps && newLocationUrl.startsWith("https://") && client.followSslRedirects) {
+ } else if (!mOriginalRequest.url().isHttps && newLocationUrl.startsWith("https://") && client.followSslRedirects()) {
request.followRedirect()
- } else if (client.followRedirects) {
+ } else if (client.followRedirects()) {
request.followRedirect()
} else {
request.cancel()
@@ -91,10 +90,9 @@
// set the default value for empty content type?
// also set ; charset="utf-8" ?
- val contentType = mResponse.header("content-type", "text/html")
- val mediaType: MediaType? = (contentType
- ?: """text/plain; charset="utf-8"""").toMediaTypeOrNull()
- val responseBody = mReceivedByteArrayOutputStream.toByteArray().toResponseBody(mediaType)
+ val contentTypeString = mResponse.header("content-type", "text/html")
+ val contentType = MediaType.parse(contentTypeString!!)
+ val responseBody: ResponseBody = ResponseBody.create(contentType, mReceivedByteArrayOutputStream.toByteArray())
val newRequest = mOriginalRequest.newBuilder()
.url(responseInfo.url)
.build()
diff --git a/envoy/build.gradle b/envoy/build.gradle
index ebb6d30..e39fbf4 100644
--- a/envoy/src/main/kotlin/org/greatfire/envoy/CronetWebViewClient.kt
+++ b/envoy/src/main/kotlin/org/greatfire/envoy/CronetWebViewClient.kt
@@ -5,7 +5,7 @@ import android.util.Log
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
-import okhttp3.Headers.Companion.toHeaders
+import okhttp3.Headers
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
@@ -25,26 +25,35 @@
.addInterceptor(interceptor)
.build()
// val client = CronetOkHttpConnectionFactory.client
- val headers = request!!.requestHeaders.toMap().toHeaders()
- val wrappedRequest: Request = Request.Builder().url(request.url.toString()).headers(headers).build()
+ val headers = request!!.requestHeaders
+ val headerBuilder = Headers.Builder()
+ for ((key, value) in headers) {
+ try {
+ headerBuilder.add(key, value)
+ } catch (e: Exception) {
+ Log.w(TAG, "Invalid header, $key: $value")
+ e.printStackTrace()
+ }
+ }
+ val wrappedRequest: Request = Request.Builder().url(request.url.toString()).headers(headerBuilder.build()).build()
Log.i(TAG, "okhttp request sent for url " + request.url.toString() + ", headers: " + headers.toString())
try {
val response: Response = client.newCall(wrappedRequest).execute()
- Log.i(TAG, "okhttp headers for " + request.url.toString() + ": " + response.headers)
- val contentType = response.body!!.contentType().toString().split("; ").first()
- val charsetStr = response.body!!.contentType()!!.charset().toString()
+ Log.i(TAG, "okhttp headers for " + request.url.toString() + ": " + response.headers())
+ val contentType = response.body()!!.contentType().toString().split("; ").first()
+ val charsetStr = response.body()!!.contentType()!!.charset().toString()
Log.i(TAG, java.lang.String.format("okhttp return for %s: %s %s", request.url, contentType, charsetStr))
val responseHeaders: MutableMap<String, String> = HashMap()
- for (i in 0 until response.headers.size) {
- responseHeaders[response.headers.name(i)] = response.headers.value(i)
+ for (i in 0 until response.headers().size()) {
+ responseHeaders[response.headers().name(i)] = response.headers().value(i)
}
- var message: String = response.message
+ var message: String = response.message()
if (message.isEmpty()) {
message = "Unknown error"
}
- return WebResourceResponse(contentType, charsetStr, response.code, message, responseHeaders,
- response.body!!.byteStream())
+ return WebResourceResponse(contentType, charsetStr, response.code(), message, responseHeaders,
+ response.body()!!.byteStream())
} catch (e: IOException) {
Log.e(TAG, "request failed", e)
}