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 46 47 class ContentEncodingModule implements HTTPClientModule, GlobalConstants 48 { 49 static 50 { 51 54 try 55 { new InflaterInputStream (null); } 56 catch (NullPointerException npe) 57 { } 58 } 59 60 61 63 ContentEncodingModule() 64 { 65 } 66 67 68 70 73 public int requestHandler(Request req, Response[] resp) 74 throws ModuleException 75 { 76 78 int idx; 79 NVPair[] hdrs = req.getHeaders(); 80 for (idx=0; idx<hdrs.length; idx++) 81 if (hdrs[idx].getName().equalsIgnoreCase("Accept-Encoding")) 82 break; 83 84 Vector pae; 85 if (idx == hdrs.length) 86 { 87 hdrs = Util.resizeArray(hdrs, idx+1); 88 req.setHeaders(hdrs); 89 pae = new Vector (); 90 } 91 else 92 { 93 try 94 { pae = Util.parseHeader(hdrs[idx].getValue()); } 95 catch (ParseException pe) 96 { throw new ModuleException(pe.toString()); } 97 } 98 99 100 102 HttpHeaderElement all = Util.getElement(pae, "*"); 103 if (all != null) 104 { 105 NVPair[] params = all.getParams(); 106 for (idx=0; idx<params.length; idx++) 107 if (params[idx].getName().equalsIgnoreCase("q")) break; 108 109 if (idx == params.length) return REQ_CONTINUE; 111 112 if (params[idx].getValue() == null || 113 params[idx].getValue().length() == 0) 114 throw new ModuleException("Invalid q value for \"*\" in " + 115 "Accept-Encoding header: "); 116 117 try 118 { 119 if (Float.valueOf(params[idx].getValue()).floatValue() > 0.) 120 return REQ_CONTINUE; 121 } 122 catch (NumberFormatException nfe) 123 { 124 throw new ModuleException("Invalid q value for \"*\" in " + 125 "Accept-Encoding header: " + nfe.getMessage()); 126 } 127 } 128 129 130 132 if (!pae.contains(new HttpHeaderElement("deflate"))) 133 pae.addElement(new HttpHeaderElement("deflate")); 134 if (!pae.contains(new HttpHeaderElement("gzip"))) 135 pae.addElement(new HttpHeaderElement("gzip")); 136 if (!pae.contains(new HttpHeaderElement("x-gzip"))) 137 pae.addElement(new HttpHeaderElement("x-gzip")); 138 if (!pae.contains(new HttpHeaderElement("compress"))) 139 pae.addElement(new HttpHeaderElement("compress")); 140 if (!pae.contains(new HttpHeaderElement("x-compress"))) 141 pae.addElement(new HttpHeaderElement("x-compress")); 142 143 hdrs[idx] = new NVPair("Accept-Encoding", Util.assembleHeader(pae)); 144 145 return REQ_CONTINUE; 146 } 147 148 149 152 public void responsePhase1Handler(Response resp, RoRequest req) 153 { 154 } 155 156 157 160 public int responsePhase2Handler(Response resp, Request req) 161 { 162 return RSP_CONTINUE; 163 } 164 165 166 169 public void responsePhase3Handler(Response resp, RoRequest req) 170 throws IOException , ModuleException 171 { 172 String ce = resp.getHeader("Content-Encoding"); 173 if (ce == null || req.getMethod().equals("HEAD") || 174 resp.getStatusCode() == 206) 175 return; 176 177 Vector pce; 178 try 179 { pce = Util.parseHeader(ce); } 180 catch (ParseException pe) 181 { throw new ModuleException(pe.toString()); } 182 183 if (pce.size() == 0) 184 return; 185 186 String encoding = ((HttpHeaderElement) pce.firstElement()).getName(); 187 if (encoding.equalsIgnoreCase("gzip") || 188 encoding.equalsIgnoreCase("x-gzip")) 189 { 190 if (DebugMods) 191 System.err.println("CEM: pushing gzip-input-stream"); 192 193 resp.inp_stream = new GZIPInputStream (resp.inp_stream); 194 pce.removeElementAt(pce.size()-1); 195 resp.deleteHeader("Content-length"); 196 } 197 else if (encoding.equalsIgnoreCase("deflate")) 198 { 199 if (DebugMods) 200 System.err.println("CEM: pushing inflater-input-stream"); 201 202 resp.inp_stream = new InflaterInputStream (resp.inp_stream); 203 pce.removeElementAt(pce.size()-1); 204 resp.deleteHeader("Content-length"); 205 } 206 else if (encoding.equalsIgnoreCase("compress") || 207 encoding.equalsIgnoreCase("x-compress")) 208 { 209 if (DebugMods) 210 System.err.println("CEM: pushing uncompress-input-stream"); 211 212 resp.inp_stream = new UncompressInputStream(resp.inp_stream); 213 pce.removeElementAt(pce.size()-1); 214 resp.deleteHeader("Content-length"); 215 } 216 else if (encoding.equalsIgnoreCase("identity")) 217 { 218 if (DebugMods) 219 System.err.println("CEM: ignoring 'identity' token"); 220 pce.removeElementAt(pce.size()-1); 221 } 222 else 223 { 224 if (DebugMods) 225 System.err.println("CEM: Unknown content encoding '" + 226 encoding + "'"); 227 } 228 229 if (pce.size() > 0) 230 resp.setHeader("Content-Encoding", Util.assembleHeader(pce)); 231 else 232 resp.deleteHeader("Content-Encoding"); 233 } 234 235 236 239 public void trailerHandler(Response resp, RoRequest req) 240 { 241 } 242 } 243 244 | Popular Tags |