-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathRetryModule.java
More file actions
285 lines (246 loc) · 7.52 KB
/
RetryModule.java
File metadata and controls
285 lines (246 loc) · 7.52 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* @(#)RetryModule.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;
/**
* This module handles request retries when a connection closes prematurely.
* It is triggered by the RetryException thrown by the StreamDemultiplexor.
*
* <P>This module is somewhat unique in that it doesn't strictly limit itself
* to the HTTPClientModule interface and its return values. That is, it
* sends request directly using the HTTPConnection.sendRequest() method. This
* is necessary because this module will not only resend its request but it
* also resend all other requests in the chain. Also, it rethrows the
* RetryException in Phase1 to restart the processing of the modules.
*
* @version 0.3-3 06/05/2001
* @author Ronald Tschal�r
* @since V0.3
*/
class RetryModule implements HTTPClientModule, GlobalConstants
{
// Constructors
/**
*/
RetryModule()
{
}
// Methods
/**
* Invoked by the HTTPClient.
*/
public int requestHandler(Request req, Response[] resp)
{
return REQ_CONTINUE;
}
/**
* Invoked by the HTTPClient.
*/
public void responsePhase1Handler(Response resp, RoRequest roreq)
throws IOException, ModuleException
{
try
{
resp.getStatusCode();
}
catch (RetryException re)
{
Log.write(Log.MODS, "RtryM: Caught RetryException");
boolean got_lock = false;
try
{
synchronized (re.first)
{
got_lock = true;
// initialize idempotent sequence checking
IdempotentSequence seq = new IdempotentSequence();
for (RetryException e=re.first; e!=null; e=e.next)
seq.add(e.request);
for (RetryException e=re.first; e!=null; e=e.next)
{
Log.write(Log.MODS, "RtryM: handling exception ", e);
Request req = e.request;
HTTPConnection con = req.getConnection();
/* Don't retry if either the sequence is not idempotent
* (Sec 8.1.4 and 9.1.2), or we've already retried enough
* times, or the headers have been read and parsed
* already
*/
if (!seq.isIdempotent(req) ||
(con.ServProtVersKnown &&
con.ServerProtocolVersion >= HTTP_1_1 &&
req.num_retries > 0) ||
((!con.ServProtVersKnown ||
con.ServerProtocolVersion <= HTTP_1_0) &&
req.num_retries > 4) ||
e.response.got_headers)
{
e.first = null;
continue;
}
/**
* if an output stream was used (i.e. we don't have the
* data to resend) then delegate the responsibility for
* resending to the application.
*/
if (req.getStream() != null)
{
if (HTTPConnection.deferStreamed)
{
req.getStream().reset();
e.response.setRetryRequest(true);
}
e.first = null;
continue;
}
/* If we have an entity then setup either the entity-delay
* or the Expect header
*/
if (req.getData() != null && e.conn_reset)
{
if (con.ServProtVersKnown &&
con.ServerProtocolVersion >= HTTP_1_1)
addToken(req, "Expect", "100-continue");
else
req.delay_entity = 5000L << req.num_retries;
}
/* If the next request in line has an entity and we're
* talking to an HTTP/1.0 server then close the socket
* after this request. This is so that the available()
* call (to watch for an error response from the server)
* will work correctly.
*/
if (e.next != null && e.next.request.getData() != null &&
(!con.ServProtVersKnown ||
con.ServerProtocolVersion < HTTP_1_1) &&
e.conn_reset)
{
addToken(req, "Connection", "close");
}
/* If this an HTTP/1.1 server then don't pipeline retries.
* The problem is that if the server for some reason
* decides not to use persistent connections and it does
* not do a correct shutdown of the connection, then the
* response will be ReSeT. If we did pipeline then we
* would keep falling into this trap indefinitely.
*
* Note that for HTTP/1.0 servers, if they don't support
* keep-alives then the normal code will already handle
* this accordingly and won't pipe over the same
* connection.
*/
if (con.ServProtVersKnown &&
con.ServerProtocolVersion >= HTTP_1_1 &&
e.conn_reset)
{
req.dont_pipeline = true;
}
// The above is too risky - for moment let's be safe
// and never pipeline retried request at all.
req.dont_pipeline = true;
// now resend the request
Log.write(Log.MODS, "RtryM: Retrying request '" +
req.getMethod() + " " +
req.getRequestURI() + "'");
if (e.conn_reset)
req.num_retries++;
e.response.http_resp.set(req,
con.sendRequest(req, e.response.timeout));
e.exception = null;
e.first = null;
}
}
}
catch (NullPointerException npe)
{ if (got_lock) throw npe; }
catch (ParseException pe)
{ throw new IOException(pe.getMessage()); }
if (re.exception != null) throw re.exception;
re.restart = true;
throw re;
}
}
/**
* Invoked by the HTTPClient.
*/
public int responsePhase2Handler(Response resp, Request req)
{
// reset any stuff we might have set previously
req.delay_entity = 0;
req.dont_pipeline = false;
req.num_retries = 0;
return RSP_CONTINUE;
}
/**
* Invoked by the HTTPClient.
*/
public void responsePhase3Handler(Response resp, RoRequest req)
{
}
/**
* Invoked by the HTTPClient.
*/
public void trailerHandler(Response resp, RoRequest req)
{
}
/**
* Add a token to the given header. If the header does not exist then
* create it with the given token.
*
* @param req the request who's headers are to be modified
* @param hdr the name of the header to add the token to (or to create)
* @param tok the token to add
* @exception ParseException if parsing the header fails
*/
private void addToken(Request req, String hdr, String tok)
throws ParseException
{
int idx;
NVPair[] hdrs = req.getHeaders();
for (idx=0; idx<hdrs.length; idx++)
{
if (hdrs[idx].getName().equalsIgnoreCase(hdr))
break;
}
if (idx == hdrs.length) // no such header, so add one
{
hdrs = Util.resizeArray(hdrs, idx+1);
hdrs[idx] = new NVPair(hdr, tok);
req.setHeaders(hdrs);
}
else // header exists, so add token
{
if (!Util.hasToken(hdrs[idx].getValue(), tok))
hdrs[idx] = new NVPair(hdr, hdrs[idx].getValue() + ", " + tok);
}
}
}