Skip to content

Commit

Permalink
Merge pull request #9 from qamalyanaren/master
Browse files Browse the repository at this point in the history
feat: add new param (`formParameters`) to get form data as map
  • Loading branch information
acsbendi authored Oct 11, 2023
2 parents fc37bc3 + 99f4034 commit 649da37
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal class RequestInspectorJavaScriptInterface(webView: WebView) {
val url: String,
val method: String,
val body: String,
val formParameters: Map<String, String>,
val headers: Map<String, String>,
val trace: String,
val enctype: String?
Expand All @@ -46,20 +47,24 @@ internal class RequestInspectorJavaScriptInterface(webView: WebView) {
) {
val formParameterJsonArray = JSONArray(formParameterList)
val headerMap = getHeadersAsMap(headers)
val formParameterMap = getFormParametersAsMap(formParameterJsonArray)

val body = when (enctype) {
"application/x-www-form-urlencoded" -> {
headerMap["content-type"] = enctype
getUrlEncodedFormBody(formParameterJsonArray)
}

"multipart/form-data" -> {
headerMap["content-type"] = "multipart/form-data; boundary=$MULTIPART_FORM_BOUNDARY"
getMultiPartFormBody(formParameterJsonArray)
}

"text/plain" -> {
headerMap["content-type"] = enctype
getPlainTextFormBody(formParameterJsonArray)
}

else -> {
Log.e(LOG_TAG, "Incorrect encoding received from JavaScript: $enctype")
""
Expand All @@ -68,7 +73,16 @@ internal class RequestInspectorJavaScriptInterface(webView: WebView) {

Log.i(LOG_TAG, "Recorded form submission from JavaScript")
addRecordedRequest(
RecordedRequest(WebViewRequestType.FORM, url, method, body, headerMap, trace, enctype)
RecordedRequest(
WebViewRequestType.FORM,
url,
method,
body,
formParameterMap,
headerMap,
trace,
enctype
)
)
}

Expand All @@ -77,7 +91,16 @@ internal class RequestInspectorJavaScriptInterface(webView: WebView) {
Log.i(LOG_TAG, "Recorded XHR from JavaScript")
val headerMap = getHeadersAsMap(headers)
addRecordedRequest(
RecordedRequest(WebViewRequestType.XML_HTTP, url, method, body, headerMap, trace, null)
RecordedRequest(
WebViewRequestType.XML_HTTP,
url,
method,
body,
mapOf(),
headerMap,
trace,
null
)
)
}

Expand All @@ -86,7 +109,16 @@ internal class RequestInspectorJavaScriptInterface(webView: WebView) {
Log.i(LOG_TAG, "Recorded fetch from JavaScript")
val headerMap = getHeadersAsMap(headers)
addRecordedRequest(
RecordedRequest(WebViewRequestType.FETCH, url, method, body, headerMap, trace, null)
RecordedRequest(
WebViewRequestType.FETCH,
url,
method,
body,
mapOf(),
headerMap,
trace,
null
)
)
}

Expand All @@ -106,6 +138,18 @@ internal class RequestInspectorJavaScriptInterface(webView: WebView) {
return map
}

private fun getFormParametersAsMap(formParameterJsonArray: JSONArray): Map<String, String> {
val map = HashMap<String, String>()
repeat(formParameterJsonArray.length()) { i ->
val formParameter = formParameterJsonArray.get(i) as JSONObject
val name = formParameter.getString("name")
val value = formParameter.getString("value")
map[name] = value
}
return map
}


private fun getUrlEncodedFormBody(formParameterJsonArray: JSONArray): String {
val resultStringBuilder = StringBuilder()
repeat(formParameterJsonArray.length()) { i ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ data class WebViewRequest(
val url: String,
val method: String,
val body: String,
val formParameters: Map<String, String>,
val headers: Map<String, String>,
val trace: String,
val enctype: String?,
Expand All @@ -20,6 +21,9 @@ data class WebViewRequest(
val headersString = headers.entries.joinToString("\n", "\n") { (key, value) ->
" $key: $value"
}
val formParametersString = formParameters.entries.joinToString("\n", "\n") { (key, value) ->
" $key: $value"
}
val traceWithIndent =
trace
.lines()
Expand All @@ -34,6 +38,7 @@ data class WebViewRequest(
Method: $method
Body: $body
Headers: $headersString
FormParameters: $formParametersString
Trace: $traceWithIndent
Encoding type (form submissions only): $enctype
Is for main frame? $isForMainFrame
Expand Down Expand Up @@ -78,7 +83,8 @@ data class WebViewRequest(
enctype = recordedRequest?.enctype,
isForMainFrame = webResourceRequest.isForMainFrame,
isRedirect = isRedirect,
hasGesture = webResourceRequest.hasGesture()
hasGesture = webResourceRequest.hasGesture(),
formParameters = recordedRequest?.formParameters ?: mapOf()
)
}
}
Expand Down

0 comments on commit 649da37

Please sign in to comment.