@@ -60,7 +60,7 @@ class InternetCard extends prefab.ManagedEnvironment with DeviceInfo {
60
60
@ Callback (direct = true , doc = """ function():boolean -- Returns whether HTTP requests can be made (config setting).""" )
61
61
def isHttpEnabled (context : Context , args : Arguments ): Array [AnyRef ] = result(Settings .get.httpEnabled)
62
62
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 .""" )
64
64
def request (context : Context , args : Arguments ): Array [AnyRef ] = this .synchronized {
65
65
checkOwner(context)
66
66
val address = args.checkString(0 )
@@ -82,7 +82,8 @@ class InternetCard extends prefab.ManagedEnvironment with DeviceInfo {
82
82
return result(Unit , " http request headers are unavailable" )
83
83
}
84
84
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)
86
87
connections += request
87
88
result(request)
88
89
}
@@ -404,10 +405,14 @@ object InternetCard {
404
405
}
405
406
406
407
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 ) {
408
409
this ()
409
410
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 )
411
416
}
412
417
413
418
private var owner : Option [InternetCard ] = None
@@ -506,7 +511,7 @@ object InternetCard {
506
511
}
507
512
508
513
// 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 ] {
510
515
override def call () = try {
511
516
checkLists(InetAddress .getByName(url.getHost), url.getHost)
512
517
val proxy = Option (MinecraftServer .getServer.getServerProxy).getOrElse(java.net.Proxy .NO_PROXY )
@@ -529,20 +534,22 @@ object InternetCard {
529
534
response = Some ((http.getResponseCode, http.getResponseMessage, http.getHeaderFields))
530
535
}
531
536
532
- // For successful responses (2xx), use getInputStream()
533
- // For error responses (4xx, 5xx), use getErrorStream() if available, otherwise return empty stream
534
537
val responseCode = http.getResponseCode
535
538
if (responseCode >= 200 && responseCode < 300 ) {
539
+ // Successful responses (2xx) - always use getInputStream()
536
540
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
539
543
val errorStream = http.getErrorStream
540
544
if (errorStream != null ) {
541
545
errorStream
542
546
} else {
543
547
// If no error stream is available, return an empty stream
544
548
new java.io.ByteArrayInputStream (Array .empty[Byte ])
545
549
}
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
546
553
}
547
554
}
548
555
catch {
0 commit comments