Browse Source

bwauth: check and use compression serving bw file

juga0 6 years ago
parent
commit
4d3502e45b
1 changed files with 17 additions and 3 deletions
  1. 17 3
      src/feature/dircache/dircache.c

+ 17 - 3
src/feature/dircache/dircache.c

@@ -1449,14 +1449,28 @@ handle_get_next_bandwidth(dir_connection_t *conn,
   (void)args;
   log_debug(LD_DIR, "Getting next bandwidth.");
   const or_options_t *options = get_options();
+  const compress_method_t compress_method =
+    find_best_compression_method(args->compression_supported, 1);
+
   if (options->V3BandwidthsFile) {
     int lifetime = 60;
     char *bandwidth = read_file_to_str(options->V3BandwidthsFile,
                                        RFTS_IGNORE_MISSING, NULL);
     if (bandwidth != NULL) {
-      site_t len = strlen(bandwidth);
-      write_http_response_header(conn, len, NO_METHOD, lifetime);
-      connection_buf_add(bandwidth, len, TO_CONN(conn));
+      ssize_t len = strlen(bandwidth);
+      write_http_response_header(conn, compress_method != NO_METHOD ? -1 : len,
+                                 compress_method, BANDWIDTH_CACHE_LIFETIME);
+      if (compress_method != NO_METHOD) {
+        conn->compress_state = tor_compress_new(1, compress_method,
+                                        choose_compression_level(len/2));
+        log_debug(LD_DIR, "Compressing bandwidth file.");
+        connection_buf_add_compress(bandwidth, len, conn, 0);
+        /* Flush the compression state. */
+        connection_buf_add_compress("", 0, conn, 1);
+      } else {
+        log_debug(LD_DIR, "Not compressing bandwidth file.");
+        connection_buf_add(bandwidth, len, TO_CONN(conn));
+      }
       tor_free(bandwidth);
       return 0;
     }