-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathContentEncodingModule.java
More file actions
220 lines (187 loc) · 5.9 KB
/
ContentEncodingModule.java
File metadata and controls
220 lines (187 loc) · 5.9 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* @(#)ContentEncodingModule.java 0.3-3 06/05/2001
*
* This file is part of the HTTPClient package
* Copyright (C) 1996-2001 Ronald Tschal�r
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* I may be contacted at:
*
* ronald@innovation.ch
*
* The HTTPClient's home page is located at:
*
* http://www.innovation.ch/java/HTTPClient/
*
*/
package HTTPClient;
import java.io.IOException;
import java.util.Vector;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import java.util.zip.GZIPInputStream;
/**
* This module handles the Content-Encoding response header. It currently
* handles the "gzip", "deflate", "compress" and "identity" tokens.
*
* @version 0.3-3 06/05/2001
* @author Ronald Tschal�r
*/
class ContentEncodingModule implements HTTPClientModule
{
// Methods
/**
* Invoked by the HTTPClient.
*/
public int requestHandler(Request req, Response[] resp)
throws ModuleException
{
// parse Accept-Encoding header
int idx;
NVPair[] hdrs = req.getHeaders();
for (idx=0; idx<hdrs.length; idx++)
if (hdrs[idx].getName().equalsIgnoreCase("Accept-Encoding"))
break;
Vector<HttpHeaderElement> pae;
if (idx == hdrs.length)
{
hdrs = Util.resizeArray(hdrs, idx+1);
req.setHeaders(hdrs);
pae = new Vector<>();
}
else
{
try
{ pae = Util.parseHeader(hdrs[idx].getValue()); }
catch (ParseException pe)
{ throw new ModuleException(pe.toString()); }
}
// done if "*;q=1.0" present
HttpHeaderElement all = Util.getElement(pae, "*");
if (all != null)
{
NVPair[] params = all.getParams();
for (idx=0; idx<params.length; idx++)
if (params[idx].getName().equalsIgnoreCase("q")) break;
if (idx == params.length) // no qvalue, i.e. q=1.0
return REQ_CONTINUE;
if (params[idx].getValue() == null ||
params[idx].getValue().length() == 0)
throw new ModuleException("Invalid q value for \"*\" in " +
"Accept-Encoding header: ");
try
{
if (Float.valueOf(params[idx].getValue()).floatValue() > 0.)
return REQ_CONTINUE;
}
catch (NumberFormatException nfe)
{
throw new ModuleException("Invalid q value for \"*\" in " +
"Accept-Encoding header: " + nfe.getMessage());
}
}
// Add gzip, deflate and compress tokens to the Accept-Encoding header
if (!pae.contains(new HttpHeaderElement("gzip")))
pae.addElement(new HttpHeaderElement("gzip"));
/*if (!pae.contains(new HttpHeaderElement("deflate")))
pae.addElement(new HttpHeaderElement("deflate"));*/
if (!pae.contains(new HttpHeaderElement("x-gzip")))
pae.addElement(new HttpHeaderElement("x-gzip"));
if (!pae.contains(new HttpHeaderElement("compress")))
pae.addElement(new HttpHeaderElement("compress"));
if (!pae.contains(new HttpHeaderElement("x-compress")))
pae.addElement(new HttpHeaderElement("x-compress"));
hdrs[idx] = new NVPair("Accept-Encoding", Util.assembleHeader(pae));
return REQ_CONTINUE;
}
/**
* Invoked by the HTTPClient.
*/
public void responsePhase1Handler(Response resp, RoRequest req)
{
}
/**
* Invoked by the HTTPClient.
*/
public int responsePhase2Handler(Response resp, Request req)
{
return RSP_CONTINUE;
}
/**
* Invoked by the HTTPClient.
*/
public void responsePhase3Handler(Response resp, RoRequest req)
throws IOException, ModuleException
{
String ce = resp.getHeader("Content-Encoding");
if (ce == null || req.getMethod().equals("HEAD") ||
resp.getStatusCode() == 206)
return;
Vector<HttpHeaderElement> pce;
try
{ pce = Util.parseHeader(ce); }
catch (ParseException pe)
{ throw new ModuleException(pe.toString()); }
if (pce.size() == 0)
return;
String encoding = pce.firstElement().getName();
if (encoding.equalsIgnoreCase("gzip") ||
encoding.equalsIgnoreCase("x-gzip"))
{
Log.write(Log.MODS, "CEM: pushing gzip-input-stream");
resp.inp_stream = new GZIPInputStream(resp.inp_stream);
pce.removeElementAt(pce.size()-1);
resp.deleteHeader("Content-length");
}
else if (encoding.equalsIgnoreCase("deflate"))
{
Log.write(Log.MODS, "CEM: pushing inflater-input-stream");
resp.inp_stream = new InflaterInputStream(resp.inp_stream, new Inflater(true));
pce.removeElementAt(pce.size()-1);
resp.deleteHeader("Content-length");
}
else if (encoding.equalsIgnoreCase("compress") ||
encoding.equalsIgnoreCase("x-compress"))
{
Log.write(Log.MODS, "CEM: pushing uncompress-input-stream");
resp.inp_stream = new UncompressInputStream(resp.inp_stream);
pce.removeElementAt(pce.size()-1);
resp.deleteHeader("Content-length");
}
else if (encoding.equalsIgnoreCase("identity"))
{
Log.write(Log.MODS, "CEM: ignoring 'identity' token");
pce.removeElementAt(pce.size()-1);
}
else
{
Log.write(Log.MODS, "CEM: Unknown content encoding '" +
encoding + "'");
}
if (pce.size() > 0)
resp.setHeader("Content-Encoding", Util.assembleHeader(pce));
else
resp.deleteHeader("Content-Encoding");
}
/**
* Invoked by the HTTPClient.
*/
public void trailerHandler(Response resp, RoRequest req)
{
}
}