11package net .lightbody .bmp .proxy .http ;
22
3- import website .magyar .mitm .proxy .ProxyServer ;
4- import website .magyar .mitm .proxy .RequestInterceptor ;
5- import website .magyar .mitm .proxy .ResponseInterceptor ;
6- import website .magyar .mitm .proxy .http .MitmJavaProxyHttpRequest ;
7- import website .magyar .mitm .proxy .http .MitmJavaProxyHttpResponse ;
83import net .lightbody .bmp .core .har .Har ;
94import net .lightbody .bmp .core .har .HarCookie ;
105import net .lightbody .bmp .core .har .HarEntry ;
6560import org .apache .http .protocol .BasicHttpContext ;
6661import org .apache .http .protocol .ExecutionContext ;
6762import org .apache .http .protocol .HttpContext ;
63+ import org .brotli .dec .BrotliInputStream ;
6864import org .slf4j .Logger ;
6965import org .slf4j .LoggerFactory ;
7066import org .xbill .DNS .Cache ;
7167import org .xbill .DNS .DClass ;
68+ import website .magyar .mitm .proxy .ProxyServer ;
69+ import website .magyar .mitm .proxy .RequestInterceptor ;
70+ import website .magyar .mitm .proxy .ResponseInterceptor ;
71+ import website .magyar .mitm .proxy .http .MitmJavaProxyHttpRequest ;
72+ import website .magyar .mitm .proxy .http .MitmJavaProxyHttpResponse ;
7273
7374import java .io .ByteArrayInputStream ;
7475import java .io .ByteArrayOutputStream ;
@@ -115,15 +116,15 @@ public class BrowserMobHttpClient {
115116 // not using CopyOnWriteArray because we're WRITE heavy and it is for READ heavy operations
116117 // instead doing it the old fashioned way with a synchronized block
117118 private final Set <ActiveRequest > activeRequests = new HashSet <ActiveRequest >();
119+ private final SimulatedSocketFactory socketFactory ;
120+ private final TrustingSSLSocketFactory sslSocketFactory ;
121+ private final PoolingHttpClientConnectionManager httpClientConnMgr ;
122+ private final HttpClient httpClient ;
118123 private Har har ;
119124 private String harPageRef ;
120125 private boolean captureHeaders ;
121126 private boolean captureContent ; // if captureContent is set, default policy is to capture binary contents too
122127 private boolean captureBinaryContent = true ;
123- private final SimulatedSocketFactory socketFactory ;
124- private final TrustingSSLSocketFactory sslSocketFactory ;
125- private final PoolingHttpClientConnectionManager httpClientConnMgr ;
126- private final HttpClient httpClient ;
127128 private int requestTimeout ;
128129 private BrowserMobHostNameResolver hostNameResolver ;
129130 private boolean decompress = true ;
@@ -425,6 +426,7 @@ private MitmJavaProxyHttpResponse execute(final MitmJavaProxyHttpRequest req, in
425426 long bytes = 0 ;
426427 boolean gzipping = false ;
427428 boolean deflating = false ;
429+ boolean brotling = false ;
428430 OutputStream os = req .getOutputStream ();
429431 if (os == null ) {
430432 os = new CappedByteArrayOutputStream (MAX_BUFFER_SIZE );
@@ -459,7 +461,7 @@ private MitmJavaProxyHttpResponse execute(final MitmJavaProxyHttpRequest req, in
459461 try {
460462 // set the User-Agent if it's not already set
461463 if (method .getHeaders ("User-Agent" ).length == 0 ) {
462- method .addHeader ("User-Agent" , "MITM-JavaProxy V/1.0 " );
464+ method .addHeader ("User-Agent" , "MITM-JavaProxy V-22 " );
463465 }
464466
465467 response = httpClient .execute (method , ctx );
@@ -480,14 +482,21 @@ private MitmJavaProxyHttpResponse execute(final MitmJavaProxyHttpRequest req, in
480482 if (is != null ) {
481483 Header contentEncodingHeader = response .getFirstHeader ("Content-Encoding" );
482484 if (contentEncodingHeader != null ) {
483- if ("gzip" .equalsIgnoreCase (contentEncodingHeader .getValue ())) {
485+ String value = contentEncodingHeader .getValue ();
486+ if ("gzip" .equalsIgnoreCase (value )) {
484487 gzipping = true ;
485- } else if ("deflate" .equalsIgnoreCase (contentEncodingHeader .getValue ())) {
486- deflating = true ;
488+ } else {
489+ if ("deflate" .equalsIgnoreCase (value )) {
490+ deflating = true ;
491+ } else {
492+ if ("br" .equalsIgnoreCase (value )) {
493+ brotling = true ;
494+ }
495+ }
487496 }
488497 }
489498
490- // deal with GZIP content!
499+ // deal with compressed content!
491500 if (decompress && response .getEntity ().getContentLength () != 0 ) { //getContentLength<0 if unknown
492501 if (gzipping ) {
493502 is = new GZIPInputStream (is );
@@ -496,6 +505,10 @@ private MitmJavaProxyHttpResponse execute(final MitmJavaProxyHttpRequest req, in
496505 // WARN : if system is using zlib<=1.1.4 the stream must be append with a dummy byte
497506 // that is not required for zlib>1.1.4 (not mentioned on current Inflater javadoc)
498507 is = new InflaterInputStream (is , new Inflater (true ));
508+ } else {
509+ if (brotling ) {
510+ is = new BrotliInputStream (is );
511+ }
499512 }
500513 }
501514 }
@@ -638,17 +651,23 @@ private MitmJavaProxyHttpResponse execute(final MitmJavaProxyHttpRequest req, in
638651 copy = bos ;
639652 }
640653 if (captureContent && enableWorkWithCopy ) {
641- if (entry .getResponse ().getBodySize () != 0 && (gzipping || deflating )) {
654+ if (entry .getResponse ().getBodySize () != 0 && (gzipping || deflating || brotling )) {
642655 // ok, we need to decompress it before we can put it in the har file
643656 try {
644657 InputStream temp = null ;
645658 if (gzipping ) {
646659 temp = new GZIPInputStream (new ByteArrayInputStream (copy .toByteArray ()));
647- } else if (deflating ) {
648- //RAW deflate only
649- // WARN : if system is using zlib<=1.1.4 the stream must be append with a dummy byte
650- // that is not required for zlib>1.1.4 (not mentioned on current Inflater javadoc)
651- temp = new InflaterInputStream (new ByteArrayInputStream (copy .toByteArray ()), new Inflater (true ));
660+ } else {
661+ if (deflating ) {
662+ //RAW deflate only
663+ // WARN : if system is using zlib<=1.1.4 the stream must be append with a dummy byte
664+ // that is not required for zlib>1.1.4 (not mentioned on current Inflater javadoc)
665+ temp = new InflaterInputStream (new ByteArrayInputStream (copy .toByteArray ()), new Inflater (true ));
666+ } else {
667+ if (brotling ) {
668+ temp = new BrotliInputStream (new ByteArrayInputStream (copy .toByteArray ()));
669+ }
670+ }
652671 }
653672 copy = new ByteArrayOutputStream ();
654673 IOUtils .copy (temp , copy );
0 commit comments