1 28 29 package HTTPClient; 30 31 import java.io.IOException ; 32 33 34 49 50 class RetryModule implements HTTPClientModule, GlobalConstants 51 { 52 54 56 RetryModule() 57 { 58 } 59 60 61 63 66 public int requestHandler(Request req, Response[] resp) 67 { 68 return REQ_CONTINUE; 69 } 70 71 72 75 public void responsePhase1Handler(Response resp, RoRequest roreq) 76 throws IOException , ModuleException 77 { 78 try 79 { 80 resp.getStatusCode(); 81 } 82 catch (RetryException re) 83 { 84 if (DebugMods) System.err.println("RtryM: Caught RetryException"); 85 86 boolean got_lock = false; 87 88 try 89 { 90 synchronized (re.first) 91 { 92 got_lock = true; 93 94 IdempotentSequence seq = new IdempotentSequence(); 96 for (RetryException e=re.first; e!=null; e=e.next) 97 seq.add(e.request); 98 99 for (RetryException e=re.first; e!=null; e=e.next) 100 { 101 Request req = e.request; 102 HTTPConnection con = req.getConnection(); 103 104 109 if (!seq.isIdempotent(req) || 110 (con.ServProtVersKnown && 111 con.ServerProtocolVersion >= HTTP_1_1 && 112 req.num_retries > 0) || 113 ((!con.ServProtVersKnown || 114 con.ServerProtocolVersion <= HTTP_1_0) && 115 req.num_retries > 4) || 116 e.response.got_headers || 117 req.getStream() != null) 118 { 119 e.first = null; 120 continue; 121 } 122 123 124 127 if (req.getData() != null && e.conn_reset) 128 { 129 if (con.ServProtVersKnown && 130 con.ServerProtocolVersion >= HTTP_1_1) 131 addToken(req, "Expect", "100-continue"); 132 else 133 req.delay_entity = 5000L << req.num_retries; 134 } 135 136 142 if (e.next != null && e.next.request.getData() != null && 143 (!con.ServProtVersKnown || 144 con.ServerProtocolVersion < HTTP_1_1) && 145 e.conn_reset) 146 { 147 addToken(req, "Connection", "close"); 148 } 149 150 151 163 if (con.ServProtVersKnown && 164 con.ServerProtocolVersion >= HTTP_1_1 && 165 e.conn_reset) 166 { 167 req.dont_pipeline = true; 168 } 169 req.dont_pipeline = true; 172 173 174 176 if (DebugDemux) 177 System.err.println("RtryM: Retrying request '" + 178 req.getMethod() + " " + 179 req.getRequestURI() + "'"); 180 181 if (e.conn_reset) 182 req.num_retries++; 183 e.response.http_resp.set(req, 184 con.sendRequest(req, e.response.timeout)); 185 e.exception = null; 186 e.first = null; 187 } 188 } 189 } 190 catch (NullPointerException npe) 191 { if (got_lock) throw npe; } 192 catch (ParseException pe) 193 { throw new IOException (pe.getMessage()); } 194 195 if (re.exception != null) throw re.exception; 196 197 re.restart = true; 198 throw re; 199 } 200 } 201 202 203 206 public int responsePhase2Handler(Response resp, Request req) 207 { 208 req.delay_entity = 0; 210 req.dont_pipeline = false; 211 req.num_retries = 0; 212 213 return RSP_CONTINUE; 214 } 215 216 217 220 public void responsePhase3Handler(Response resp, RoRequest req) 221 { 222 } 223 224 225 228 public void trailerHandler(Response resp, RoRequest req) 229 { 230 } 231 232 233 242 private void addToken(Request req, String hdr, String tok) 243 throws ParseException 244 { 245 int idx; 246 NVPair[] hdrs = req.getHeaders(); 247 for (idx=0; idx<hdrs.length; idx++) 248 { 249 if (hdrs[idx].getName().equalsIgnoreCase(hdr)) 250 break; 251 } 252 253 if (idx == hdrs.length) { 255 hdrs = Util.resizeArray(hdrs, idx+1); 256 hdrs[idx] = new NVPair(hdr, tok); 257 req.setHeaders(hdrs); 258 } 259 else { 261 if (!Util.hasToken(hdrs[idx].getValue(), tok)) 262 hdrs[idx] = new NVPair(hdr, hdrs[idx].getValue() + ", " + tok); 263 } 264 } 265 } 266 267 | Popular Tags |