Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure to close gzip stream in JsonHttpResponse yielded an empty/broken response #170

Merged
merged 3 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions core/src/main/java/org/kohsuke/stapler/FunctionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,8 @@

package org.kohsuke.stapler;

import org.kohsuke.stapler.interceptor.RequirePOST;
import org.kohsuke.stapler.interceptor.RespondSuccess;
import org.kohsuke.stapler.json.JsonBody;
import org.kohsuke.stapler.json.JsonResponse;
import org.kohsuke.stapler.json.SubmittedForm;
import org.kohsuke.stapler.verb.DELETE;
import org.kohsuke.stapler.verb.GET;
import org.kohsuke.stapler.verb.POST;
import org.kohsuke.stapler.verb.PUT;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;
import java.util.*;
import java.util.regex.Pattern;

/**
* Immutable list of {@link Function}s.
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/kohsuke/stapler/json/JsonBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@
* return pt;
* }
*
* public class Point { public int x, y; }
* public static class Point { public int x, y; }
* </pre>
*
* Request:
* <pre>
* POST ..../double
* Content-Type: application/json
*
* {x:10,y:5}
* {"x":10,"y":5}
* </pre>
*
* Response:
* <pre>
* 200 OK
* Content-Type: application/json;charset=UTF-8
*
* {x:20,y:10}
* {"x":20,"y":10}
* </pre>
*
* @author Kohsuke Kawaguchi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

/**
* {@link JSONObject} as a response.
Expand Down Expand Up @@ -47,7 +48,9 @@ public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object nod
}
if (responseJson != null) {
rsp.setContentType("application/json;charset=UTF-8");
responseJson.write(rsp.getCompressedWriter(req));
try (Writer w = rsp.getCompressedWriter(req)) {
responseJson.write(w);
}
}
}

Expand Down
39 changes: 39 additions & 0 deletions core/src/test/java/org/kohsuke/stapler/json/JsonBodyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.kohsuke.stapler.json;

import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.WebResponse;
import java.net.URL;
import static junit.framework.TestCase.assertEquals;
import org.kohsuke.stapler.test.JettyTestCase;

public class JsonBodyTest extends JettyTestCase {

public void testSmokes() throws Exception {
WebRequestSettings req = new WebRequestSettings(new URL(url, "double"), HttpMethod.POST);
req.setAdditionalHeader("Content-Type", "application/json");
req.setRequestBody("{\"x\":10,\"y\":5}");
WebResponse response = new WebClient().getPage(req).getWebResponse();
assertEquals("application/json", response.getContentType());
assertEquals("{\"x\":20,\"y\":10}", response.getContentAsString());
// and then with compression:
req.setAdditionalHeader("Accept-Encoding", "gzip");
response = new WebClient().getPage(req).getWebResponse();
assertEquals("application/json", response.getContentType());
assertEquals("{\"x\":20,\"y\":10}", response.getContentAsString());
}

@JsonResponse
public Point doDouble(@JsonBody Point p) {
Point pt = new Point();
pt.x = p.x * 2;
pt.y = p.y * 2;
return pt;
}

public static class Point {
public int x, y;
}

}