Ejemplo n.º 1
0
  /**
   * Walk down the path starting from the portion immediately following the scheme. i.e. schemeWalk
   * is responsible for parsing the host and port from the URL.
   *
   * @param userPath the user's passed in path
   * @param attributes the attributes for the new path
   * @param uri the normalized full uri
   * @param offset offset into the uri to start processing, i.e. after the scheme.
   * @return the looked-up path.
   */
  @Override
  public Path schemeWalk(String userPath, Map<String, Object> attributes, String uri, int offset) {
    int length = uri.length();

    if (length < 2 + offset || uri.charAt(offset) != '/' || uri.charAt(offset + 1) != '/')
      throw new RuntimeException(L.l("bad scheme in `{0}'", uri));

    CharBuffer buf = CharBuffer.allocate();
    int i = 2 + offset;
    int ch = 0;
    boolean isInBrace = false;

    for (;
        i < length && ((ch = uri.charAt(i)) != ':' || isInBrace) && ch != '/' && ch != '?';
        i++) {
      buf.append((char) ch);

      if (ch == '[') isInBrace = true;
      else if (ch == ']') isInBrace = false;
    }

    String host = buf.close();
    if (host.length() == 0) throw new RuntimeException(L.l("bad host in `{0}'", uri));

    int port = 0;
    if (ch == ':') {
      for (i++; i < length && (ch = uri.charAt(i)) >= '0' && ch <= '9'; i++) {
        port = 10 * port + uri.charAt(i) - '0';
      }
    }

    if (port == 0) port = 80;

    HttpPath root = create(host, port);

    return root.fsWalk(userPath, attributes, uri.substring(i));
  }
Ejemplo n.º 2
0
  /**
   * Opens a new HTTP stream for reading, i.e. a GET request.
   *
   * @param path the URL for the stream
   * @return the opened stream
   */
  static HttpStreamWrapper openRead(HttpPath path) throws IOException {
    HttpStream stream = createStream(path);
    stream._isPost = false;

    HttpStreamWrapper wrapper = new HttpStreamWrapper(stream);

    String status = (String) wrapper.getAttribute("status");

    // ioc/23p0
    if ("404".equals(status)) {
      throw new FileNotFoundException(L.l("'{0}' returns a HTTP 404.", path.getURL()));
    }

    return wrapper;
  }
Ejemplo n.º 3
0
  /**
   * Creates a new HTTP stream. If there is a saved connection to the same host, use it.
   *
   * @param path the URL for the stream
   * @return the opened stream
   */
  private static HttpStream createStream(HttpPath path) throws IOException {
    String host = path.getHost();
    int port = path.getPort();

    HttpStream stream = null;
    long streamTime = 0;
    synchronized (LOCK) {
      if (_savedStream != null
          && host.equals(_savedStream.getHost())
          && port == _savedStream.getPort()) {
        stream = _savedStream;
        streamTime = _saveTime;
        _savedStream = null;
      }
    }

    if (stream != null) {
      long now;

      now = CurrentTime.getCurrentTime();

      if (now < streamTime + 5000) {
        // if the stream is still valid, use it
        stream.init(path);
        return stream;
      } else {
        // if the stream has timed out, close it
        try {
          stream._isKeepalive = false;
          stream.close();
        } catch (IOException e) {
          log.log(Level.FINE, e.toString(), e);
        }
      }
    }

    Socket s;

    try {
      s = new Socket(host, port);

      if (path instanceof HttpsPath) {
        SSLContext context = SSLContext.getInstance("TLSv1");

        javax.net.ssl.TrustManager tm =
            new javax.net.ssl.X509TrustManager() {
              public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
              }

              public void checkClientTrusted(
                  java.security.cert.X509Certificate[] cert, String foo) {}

              public void checkServerTrusted(
                  java.security.cert.X509Certificate[] cert, String foo) {}
            };

        context.init(null, new javax.net.ssl.TrustManager[] {tm}, null);
        SSLSocketFactory factory = context.getSocketFactory();

        SSLSocket sslSock = (SSLSocket) factory.createSocket(s, host, port, true);

        s = sslSock;
      }
    } catch (ConnectException e) {
      throw new ConnectException(path.getURL() + ": " + e.getMessage());
    } catch (Exception e) {
      throw new ConnectException(path.getURL() + ": " + e.toString());
    }

    int socketTimeout = 300 * 1000;

    try {
      s.setSoTimeout(socketTimeout);
    } catch (Exception e) {
    }

    return new HttpStream(path, host, port, s);
  }