1 16 17 package org.apache.taglibs.scrape; 18 19 import java.net.*; 20 import java.io.*; 21 import java.util.*; 22 import java.security.KeyStore ; 23 import javax.servlet.jsp.PageContext ; 24 31 40 public class HttpConnection { 41 42 45 private URL url = null; 46 49 private Socket socket = null; 50 53 private HashMap keys = null; 54 57 private Vector headers = new Vector(12, 5); 58 61 private boolean gotten = false; 62 65 private static final char keySeparator = ':'; 66 69 private boolean useproxy = false; 70 73 private int pport = 3128; 74 77 private String pserver = null; 78 81 private String proxyerror = null; 82 86 90 private String auth = null; 91 94 private OutputStream out = null; 95 98 private int port; 99 102 private InetAddress dst = null; 103 106 private String sslpass = null; 107 110 private int responseCode = 0; 111 114 private String responseMessage = null; 115 118 private String requestMethod = "GET"; 119 122 private boolean connected = false; 123 126 private PageContext pageContext = null; 127 130 private PageData pagedata = null; 131 132 139 public HttpConnection(URL url, PageData pd, PageContext pc) { 140 this.url = url; 142 pageContext = pc; 145 pagedata = pd; 146 if (pagedata.getProxyServer() != null) { 147 useproxy = true; 148 } 149 } 150 151 164 public HttpConnection(URL url, int port, String server, String authstring, 165 PageContext pc) { 166 this.url = url; 169 pport = port; 170 pserver = server; 171 useproxy = true; 172 pageContext = pc; 175 if (authstring != null) 176 auth = "Basic " + 177 new sun.misc.BASE64Encoder().encode(authstring.getBytes()); 178 } 179 180 187 public void connect() throws IOException { 188 189 if (connected) return; 191 192 if ((port = getURL().getPort()) == -1) { 194 if (pagedata.getSSL()) 195 port = 443; else 197 port = 80; } 199 200 if (useproxy) { 204 InetAddress proxy = InetAddress.getByName(pagedata.getProxyServer()); 206 socket = new Socket(proxy, pagedata.getProxyPort()); 208 } else { 209 InetAddress dst = InetAddress.getByName(getURL().getHost()); 211 socket = new Socket(dst, port); 213 } 214 out = socket.getOutputStream(); 216 220 try { 222 socket.setSoTimeout(20000); 223 } catch (SocketException se) { 224 pageContext.getServletContext(). 225 log("timeout could not be set on the socket"); 226 } 227 connected = true; } 229 230 235 public void disconnect() { 236 try { 237 socket.close(); socket = null; 239 } catch (IOException ie) {} 240 241 out = null; 243 gotten = false; 244 keys = null; 245 246 connected = false; } 248 249 256 public boolean usingProxy() { 257 return useproxy; 258 } 259 260 264 public InputStream getInputStream() throws IOException { 265 if (!connected) 266 connect(); 267 268 return socket.getInputStream(); 269 } 270 271 275 public OutputStream getOutputStream() throws IOException { 276 if (!connected) 277 connect(); 278 279 return socket.getOutputStream(); 280 } 281 282 288 public String getRequestMethod() { 289 return requestMethod; 290 } 291 292 298 public void setRequestMethod(String value) { 299 requestMethod = value; 300 } 301 302 309 public int getResponseCode() { 310 return responseCode; 311 } 312 313 320 public String getResponseMessage() { 321 return responseMessage; 322 } 323 324 330 public void sendRequest() throws IOException { 331 ArrayList headers = pagedata.getHeaders(); 332 if (useproxy && !pagedata.getSSL()) { 338 send(out, getRequestMethod() + " " + url.toString() + " HTTP/1.1\r\n"); 339 if (pagedata.getAuth() != null) 340 send(out, "Proxy-Authorization: " + pagedata.getAuth() + "\r\n"); 341 if (headers != null) { 342 ArrayList name = (ArrayList) headers.get(0); 343 ArrayList value = (ArrayList) headers.get(1); 344 for (int i = 0; i < name.size(); i++) { 345 send(out, name.get(i) + ": " + value.get(i) + "\r\n"); 346 } 347 } 348 send(out, "Connection: close\r\n"); 349 } else { 350 send(out, getRequestMethod() + " " + url.getFile() + " HTTP/1.1\r\n"); 351 send(out, "Host: " + url.getHost() + "\r\n"); 352 if (headers != null) { 353 ArrayList name = (ArrayList) headers.get(0); 354 ArrayList value = (ArrayList) headers.get(1); 355 for (int i = 0; i < name.size(); i++) { 356 send(out, name.get(i) + ": " + value.get(i) + "\r\n"); 357 } 358 } 359 } 360 send(out, "\r\n"); 362 363 responseMessage = recv(socket.getInputStream()); 365 try { 366 responseCode = Integer.parseInt(responseMessage.substring(9, 12)); 367 } catch (NumberFormatException nfe) {responseCode = -1;} 368 369 if (responseCode == 301 || responseCode == 302) 371 doRedirection(); 372 } 373 374 378 private void doRedirection() throws IOException { 379 setURL(getHeaderField("Location")); 380 pageContext.getServletContext(). 382 log(getResponseMessage() + ": Connection redirected to " + 383 getHeaderField("Location")); 384 disconnect(); 386 connect(); 387 sendRequest(); 388 } 389 390 391 397 boolean makeTunnelConnection() throws IOException { 398 401 InetAddress proxy = InetAddress.getByName(pagedata.getProxyServer()); 403 socket = new Socket(proxy, pagedata.getProxyPort()); 405 406 out = socket.getOutputStream(); 408 409 send(out, "CONNECT " + url.getHost() + ":" + port + " HTTP/1.0\r\n"); 410 if (pagedata.getAuth() != null) { 411 send(out, "Proxy-Authorization: " + pagedata.getAuth() + "\r\n"); 413 } 414 send(out, "\r\n"); 415 416 String sb = recv(socket.getInputStream()); 418 419 if (sb.startsWith("HTTP/1.0 200")) 420 return true; 422 else { 423 proxyerror = sb.substring(9); 425 return false; 426 } 427 } 428 429 489 495 public String getHeaderField(int n) { 496 getHeaders(); 498 if (n < headers.size()) { return getField((String )headers.elementAt(n)); } 501 return null; 502 } 503 504 510 public long getLastModified() { 511 getHeaders(); 513 String header = (String )keys.get("LastModified"); 514 if (header != null) { 515 try { 516 return new Integer (header).longValue(); 517 } catch (NumberFormatException nfe) { 518 return 0; 519 } 520 } 521 return 0; 522 } 523 524 530 public long getExpiration() { 531 getHeaders(); 533 String header = (String )keys.get("Expiration"); 534 if (header != null) { 535 try { 536 return new Integer (header).longValue(); 537 } catch (NumberFormatException nfe) { 538 return 0; 539 } 540 } 541 return 0; 542 } 543 544 550 public String getHeaderField(String key) { 551 getHeaders(); 553 return (String )keys.get(key.toLowerCase()); } 555 556 564 public int getHeaderFieldInt(String key, int n) { 565 getHeaders(); 567 String header = (String )keys.get(key.toLowerCase()); 568 if (header != null) { 569 try { 570 n = new Integer (header).intValue(); 571 } catch (NumberFormatException nfe) { 572 } 574 } 575 return n; 576 } 577 578 584 public String getHeaderFieldKey(int n) { 585 getHeaders(); 587 if (n < headers.size()) { return getKey((String )headers.elementAt(n)); 590 } 591 return null; 592 } 593 594 600 protected void setURL(String value) throws MalformedURLException { 601 url = new URL(value); 602 } 603 604 609 public URL getURL() { 610 return url; 611 } 612 613 621 private String getKey(String str) { 622 if (str == null) 623 return null; 624 int ind = str.indexOf(keySeparator); 625 if (ind >= 0) 626 return str.substring(0, ind).toLowerCase(); 627 return null; 628 } 629 630 638 private String getField(String str) { 639 if (str == null) 640 return null; 641 int ind = str.indexOf(keySeparator); 642 if (ind >= 0) 643 return str.substring(ind+1).trim(); 644 else 645 return str; 646 } 647 648 652 private void getHeaders() { 653 654 String header; 656 if (gotten) 658 return; 659 gotten = true; 661 keys = new HashMap(); 663 664 try { 665 connect(); 667 InputStream in = getInputStream(); 669 670 header = recv(in); 672 while (true) { 673 if (header.length() == 0) break; 675 headers.addElement(header); String key = getKey(header); if (key != null) { 678 keys.put(key, getField(header)); } 680 header = recv(in); } 682 } catch (IOException e) { 683 e.printStackTrace(); 684 } 685 } 686 687 695 private void send(OutputStream out, String msg) throws IOException { 696 OutputStreamWriter output = new OutputStreamWriter(out); 698 699 output.write(msg, 0, msg.length()); output.flush(); 701 } 702 703 711 private String recv(InputStream in) throws IOException { 712 String result = ""; int c = in.read(); 715 while (c >= 0 && c != '\n') { 716 if (c != '\r') { 717 result += (char)c; 718 } 719 c = in.read(); 720 } 721 return result; 723 } 724 } 725 | Popular Tags |