Skip to content

Commit 7b9a75c

Browse files
Fix/handling non utf 8 body (#82)
added handling of non-utf-8 bodies
1 parent cf62b6a commit 7b9a75c

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 1.1.1 (2020-12-15)
4+
Bugfixes:
5+
- Added handling of non utf-8 body in the request
6+
37
## 1.1.0 (2020-12-02)
48
Features:
59
- Added option to enable/disable 'STARTTLS' in the email settings

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>unifi_reporting_api</groupId>
77
<artifactId>api</artifactId>
88
<packaging>war</packaging>
9-
<version>1.1.0</version>
9+
<version>1.1.1</version>
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/main/view/BaseServlet.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
import javax.servlet.http.HttpServletResponse;
1717
import java.io.*;
1818
import java.net.URLDecoder;
19+
import java.nio.charset.StandardCharsets;
1920
import java.util.Arrays;
2021
import java.util.Objects;
2122
import java.util.logging.Logger;
2223

23-
import static java.nio.charset.StandardCharsets.*;
24-
import static javax.ws.rs.core.MediaType.*;
24+
import static java.nio.charset.StandardCharsets.UTF_8;
25+
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
2526

2627
public class BaseServlet extends HttpServlet {
2728
protected static Logger log = Logger.getLogger(BaseServlet.class.getName());
@@ -46,15 +47,11 @@ private String replacer(String value) {
4647
}
4748

4849
protected String getRequestJson(@NotNull HttpServletRequest req) {
49-
try {
50-
req.setCharacterEncoding(UTF_8.toString());
51-
} catch (UnsupportedEncodingException e) {
52-
e.printStackTrace();
53-
}
5450
StringBuilder sb = new StringBuilder();
5551
String s;
5652
try {
57-
while ((s = req.getReader().readLine()) != null) {
53+
BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream(), StandardCharsets.UTF_8));
54+
while ((s = reader.readLine()) != null) {
5855
sb.append(s);
5956
}
6057
} catch (IOException e) {
@@ -115,12 +112,12 @@ protected void setGetResponseHeaders(@NotNull HttpServletResponse resp) {
115112
protected void setOptionsResponseHeaders(@NotNull HttpServletResponse resp) {
116113
resp.addHeader("Access-Control-Allow-Credentials", "true");
117114
resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, DELETE, POST, GET, PUT");
118-
resp.addHeader("Access-Control-Allow-Origin",getOrigin());
115+
resp.addHeader("Access-Control-Allow-Origin", getOrigin());
119116
resp.addHeader("Access-Control-Allow-Headers", "Authorization, content-type");
120117
resp.setStatus(204);
121118
}
122119

123-
private String getOrigin(){
120+
private String getOrigin() {
124121
return World.getInstance().getBaseURL() != null ? World.getInstance().getBaseURL() : "*";
125122
}
126123

@@ -140,12 +137,12 @@ private String getSessionId(@NotNull HttpServletRequest req) throws AqualityExce
140137
validateAuthHeader(header);
141138
String[] strings = header.split(" ");
142139
return strings[1];
143-
} else if(cookies != null){
140+
} else if (cookies != null) {
144141
Cookie iio78 = Arrays.stream(cookies).filter(x -> x.getName().equals("iio78")).findFirst().orElse(null);
145-
if(iio78 != null) {
146-
try{
142+
if (iio78 != null) {
143+
try {
147144
return URLDecoder.decode(iio78.getValue(), "utf-8");
148-
} catch (UnsupportedEncodingException e){
145+
} catch (UnsupportedEncodingException e) {
149146
throw new AuthenticationException("Your cookie is wrong!");
150147
}
151148
}

0 commit comments

Comments
 (0)