Package jodd.http

Examples of jodd.http.HttpException


      while (true) {
        char c = (char) in.read();
        recv.append(c);
        if (recv.length() > 1024) {
          throw new HttpException(ProxyInfo.ProxyType.HTTP, "Received header longer then 1024 chars");
        }
        if (c == -1) {
          throw new HttpException(ProxyInfo.ProxyType.HTTP, "Invalid response");
        }
        if ((nlchars == 0 || nlchars == 2) && c == '\r') {
          nlchars++;
        } else if ((nlchars == 1 || nlchars == 3) && c == '\n') {
          nlchars++;
        } else {
          nlchars = 0;
        }
        if (nlchars == 4) {
          break;
        }
      }

      String recvStr = recv.toString();

      BufferedReader br = new BufferedReader(new StringReader(recvStr));
      String response = br.readLine();

      if (response == null) {
        throw new HttpException(ProxyInfo.ProxyType.HTTP, "Empty proxy response");
      }

      Matcher m = RESPONSE_PATTERN.matcher(response);
      if (!m.matches()) {
        throw new HttpException(ProxyInfo.ProxyType.HTTP, "Unexpected proxy response");
      }

      int code = Integer.parseInt(m.group(1));

      if (code != HttpURLConnection.HTTP_OK) {
        throw new HttpException(ProxyInfo.ProxyType.HTTP, "Invalid code");
      }

      return socket;
    } catch (RuntimeException rtex) {
      closeSocket(socket);
      throw rtex;
    } catch (Exception ex) {
      closeSocket(socket);
      throw new HttpException(ProxyInfo.ProxyType.HTTP, ex.toString(), ex);
    }

  }
View Full Code Here


      if (!check) {
        try {
          socket.close();
        } catch (Exception ignore) {
        }
        throw new HttpException(ProxyInfo.ProxyType.SOCKS5, "check failed");
      }

      // 5) CONNECT

      index = 0;
      buf[index++] = 5;
      buf[index++] = 1; // CONNECT
      buf[index++] = 0;

      byte[] hostb = host.getBytes();
      int len = hostb.length;
      buf[index++] = 3; // DOMAINNAME
      buf[index++] = (byte) (len);
      System.arraycopy(hostb, 0, buf, index, len);

      index += len;
      buf[index++] = (byte) (port >>> 8);
      buf[index++] = (byte) (port & 0xff);

      out.write(buf, 0, index);

      // 6) RESPONSE

      // in.read(buf, 0, 4);
      fill(in, buf, 4);

      if (buf[1] != 0) {
        try {
          socket.close();
        } catch (Exception ignore) {
        }
        throw new HttpException(ProxyInfo.ProxyType.SOCKS5, "proxy returned " + buf[1]);
      }

      switch (buf[3] & 0xff) {
        case 1:
          // in.read(buf, 0, 6);
          fill(in, buf, 6);
          break;
        case 3:
          // in.read(buf, 0, 1);
          fill(in, buf, 1);
          // in.read(buf, 0, buf[0]+2);
          fill(in, buf, (buf[0] & 0xff) + 2);
          break;
        case 4:
          // in.read(buf, 0, 18);
          fill(in, buf, 18);
          break;
        default:
      }
      return socket;

    } catch (RuntimeException rttex) {
      closeSocket(socket);
      throw rttex;
    } catch (Exception ex) {
      closeSocket(socket);
      throw new HttpException(ProxyInfo.ProxyType.SOCKS5, ex.toString(), ex);
    }
  }
View Full Code Here

  private void fill(InputStream in, byte[] buf, int len) throws IOException {
    int s = 0;
    while (s < len) {
      int i = in.read(buf, s, len - s);
      if (i <= 0) {
        throw new HttpException(ProxyInfo.ProxyType.SOCKS5, "stream is closed");
      }
      s += i;
    }
  }
View Full Code Here

      int len = 6;
      int s = 0;
      while (s < len) {
        int i = in.read(buf, s, len - s);
        if (i <= 0) {
          throw new HttpException(ProxyInfo.ProxyType.SOCKS4, "stream is closed");
        }
        s += i;
      }
      if (buf[0] != 0) {
        throw new HttpException(ProxyInfo.ProxyType.SOCKS4, "proxy returned VN " + buf[0]);
      }
      if (buf[1] != 90) {
        try {
          socket.close();
        } catch (Exception ignore) {
        }
        throw new HttpException(ProxyInfo.ProxyType.SOCKS4, "proxy returned CD " + buf[1]);
      }

      byte[] temp = new byte[2];
      in.read(temp, 0, 2);
      return socket;
    } catch (RuntimeException rtex) {
      closeSocket(socket);
      throw rtex;
    } catch (Exception ex) {
      closeSocket(socket);
      throw new HttpException(ProxyInfo.ProxyType.SOCKS4, ex.toString(), ex);
    }
  }
