Skip to content

Commit

Permalink
Fix Forge-exclusive crash
Browse files Browse the repository at this point in the history
  • Loading branch information
BluSpring committed Sep 18, 2024
1 parent d1a5dcd commit b899e04
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
9 changes: 5 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ dependencies {
minecraftRuntimeLibraries(jws)
}

includeOrShade("org.apache.httpcomponents:httpcore:4.4.16")
includeOrShade("org.apache.httpcomponents:httpclient:4.5.13")
includeOrShade("org.apache.httpcomponents:httpcore:4.4.16")
includeOrShade("commons-logging:commons-logging:1.3.4")
if (!mcData.isForgeLike) {// fuck you Forge
includeOrShade("commons-logging:commons-logging:1.3.4")
includeOrShade("org.apache.httpcomponents:httpcore:4.4.16")
includeOrShade("org.apache.httpcomponents:httpclient:4.5.13")
}
}

toolkitReleases {
Expand Down
75 changes: 75 additions & 0 deletions src/main/kotlin/xyz/bluspring/unitytranslate/util/HttpHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,84 @@ import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
import xyz.bluspring.unitytranslate.UnityTranslate
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.net.HttpURLConnection
import java.net.URL

object HttpHelper {
private var hasApache: Boolean? = null

fun post(uri: String, body: JsonObject, headers: Map<String, String> = mapOf()): JsonElement {
if (hasApache == null) {
try {
// This is literally only here because of Forge.
Class.forName("org.apache.http.impl.client.HttpClients")
Class.forName("org.apache.http.HttpHeaders")
Class.forName("org.apache.http.util.EntityUtils")
Class.forName("org.apache.commons.logging.LogFactory")
hasApache = true
} catch (e: Throwable) {
UnityTranslate.logger.error("Detected that Apache HTTP support is unavailable! Translations may fail after a certain period of time.")
UnityTranslate.logger.error("For more information, view: https://github.com/BluSpring/UnityTranslate/issues/4")
e.printStackTrace()
hasApache = false
}
}

return if (hasApache == true) {
postApache(uri, body, headers)
} else {
postJava(uri, body, headers)
}
}

fun postJava(uri: String, body: JsonObject, headers: Map<String, String> = mapOf()): JsonElement {
val url = URL(uri)
var connection: HttpURLConnection? = null

val data = body.toString().toByteArray(Charsets.UTF_8)

try {
connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.connectTimeout = 60_000
connection.readTimeout = 60_000

connection.setRequestProperty("Accept", "application/json")
connection.setRequestProperty("Content-Type", "application/json")
headers.forEach { (key, value) ->
connection.setRequestProperty(key, value)
}

connection.doOutput = true
connection.setFixedLengthStreamingMode(data.size)

val outputStream = BufferedOutputStream(connection.outputStream)

outputStream.write(data)
outputStream.flush()
outputStream.close()

if (connection.responseCode / 100 != 2) {
throw Exception("Failed to load $uri (code: ${connection.responseCode})")
} else {
val inputStream = BufferedInputStream(connection.inputStream)
val reader = inputStream.bufferedReader(Charsets.UTF_8)
val result = JsonParser.parseReader(reader)

reader.close()
inputStream.close()

return result
}
} finally {
connection?.disconnect()
}
}

fun postApache(uri: String, body: JsonObject, headers: Map<String, String> = mapOf()): JsonElement {
HttpClients.createDefault().use { httpClient ->
val request = HttpPost(uri)
request.config = RequestConfig.custom()
Expand Down

0 comments on commit b899e04

Please sign in to comment.