-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathWebResponse.java
More file actions
executable file
·158 lines (137 loc) · 4.05 KB
/
Copy pathWebResponse.java
File metadata and controls
executable file
·158 lines (137 loc) · 4.05 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package javaforce.service;
import java.io.*;
import java.util.*;
import java.util.zip.*;
/** WebResponse
*
* Created : Aug 23, 2013
*/
public class WebResponse extends OutputStream {
OutputStream os;
private ByteArrayOutputStream buf = new ByteArrayOutputStream();
private int statusCode = 200;
private String statusString = "OK";
private String contentType = "text/html; charset=UTF-8";
private ArrayList<String> cookies = new ArrayList<String>();
private ArrayList<String> headers = new ArrayList<String>();
private PrintWriter writer = new PrintWriter(this);
public void write(int b) throws IOException {
buf.write(b);
}
public void write(byte[] b, int off, int len) throws IOException {
buf.write(b, off, len);
}
void writeAll(WebRequest req) throws Exception {
writer.flush();
flush();
byte[] data = buf.toByteArray();
boolean gzip = false;
if (data.length > 0) {
if (WebServer.config_enable_gzip) {
String accept = req.getHeader("Accept-Encoding");
if (accept != null) {
String[] encodings = accept.split(",");
for(int a=0;a<encodings.length;a++) {
if (encodings[a].trim().equals("gzip")) {
gzip = true;
break;
}
}
}
}
if (gzip) {
addHeader("Content-Encoding: gzip");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(data);
gos.finish();
data = baos.toByteArray();
}
}
int size = data.length;
writeHeaders(size, gzip);
if (data.length > 0) {
os.write(WebServer.chunkHeader(data));
os.write(data);
os.write("\r\n0\r\n\r\n".getBytes());
}
}
void writeHeaders(int contentLength, boolean gzip) throws Exception {
StringBuilder res = new StringBuilder();
res.append("HTTP/1.1 " + statusCode + " " + statusString + "\r\n");
if (contentLength != -1) {
res.append("Content-Length: " + contentLength + "\r\n");
}
res.append("Content-Type: " + contentType + "\r\n");
if (contentLength != 0) {
res.append("Transfer-Encoding: chunked\r\n");
}
for(int a=0;a<cookies.size();a++) {
res.append("Set-Cookie: ");
res.append(cookies.get(a));
res.append("\r\n");
}
for(int a=0;a<headers.size();a++) {
res.append(headers.get(a));
res.append("\r\n");
}
res.append("\r\n"); //terminate response
os.write(res.toString().getBytes());
}
//public methods
/** Returns buffered output stream.
* When doGet() or doPut() returns the headers and content are sent to client.
*
* @return OutputStream
*/
public OutputStream getOutputStream() {
return this;
}
public PrintWriter getWriter() {
return writer;
}
public void setStatus(int sc, String msg) {
statusCode = sc;
statusString = msg;
}
public void setContentType(String type) {
contentType = type;
}
public String getContentType() {
return contentType;
}
public void setContentLength(int length) {
addHeader("Content-Length: " + length);
}
public void addCookie(String name, String value) {
cookies.add(name + "=" + value);
}
public void addHeader(String header) {
headers.add(header);
}
public void sendRedirect(String url) {
setStatus(301, "Moved");
addHeader("Location: " + url);
}
public int getLength() {
return buf.size();
}
public void flush() {
try {super.flush();} catch (Exception e) {}
}
public HashMap<String, Object> toHashMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("ContentType", getContentType());
map.put("OutputStream", getOutputStream());
map.put("Writer", getWriter());
return map;
}
public void fromHashMap(HashMap<String, Object> map) {
setContentType((String)map.get("ContentType"));
Integer length = (Integer)map.get("ContentLength");
if (length != null) {
System.out.println("WebResponse:length=" + length);
setContentLength(length);
}
}
}