1 28 29 package HTTPClient; 30 31 import java.io.IOException ; 32 import java.util.Vector ; 33 import java.util.zip.InflaterInputStream ; 34 import java.util.zip.GZIPInputStream ; 35 36 37 47 48 class TransferEncodingModule implements HTTPClientModule, GlobalConstants 49 { 50 static 51 { 52 55 try 56 { new InflaterInputStream (null); } 57 catch (NullPointerException npe) 58 { } 59 } 60 61 62 64 TransferEncodingModule() 65 { 66 } 67 68 69 71 74 public int requestHandler(Request req, Response[] resp) 75 throws ModuleException 76 { 77 79 int idx; 80 NVPair[] hdrs = req.getHeaders(); 81 for (idx=0; idx<hdrs.length; idx++) 82 if (hdrs[idx].getName().equalsIgnoreCase("TE")) 83 break; 84 85 Vector pte; 86 if (idx == hdrs.length) 87 { 88 hdrs = Util.resizeArray(hdrs, idx+1); 89 req.setHeaders(hdrs); 90 pte = new Vector (); 91 } 92 else 93 { 94 try 95 { pte = Util.parseHeader(hdrs[idx].getValue()); } 96 catch (ParseException pe) 97 { throw new ModuleException(pe.toString()); } 98 } 99 100 101 103 HttpHeaderElement all = Util.getElement(pte, "*"); 104 if (all != null) 105 { 106 NVPair[] params = all.getParams(); 107 for (idx=0; idx<params.length; idx++) 108 if (params[idx].getName().equalsIgnoreCase("q")) break; 109 110 if (idx == params.length) return REQ_CONTINUE; 112 113 if (params[idx].getValue() == null || 114 params[idx].getValue().length() == 0) 115 throw new ModuleException("Invalid q value for \"*\" in TE " + 116 "header: "); 117 118 try 119 { 120 if (Float.valueOf(params[idx].getValue()).floatValue() > 0.) 121 return REQ_CONTINUE; 122 } 123 catch (NumberFormatException nfe) 124 { 125 throw new ModuleException("Invalid q value for \"*\" in TE " + 126 "header: " + nfe.getMessage()); 127 } 128 } 129 130 131 133 if (!pte.contains(new HttpHeaderElement("deflate"))) 134 pte.addElement(new HttpHeaderElement("deflate")); 135 if (!pte.contains(new HttpHeaderElement("gzip"))) 136 pte.addElement(new HttpHeaderElement("gzip")); 137 if (!pte.contains(new HttpHeaderElement("compress"))) 138 pte.addElement(new HttpHeaderElement("compress")); 139 140 hdrs[idx] = new NVPair("TE", Util.assembleHeader(pte)); 141 142 return REQ_CONTINUE; 143 } 144 145 146 149 public void responsePhase1Handler(Response resp, RoRequest req) 150 { 151 } 152 153 154 157 public int responsePhase2Handler(Response resp, Request req) 158 { 159 return RSP_CONTINUE; 160 } 161 162 163 166 public void responsePhase3Handler(Response resp, RoRequest req) 167 throws IOException , ModuleException 168 { 169 String te = resp.getHeader("Transfer-Encoding"); 170 if (te == null || req.getMethod().equals("HEAD")) 171 return; 172 173 Vector pte; 174 try 175 { pte = Util.parseHeader(te); } 176 catch (ParseException pe) 177 { throw new ModuleException(pe.toString()); } 178 179 while (pte.size() > 0) 180 { 181 String encoding = ((HttpHeaderElement) pte.lastElement()).getName(); 182 if (encoding.equalsIgnoreCase("gzip")) 183 { 184 if (DebugMods) 185 System.err.println("TEM: pushing gzip-input-stream"); 186 187 resp.inp_stream = new GZIPInputStream (resp.inp_stream); 188 } 189 else if (encoding.equalsIgnoreCase("deflate")) 190 { 191 if (DebugMods) 192 System.err.println("TEM: pushing inflater-input-stream"); 193 194 resp.inp_stream = new InflaterInputStream (resp.inp_stream); 195 } 196 else if (encoding.equalsIgnoreCase("compress")) 197 { 198 if (DebugMods) 199 System.err.println("TEM: pushing uncompress-input-stream"); 200 201 resp.inp_stream = new UncompressInputStream(resp.inp_stream); 202 } 203 else if (encoding.equalsIgnoreCase("chunked")) 204 { 205 if (DebugMods) 206 System.err.println("TEM: pushing chunked-input-stream"); 207 208 resp.inp_stream = new ChunkedInputStream(resp.inp_stream); 209 } 210 else if (encoding.equalsIgnoreCase("identity")) 211 { 212 if (DebugMods) 213 System.err.println("TEM: ignoring 'identity' token"); 214 } 215 else 216 { 217 if (DebugMods) 218 System.err.println("TEM: Unknown transfer encoding '" + 219 encoding + "'"); 220 221 break; 222 } 223 224 pte.removeElementAt(pte.size()-1); 225 } 226 227 if (pte.size() > 0) 228 resp.setHeader("Transfer-Encoding", Util.assembleHeader(pte)); 229 else 230 resp.deleteHeader("Transfer-Encoding"); 231 } 232 233 234 237 public void trailerHandler(Response resp, RoRequest req) 238 { 239 } 240 } 241 242 | Popular Tags |