Skip to content

Commit 570df48

Browse files
committed
Add gzip and deflate endpoints
1 parent 33c3777 commit 570df48

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Java httpbin supports a subset of httpbin endpoints:
3434
an optional initial _delay_, then optionally returns with the given status _code_.
3535
- `/cache` Returns 200 unless an If-Modified-Since or If-None-Match header is provided, when it returns a 304.
3636
- `/cache/:n` Sets a Cache-Control header for _n_ seconds.
37+
- `/gzip` Returns gzip-encoded data.
38+
- `/deflate` Returns deflate-encoded data.
3739
- `/robots.txt` Returns some robots.txt rules.
3840
- `/deny` Denied by robots.txt file.
3941
- `/basic-auth/:user/:passwd` Challenges HTTP Basic Auth.

src/main/java/org/gaul/httpbin/HttpBinHandler.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import java.util.Map;
2929
import java.util.Random;
3030
import java.util.concurrent.TimeUnit;
31+
import java.util.zip.Deflater;
32+
import java.util.zip.DeflaterOutputStream;
33+
import java.util.zip.GZIPOutputStream;
3134

3235
import javax.servlet.ServletException;
3336
import javax.servlet.http.Cookie;
@@ -125,6 +128,62 @@ private void handleHelper(Request baseRequest, HttpServletRequest request,
125128
respondJSON(servletResponse, os, response);
126129
baseRequest.setHandled(true);
127130
return;
131+
} else if (method.equals("GET") && uri.equals("/gzip")) {
132+
Utils.copy(is, Utils.NULL_OUTPUT_STREAM);
133+
134+
JSONObject response = new JSONObject();
135+
response.put("args", mapParametersToJSON(request));
136+
response.put("headers", mapHeadersToJSON(request));
137+
response.put("origin", request.getRemoteAddr());
138+
response.put("url", getFullURL(request));
139+
response.put("gzipped", true);
140+
141+
byte[] uncompressed = response.toString(/*indent=*/ 2).getBytes(
142+
StandardCharsets.UTF_8);
143+
ByteArrayOutputStream baos = new ByteArrayOutputStream(
144+
uncompressed.length);
145+
try (GZIPOutputStream gzipos = new GZIPOutputStream(baos)) {
146+
gzipos.write(uncompressed);
147+
}
148+
byte[] compressed = baos.toByteArray();
149+
150+
servletResponse.setContentLength(compressed.length);
151+
servletResponse.setHeader("Content-Encoding", "gzip");
152+
servletResponse.setContentType("application/json");
153+
servletResponse.setStatus(HttpServletResponse.SC_OK);
154+
os.write(compressed);
155+
os.flush();
156+
baseRequest.setHandled(true);
157+
return;
158+
} else if (method.equals("GET") && uri.equals("/deflate")) {
159+
Utils.copy(is, Utils.NULL_OUTPUT_STREAM);
160+
161+
JSONObject response = new JSONObject();
162+
response.put("args", mapParametersToJSON(request));
163+
response.put("headers", mapHeadersToJSON(request));
164+
response.put("origin", request.getRemoteAddr());
165+
response.put("url", getFullURL(request));
166+
response.put("deflated", true);
167+
168+
byte[] uncompressed = response.toString(/*indent=*/ 2).getBytes(
169+
StandardCharsets.UTF_8);
170+
ByteArrayOutputStream baos = new ByteArrayOutputStream(
171+
uncompressed.length);
172+
try (DeflaterOutputStream dos = new DeflaterOutputStream(
173+
baos, new Deflater(Deflater.DEFAULT_COMPRESSION,
174+
/*nowrap=*/ true))) {
175+
dos.write(uncompressed);
176+
}
177+
byte[] compressed = baos.toByteArray();
178+
179+
servletResponse.setContentLength(compressed.length);
180+
servletResponse.setHeader("Content-Encoding", "deflate");
181+
servletResponse.setContentType("application/json");
182+
servletResponse.setStatus(HttpServletResponse.SC_OK);
183+
os.write(compressed);
184+
os.flush();
185+
baseRequest.setHandled(true);
186+
return;
128187
} else if (method.equals("GET") && uri.equals("/cache")) {
129188
Utils.copy(is, Utils.NULL_OUTPUT_STREAM);
130189

0 commit comments

Comments
 (0)