@@ -9,6 +9,7 @@ import org.cef.misc.StringRef
99import org.cef.network.CefRequest
1010import org.cef.network.CefResponse
1111import org.digma.intellij.plugin.analytics.AnalyticsService
12+ import org.digma.intellij.plugin.common.Backgroundable
1213import org.digma.intellij.plugin.errorreporting.ErrorReporter
1314import org.digma.intellij.plugin.log.Log
1415import org.digma.intellij.plugin.model.rest.lowlevel.HttpRequest
@@ -113,78 +114,96 @@ class ApiProxyResourceHandler(val project: Project) : CefResourceHandler {
113114
114115
115116 override fun processRequest (request : CefRequest , callback : CefCallback ): Boolean {
116-
117117 Log .log(logger::trace, " processing request {}, [request id:{}]" , request.url, request.identifier)
118118
119- try {
119+ if ((request.postData?.elementCount ? : - 1 ) > 1 ) {
120+ Log .log(
121+ logger::warn,
122+ " encountered multi part post data. it is not supported by Digma api proxy, [request id:{}]" ,
123+ request.identifier
124+ )
120125
121- val apiUrl = buildProxyUrl(buildApiBaseUrl(), request.url)
122-
123- Log .log(logger::trace, " proxying to api url {}, [request id:{}]" , apiUrl, request.identifier)
124-
125- apiResponse = if ((request.postData?.elementCount ? : - 1 ) > 1 ) {
126-
127- // todo: we don't support multi part form data yet
128-
129- Log .log(
130- logger::warn,
131- " encountered multi part post data. it is not supported by Digma api proxy, [request id:{}]" ,
132- request.identifier
133- )
134-
135- HttpResponse (
136- 500 ,
137- mutableMapOf (),
138- null ,
139- " text/plain" ,
140- " encountered multi part post data. it is not supported by Digma api proxy." .byteInputStream()
141- )
142- } else if (hasFileElements(request.postData)) {
143-
144- // todo: we don't support multi part form data yet or file elements in the post data
145-
146- Log .log(
147- logger::warn,
148- " encountered file element in post data. it is not supported by Digma api proxy, [request id:{}]" ,
149- request.identifier
150- )
151-
152- HttpResponse (
153- 500 ,
154- mutableMapOf (),
155- null ,
156- " text/plain" ,
157- " encountered file element in post data. it is not supported by Digma api proxy." .byteInputStream()
158- )
159- } else {
160- val body: HttpRequestBody ? =
161- request.postData?.let {
162- HttpRequestBody (postDataToByteArray(request, it))
163- }
164-
165- Log .log(logger::trace, " body for request is {}, [request id:{}]" , body, request.identifier)
166-
167- val headers = mutableMapOf<String , String >()
168- request.getHeaderMap(headers)
169- val httpRequest = HttpRequest (request.method, apiUrl, headers.toMutableMap(), body)
170-
171- Log .log(logger::trace, " sending request {}, [request id:{}]" , httpRequest, request.identifier)
172- AnalyticsService .getInstance(project).proxyCall(httpRequest)
173- }
126+ apiResponse = HttpResponse (
127+ 500 ,
128+ mutableMapOf (),
129+ null ,
130+ " text/plain" ,
131+ " encountered multi part post data. it is not supported by Digma api proxy." .byteInputStream()
132+ )
133+ callback.Continue ()
134+ return true
135+ }
174136
175- Log .log(logger::trace, " got api response {}, [request id:{}]" , apiResponse, request.identifier)
176137
177- if (apiResponse == null ) {
178- Log .log(logger::warn, " apiResponse is null , canceling request {}, [request id:{}]" , request.url, request.identifier)
179- callback.cancel()
180- return false
181- }
138+ if (hasFileElements(request.postData)) {
139+
140+ // todo: we don't support multi part form data yet or file elements in the post data
141+
142+ Log .log(
143+ logger::warn,
144+ " encountered file element in post data. it is not supported by Digma api proxy, [request id:{}]" ,
145+ request.identifier
146+ )
182147
148+ apiResponse = HttpResponse (
149+ 500 ,
150+ mutableMapOf (),
151+ null ,
152+ " text/plain" ,
153+ " encountered file element in post data. it is not supported by Digma api proxy." .byteInputStream()
154+ )
183155 callback.Continue ()
184156 return true
185- } catch (e: Throwable ) {
157+ }
158+
159+
160+ // the request is valid only in the scope of this method , so take the data we need before starting a background thread
161+ val postData = request.postData?.let {
162+ postDataToByteArray(request, it)
163+ }
164+ val requestId = request.identifier
165+ val requestUrl = request.url
166+ val requestMethod = request.method
167+ val headers = mutableMapOf<String , String >()
168+ request.getHeaderMap(headers)
169+
170+ Backgroundable .executeOnPooledThread {
171+ proxyRequest(requestId, requestUrl, requestMethod, postData, headers, callback)
172+ }
173+
174+ return true
175+ }
176+
177+
178+ private fun proxyRequest (
179+ requestId : Long ,
180+ requestUrl : String ,
181+ requestMethod : String ,
182+ postData : ByteArray? ,
183+ headers : Map <String , String >,
184+ callback : CefCallback
185+ ) {
186+ try {
187+
188+ val apiUrl = buildProxyUrl(buildApiBaseUrl(), requestUrl)
189+
190+ Log .log(logger::trace, " proxying to api url {}, [request id:{}]" , apiUrl, requestId)
191+
192+ val body: HttpRequestBody ? =
193+ postData?.let {
194+ HttpRequestBody (postData)
195+ }
196+
197+ Log .log(logger::trace, " body for request is {}, [request id:{}]" , body, requestId)
198+
199+ val httpRequest = HttpRequest (requestMethod, apiUrl, headers.toMutableMap(), body)
186200
187- Log .warnWithException(logger, e, " processRequest {} failed, [request id:{}]" , request.url, request.identifier)
201+ Log .log(logger::trace, " sending request {}, [request id:{}]" , httpRequest, requestId)
202+ apiResponse = AnalyticsService .getInstance(project).proxyCall(httpRequest)
203+ Log .log(logger::trace, " got api response {}, [request id:{}]" , apiResponse, requestId)
204+
205+ } catch (e: Throwable ) {
206+ Log .warnWithException(logger, e, " processRequest {} failed, [request id:{}]" , requestUrl, requestId)
188207 ErrorReporter .getInstance().reportError(" ApiProxyResourceHandler.processRequest" , e)
189208
190209 apiResponse = HttpResponse (
@@ -194,14 +213,12 @@ class ApiProxyResourceHandler(val project: Project) : CefResourceHandler {
194213 " text/plain" ,
195214 " encountered exception in proxy [$e ]. please check the logs" .byteInputStream()
196215 )
197-
216+ } finally {
198217 callback.Continue ()
199- return true
200218 }
201219 }
202220
203221
204-
205222 override fun getResponseHeaders (response : CefResponse , responseLength : IntRef , redirectUrl : StringRef ) {
206223 apiResponse?.let { res ->
207224 response.status = res.status
0 commit comments