Skip to content

Commit

Permalink
add custom HTTP method support
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny-chung committed Feb 12, 2024
1 parent f92826a commit f759db9
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 19 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ Windows laptop with 8 GB RAM without a noticeable performance degrade of other a
Following features are on the TODO list (not in order). Feel free to raise feature requests or
express your desired priorities in the issue tracker.

- Custom HTTP methods
- Response JSON folding
- Copy buttons in response
- Binary response view
- Binary request editor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,38 @@ data class HttpRequest(
.let { URI.create(it.toASCIIString()) }
}
}

/**
* According to RFC 2616,
*
* Method = "OPTIONS" ; Section 9.2
* | "GET" ; Section 9.3
* | "HEAD" ; Section 9.4
* | "POST" ; Section 9.5
* | "PUT" ; Section 9.6
* | "DELETE" ; Section 9.7
* | "TRACE" ; Section 9.8
* | "CONNECT" ; Section 9.9
* | extension-method
* extension-method = token
*
* token = 1*<any CHAR except CTLs or separators>
* separators = "(" | ")" | "<" | ">" | "@"
* | "," | ";" | ":" | "\" | <">
* | "/" | "[" | "]" | "?" | "="
* | "{" | "}" | SP | HT
*
* CHAR = <any US-ASCII character (octets 0 - 127)>
* CTL = <any US-ASCII control character
* (octets 0 - 31) and DEL (127)>
* SP = <US-ASCII SP, space (32)>
* HT = <US-ASCII HT, horizontal-tab (9)>
* <"> = <US-ASCII double-quote mark (34)>
*/
fun String.isValidHttpMethod(): Boolean {
if (isEmpty()) return false
if (any { it.code <= 31 || it.code >= 127 || it in setOf('(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t') }) {
return false
}
return true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.sunnychung.application.multiplatform.hellohttp.ux

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.unit.dp
import com.sunnychung.application.multiplatform.hellohttp.AppContext
import com.sunnychung.application.multiplatform.hellohttp.model.isValidHttpMethod
import com.sunnychung.application.multiplatform.hellohttp.ux.local.LocalColor

@Composable
fun CustomHttpMethodDialog(isEnabled: Boolean, onDismiss: () -> Unit, onConfirm: (String) -> Unit) {
val colours = LocalColor.current
val errorMessageVM = AppContext.ErrorMessagePromptViewModel
var text by remember { mutableStateOf("") }

fun onDone() {
if (!text.isValidHttpMethod()) {
errorMessageVM.showErrorMessage("Provided input is not a valid HTTP method.")
} else {
onConfirm(text)
}
}

MainWindowDialog(
key = "CustomHttpMethodDialog",
isEnabled = isEnabled,
onDismiss = onDismiss) {

val focusRequester = remember { FocusRequester() }

Column(horizontalAlignment = Alignment.CenterHorizontally) {
AppTextFieldWithPlaceholder(
value = text,
onValueChange = {
if (it.isEmpty() || it.isValidHttpMethod()) {
text = it
}
},
placeholder = {
AppText(
text = "HTTP method name",
color = colours.placeholder
)
},
singleLine = true,
modifier = Modifier.focusRequester(focusRequester)
.onPreviewKeyEvent {
if (it.key == Key.Enter && it.type == KeyEventType.KeyDown) {
onDone()
true
} else {
false
}
}
.defaultMinSize(minWidth = 200.dp),
)
AppTextButton(
text = "Done",
onClick = { onDone() },
modifier = Modifier.padding(top = 4.dp),
)
}

LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import com.sunnychung.application.multiplatform.hellohttp.model.UserGrpcRequest
import com.sunnychung.application.multiplatform.hellohttp.model.UserKeyValuePair
import com.sunnychung.application.multiplatform.hellohttp.model.UserRequestExample
import com.sunnychung.application.multiplatform.hellohttp.model.UserRequestTemplate
import com.sunnychung.application.multiplatform.hellohttp.model.isValidHttpMethod
import com.sunnychung.application.multiplatform.hellohttp.network.ConnectionStatus
import com.sunnychung.application.multiplatform.hellohttp.network.hostFromUrl
import com.sunnychung.application.multiplatform.hellohttp.platform.MacOS
Expand Down Expand Up @@ -148,8 +149,26 @@ fun RequestEditorView(
}
}

var isShowCustomHttpMethodDialog by remember { mutableStateOf(false) }

log.d { "RequestEditorView recompose $request" }

CustomHttpMethodDialog(
isEnabled = isShowCustomHttpMethodDialog,
onDismiss = { isShowCustomHttpMethodDialog = false },
onConfirm = { customHttpMethod ->
if (customHttpMethod.isValidHttpMethod()) {
onRequestModified(
request.copyForApplication(
application = ProtocolApplication.Http,
method = customHttpMethod
)
)
isShowCustomHttpMethodDialog = false
}
},
)

Column(modifier = modifier
.onKeyEvent { e ->
if (isEnableSendButton && e.type == KeyEventType.KeyDown && e.key == Key.Enter && !e.isAltPressed && !e.isShiftPressed) {
Expand All @@ -169,13 +188,18 @@ fun RequestEditorView(
.height(IntrinsicSize.Max),
verticalAlignment = Alignment.CenterVertically
) {
val customHttpMethodOption = DropDownKeyValue(
key = ProtocolMethod(application = ProtocolApplication.Http, method = "<Custom>"),
displayText = "Custom"
)
val options = DropDownMap(
listOf("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD")
.map { DropDownKeyValue(
key = ProtocolMethod(application = ProtocolApplication.Http, method = it),
displayText = it
) }
+ listOf(
customHttpMethodOption,
DropDownKeyValue(
key = ProtocolMethod(application = ProtocolApplication.Graphql, method = ""),
displayText = "GraphQL"
Expand All @@ -191,25 +215,31 @@ fun RequestEditorView(
)
)
DropDownView(
selectedItem = options.dropdownables.first { it.key.application == request.application && (it.key.method == request.method || it.key.method.isEmpty()) },
selectedItem = options.dropdownables.firstOrNull {
it.key.application == request.application && (it.key.method == request.method || it.key.method.isEmpty())
} ?: customHttpMethodOption,
items = options.dropdownables,
contentView = { it, isLabel, isSelected, isClickable ->
val (text, color) = when (it!!.key.application) {
ProtocolApplication.Http -> Pair(
it.displayText,
when (it.displayText) {
"GET" -> colors.httpRequestGet
"POST" -> colors.httpRequestPost
"PUT" -> colors.httpRequestPut
"PATCH" -> colors.httpRequestPatch
"DELETE" -> colors.httpRequestDelete
else -> colors.httpRequestOthers
}
)
val (text, color) = if (isLabel && it!!.key.application == ProtocolApplication.Http && it!!.key.method == "<Custom>") {
Pair(request.method, colors.httpRequestOthers)
} else {
when (it!!.key.application) {
ProtocolApplication.Http -> Pair(
it.displayText,
when (it.displayText) {
"GET" -> colors.httpRequestGet
"POST" -> colors.httpRequestPost
"PUT" -> colors.httpRequestPut
"PATCH" -> colors.httpRequestPatch
"DELETE" -> colors.httpRequestDelete
else -> colors.httpRequestOthers
}
)

ProtocolApplication.WebSocket -> Pair("WebSocket", colors.websocketRequest)
ProtocolApplication.Grpc -> Pair("gRPC", colors.grpcRequest)
ProtocolApplication.Graphql -> Pair("GraphQL", colors.graphqlRequest)
ProtocolApplication.WebSocket -> Pair("WebSocket", colors.websocketRequest)
ProtocolApplication.Grpc -> Pair("gRPC", colors.grpcRequest)
ProtocolApplication.Graphql -> Pair("GraphQL", colors.graphqlRequest)
}
}
val modifier = if (isLabel) {
Modifier
Expand All @@ -227,10 +257,16 @@ fun RequestEditorView(
modifier = modifier.padding(horizontal = 8.dp) //.width(width = 48.dp)
)
},
onClickItem = {
onClickItem = onClickItem@ {
if (request.application != it.key.application) {
selectedRequestTabIndex = 0
}

if (it.key.application == ProtocolApplication.Http && it.key.method == "<Custom>") {
isShowCustomHttpMethodDialog = true
return@onClickItem true
}

onRequestModified(request.copyForApplication(application = it.key.application, method = it.key.method))
true
},
Expand Down

0 comments on commit f759db9

Please sign in to comment.