📄 server.java
字号:
String value = (str.substring(delimitPos + 1)).trim();
if(key.equals("host")) {
delimitPos = value.indexOf(':');
if(delimitPos == -1) {
requestHost = value;
} else {
requestHost = value.substring(0, delimitPos);
requestPort = Integer.valueOf(value.substring(delimitPos + 1)).intValue();
}
}
requestHeaderField.put(key, value);
}
}
private void sendRequest() throws MalformedURLException, ProtocolException, IOException {
URL url;
if(host != null) {
url = new URL("http", host, port, requestURI);
} else {
url = new URL(requestURI);
}
hc = (HttpURLConnection)url.openConnection();
hc.setConnectTimeout(30 * 1000);
hc.setReadTimeout(30 * 1000);
hc.setRequestMethod(requestMethod);
Enumeration e = requestHeaderField.keys();
String key;
String value;
while (e.hasMoreElements()){
key = (String)e.nextElement();
value = (String)requestHeaderField.get(key);
hc.setRequestProperty(key, value);
// Pihatonttu.PihatonttuMain.logFrame.addElement(key +": " + value);
}
if(requestMethod.equals("POST")) {
hc.setDoOutput(true);
hc.connect();
writer = new DataOutputStream(hc.getOutputStream());
writer.write(postData);
writer.flush();
postData = null;
} else {
hc.connect();
}
}
private void readResponseHeader() throws IOException {
reader = new DataInputStream(hc.getInputStream());
responseProtocol = "HTTP/1.1";
responseCode = Integer.toString(hc.getResponseCode());
responseMessage = hc.getResponseMessage();
comWriter.write((responseProtocol + " " + responseCode + " " + responseMessage + "\r\n").getBytes());
String key;
for(int i = 1; ((key = hc.getHeaderFieldKey(i)) != null); i++){
if(!key.equalsIgnoreCase("Transfer-Encoding")) {
comWriter.write((key + ": " + hc.getHeaderField(i) +"\r\n").getBytes());
// Pihatonttu.PihatonttuMain.logFrame.addElement(key + ": " + hc.getHeaderField(i));
}
}
comWriter.write("\r\n".getBytes());
}
private void forwardResponse() throws IOException {
int contentLength = (int)hc.getContentLength();
if(contentLength > 0) {
int temp = 0;
int read = 0 ;
byte[] buf = new byte[2048];
while ((read < contentLength) && (temp != -1)) {
temp = reader.read(buf, 0, 2048);
if(temp != -1) {
comWriter.write(buf, 0, temp);
read += temp;
byteTranfered += temp;
Pihatonttu.PihatonttuMain.logFrame.addElementFraction("localhost - - [" + accessTime + "] \"" +
requestMethod + " " + requestURI + " " + requestProtocol + "\" " + responseCode + " " + byteTranfered);
}
}
buf = null;
} else {
byte[] buf = new byte[2048];
int len = -1;
while(true) {
if((len = reader.read(buf, 0, 2048)) == -1) {
break;
} else {
comWriter.write(buf, 0, len);
byteTranfered += len;
Pihatonttu.PihatonttuMain.logFrame.addElementFraction("localhost - - [" + accessTime + "] \"" +
requestMethod + " " + requestURI + " " + requestProtocol + "\" " + responseCode + " " + byteTranfered);
}
}
buf = null;
}
comWriter.flush();
}
private void sendStreamingRequest() throws IOException{
int delimitPos = requestURI.lastIndexOf(':');
if(delimitPos == -1) {
requestHost = requestURI;
requestPort = 554;
} else {
requestHost = requestURI.substring(7, delimitPos);
requestPort = Integer.valueOf(requestURI.substring(delimitPos + 1)).intValue();
}
socket = new Socket(requestHost, requestPort);
writer = new DataOutputStream(socket.getOutputStream());
writer.write((requestMethod + " " + requestURI + " " + requestProtocol + "\r\n").getBytes());
Enumeration e = requestHeaderField.keys();
String key;
String value;
while (e.hasMoreElements()){
key = (String)e.nextElement();
value = (String)requestHeaderField.get(key);
if(!key.equals("connection") && !key.equals("host")) {
writer.write((key + ": " + value + "\r\n").getBytes());
}
}
writer.write("\r\n".getBytes());
writer.flush();
}
private void readStreamingResponse() throws IOException{
reader = new DataInputStream(socket.getInputStream());
StringBuffer sb = new StringBuffer();
int c;
int line = 0;
while((c = readbuffer(reader, 10)) != -1) {
if((char)c == '\r') {
if(sb.length() == 0) {
break;
} else {
// Pihatonttu.PihatonttuMain.logFrame.addElement(sb.toString());
comWriter.write((sb.toString() + "\r\n").getBytes() );
sb.delete(0, sb.length());
line++;
}
} else if((char)c == '\n') {
} else {
sb.append((char)c);
}
}
sb = null;
comWriter.write("\r\n".getBytes());
comWriter.flush();
}
private void returnUnknownHostException(DataOutputStream output) {
try {
output.write("HTTP/1.1 504 Gateway Timeout\r\n".getBytes());
output.write("Content-Type: text/html\r\n\r\n".getBytes());
output.write("<html>".getBytes());
output.write("<head><title>Problem loading page</title></head>".getBytes());
output.write("<body>".getBytes());
output.write("<h1>Server not found</h1>".getBytes());
output.write(("<p>Pihatonttu Proxy can't find the server at "+ requestHost + ".</p>").getBytes());
output.write("</body>".getBytes());
output.write("</html>".getBytes());
output.flush();
} catch(IOException e) {
Pihatonttu.PihatonttuMain.showErrorDialog("Bluetooth connnection is not available.");
} finally {
responseProtocol = "HTTP/1.1";
responseCode = "504";
responseMessage = "Gateway Timeout";
byteTranfered = 0;
Pihatonttu.PihatonttuMain.logFrame.addElementFraction("localhost - - [" + accessTime + "] \"" +
requestMethod + " " + requestURI + " " + requestProtocol + "\" " + responseCode + " " + byteTranfered);
}
}
private void returnInternalServerException(DataOutputStream output) {
try {
if(byteTranfered == 0) {
output.write("HTTP/1.1 500 Internal Server Error\r\n".getBytes());
output.write("Content-Type: text/html\r\n\r\n".getBytes());
output.write("<html>".getBytes());
output.write("<head><title>Problem loading page</title></head>".getBytes());
output.write("<body>".getBytes());
output.write("<h1>500 Internal Server Error</h1>".getBytes());
output.write(("<p>Pihatonttu Proxy encountered an internal error.</p>").getBytes());
output.write("</body>".getBytes());
output.write("</html>".getBytes());
output.flush();
}
} catch(IOException e) {
Pihatonttu.PihatonttuMain.showErrorDialog("Bluetooth connnection is not available.");
} finally {
responseProtocol = "HTTP/1.1";
responseCode = "500";
responseMessage = "Internal Server Error";
// byteTranfered = 0;
Pihatonttu.PihatonttuMain.logFrame.addElementFraction("localhost - - [" + accessTime + "] \"" +
requestMethod + " " + requestURI + " " + requestProtocol + "\" " + responseCode + " " + byteTranfered);
}
}
private void returnUnknownProtocolException(DataOutputStream output) {
try {
output.write("HTTP/1.1 501 Not Implemented\r\n".getBytes());
output.write("Content-Type: text/html\r\n\r\n".getBytes());
output.write("<html>".getBytes());
output.write("<head><title>Problem loading page</title></head>".getBytes());
output.write("<body>".getBytes());
output.write("<h1>Protocol not supported</h1>".getBytes());
output.write(("<p>Pihatonttu Proxy can't support requested protocol.</p>").getBytes());
output.write("</body>".getBytes());
output.write("</html>".getBytes());
output.flush();
} catch(IOException e) {
Pihatonttu.PihatonttuMain.showErrorDialog("Bluetooth connnection is not available.");
} finally {
responseProtocol = "HTTP/1.1";
responseCode = "501";
responseMessage = "Not Implemented";
byteTranfered = 0;
Pihatonttu.PihatonttuMain.logFrame.addElementFraction("localhost - - [" + accessTime + "] \"" +
requestMethod + " " + requestURI + " " + requestProtocol + "\" " + responseCode + " " + byteTranfered);
}
}
private String getAccessTime() {
cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
switch(cal.get(Calendar.MONTH) + 1) {
case 1: month = "Jan"; break;
case 2: month = "Feb"; break;
case 3: month = "Mar"; break;
case 4: month = "Apr"; break;
case 5: month = "May"; break;
case 6: month = "Jun"; break;
case 7: month = "Jul"; break;
case 8: month = "Aug"; break;
case 9: month = "Sep"; break;
case 10: month = "Oct"; break;
case 11: month = "Nov"; break;
case 12: month = "Dec"; break;
}
day_of_month = cal.get(Calendar.DAY_OF_MONTH);
hour = cal.get(Calendar.HOUR_OF_DAY);
minute = cal.get(Calendar.MINUTE);
second = cal.get(Calendar.SECOND);
return formatNum(day_of_month) + "/" + month + "/" + year +
":" + formatNum(hour) + ":" + formatNum(minute) + ":" + formatNum(second) + " +0900";
}
private String formatNum(int num) {
if(num < 10) return "0" + num;
else return "" + num;
}
private static int readbuffer(DataInputStream stream, int reread) throws IOException {
int c = -1;
for(int i = 0; i < reread; i++) {
if(stream.available() != 0) {
c = stream.read();
break;
}
try {
Thread.sleep(100);
} catch(InterruptedException e) {
}
}
return c;
}
private static int readbuffer(DataInputStream stream, byte[] buf, int len, int reread) throws IOException {
int c = -1;
for(int i = 0; i < reread; i++) {
if(stream.available() != 0) {
c = stream.read(buf, 0, len);
break;
}
try {
Thread.sleep(100);
} catch(InterruptedException e) {
}
}
return c;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -