Guard against null request in Android 1.5/1.6

This commit is contained in:
macdonst 2011-01-06 00:29:17 +08:00
parent a7415bcfc9
commit 54eff557d9

View File

@ -188,54 +188,57 @@ public class CallbackServer implements Runnable {
request = xhrReader.readLine(); request = xhrReader.readLine();
String response = ""; String response = "";
//System.out.println("CallbackServerRequest="+request); //System.out.println("CallbackServerRequest="+request);
if (request.contains("GET")) { if (request != null) {
if (request.contains("GET")) {
// Must have security token
if (request.substring(5,41).equals(this.token)) { // Must have security token
//System.out.println("CallbackServer -- Processing GET request"); if (request.substring(5,41).equals(this.token)) {
//System.out.println("CallbackServer -- Processing GET request");
// Wait until there is some data to send, or send empty data every 10 sec
// to prevent XHR timeout on the client // Wait until there is some data to send, or send empty data every 10 sec
synchronized (this) { // to prevent XHR timeout on the client
while (this.empty) { synchronized (this) {
try { while (this.empty) {
this.wait(10000); // prevent timeout from happening try {
//System.out.println("CallbackServer>>> break <<<"); this.wait(10000); // prevent timeout from happening
break; //System.out.println("CallbackServer>>> break <<<");
break;
}
catch (Exception e) { }
} }
catch (Exception e) { } }
}
} // If server is still running
if (this.active) {
// If server is still running
if (this.active) { // If no data, then send 404 back to client before it times out
if (this.empty) {
// If no data, then send 404 back to client before it times out //System.out.println("CallbackServer -- sending data 0");
if (this.empty) { response = "HTTP/1.1 404 NO DATA\r\n\r\n "; // need to send content otherwise some Android devices fail, so send space
//System.out.println("CallbackServer -- sending data 0"); }
response = "HTTP/1.1 404 NO DATA\r\n\r\n "; // need to send content otherwise some Android devices fail, so send space else {
//System.out.println("CallbackServer -- sending item");
response = "HTTP/1.1 200 OK\r\n\r\n"+this.getJavascript();
}
} }
else { else {
//System.out.println("CallbackServer -- sending item"); response = "HTTP/1.1 503 Service Unavailable\r\n\r\n ";
response = "HTTP/1.1 200 OK\r\n\r\n"+this.getJavascript();
} }
} }
else { else {
response = "HTTP/1.1 503 Service Unavailable\r\n\r\n "; response = "HTTP/1.1 403 Forbidden\r\n\r\n ";
} }
} }
else { else {
response = "HTTP/1.1 403 Forbidden\r\n\r\n "; response = "HTTP/1.1 400 Bad Request\r\n\r\n ";
} }
} //System.out.println("CallbackServer: response="+response);
else { //System.out.println("CallbackServer: closing output");
response = "HTTP/1.1 400 Bad Request\r\n\r\n "; output.writeBytes(response);
} output.flush();
//System.out.println("CallbackServer: response="+response); }
//System.out.println("CallbackServer: closing output"); output.close();
output.writeBytes(response); xhrReader.close();
output.flush();
output.close();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();