|
@@ -1,5 +1,8 @@
|
|
|
package de.tudarmstadt.informatik.hostage.protocol;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.Socket;
|
|
|
import java.security.SecureRandom;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
@@ -9,6 +12,7 @@ import java.util.Locale;
|
|
|
import java.util.TimeZone;
|
|
|
|
|
|
import android.content.Context;
|
|
|
+import android.os.AsyncTask;
|
|
|
import de.tudarmstadt.informatik.hostage.Hostage;
|
|
|
import de.tudarmstadt.informatik.hostage.R;
|
|
|
import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
|
|
@@ -22,7 +26,14 @@ import de.tudarmstadt.informatik.hostage.wrapper.Packet;
|
|
|
* @author Wulf Pfeiffer
|
|
|
*/
|
|
|
public class HTTP implements Protocol {
|
|
|
-
|
|
|
+
|
|
|
+ public HTTP() {
|
|
|
+ boolean useQotd = Hostage.getContext().getSharedPreferences(Hostage.getContext().getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE).getBoolean("useQotd", true);
|
|
|
+ if (useQotd) {
|
|
|
+ new QotdTask().execute(new String[] {});
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Get the current time in html header format.
|
|
|
*
|
|
@@ -181,6 +192,11 @@ public class HTTP implements Protocol {
|
|
|
} else {
|
|
|
responsePackets.add(buildPacket(STATUS_CODE_400, ""));
|
|
|
}
|
|
|
+
|
|
|
+ boolean useQotd = Hostage.getContext().getSharedPreferences(Hostage.getContext().getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE).getBoolean("useQotd", true);
|
|
|
+ if (useQotd) {
|
|
|
+ new QotdTask().execute(new String[] {});
|
|
|
+ }
|
|
|
return responsePackets;
|
|
|
}
|
|
|
|
|
@@ -218,4 +234,40 @@ public class HTTP implements Protocol {
|
|
|
return new Packet(httpVersion + " " + code + headerPrefix
|
|
|
+ document.length() + headerSuffix + document, toString());
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Task for accuiring a qotd from one of four possible servers.
|
|
|
+ *
|
|
|
+ * @author Wulf Pfeiffer
|
|
|
+ */
|
|
|
+ private class QotdTask extends AsyncTask<String, Void, String> {
|
|
|
+ @Override
|
|
|
+ protected String doInBackground(String... unused) {
|
|
|
+ String[] sources = new String[] { "djxmmx.net", "ota.iambic.com", "alpha.mike-r.com", "electricbiscuit.org" };
|
|
|
+ SecureRandom rndm = new SecureRandom();
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ try {
|
|
|
+ Socket client = new Socket(sources[rndm.nextInt(4)], 17);
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
|
|
+ while (!in.ready())
|
|
|
+ ;
|
|
|
+ while (in.ready()) {
|
|
|
+ sb.append(in.readLine());
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ client.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onPostExecute(String result) {
|
|
|
+ if (result != null)
|
|
|
+ HTTP.setHtmlDocumentContent(result);
|
|
|
+ else
|
|
|
+ HTTP.setHtmlDocumentContent(HelperUtils.getRandomString(32, false));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|