Skip to content

Commit c1152c5

Browse files
author
Ваше Имя
committed
Add allowErrorBody parameter to HTTP requests for backward-compatible error handling
1 parent e342762 commit c1152c5

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/main/scala/li/cil/oc/server/component/InternetCard.scala

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class InternetCard extends prefab.ManagedEnvironment with DeviceInfo {
6060
@Callback(direct = true, doc = """function():boolean -- Returns whether HTTP requests can be made (config setting).""")
6161
def isHttpEnabled(context: Context, args: Arguments): Array[AnyRef] = result(Settings.get.httpEnabled)
6262

63-
@Callback(doc = """function(url:string[, postData:string[, headers:table[, method:string]]]):userdata -- Starts an HTTP request. If this returns true, further results will be pushed using `http_response` signals.""")
63+
@Callback(doc = """function(url:string[, postData:string[, headers:table[, method:string[, allowErrorBody:boolean]]]]):userdata -- Starts an HTTP request. If allowErrorBody is true, HTTP error responses will return their body instead of throwing an exception.""")
6464
def request(context: Context, args: Arguments): Array[AnyRef] = this.synchronized {
6565
checkOwner(context)
6666
val address = args.checkString(0)
@@ -82,7 +82,8 @@ class InternetCard extends prefab.ManagedEnvironment with DeviceInfo {
8282
return result(Unit, "http request headers are unavailable")
8383
}
8484
val method = if (args.isString(3)) Option(args.checkString(3)) else None
85-
val request = new InternetCard.HTTPRequest(this, checkAddress(address), post, headers, method)
85+
val allowErrorBody = args.optBoolean(4, false)
86+
val request = new InternetCard.HTTPRequest(this, checkAddress(address), post, headers, method, allowErrorBody)
8687
connections += request
8788
result(request)
8889
}
@@ -404,10 +405,14 @@ object InternetCard {
404405
}
405406

406407
class HTTPRequest extends AbstractValue with Closable {
407-
def this(owner: InternetCard, url: URL, post: Option[String], headers: Map[String, String], method: Option[String]) {
408+
def this(owner: InternetCard, url: URL, post: Option[String], headers: Map[String, String], method: Option[String], allowErrorBody: Boolean) {
408409
this()
409410
this.owner = Some(owner)
410-
this.stream = threadPool.submit(new RequestSender(url, post, headers, method))
411+
this.stream = threadPool.submit(new RequestSender(url, post, headers, method, allowErrorBody))
412+
}
413+
414+
def this(owner: InternetCard, url: URL, post: Option[String], headers: Map[String, String], method: Option[String]) {
415+
this(owner, url, post, headers, method, false)
411416
}
412417

413418
private var owner: Option[InternetCard] = None
@@ -506,7 +511,7 @@ object InternetCard {
506511
}
507512

508513
// This one doesn't (see comment in TCP socket), but I like to keep it consistent.
509-
private class RequestSender(val url: URL, val post: Option[String], val headers: Map[String, String], val method: Option[String]) extends Callable[InputStream] {
514+
private class RequestSender(val url: URL, val post: Option[String], val headers: Map[String, String], val method: Option[String], val allowErrorBody: Boolean) extends Callable[InputStream] {
510515
override def call() = try {
511516
checkLists(InetAddress.getByName(url.getHost), url.getHost)
512517
val proxy = Option(MinecraftServer.getServer.getServerProxy).getOrElse(java.net.Proxy.NO_PROXY)
@@ -529,20 +534,22 @@ object InternetCard {
529534
response = Some((http.getResponseCode, http.getResponseMessage, http.getHeaderFields))
530535
}
531536

532-
// For successful responses (2xx), use getInputStream()
533-
// For error responses (4xx, 5xx), use getErrorStream() if available, otherwise return empty stream
534537
val responseCode = http.getResponseCode
535538
if (responseCode >= 200 && responseCode < 300) {
539+
// Successful responses (2xx) - always use getInputStream()
536540
http.getInputStream
537-
} else {
538-
// For HTTP error responses, try to get the error stream
541+
} else if (allowErrorBody) {
542+
// HTTP error responses (4xx, 5xx) with allowErrorBody=true - try to get error stream
539543
val errorStream = http.getErrorStream
540544
if (errorStream != null) {
541545
errorStream
542546
} else {
543547
// If no error stream is available, return an empty stream
544548
new java.io.ByteArrayInputStream(Array.empty[Byte])
545549
}
550+
} else {
551+
// HTTP error responses (4xx, 5xx) with allowErrorBody=false - throw exception (old behavior)
552+
http.getInputStream // This will throw an exception for HTTP error responses
546553
}
547554
}
548555
catch {

0 commit comments

Comments
 (0)