2727 */
2828class Google_Http_Request
2929{
30+ const GZIP_UA = " (gzip) " ;
31+
3032 private $ batchHeaders = array (
3133 'Content-Type ' => 'application/http ' ,
3234 'Content-Transfer-Encoding ' => 'binary ' ,
@@ -40,6 +42,7 @@ class Google_Http_Request
4042 protected $ path ;
4143 protected $ postBody ;
4244 protected $ userAgent ;
45+ protected $ canGzip = null ;
4346
4447 protected $ responseHttpCode ;
4548 protected $ responseHeaders ;
@@ -79,6 +82,40 @@ public function setBaseComponent($baseComponent)
7982 {
8083 $ this ->baseComponent = $ baseComponent ;
8184 }
85+
86+ /**
87+ * Enable support for gzipped responses with this request.
88+ */
89+ public function enableGzip ()
90+ {
91+ $ this ->setRequestHeaders (array ("Accept-Encoding " => "gzip " ));
92+ $ this ->canGzip = true ;
93+ $ this ->setUserAgent ($ this ->userAgent );
94+ }
95+
96+ /**
97+ * Disable support for gzip responses with this request.
98+ */
99+ public function disableGzip ()
100+ {
101+ if (
102+ isset ($ this ->requestHeaders ['accept-encoding ' ]) &&
103+ $ this ->requestHeaders ['accept-encoding ' ] == "gzip "
104+ ) {
105+ unset($ this ->requestHeaders ['accept-encoding ' ]);
106+ }
107+ $ this ->canGzip = false ;
108+ $ this ->userAgent = str_replace (self ::GZIP_UA , "" , $ this ->userAgent );
109+ }
110+
111+ /**
112+ * Can this request accept a gzip response?
113+ * @return bool
114+ */
115+ public function canGzip ()
116+ {
117+ return $ this ->canGzip ;
118+ }
82119
83120 /**
84121 * Misc function that returns an array of the query parameters of the current
@@ -297,6 +334,9 @@ public function setPostBody($postBody)
297334 public function setUserAgent ($ userAgent )
298335 {
299336 $ this ->userAgent = $ userAgent ;
337+ if ($ this ->canGzip ) {
338+ $ this ->userAgent = $ userAgent . self ::GZIP_UA ;
339+ }
300340 }
301341
302342 /**
0 commit comments