| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using Invercargill;
- using Invercargill.DataStructures;
- using Astralis;
- namespace Statum {
- /**
- * A precompressed, named resource served from the Statum resource endpoint
- * (`/_statum/resource/{name}`).
- *
- * Mirrors the old Spry 0.1 `StaticResource` (removed in Spry 0.2): each
- * concrete resource exposes
- * the encodings it was precompressed with (identity/gzip/zstd/br), an ETag
- * per encoding, and the ability to render itself as an {@link HttpResult}.
- */
- public interface StatumResource : Object {
- /** The name the resource is served under (the `{name}` path segment). */
- public abstract string name { get; }
- /**
- * Selects the best supported encoding for a request given the set of
- * `Accept-Encoding` values the client advertised.
- */
- public abstract string get_best_encoding(Set<string> supported);
- /** The ETag to send for a given encoding. */
- public abstract string get_etag_for(string encoding);
- /** Renders the resource for the given encoding as an {@link HttpResult}. */
- public abstract HttpResult to_result(string encoding, StatusCode status = StatusCode.OK) throws GLib.Error;
- }
- /**
- * A {@link StatumResource} whose bytes are baked in at compile time.
- *
- * Concrete subclasses (typically generated by `statum-mkres`) provide the
- * precompressed byte arrays and the abstract metadata/encoding members; this
- * base supplies the {@link get_etag_for}/{@link to_result} implementation.
- */
- public abstract class ConstantStatumResource : Object, StatumResource {
- public abstract string name { get; }
- public abstract string file_hash { get; }
- public abstract string content_type { get; }
- public abstract string get_best_encoding(Set<string> supported);
- public abstract unowned uint8[] get_encoding(string encoding);
- public string get_etag_for(string encoding) {
- return @"\"$encoding-$file_hash\"";
- }
- public HttpResult to_result(string encoding, StatusCode status = StatusCode.OK) throws GLib.Error {
- var result = new HttpDataResult(Wrap.byte_array(get_encoding(encoding)), status);
- result.set_header("Content-Type", content_type);
- result.set_header("Content-Encoding", encoding);
- result.set_header("ETag", get_etag_for(encoding));
- return result;
- }
- }
- }
|