public byte[] getResponseData() throws Exception {
   ByteArrayOutputStream os = new ByteArrayOutputStream();
   os.write(header_.getHeaderDataAsByte());
   if (header_.getStatusCode() == HttpResponseHeader.NOT_MODIFIED_CODE_304) os.toByteArray();
   if (header_.getStatusCode() == HttpResponseHeader.STATUS_STRING_MOVED_CODE_302)
     return os.toByteArray();
   os.write(body_.getBodyDataAsByte());
   return os.toByteArray();
 }
 public byte[] getOriginalResponseData() throws Exception {
   ByteArrayOutputStream os = new ByteArrayOutputStream();
   if (header_.getOriginalHeaderDataAsByte() != null) {
     os.write(header_.getOriginalHeaderDataAsByte());
   }
   byte[] bodyData = body_.getBodyDataAsByte();
   if (bodyData.length > 0) {
     os.write(bodyData);
   }
   return os.toByteArray();
 }
Exemple #3
0
  /**
   * Returns the content length of the specified web socket message. If the specified message is not
   * a web socket message, {@code -1} is returned.
   */
  private static int getWebSocketContentLength(HttpHeader message) {
    // WebSockset messages have constant content-lengths.
    if (message instanceof HttpRequestHeader) {
      HttpRequestHeader req = (HttpRequestHeader) message;
      if (HttpMethod.GET.equals(req.getMethod())
          && req.containsHeader(Names.SEC_WEBSOCKET_KEY1)
          && req.containsHeader(Names.SEC_WEBSOCKET_KEY2)) {
        return 8;
      }
    } else if (message instanceof HttpResponseHeader) {
      HttpResponseHeader res = (HttpResponseHeader) message;
      if (res.getStatus().getCode() == 101
          && res.containsHeader(Names.SEC_WEBSOCKET_ORIGIN)
          && res.containsHeader(Names.SEC_WEBSOCKET_LOCATION)) {
        return 16;
      }
    }

    // Not a web socket message
    return -1;
  }
 public void forward(OutputStream out) throws Exception {
   out.write(header_.getOriginalHeaderDataAsByte());
   if (header_.getStatusCode() == HttpResponseHeader.NOT_MODIFIED_CODE_304) return;
   if (header_.getStatusCode() == HttpResponseHeader.STATUS_STRING_MOVED_CODE_302) return;
   out.write(body_.getBodyDataAsByte());
 }
  public Socket createSocket(String host, int port)
      throws IOException, FingerprintException, FingerprintError {

    Socket s = new Socket();
    s.connect(new InetSocketAddress(proxyHost, proxyPort), 1000 * timeout);

    PrintWriter out = new PrintWriter(s.getOutputStream());
    InputStreamReader isr = new InputStreamReader(s.getInputStream());
    BufferedReader in = new BufferedReader(isr);

    String[] connHdr = {
      "CONNECT " + host + ":" + port + " HTTP/1.1",
      "Host: " + host + ":" + port,
      "Proxy-Connection: close"
    };

    String[] connReq = null;

    if (authRequired) {
      connReq = new String[connHdr.length + 1];
      for (int i = 0; i < connHdr.length; i++) {
        connReq[i] = connHdr[i];
      }
      connReq[connHdr.length] = "Proxy-Authorization: " + authenticator;
    } else {
      connReq = new String[connHdr.length];
      for (int i = 0; i < connHdr.length; i++) {
        connReq[i] = connHdr[i];
      }
    }
    sendRequest(out, connReq);

    HttpResponseHeader hdr = new HttpResponseHeader(in);
    int status = hdr.getStatusCode();

    if (Debug.get(Debug.Communication)) {
      System.err.println("Got status " + status);
    }

    /* Now check the status code and act upon it */
    if ((status / 100) == 2) { // Code is 2xx
      return s;
    }

    if (((status / 100) == 4) && (status != 407)) {
      throw new HttpProxyError("Proxy request failed");
    }

    if ((status == 407) && authRequired) {
      /* We already sent authorisation info,
       * but the proxy did not accept it.
       * So we can only stop here, as
       * we do not have the right credentials
       * at hand.
       */
      throw new HttpProxyError("Cannot authenticate " + "to proxy, wrong " + "credentials?");
    }
    if (status == 407) {
      if (Debug.get(Debug.SockInit)) {
        System.err.println("Preparing proxy " + "authenticator for " + hdr.getProxyAuthInfo());
      }
      prepareAuthorisation(hdr.getProxyAuthInfo());
      authRequired = true;
      return createSocket(host, port);
    }
    return s;
  }