Class NativeHttpBindings
java.lang.Object
ai.tegmentum.wasmtime4j.panama.NativeHttpBindings
Native function bindings for WASI HTTP operations.
Provides type-safe wrappers for all WASI HTTP native functions including config builder operations, config management, HTTP context lifecycle, statistics collection, and linker integration.
-
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddFunctionBinding(String functionName, FunctionDescriptor descriptor) Registers a native function binding with a given descriptor.<T> TcallNativeFunction(String functionName, Class<T> returnType, Object... args) Calls a native function with type safety.protected Object[]extractValueForNative(ai.tegmentum.wasmtime4j.WasmValue value) Extracts value components from WasmValue for passing to native code.protected ai.tegmentum.wasmtime4j.panama.NativeBindingsBase.FunctionBindinggetFunctionBinding(String functionName) Gets a registered function binding by name.static NativeHttpBindingsGets the singleton instance.booleanChecks if the bindings are initialized and available.protected voidMarks this binding set as initialized.protected voidvalidatePointer(MemorySegment pointer, String name) Validates that a pointer is not null.protected voidvalidateSize(long size, String name) Validates that a size is positive.intwasiHttpConfigBuilderAllowAllHosts(MemorySegment builderPtr, boolean allow) Sets whether all hosts are allowed.intwasiHttpConfigBuilderAllowHost(MemorySegment builderPtr, MemorySegment hostPtr) Adds an allowed host to the config builder.intwasiHttpConfigBuilderBlockHost(MemorySegment builderPtr, MemorySegment hostPtr) Adds a blocked host to the config builder.wasiHttpConfigBuilderBuild(MemorySegment builderPtr) Builds a WASI HTTP config from the builder.voidwasiHttpConfigBuilderFree(MemorySegment builderPtr) Frees a WASI HTTP config builder.Creates a new WASI HTTP config builder.intwasiHttpConfigBuilderSetCertificateValidation(MemorySegment builderPtr, boolean enabled) Sets whether certificate validation is enabled.intwasiHttpConfigBuilderSetConnectionPooling(MemorySegment builderPtr, boolean enabled) Sets whether connection pooling is enabled.intwasiHttpConfigBuilderSetConnectTimeout(MemorySegment builderPtr, long timeoutMs) Sets the connect timeout in milliseconds.intwasiHttpConfigBuilderSetFollowRedirects(MemorySegment builderPtr, boolean follow) Sets whether to follow redirects.intwasiHttpConfigBuilderSetHttp2Enabled(MemorySegment builderPtr, boolean enabled) Sets whether HTTP/2 is enabled.intwasiHttpConfigBuilderSetHttpsRequired(MemorySegment builderPtr, boolean required) Sets whether HTTPS is required.intwasiHttpConfigBuilderSetMaxConnections(MemorySegment builderPtr, int maxConnections) Sets the maximum number of connections.intwasiHttpConfigBuilderSetMaxConnectionsPerHost(MemorySegment builderPtr, int maxConnectionsPerHost) Sets the maximum connections per host.intwasiHttpConfigBuilderSetMaxRedirects(MemorySegment builderPtr, int maxRedirects) Sets the maximum number of redirects to follow.intwasiHttpConfigBuilderSetMaxRequestBodySize(MemorySegment builderPtr, long maxSize) Sets the maximum request body size.intwasiHttpConfigBuilderSetMaxResponseBodySize(MemorySegment builderPtr, long maxSize) Sets the maximum response body size.intwasiHttpConfigBuilderSetReadTimeout(MemorySegment builderPtr, long timeoutMs) Sets the read timeout in milliseconds.intwasiHttpConfigBuilderSetUserAgent(MemorySegment builderPtr, MemorySegment userAgentPtr) Sets the user agent string.intwasiHttpConfigBuilderSetWriteTimeout(MemorySegment builderPtr, long timeoutMs) Sets the write timeout in milliseconds.Creates a default WASI HTTP config.voidwasiHttpConfigFree(MemorySegment configPtr) Frees a WASI HTTP config.voidwasiHttpContextFree(MemorySegment contextPtr) Frees a WASI HTTP context.longwasiHttpContextGetId(MemorySegment contextPtr) Gets the ID of a WASI HTTP context.intwasiHttpContextIsHostAllowed(MemorySegment contextPtr, MemorySegment hostPtr) Checks if a host is allowed by the context.intwasiHttpContextIsValid(MemorySegment contextPtr) Checks if a WASI HTTP context is valid.wasiHttpContextNew(MemorySegment configPtr) Creates a new WASI HTTP context with the specified config.Creates a new WASI HTTP context with default config.intwasiHttpContextResetStats(MemorySegment contextPtr) Resets the statistics for a WASI HTTP context.intwasiHttpContextStatsActiveConnections(MemorySegment contextPtr) Gets the number of active connections.intwasiHttpContextStatsActiveRequests(MemorySegment contextPtr) Gets the number of active requests.longwasiHttpContextStatsAvgDurationMs(MemorySegment contextPtr) Gets the average request duration in milliseconds.longwasiHttpContextStatsBlockedRequests(MemorySegment contextPtr) Gets the number of blocked requests.longwasiHttpContextStatsBodySizeViolations(MemorySegment contextPtr) Gets the number of body size violations.longwasiHttpContextStatsBytesReceived(MemorySegment contextPtr) Gets the total bytes received.longwasiHttpContextStatsBytesSent(MemorySegment contextPtr) Gets the total bytes sent.longwasiHttpContextStatsConnectionTimeouts(MemorySegment contextPtr) Gets the number of connection timeouts.longwasiHttpContextStatsFailedRequests(MemorySegment contextPtr) Gets the number of failed requests.intwasiHttpContextStatsIdleConnections(MemorySegment contextPtr) Gets the number of idle connections.longwasiHttpContextStatsMaxDurationMs(MemorySegment contextPtr) Gets the maximum request duration in milliseconds.longwasiHttpContextStatsMinDurationMs(MemorySegment contextPtr) Gets the minimum request duration in milliseconds.longwasiHttpContextStatsReadTimeouts(MemorySegment contextPtr) Gets the number of read timeouts.longwasiHttpContextStatsSuccessfulRequests(MemorySegment contextPtr) Gets the number of successful requests.longwasiHttpContextStatsTotalRequests(MemorySegment contextPtr) Gets the total number of requests.intChecks if WASI HTTP support is available.
-
Method Details
-
getInstance
Gets the singleton instance.- Returns:
- the singleton instance
- Throws:
RuntimeException- if initialization fails
-
wasiHttpConfigBuilderNew
Creates a new WASI HTTP config builder.- Returns:
- pointer to the config builder, or null on failure
-
wasiHttpConfigBuilderAllowHost
Adds an allowed host to the config builder.- Parameters:
builderPtr- pointer to the config builderhostPtr- pointer to the host string- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderBlockHost
Adds a blocked host to the config builder.- Parameters:
builderPtr- pointer to the config builderhostPtr- pointer to the host string- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderAllowAllHosts
Sets whether all hosts are allowed.- Parameters:
builderPtr- pointer to the config builderallow- true to allow all hosts- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetConnectTimeout
Sets the connect timeout in milliseconds.- Parameters:
builderPtr- pointer to the config buildertimeoutMs- timeout in milliseconds- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetReadTimeout
Sets the read timeout in milliseconds.- Parameters:
builderPtr- pointer to the config buildertimeoutMs- timeout in milliseconds- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetWriteTimeout
Sets the write timeout in milliseconds.- Parameters:
builderPtr- pointer to the config buildertimeoutMs- timeout in milliseconds- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetMaxConnections
Sets the maximum number of connections.- Parameters:
builderPtr- pointer to the config buildermaxConnections- maximum connections- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetMaxConnectionsPerHost
public int wasiHttpConfigBuilderSetMaxConnectionsPerHost(MemorySegment builderPtr, int maxConnectionsPerHost) Sets the maximum connections per host.- Parameters:
builderPtr- pointer to the config buildermaxConnectionsPerHost- maximum connections per host- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetMaxRequestBodySize
Sets the maximum request body size.- Parameters:
builderPtr- pointer to the config buildermaxSize- maximum size in bytes- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetMaxResponseBodySize
Sets the maximum response body size.- Parameters:
builderPtr- pointer to the config buildermaxSize- maximum size in bytes- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetHttpsRequired
Sets whether HTTPS is required.- Parameters:
builderPtr- pointer to the config builderrequired- true to require HTTPS- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetCertificateValidation
Sets whether certificate validation is enabled.- Parameters:
builderPtr- pointer to the config builderenabled- true to enable certificate validation- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetHttp2Enabled
Sets whether HTTP/2 is enabled.- Parameters:
builderPtr- pointer to the config builderenabled- true to enable HTTP/2- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetConnectionPooling
Sets whether connection pooling is enabled.- Parameters:
builderPtr- pointer to the config builderenabled- true to enable connection pooling- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetFollowRedirects
Sets whether to follow redirects.- Parameters:
builderPtr- pointer to the config builderfollow- true to follow redirects- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetMaxRedirects
Sets the maximum number of redirects to follow.- Parameters:
builderPtr- pointer to the config buildermaxRedirects- maximum redirects- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderSetUserAgent
Sets the user agent string.- Parameters:
builderPtr- pointer to the config builderuserAgentPtr- pointer to the user agent string- Returns:
- 0 on success, negative error code on failure
-
wasiHttpConfigBuilderBuild
Builds a WASI HTTP config from the builder.- Parameters:
builderPtr- pointer to the config builder- Returns:
- pointer to the config, or null on failure
-
wasiHttpConfigBuilderFree
Frees a WASI HTTP config builder.- Parameters:
builderPtr- pointer to the config builder
-
wasiHttpConfigDefault
Creates a default WASI HTTP config.- Returns:
- pointer to the config, or null on failure
-
wasiHttpConfigFree
Frees a WASI HTTP config.- Parameters:
configPtr- pointer to the config
-
wasiHttpContextNew
Creates a new WASI HTTP context with the specified config.- Parameters:
configPtr- pointer to the config- Returns:
- pointer to the context, or null on failure
-
wasiHttpContextNewDefault
Creates a new WASI HTTP context with default config.- Returns:
- pointer to the context, or null on failure
-
wasiHttpContextGetId
Gets the ID of a WASI HTTP context.- Parameters:
contextPtr- pointer to the context- Returns:
- the context ID
-
wasiHttpContextIsValid
Checks if a WASI HTTP context is valid.- Parameters:
contextPtr- pointer to the context- Returns:
- 1 if valid, 0 otherwise
-
wasiHttpContextIsHostAllowed
Checks if a host is allowed by the context.- Parameters:
contextPtr- pointer to the contexthostPtr- pointer to the host string- Returns:
- 1 if allowed, 0 otherwise
-
wasiHttpContextResetStats
Resets the statistics for a WASI HTTP context.- Parameters:
contextPtr- pointer to the context- Returns:
- 0 on success, negative error code on failure
-
wasiHttpContextStatsTotalRequests
Gets the total number of requests.- Parameters:
contextPtr- pointer to the context- Returns:
- total request count
-
wasiHttpContextStatsSuccessfulRequests
Gets the number of successful requests.- Parameters:
contextPtr- pointer to the context- Returns:
- successful request count
-
wasiHttpContextStatsFailedRequests
Gets the number of failed requests.- Parameters:
contextPtr- pointer to the context- Returns:
- failed request count
-
wasiHttpContextStatsActiveRequests
Gets the number of active requests.- Parameters:
contextPtr- pointer to the context- Returns:
- active request count
-
wasiHttpContextStatsBytesSent
Gets the total bytes sent.- Parameters:
contextPtr- pointer to the context- Returns:
- total bytes sent
-
wasiHttpContextStatsBytesReceived
Gets the total bytes received.- Parameters:
contextPtr- pointer to the context- Returns:
- total bytes received
-
wasiHttpContextStatsConnectionTimeouts
Gets the number of connection timeouts.- Parameters:
contextPtr- pointer to the context- Returns:
- connection timeout count
-
wasiHttpContextStatsReadTimeouts
Gets the number of read timeouts.- Parameters:
contextPtr- pointer to the context- Returns:
- read timeout count
-
wasiHttpContextStatsBlockedRequests
Gets the number of blocked requests.- Parameters:
contextPtr- pointer to the context- Returns:
- blocked request count
-
wasiHttpContextStatsBodySizeViolations
Gets the number of body size violations.- Parameters:
contextPtr- pointer to the context- Returns:
- body size violation count
-
wasiHttpContextStatsActiveConnections
Gets the number of active connections.- Parameters:
contextPtr- pointer to the context- Returns:
- active connection count
-
wasiHttpContextStatsIdleConnections
Gets the number of idle connections.- Parameters:
contextPtr- pointer to the context- Returns:
- idle connection count
-
wasiHttpContextStatsAvgDurationMs
Gets the average request duration in milliseconds.- Parameters:
contextPtr- pointer to the context- Returns:
- average duration in milliseconds
-
wasiHttpContextStatsMinDurationMs
Gets the minimum request duration in milliseconds.- Parameters:
contextPtr- pointer to the context- Returns:
- minimum duration in milliseconds
-
wasiHttpContextStatsMaxDurationMs
Gets the maximum request duration in milliseconds.- Parameters:
contextPtr- pointer to the context- Returns:
- maximum duration in milliseconds
-
wasiHttpContextFree
Frees a WASI HTTP context.- Parameters:
contextPtr- pointer to the context
-
wasiHttpIsAvailable
public int wasiHttpIsAvailable()Checks if WASI HTTP support is available.- Returns:
- 1 if WASI HTTP is available, 0 otherwise
-
markInitialized
protected void markInitialized()Marks this binding set as initialized. Subclasses should call this after registering all function bindings and initializing hot-path MethodHandles. -
isInitialized
public boolean isInitialized()Checks if the bindings are initialized and available.- Returns:
- true if initialized, false otherwise
-
addFunctionBinding
Registers a native function binding with a given descriptor.- Parameters:
functionName- the native function symbol namedescriptor- the Panama FunctionDescriptor for the native function
-
getFunctionBinding
protected ai.tegmentum.wasmtime4j.panama.NativeBindingsBase.FunctionBinding getFunctionBinding(String functionName) Gets a registered function binding by name.- Parameters:
functionName- the native function symbol name- Returns:
- the function binding, or null if not registered
-
callNativeFunction
Calls a native function with type safety.- Parameters:
functionName- the name of the functionreturnType- the expected return typeargs- the function arguments- Returns:
- the function result
- Throws:
IllegalStateException- if the function call fails
-
validatePointer
Validates that a pointer is not null.- Parameters:
pointer- the pointer to validatename- the name of the parameter for error messages- Throws:
IllegalArgumentException- if the pointer is null
-
validateSize
Validates that a size is positive.- Parameters:
size- the size to validatename- the name of the parameter for error messages- Throws:
IllegalArgumentException- if the size is not positive
-
extractValueForNative
Extracts value components from WasmValue for passing to native code.- Parameters:
value- the WasmValue to extract components from- Returns:
- array containing [i32Value, i64Value, f32Value, f64Value, refValue]
-