| 1 28 29 package HTTPClient; 30 31 32 import java.io.OutputStream ; 33 import java.io.DataOutputStream ; 34 import java.io.FilterOutputStream ; 35 import java.io.ByteArrayOutputStream ; 36 import java.io.IOException ; 37 import java.io.InterruptedIOException ; 38 import java.net.URL ; 39 import java.net.Socket ; 40 import java.net.InetAddress ; 41 import java.net.SocketException ; 42 import java.net.UnknownHostException ; 43 import java.util.Vector ; 44 import java.util.Hashtable ; 45 import java.applet.Applet ; 46 47 48 193 194 public class HTTPConnection 195 implements GlobalConstants, HTTPClientModuleConstants 196 { 197 198 public final static String version = "RPT-HTTPClient/0.3-2"; 199 200 201 private final static Object dflt_context = new Object (); 202 203 204 private Object Context = null; 205 206 207 private int Protocol; 208 209 210 int ServerProtocolVersion; 211 212 213 boolean ServProtVersKnown; 214 215 217 private String RequestProtocolVersion; 218 219 220 private static boolean no_chunked = false; 221 222 223 private static boolean force_1_0 = false; 224 225 226 private String Host; 227 228 229 private int Port; 230 231 232 private String Proxy_Host = null; 233 234 235 private int Proxy_Port; 236 237 238 private static String Default_Proxy_Host = null; 239 240 241 private static int Default_Proxy_Port; 242 243 244 private static CIHashtable non_proxy_host_list = new CIHashtable(); 245 private static Vector non_proxy_dom_list = new Vector (); 246 private static Vector non_proxy_addr_list = new Vector (); 247 private static Vector non_proxy_mask_list = new Vector (); 248 249 250 private SocksClient Socks_client = null; 251 252 253 private static SocksClient Default_Socks_client = null; 254 255 256 private StreamDemultiplexor input_demux = null; 257 258 259 LinkedList DemuxList = new LinkedList(); 260 261 262 private LinkedList RequestList = new LinkedList(); 263 264 265 private boolean DoesKeepAlive = false; 266 267 268 private boolean KeepAliveUnknown = true; 269 270 271 private int KeepAliveReqMax = -1; 272 273 274 private int KeepAliveReqLeft; 275 276 277 private static boolean NeverPipeline = false; 278 279 280 private static boolean NoKeepAlives = false; 281 282 283 private static boolean haveMSLargeWritesBug = false; 284 285 286 private static int DefaultTimeout = 0; 287 288 289 private int Timeout; 290 291 292 private NVPair[] DefaultHeaders = new NVPair[0]; 293 294 295 private static Vector DefaultModuleList; 296 297 298 private Vector ModuleList; 299 300 301 private static boolean DefaultAllowUI = true; 302 303 304 private boolean AllowUI; 305 306 307 static 308 { 309 313 314 try { 316 String host = System.getProperty("http.proxyHost"); 317 if (host == null) 318 throw new Exception (); int port = Integer.getInteger("http.proxyPort", -1).intValue(); 320 321 if (DebugConn) 322 System.err.println("Conn: using proxy " + host + ":" + port); 323 setProxyServer(host, port); 324 } 325 catch (Exception e) 326 { 327 try { 329 if (Boolean.getBoolean("proxySet")) 330 { 331 String host = System.getProperty("proxyHost"); 332 int port = Integer.getInteger("proxyPort", -1).intValue(); 333 if (DebugConn) 334 System.err.println("Conn: using proxy " + host + ":" + port); 335 setProxyServer(host, port); 336 } 337 } 338 catch (Exception ee) 339 { Default_Proxy_Host = null; } 340 } 341 342 343 346 String hosts = System.getProperty("HTTPClient.nonProxyHosts"); 349 if (hosts == null) 350 hosts = System.getProperty("http.nonProxyHosts"); 351 352 String [] _list = Util.splitProperty(hosts); 353 dontProxyFor(_list); 354 359 360 366 try 367 { 368 String host = System.getProperty("HTTPClient.socksHost"); 369 if (host != null && host.length() > 0) 370 { 371 int port = Integer.getInteger("HTTPClient.socksPort", -1).intValue(); 372 int version = Integer.getInteger("HTTPClient.socksVersion", -1).intValue(); 373 if (DebugConn) 374 System.err.println("Conn: using SOCKS " + host + ":" + port); 375 if (version == -1) 376 setSocksServer(host, port); 377 else 378 setSocksServer(host, port, version); 379 } 380 } 381 catch (Exception e) 382 { Default_Socks_client = null; } 383 384 385 387 String modules = "HTTPClient.RetryModule|" + 388 "HTTPClient.CookieModule|" + 389 "HTTPClient.RedirectionModule|" + 390 "HTTPClient.AuthorizationModule|" + 391 "HTTPClient.DefaultModule|" + 392 "HTTPClient.TransferEncodingModule|" + 393 "HTTPClient.ContentMD5Module|" + 394 "HTTPClient.ContentEncodingModule"; 395 396 boolean in_applet = false; 397 try 398 { modules = System.getProperty("HTTPClient.Modules", modules); } 399 catch (SecurityException se) 400 { in_applet = true; } 401 402 DefaultModuleList = new Vector (); 403 String [] list = Util.splitProperty(modules); 404 for (int idx=0; idx<list.length; idx++) 405 { 406 try 407 { 408 DefaultModuleList.addElement(Class.forName(list[idx])); 409 if (DebugConn) 410 System.err.println("Conn: added module " + list[idx]); 411 } 412 catch (ClassNotFoundException cnfe) 413 { 414 if (!in_applet) 415 throw new NoClassDefFoundError (cnfe.getMessage()); 416 417 423 } 424 } 425 426 427 430 try 431 { 432 NeverPipeline = Boolean.getBoolean("HTTPClient.disable_pipelining"); 433 if (DebugConn) 434 if (NeverPipeline) System.err.println("Conn: disabling pipelining"); 435 } 436 catch (Exception e) 437 { } 438 439 442 try 443 { 444 NoKeepAlives = Boolean.getBoolean("HTTPClient.disableKeepAlives"); 445 if (DebugConn) 446 if (NoKeepAlives) System.err.println("Conn: disabling keep-alives"); 447 } 448 catch (Exception e) 449 { } 450 451 454 try 455 { 456 force_1_0 = Boolean.getBoolean("HTTPClient.forceHTTP_1.0"); 457 if (DebugConn) 458 if (force_1_0) System.err.println("Conn: forcing HTTP/1.0 requests"); 459 } 460 catch (Exception e) 461 { } 462 463 466 try 467 { 468 no_chunked = Boolean.getBoolean("HTTPClient.dontChunkRequests"); 469 if (DebugConn) 470 if (no_chunked) System.err.println("Conn: never chunking requests"); 471 } 472 catch (Exception e) 473 { } 474 475 478 try 479 { 480 if (System.getProperty("os.name").indexOf("Windows") >= 0 && 481 System.getProperty("java.version").startsWith("1.1")) 482 haveMSLargeWritesBug = true; 483 if (DebugConn) 484 if (haveMSLargeWritesBug) 485 System.err.println("Conn: splitting large writes into 20K chunks (M$ bug)"); 486 } 487 catch (Exception e) 488 { } 489 } 490 491 492 494 500 public HTTPConnection(Applet applet) throws ProtocolNotSuppException 501 { 502 this(applet.getCodeBase().getProtocol(), 503 applet.getCodeBase().getHost(), 504 applet.getCodeBase().getPort()); 505 } 506 507 512 public HTTPConnection(String host) 513 { 514 Setup(HTTP, host, 80); 515 } 516 517 523 public HTTPConnection(String host, int port) 524 { 525 Setup(HTTP, host, port); 526 } 527 528 537 public HTTPConnection(String prot, String host, int port) throws 538 ProtocolNotSuppException 539 { 540 prot = prot.trim().toLowerCase(); 541 542 if (!prot.equals("http")) 544 throw new ProtocolNotSuppException("Unsupported protocol '" + prot + "'"); 545 546 if (prot.equals("http")) 547 Setup(HTTP, host, port); 548 else if (prot.equals("https")) 549 Setup(HTTPS, host, port); 550 else if (prot.equals("shttp")) 551 Setup(SHTTP, host, port); 552 else if (prot.equals("http-ng")) 553 Setup(HTTP_NG, host, port); 554 } 555 556 562 public HTTPConnection(URL url) throws ProtocolNotSuppException 563 { 564 this(url.getProtocol(), url.getHost(), url.getPort()); 565 } 566 567 574 private void Setup(int prot, String host, int port) 575 { 576 Protocol = prot; 577 Host = host.trim().toLowerCase(); 578 Port = port; 579 580 if (Port == -1) 581 Port = URI.defaultPort(getProtocol()); 582 583 if (Default_Proxy_Host != null && !matchNonProxy(Host)) 584 setCurrentProxy(Default_Proxy_Host, Default_Proxy_Port); 585 else 586 setCurrentProxy(null, 0); 587 588 Socks_client = Default_Socks_client; 589 Timeout = DefaultTimeout; 590 ModuleList = (Vector ) DefaultModuleList.clone(); 591 AllowUI = DefaultAllowUI; 592 if (NoKeepAlives) 593 setDefaultHeaders(new NVPair[] { new NVPair("Connection", "close") }); 594 } 595 596 597 604 private boolean matchNonProxy(String host) 605 { 606 608 if (non_proxy_host_list.get(host) != null) 609 return true; 610 611 612 614 for (int idx=0; idx<non_proxy_dom_list.size(); idx++) 615 if (host.endsWith((String ) non_proxy_dom_list.elementAt(idx))) 616 return true; 617 618 619 621 if (non_proxy_addr_list.size() == 0) 622 return false; 623 624 InetAddress [] host_addr; 625 try 626 { host_addr = InetAddress.getAllByName(host); } 627 catch (UnknownHostException uhe) 628 { return false; } 630 for (int idx=0; idx<non_proxy_addr_list.size(); idx++) 631 { 632 byte[] addr = (byte[]) non_proxy_addr_list.elementAt(idx); 633 byte[] mask = (byte[]) non_proxy_mask_list.elementAt(idx); 634 635 ip_loop: for (int idx2=0; idx2<host_addr.length; idx2++) 636 { 637 byte[] raw_addr = host_addr[idx2].getAddress(); 638 if (raw_addr.length != addr.length) continue; 639 640 for (int idx3=0; idx3<raw_addr.length; idx3++) 641 { 642 if ((raw_addr[idx3] & mask[idx3]) != (addr[idx3] & mask[idx3])) 643 continue ip_loop; 644 } 645 return true; 646 } 647 } 648 649 return false; 650 } 651 652 653 655 666 public HTTPResponse Head(String file) throws IOException , ModuleException 667 { 668 return Head(file, (String ) null, null); 669 } 670 671 683 public HTTPResponse Head(String file, NVPair form_data[]) 684 throws IOException , ModuleException 685 { 686 return Head(file, form_data, null); 687 } 688 689 702 public HTTPResponse Head(String file, NVPair[] form_data, NVPair[] headers) 703 throws IOException , ModuleException 704 { 705 String File = stripRef(file), 706 query = Codecs.nv2query(form_data); 707 if (query != null && query.length() > 0) 708 File += "?" + query; 709 710 return setupRequest("HEAD", File, headers, null, null); 711 } 712 713 725 public HTTPResponse Head(String file, String query) 726 throws IOException , ModuleException 727 { 728 return Head(file, query, null); 729 } 730 731 732 745 public HTTPResponse Head(String file, String query, NVPair[] headers) 746 throws IOException , ModuleException 747 { 748 String File = stripRef(file); 749 if (query != null && query.length() > 0) 750 File += "?" + Codecs.URLEncode(query); 751 752 return setupRequest("HEAD", File, headers, null, null); 753 } 754 755 756 765 public HTTPResponse Get(String file) throws IOException , ModuleException 766 { 767 return Get(file, (String ) null, null); 768 } 769 770 782 public HTTPResponse Get(String file, NVPair form_data[]) 783 throws IOException , ModuleException 784 { 785 return Get(file, form_data, null); 786 } 787 788 801 public HTTPResponse Get(String file, NVPair[] form_data, NVPair[] headers) 802 throws IOException , ModuleException 803 { 804 String File = stripRef(file), 805 query = Codecs.nv2query(form_data); 806 if (query != null && query.length() > 0) 807 File += "?" + query; 808 809 return setupRequest("GET", File, headers, null, null); 810 } 811 812 823 public HTTPResponse Get(String file, String query) 824 throws IOException , ModuleException 825 { 826 return Get(file, query, null); 827 } 828 829 841 public HTTPResponse Get(String file, String query, NVPair[] headers) 842 throws IOException , ModuleException 843 { 844 String File = stripRef(file); 845 if (query != null && query.length() > 0) 846 File += "?" + Codecs.URLEncode(query); 847 848 return setupRequest("GET", File, headers, null, null); 849 } 850 851 852 861 public HTTPResponse Post(String file) throws IOException , ModuleException 862 { 863 return Post(file, (byte []) null, null); 864 } 865 866 879 public HTTPResponse Post(String file, NVPair form_data[]) 880 throws IOException , ModuleException 881 { 882 NVPair[] headers = 883 { new NVPair("Content-type", "application/x-www-form-urlencoded") }; 884 885 return Post(file, Codecs.nv2query(form_data), headers); 886 } 887 888 903 public HTTPResponse Post(String file, NVPair form_data[], NVPair headers[]) 904 throws IOException , ModuleException 905 { 906 int idx; 907 for (idx=0; idx<headers.length; idx++) 908 if (headers[idx].getName().equalsIgnoreCase("Content-type")) break; 909 if (idx == headers.length) 910 { 911 headers = Util.resizeArray(headers, idx+1); 912 headers[idx] = 913 new NVPair("Content-type", "application/x-www-form-urlencoded"); 914 } 915 916 return Post(file, Codecs.nv2query(form_data), headers); 917 } 918 919 932 public HTTPResponse Post(String file, String data) 933 throws IOException , ModuleException 934 { 935 return Post(file, data, null); 936 } 937 938 949 public HTTPResponse Post(String file, String data, NVPair[] headers) 950 throws IOException , ModuleException 951 { 952 byte tmp[] = null; 953 954 if (data != null && data.length() > 0) 955 { 956 tmp = new byte[data.length()]; 957 data.getBytes(0, data.length(), tmp, 0); |