-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpMessageReader.java
More file actions
58 lines (50 loc) · 1.83 KB
/
HttpMessageReader.java
File metadata and controls
58 lines (50 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package core;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import static core.HttpStatusCode.REQUEST_TIMEOUT;
public class HttpMessageReader {
private static final byte[] NEWLINE = {(byte) 13, (byte) 10};
static String readExactNumberOfBytes(InputStream in, int contentLength, Charset charset) {
byte[] buffer = new byte[contentLength];
int bytesActuallyRead;
try {
bytesActuallyRead = in.read(buffer, 0, contentLength);
} catch (SocketTimeoutException e) {
throw new HttpError(REQUEST_TIMEOUT);
} catch (IOException e) {
throw new RuntimeException("Failed to read body from input stream");
}
return new String(buffer, 0, bytesActuallyRead, charset);
}
static String readStartLineAndHeaders(InputStream in) {
byte[] bytes = new byte[0];
int byteRead;
try {
while ((byteRead = in.read()) != -1) {
bytes = Arrays.copyOf(bytes, bytes.length + 1);
bytes[bytes.length - 1] = (byte) byteRead;
bytes = Arrays.equals(bytes, NEWLINE) ? new byte[0] : bytes;
if (isEndOfHeaders(bytes)) break;
}
} catch (SocketTimeoutException e) {
throw new HttpError(REQUEST_TIMEOUT);
} catch (IOException e) {
throw new RuntimeException("Failed to read start-line and headers from input stream");
}
if (bytes.length >= 4)
bytes = Arrays.copyOfRange(bytes, 0, bytes.length-4);
return new String(bytes, StandardCharsets.ISO_8859_1);
}
private static boolean isEndOfHeaders(byte[] bytes) {
int size = bytes.length;
return size>=4 &&
NEWLINE[0] == bytes[size-4] &&
NEWLINE[1] == bytes[size-3] &&
NEWLINE[0] == bytes[size-2] &&
NEWLINE[1] == bytes[size-1];
}
}