publicclass B6726695 extendsThread { private ServerSocket server = null; privateint port = 0; privatebyte[] data = newbyte[512]; private String boundary = "----------------7774563516523621";
publicstaticvoid main(String[] args) throws Exception {
B6726695 test = new B6726695(); // Exit even if server is still running
test.setDaemon(true); // start server
test.start(); // run test
test.test();
}
public B6726695() { try {
server = new ServerSocket();
server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
port = server.getLocalPort();
} catch (IOException e) {
e.printStackTrace();
}
}
// 1st Connection. Should be rejected. I.E. get a ProtocolException
URLConnection con = url.openConnection(Proxy.NO_PROXY);
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.setRequestProperty("Expect", "100-Continue");
http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
http.setDoOutput(true);
http.setFixedLengthStreamingMode(512);
OutputStream out = null; int errorCode = -1; try {
out = http.getOutputStream();
} catch (ProtocolException e) {
errorCode = http.getResponseCode();
} if (errorCode != 417) { thrownew RuntimeException("Didn't get the ProtocolException");
}
// 2nd connection. Should be accepted by server.
http = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
http.setRequestMethod("POST");
http.setRequestProperty("Expect", "100-Continue");
http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
http.setDoOutput(true);
http.setFixedLengthStreamingMode(data.length);
out = null; try {
out = http.getOutputStream();
} catch (ProtocolException e) {
} if (out == null) { thrownew RuntimeException("Didn't get an OutputStream");
}
out.write(data);
out.flush();
errorCode = http.getResponseCode(); if (errorCode != 200) { thrownew RuntimeException("Response code is " + errorCode);
}
out.close();
// 3rd connection. Simulate a server that doesn't implement 100-continue
http = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
http.setRequestMethod("POST");
http.setRequestProperty("Expect", "100-Continue");
http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
http.setDoOutput(true);
http.setFixedLengthStreamingMode(data.length);
out = null; try {
out = http.getOutputStream();
} catch (ProtocolException e) {
} if (out == null) { thrownew RuntimeException("Didn't get an OutputStream");
}
out.write(data);
out.flush();
out.close();
errorCode = http.getResponseCode(); if (errorCode != 200) { thrownew RuntimeException("Response code is " + errorCode);
}
}
@Override publicvoid run() { try { // Fist connection: don't accetpt the request
Socket s = server.accept();
serverReject(s); // Second connection: accept the request (send 100-continue)
s = server.accept();
serverAccept(s); // 3rd connection: just ignore the 'Expect:' header
s = server.accept();
serverIgnore(s);
} catch (IOException e) {
e.printStackTrace();
} finally { try { server.close(); } catch (IOException unused) {}
}
}
publicvoid serverReject(Socket s) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));
String line = null; do {
line = in.readLine();
} while (line != null && line.length() != 0);
publicvoid serverAccept(Socket s) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));
String line = null; do {
line = in.readLine();
} while (line != null && line.length() != 0);
// Send 100-Continue
out.print("HTTP/1.1 100 Continue\r\n");
out.print("\r\n");
out.flush(); // Then read the body char[] cbuf = newchar[512];
in.read(cbuf);
/* Force the server to not respond for more that the expect 100-Continue *timeoutsetbytheHTTPhandler(5000millis).Thisensuresthe *timeoutiscorrectlyresetsthedefaultreadtimeout,infinity.
* See 6993490. */
System.out.println("server sleeping..."); try {Thread.sleep(6000); } catch (InterruptedException e) {}
// finally send the 200 OK
out.print("HTTP/1.1 200 OK");
out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");
out.print("Connection: close\r\n");
out.print("Content-Length: 0\r\n");
out.print("\r\n");
out.flush();
out.close();
in.close();
}
publicvoid serverIgnore(Socket s) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));
String line = null; do {
line = in.readLine();
} while (line != null && line.length() != 0);
// Then read the body char[] cbuf = newchar[512]; int l = in.read(cbuf); // finally send the 200 OK
out.print("HTTP/1.1 200 OK");
out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");
out.print("Content-Length: 0\r\n");
out.print("Connection: close\r\n");
out.print("\r\n");
out.flush();
out.close();
in.close();
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.