Compressor.vala 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using Invercargill;
  2. using Invercargill.DataStructures;
  3. using Inversion;
  4. namespace Astralis {
  5. public abstract class Compressor : Object, PipelineComponent {
  6. protected uint64 max_buffer_size = 1024 * 1024 * 16;
  7. protected Compressor(uint64 max_buffer_size = 1024 * 1024 * 16) {
  8. this.max_buffer_size = max_buffer_size;
  9. }
  10. // The encoding token that identifies this compression format (e.g., "gzip")
  11. public abstract string encoding_token { get; }
  12. public async HttpResult process_request(HttpContext http_context, PipelineContext pipeline_context) throws Error {
  13. var result = yield pipeline_context.next();
  14. // Check existing encoding on http result (to avoid double encoding)
  15. // Do not compress if: DO_NOT_COMPRESS flag set, or `Content-Encoding` is set and is not "identity"
  16. var existing_encoding = http_context.request.headers.get_any_or_default("Content-Encoding");
  17. if (result.flag_is_set(HttpResultFlag.DO_NOT_COMPRESS) || (existing_encoding != null && existing_encoding != "identity")) {
  18. return result;
  19. }
  20. var accept_encoding = http_context.request.headers.get_any_or_default("Accept-Encoding");
  21. // Check if the client accepts this encoding
  22. if (accept_encoding == null || !accept_encoding.contains(encoding_token)) {
  23. return result;
  24. }
  25. // Don't event bother compressing if the length is less than 10 bytes (length of GZip header)
  26. if(result.content_length != null && result.content_length <= 10) {
  27. return result;
  28. }
  29. // Case 1: Content length known and within threshold -> buffer and compress
  30. if (result.content_length != null && result.content_length <= max_buffer_size) {
  31. var buffered_result = yield buffer_and_compress(result);
  32. if (buffered_result != null) {
  33. buffered_result.set_header("Content-Encoding", encoding_token);
  34. buffered_result.set_header("Vary", "Accept-Encoding");
  35. return buffered_result;
  36. }
  37. return result;
  38. }
  39. // Case 2: Content length known and above threshold, and `DO_NOT_CHUNK` is set -> do nothing
  40. else if(result.content_length != null && result.flag_is_set(HttpResultFlag.DO_NOT_CHUNK)) {
  41. return result;
  42. }
  43. // Case 3: Content length above threshold or is unknown -> send chunked response (no Content-Length header)
  44. var streaming_result = compress_chunked(result);
  45. streaming_result.set_header("Content-Encoding", encoding_token);
  46. streaming_result.set_header("Vary", "Accept-Encoding");
  47. return streaming_result;
  48. }
  49. /// Compress a ByteBuffer of data, returning null if compression doesn't reduce size
  50. /// @param data The data to compress
  51. /// @param content_type Optional content type hint for compression optimization
  52. /// @return Compressed data as ByteBuffer, or null if compression doesn't reduce size
  53. public abstract ByteBuffer compress_buffer(ByteBuffer data, string? content_type) throws Error;
  54. /// Create a streaming compression result for the given inner result, or null if not compressible
  55. public abstract HttpResult compress_chunked(HttpResult inner_result) throws Error;
  56. /// Buffer and compress the result, returning null if compression doesn't reduce size
  57. private async HttpResult? buffer_and_compress(HttpResult inner_result) throws Error {
  58. // Buffer the input data
  59. var input_buffer = new BufferAsyncOutput();
  60. yield inner_result.send_body(input_buffer);
  61. var input_data = input_buffer.get_buffer();
  62. // Get content type for potential compression optimization
  63. string? content_type = null;
  64. if (inner_result.headers.has("Content-Type")) {
  65. content_type = inner_result.headers["Content-Type"];
  66. }
  67. // Compress the data
  68. ByteBuffer? compressed_data = compress_buffer(input_data, content_type);
  69. if (compressed_data == null) {
  70. return null;
  71. }
  72. // Check if compressed size is larger than or equal to original
  73. if (compressed_data.length >= input_data.length) {
  74. return null;
  75. }
  76. // Construct HttpDataResult and copy headers
  77. var http_result = new HttpDataResult(compressed_data, inner_result.status);
  78. copy_headers(inner_result, http_result, {"content-length", "content-encoding"});
  79. return http_result;
  80. }
  81. protected static void copy_headers(HttpResult? source, HttpResult dest, string[] skip_headers) {
  82. if (source == null) {
  83. return;
  84. }
  85. foreach (var header in source.headers) {
  86. var lower_key = header.key.down();
  87. bool skip = false;
  88. foreach (var skip_key in skip_headers) {
  89. if (lower_key == skip_key) {
  90. skip = true;
  91. break;
  92. }
  93. }
  94. if (!skip) {
  95. dest.set_header(header.key, header.value);
  96. }
  97. }
  98. }
  99. }
  100. }