66import java .io .ByteArrayOutputStream ;
77import java .io .IOException ;
88import java .io .OutputStreamWriter ;
9+ import java .net .HttpURLConnection ;
910import java .net .InetSocketAddress ;
1011import java .net .URLDecoder ;
12+ import java .util .List ;
1113import java .util .Set ;
1214import java .util .HashSet ;
1315import java .util .concurrent .Executors ;
16+ import java .util .zip .GZIPOutputStream ;
1417
1518import com .sun .net .httpserver .HttpHandler ;
1619import com .sun .net .httpserver .HttpServer ;
2730 * </pre>
2831 * */
2932public class HTTPServer {
33+ private static class LocalByteArray extends ThreadLocal <ByteArrayOutputStream > {
34+ protected ByteArrayOutputStream initialValue ()
35+ {
36+ return new ByteArrayOutputStream (1 << 20 );
37+ }
38+ }
39+
3040 static class HTTPMetricHandler implements HttpHandler {
3141 private CollectorRegistry registry ;
42+ private final LocalByteArray response = new LocalByteArray ();
3243
3344 HTTPMetricHandler (CollectorRegistry registry ) {
3445 this .registry = registry ;
@@ -38,7 +49,8 @@ static class HTTPMetricHandler implements HttpHandler {
3849 public void handle (HttpExchange t ) throws IOException {
3950 String query = t .getRequestURI ().getRawQuery ();
4051
41- ByteArrayOutputStream response = new ByteArrayOutputStream (1 << 20 );
52+ ByteArrayOutputStream response = this .response .get ();
53+ response .reset ();
4254 OutputStreamWriter osw = new OutputStreamWriter (response );
4355 TextFormat .write004 (osw ,
4456 registry .filteredMetricFamilySamples (parseQuery (query )));
@@ -51,13 +63,36 @@ public void handle(HttpExchange t) throws IOException {
5163 TextFormat .CONTENT_TYPE_004 );
5264 t .getResponseHeaders ().set ("Content-Length" ,
5365 String .valueOf (response .size ()));
54- t .sendResponseHeaders (200 , response .size ());
55- response .writeTo (t .getResponseBody ());
66+ if (shouldUseCompression (t )) {
67+ t .getResponseHeaders ().set ("Content-Encoding" , "gzip" );
68+ t .sendResponseHeaders (HttpURLConnection .HTTP_OK , 0 );
69+ final GZIPOutputStream os = new GZIPOutputStream (t .getResponseBody ());
70+ response .writeTo (os );
71+ os .finish ();
72+ } else {
73+ t .sendResponseHeaders (HttpURLConnection .HTTP_OK , response .size ());
74+ response .writeTo (t .getResponseBody ());
75+ }
5676 t .close ();
5777 }
5878
5979 }
6080
81+ protected static boolean shouldUseCompression (HttpExchange exchange ) {
82+ List <String > encodingHeaders = exchange .getRequestHeaders ().get ("Accept-Encoding" );
83+ if (encodingHeaders == null ) return false ;
84+
85+ for (String encodingHeader : encodingHeaders ) {
86+ String [] encodings = encodingHeader .split ("," );
87+ for (String encoding : encodings ) {
88+ if (encoding .trim ().toLowerCase ().equals ("gzip" )) {
89+ return true ;
90+ }
91+ }
92+ }
93+ return false ;
94+ }
95+
6196 protected static Set <String > parseQuery (String query ) throws IOException {
6297 Set <String > names = new HashSet <String >();
6398 if (query != null ) {
0 commit comments