Reputation: 680
Forewarning: I’m very new to Haxe.
I’m trying to use http.customRequest (with the intention of later making PUT and DELETE requests). But when I try to access the result bytes I get a segmentation fault with C++ and a NullPointerException with Java.
I’ve googled for some other uses of customRequest, and what I’m doing doesn’t seem wrong, but clearly it is.
class Main {
static function main() {
var req = new haxe.Http("https://httpbin.org/put");
var responseBytes = new haxe.io.BytesOutput();
req.onError = function(err) {
trace("onError");
trace(err); // Java says NullPointerException
};
req.onStatus = function(status) {
trace("About to get bytes");
// Removing these lines prevents the errors
var b = responseBytes.getBytes();
trace("Got the bytes");
trace(b.length); // Shouldn't be empty, but is
};
req.customRequest(false, responseBytes, null, "PUT");
}
}
I’ve tried this with the current release and with HEAD (via Brew).
I think my command lines are pretty basic:
$ haxe -cp src -main Main -java bin/java
$ java -jar bin/java/Main.jar
src/Main.hx:12: About to get bytes
src/Main.hx:16: Got the bytes
src/Main.hx:17: 0
src/Main.hx:7: onError
src/Main.hx:8: java.lang.NullPointerException
$ haxe -cp src -main Main -cpp bin/cpp
$ ./bin/cpp/Main
src/Main.hx:12: About to get bytes
src/Main.hx:16: Got the bytes
src/Main.hx:17: 0
[1] 54544 segmentation fault ./bin/cpp/Main
In case it’s useful, here’s the differently broken output for Python:
$ haxe -cp src -main Main -python bin/Main.py
$ python3 bin/Main.py
onError
True
About to trace err
SSLError(1, '[SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1045)')
I’d really appreciate any guidance. TIA
(Reposted from the Haxe forum, where I haven't had a response.)
Upvotes: 1
Views: 334
Reputation: 34138
That's quite an interesting interaction there, here's what happens in order:
sys.Http
receives the response and calls onStatus()
.responseBytes.getBytes()
, which ends up invalidating the internal buffer in haxe.io.BytesBuffer.getBytes()
. The docs of that method state "Once called, the buffer can no longer be used", as it sets the internal buffer b
to null
.Http
class then attempts to write to that same buffer, which is no longer valid.onError()
callback is called due to the null reference.The status code passed to onStatus()
is 200 (OK), so apart from the getBytes()
call, the request seems to work as expected. And according to apitester.com, data
is empty for this particular request.
Upvotes: 3