View Full Code Here

      while (true) {
        char c = (char) in.read();
        recv.append(c);
        if (recv.length() > 1024) {
          throw new HttpException(ProxyInfo.ProxyType.HTTP, "Received header longer then 1024 chars");
        }
        if (c == -1) {
          throw new HttpException(ProxyInfo.ProxyType.HTTP, "Invalid response");
        }
        if ((nlchars == 0 || nlchars == 2) && c == '\r') {
          nlchars++;
        } else if ((nlchars == 1 || nlchars == 3) && c == '\n') {
          nlchars++;
        } else {
          nlchars = 0;
        }
        if (nlchars == 4) {
          break;
        }
      }

      String recvStr = recv.toString();

      BufferedReader br = new BufferedReader(new StringReader(recvStr));
      String response = br.readLine();

      if (response == null) {
        throw new HttpException(ProxyInfo.ProxyType.HTTP, "Empty proxy response");
      }

      Matcher m = RESPONSE_PATTERN.matcher(response);
      if (!m.matches()) {
        throw new HttpException(ProxyInfo.ProxyType.HTTP, "Unexpected proxy response");
      }

      int code = Integer.parseInt(m.group(1));

      if (code != HttpURLConnection.HTTP_OK) {
        throw new HttpException(ProxyInfo.ProxyType.HTTP, "Invalid code");
      }

      return socket;
    } catch (RuntimeException rtex) {
      closeSocket(socket);
      throw rtex;
    } catch (Exception ex) {
      closeSocket(socket);
      throw new HttpException(ProxyInfo.ProxyType.HTTP, ex.toString(), ex);
    }

  }
View Full Code Here

      if (!check) {
        try {
          socket.close();
        } catch (Exception ignore) {
        }
        throw new HttpException(ProxyInfo.ProxyType.SOCKS5, "check failed");
      }

      // 5) CONNECT

      index = 0;
      buf[index++] = 5;
      buf[index++] = 1; // CONNECT
      buf[index++] = 0;

      byte[] hostb = host.getBytes();
      int len = hostb.length;
      buf[index++] = 3; // DOMAINNAME
      buf[index++] = (byte) (len);
      System.arraycopy(hostb, 0, buf, index, len);

      index += len;
      buf[index++] = (byte) (port >>> 8);
      buf[index++] = (byte) (port & 0xff);

      out.write(buf, 0, index);

      // 6) RESPONSE

      // in.read(buf, 0, 4);
      fill(in, buf, 4);

      if (buf[1] != 0) {
        try {
          socket.close();
        } catch (Exception ignore) {
        }
        throw new HttpException(ProxyInfo.ProxyType.SOCKS5, "proxy returned " + buf[1]);
      }

      switch (buf[3] & 0xff) {
        case 1:
          // in.read(buf, 0, 6);
          fill(in, buf, 6);
          break;
        case 3:
          // in.read(buf, 0, 1);
          fill(in, buf, 1);
          // in.read(buf, 0, buf[0]+2);
          fill(in, buf, (buf[0] & 0xff) + 2);
          break;
        case 4:
          // in.read(buf, 0, 18);
          fill(in, buf, 18);
          break;
        default:
      }
      return socket;

    } catch (RuntimeException rttex) {
      closeSocket(socket);
      throw rttex;
    } catch (Exception ex) {
      closeSocket(socket);
      throw new HttpException(ProxyInfo.ProxyType.SOCKS5, ex.toString(), ex);
    }
  }
View Full Code Here

  private void fill(InputStream in, byte[] buf, int len) throws IOException {
    int s = 0;
    while (s < len) {
      int i = in.read(buf, s, len - s);
      if (i <= 0) {
        throw new HttpException(ProxyInfo.ProxyType.SOCKS5, "stream is closed");
      }
      s += i;
    }
  }
View Full Code Here

      int len = 6;
      int s = 0;
      while (s < len) {
        int i = in.read(buf, s, len - s);
        if (i <= 0) {
          throw new HttpException(ProxyInfo.ProxyType.SOCKS4, "stream is closed");
        }
        s += i;
      }
      if (buf[0] != 0) {
        throw new HttpException(ProxyInfo.ProxyType.SOCKS4, "proxy returned VN " + buf[0]);
      }
      if (buf[1] != 90) {
        try {
          socket.close();
        } catch (Exception ignore) {
        }
        throw new HttpException(ProxyInfo.ProxyType.SOCKS4, "proxy returned CD " + buf[1]);
      }

      byte[] temp = new byte[2];
      in.read(temp, 0, 2);
      return socket;
    } catch (RuntimeException rtex) {
      closeSocket(socket);
      throw rtex;
    } catch (Exception ex) {
      closeSocket(socket);
      throw new HttpException(ProxyInfo.ProxyType.SOCKS4, ex.toString(), ex);
    }
  }
View Full Code Here

  public void setTimeout(int milliseconds) {
    try {
      socket.setSoTimeout(milliseconds);
    } catch (SocketException sex) {
      throw new HttpException(sex);
    }
  }
View Full Code Here

  public byte[] getBytes() {
    try {
      return FileUtil.readBytes(file);
    } catch (IOException ioex) {
      throw new HttpException(ioex);
    }
  }
View Full Code Here

TOP

Related Classes of jodd.http.HttpException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.