PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/JAVA_ANDROID/src/APJP/ANDROID/HTTPS/HTTPSServerWorker.java

http://apjp.googlecode.com/
Java | 538 lines | 400 code | 128 blank | 10 comment | 48 complexity | 2982f43d0565e755d7d88075201fd9fb MD5 | raw file
  1. /*
  2. APJP, A PHP/JAVA PROXY
  3. Copyright (C) 2009-2011 Jeroen Van Steirteghem
  4. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  5. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  6. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7. */
  8. package APJP.ANDROID.HTTPS;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import javax.net.ssl.SSLSocket;
  12. import APJP.ANDROID.APJP;
  13. import APJP.ANDROID.Logger;
  14. import APJP.ANDROID.HTTP11.HTTPMessageHeader;
  15. import APJP.ANDROID.HTTP11.HTTPRequestMessage;
  16. import APJP.ANDROID.HTTP11.HTTPResponseMessage;
  17. import APJP.ANDROID.HTTP11.HTTPSRequest;
  18. import APJP.ANDROID.HTTP11.HTTPSRequests;
  19. public class HTTPSServerWorker implements Runnable
  20. {
  21. private static Logger logger;
  22. private Thread thread;
  23. private HTTPSServer httpsServer;
  24. private SSLSocket inputSSLSocket;
  25. private boolean keepAlive;
  26. static
  27. {
  28. logger = Logger.getLogger(APJP.APJP_LOCAL_HTTPS_SERVER_LOGGER_ID);
  29. }
  30. protected HTTPSServerWorker(HTTPSServer httpsServer, SSLSocket inputSSLSocket)
  31. {
  32. this.httpsServer = httpsServer;
  33. this.inputSSLSocket = inputSSLSocket;
  34. }
  35. protected synchronized void start() throws HTTPSServerException
  36. {
  37. logger.log(2, "HTTPS_SERVER_WORKER/START");
  38. try
  39. {
  40. startHTTPSServerWorker();
  41. }
  42. catch(Exception e)
  43. {
  44. logger.log(2, "HTTPS_SERVER_WORKER/START: EXCEPTION", e);
  45. throw new HTTPSServerException("HTTPS_SERVER_WORKER/START", e);
  46. }
  47. }
  48. protected synchronized void stop() throws HTTPSServerException
  49. {
  50. logger.log(2, "HTTPS_SERVER_WORKER/STOP");
  51. try
  52. {
  53. stopHTTPSServerWorker();
  54. }
  55. catch(Exception e)
  56. {
  57. logger.log(2, "HTTPS_SERVER_WORKER/STOP: EXCEPTION", e);
  58. throw new HTTPSServerException("HTTPS_SERVER_WORKER/STOP", e);
  59. }
  60. }
  61. protected synchronized void startHTTPSServerWorker() throws HTTPSServerException
  62. {
  63. logger.log(2, "HTTPS_SERVER_WORKER/START_HTTPS_SERVER_WORKER");
  64. try
  65. {
  66. thread = new Thread(this);
  67. thread.start();
  68. }
  69. catch(Exception e)
  70. {
  71. logger.log(2, "HTTPS_SERVER_WORKER/START_HTTPS_SERVER_WORKER: EXCEPTION", e);
  72. throw new HTTPSServerException("HTTPS_SERVER_WORKER/START_HTTPS_SERVER_WORKER", e);
  73. }
  74. }
  75. protected synchronized void stopHTTPSServerWorker() throws HTTPSServerException
  76. {
  77. logger.log(2, "HTTPS_SERVER_WORKER/STOP_HTTPS_SERVER_WORKER");
  78. try
  79. {
  80. thread = null;
  81. try
  82. {
  83. inputSSLSocket.close();
  84. }
  85. catch(Exception e)
  86. {
  87. }
  88. }
  89. catch(Exception e)
  90. {
  91. logger.log(2, "HTTPS_SERVER_WORKER/STOP_HTTPS_SERVER_WORKER: EXCEPTION", e);
  92. throw new HTTPSServerException("HTTPS_SERVER_WORKER/STOP_HTTPS_SERVER_WORKER", e);
  93. }
  94. }
  95. public void run()
  96. {
  97. try
  98. {
  99. do
  100. {
  101. keepAlive = true;
  102. process();
  103. }
  104. while(keepAlive);
  105. }
  106. catch(Exception e)
  107. {
  108. if(thread != null)
  109. {
  110. logger.log(2, "HTTPS_SERVER_WORKER: EXCEPTION", e);
  111. }
  112. }
  113. finally
  114. {
  115. if(thread != null)
  116. {
  117. try
  118. {
  119. httpsServer.stopHTTPSServerWorker(this);
  120. }
  121. catch(Exception e)
  122. {
  123. }
  124. }
  125. }
  126. }
  127. protected void processHTTPRequestMessage(HTTPRequestMessage httpRequestMessage) throws Exception
  128. {
  129. HTTPMessageHeader httpRequestMessage1Header01 = httpRequestMessage.getHTTPMessageHeader("Proxy-Connection");
  130. if(httpRequestMessage1Header01 != null)
  131. {
  132. String httpRequestMessage1Header1Value01 = httpRequestMessage1Header01.getValue();
  133. if(httpRequestMessage1Header1Value01.equalsIgnoreCase("close"))
  134. {
  135. keepAlive = false;
  136. }
  137. }
  138. HTTPMessageHeader httpRequestMessage1Header02 = httpRequestMessage.getHTTPMessageHeader("Connection");
  139. if(httpRequestMessage1Header02 != null)
  140. {
  141. String httpRequestMessage1Header1Value02 = httpRequestMessage1Header02.getValue();
  142. if(httpRequestMessage1Header1Value02.equalsIgnoreCase("close"))
  143. {
  144. keepAlive = false;
  145. }
  146. }
  147. }
  148. protected void processHTTPResponseMessage(HTTPResponseMessage httpResponseMessage) throws Exception
  149. {
  150. HTTPMessageHeader httpResponseMessageHeader1 = httpResponseMessage.getHTTPMessageHeader("Content-Length");
  151. if(httpResponseMessageHeader1 == null)
  152. {
  153. keepAlive = false;
  154. }
  155. HTTPMessageHeader httpResponseMessageHeader2 = httpResponseMessage.getHTTPMessageHeader("Proxy-Connection");
  156. if(httpResponseMessageHeader2 != null)
  157. {
  158. httpResponseMessage.removeHTTPMessageHeader(httpResponseMessageHeader2);
  159. }
  160. if(keepAlive == true)
  161. {
  162. httpResponseMessage.addHTTPMessageHeader(new HTTPMessageHeader("Proxy-Connection", "Keep-Alive"));
  163. }
  164. else
  165. {
  166. httpResponseMessage.addHTTPMessageHeader(new HTTPMessageHeader("Proxy-Connection", "close"));
  167. }
  168. HTTPMessageHeader httpResponseMessageHeader3 = httpResponseMessage.getHTTPMessageHeader("Connection");
  169. if(httpResponseMessageHeader3 != null)
  170. {
  171. httpResponseMessage.removeHTTPMessageHeader(httpResponseMessageHeader3);
  172. }
  173. if(keepAlive == true)
  174. {
  175. httpResponseMessage.addHTTPMessageHeader(new HTTPMessageHeader("Connection", "Keep-Alive"));
  176. }
  177. else
  178. {
  179. httpResponseMessage.addHTTPMessageHeader(new HTTPMessageHeader("Connection", "close"));
  180. }
  181. }
  182. public void process() throws Exception
  183. {
  184. InputStream inputSSLSocketInputStream = inputSSLSocket.getInputStream();
  185. OutputStream inputSSLSocketOutputStream = inputSSLSocket.getOutputStream();
  186. HTTPSRequests httpsRequests = HTTPSRequests.getHTTPSRequests();
  187. HTTPRequestMessage httpRequestMessage1 = new HTTPRequestMessage(inputSSLSocketInputStream);
  188. httpRequestMessage1.read();
  189. HTTPMessageHeader httpRequestMessage1Header1 = httpRequestMessage1.getHTTPMessageHeader("");
  190. if(httpRequestMessage1Header1 == null)
  191. {
  192. keepAlive = false;
  193. return;
  194. }
  195. String httpRequestMessage1Header1Value1 = httpRequestMessage1Header1.getValue();
  196. if(httpRequestMessage1Header1Value1.equalsIgnoreCase(""))
  197. {
  198. keepAlive = false;
  199. return;
  200. }
  201. processHTTPRequestMessage(httpRequestMessage1);
  202. HTTPResponseMessage httpResponseMessage1 = null;
  203. try
  204. {
  205. HTTPSRequest httpsRequest1 = httpsRequests.createHTTPSRequest(httpRequestMessage1);
  206. httpsRequest1.open();
  207. try
  208. {
  209. httpResponseMessage1 = httpsRequest1.getHTTPResponseMessage();
  210. processHTTPResponseMessage(httpResponseMessage1);
  211. HTTPMessageHeader[] httpResponseMessage1Headers1 = httpResponseMessage1.getHTTPMessageHeaders();
  212. HTTPMessageHeader httpResponseMessage1Header1 = httpResponseMessage1Headers1[0];
  213. String httpResponseMessage1Header1Key1 = httpResponseMessage1Header1.getKey();
  214. String httpResponseMessage1Header1Value1 = httpResponseMessage1Header1.getValue();
  215. inputSSLSocketOutputStream.write((httpResponseMessage1Header1Value1 + "\r\n").getBytes());
  216. for(int i = 1; i < httpResponseMessage1Headers1.length; i = i + 1)
  217. {
  218. httpResponseMessage1Header1 = httpResponseMessage1Headers1[i];
  219. httpResponseMessage1Header1Key1 = httpResponseMessage1Header1.getKey();
  220. httpResponseMessage1Header1Value1 = httpResponseMessage1Header1.getValue();
  221. inputSSLSocketOutputStream.write((httpResponseMessage1Header1Key1 + ": " + httpResponseMessage1Header1Value1 + "\r\n").getBytes());
  222. }
  223. inputSSLSocketOutputStream.write(("\r\n").getBytes());
  224. httpResponseMessage1.read(inputSSLSocketOutputStream);
  225. }
  226. catch(Exception e)
  227. {
  228. throw e;
  229. }
  230. finally
  231. {
  232. try
  233. {
  234. httpsRequest1.close();
  235. }
  236. catch(Exception e)
  237. {
  238. }
  239. }
  240. }
  241. catch(Exception e)
  242. {
  243. logger.log(2, "HTTPS_SERVER_WORKER/PROCESS: EXCEPTION", e);
  244. if(httpResponseMessage1 == null)
  245. {
  246. String[] httpRequestMessage1Header1Values1 = httpRequestMessage1Header1Value1.split(" ");
  247. String httpRequestMessage1Header1Value2 = httpRequestMessage1Header1Values1[0];
  248. if(!httpRequestMessage1Header1Value2.equalsIgnoreCase("GET"))
  249. {
  250. throw new HTTPSServerException("HTTPS_SERVER_WORKER/PROCESS: REQUEST/METHOD != \"GET\", REQUEST/METHOD == \"" + httpRequestMessage1Header1Value2 + "\"");
  251. }
  252. HTTPMessageHeader httpRequestMessage1Header2 = httpRequestMessage1.getHTTPMessageHeader("If-Range");
  253. if(httpRequestMessage1Header2 != null)
  254. {
  255. String httpRequestMessage1Header2Value1 = httpRequestMessage1Header2.getValue();
  256. throw new HTTPSServerException("HTTPS_SERVER_WORKER/PROCESS: REQUEST/IF_RANGE != \"\", REQUEST/IF_RANGE == \"" + httpRequestMessage1Header2Value1 + "\"");
  257. }
  258. HTTPMessageHeader httpRequestMessage1Header3 = httpRequestMessage1.getHTTPMessageHeader("Range");
  259. if(httpRequestMessage1Header3 != null)
  260. {
  261. String httpRequestMessage1Header3Value1 = httpRequestMessage1Header3.getValue();
  262. throw new HTTPSServerException("HTTPS_SERVER_WORKER/PROCESS: REQUEST/RANGE != \"\", REQUEST/RANGE == \"" + httpRequestMessage1Header3Value1 + "\"");
  263. }
  264. httpRequestMessage1Header1Values1[0] = "HEAD";
  265. httpRequestMessage1Header1Value1 = httpRequestMessage1Header1Values1[0];
  266. for(int i = 1; i < httpRequestMessage1Header1Values1.length; i = i + 1)
  267. {
  268. httpRequestMessage1Header1Value1 = httpRequestMessage1Header1Value1 + " " + httpRequestMessage1Header1Values1[i];
  269. }
  270. httpRequestMessage1Header1.setValue(httpRequestMessage1Header1Value1);
  271. HTTPSRequest httpsRequest1 = httpsRequests.createHTTPSRequest(httpRequestMessage1);
  272. httpsRequest1.open();
  273. try
  274. {
  275. httpResponseMessage1 = httpsRequest1.getHTTPResponseMessage();
  276. }
  277. catch(Exception e2)
  278. {
  279. throw e2;
  280. }
  281. finally
  282. {
  283. try
  284. {
  285. httpsRequest1.close();
  286. }
  287. catch(Exception e2)
  288. {
  289. }
  290. }
  291. HTTPMessageHeader httpResponseMessage1Header1 = httpResponseMessage1.getHTTPMessageHeader("Accept-Ranges");
  292. if(httpResponseMessage1Header1 == null)
  293. {
  294. throw new HTTPSServerException("HTTPS_SERVER_WORKER/PROCESS: RESPONSE/ACCEPT_RANGES != \"bytes\", RESPONSE/ACCEPT_RANGES == \"\"");
  295. }
  296. String httpResponseMessage1Header1Value1 = httpResponseMessage1Header1.getValue();
  297. if(!httpResponseMessage1Header1Value1.equalsIgnoreCase("bytes"))
  298. {
  299. throw new HTTPSServerException("HTTPS_SERVER_WORKER/PROCESS: RESPONSE/ACCEPT_RANGES != \"bytes\", RESPONSE/ACCEPT_RANGES == \"" + httpResponseMessage1Header1Value1 + "\"");
  300. }
  301. processHTTPResponseMessage(httpResponseMessage1);
  302. HTTPMessageHeader[] httpResponseMessage1Headers1 = httpResponseMessage1.getHTTPMessageHeaders();
  303. HTTPMessageHeader httpResponseMessage1Header2 = httpResponseMessage1Headers1[0];
  304. String httpResponseMessage1Header2Key1 = httpResponseMessage1Header2.getKey();
  305. String httpResponseMessage1Header2Value1 = httpResponseMessage1Header2.getValue();
  306. inputSSLSocketOutputStream.write((httpResponseMessage1Header2Value1 + "\r\n").getBytes());
  307. for(int i = 1; i < httpResponseMessage1Headers1.length; i = i + 1)
  308. {
  309. httpResponseMessage1Header2 = httpResponseMessage1Headers1[i];
  310. httpResponseMessage1Header2Key1 = httpResponseMessage1Header2.getKey();
  311. httpResponseMessage1Header2Value1 = httpResponseMessage1Header2.getValue();
  312. inputSSLSocketOutputStream.write((httpResponseMessage1Header2Key1 + ": " + httpResponseMessage1Header2Value1 + "\r\n").getBytes());
  313. }
  314. inputSSLSocketOutputStream.write(("\r\n").getBytes());
  315. HTTPMessageHeader httpResponseMessage1Header3 = httpResponseMessage1.getHTTPMessageHeader("Content-Length");
  316. int httpResponseMessage1Header3Value1 = 0;
  317. if(httpResponseMessage1Header3 != null)
  318. {
  319. try
  320. {
  321. httpResponseMessage1Header3Value1 = new Integer(httpResponseMessage1Header3.getValue());
  322. }
  323. catch(Exception e2)
  324. {
  325. }
  326. }
  327. httpRequestMessage1Header1Values1[0] = "GET";
  328. httpRequestMessage1Header1Value1 = httpRequestMessage1Header1Values1[0];
  329. for(int i = 1; i < httpRequestMessage1Header1Values1.length; i = i + 1)
  330. {
  331. httpRequestMessage1Header1Value1 = httpRequestMessage1Header1Value1 + " " + httpRequestMessage1Header1Values1[i];
  332. }
  333. httpRequestMessage1Header1.setValue(httpRequestMessage1Header1Value1);
  334. long i = 0;
  335. long j = 1048576;
  336. if(j > httpResponseMessage1Header3Value1 - 1)
  337. {
  338. j = httpResponseMessage1Header3Value1 - 1;
  339. }
  340. while(i < httpResponseMessage1Header3Value1 - 1)
  341. {
  342. HTTPMessageHeader httpRequestMessage1Header4 = httpRequestMessage1.getHTTPMessageHeader("Range");
  343. if(httpRequestMessage1Header4 != null)
  344. {
  345. httpRequestMessage1.removeHTTPMessageHeader(httpRequestMessage1Header4);
  346. }
  347. httpRequestMessage1.addHTTPMessageHeader(new HTTPMessageHeader("Range", "bytes=" + i + "-" + j));
  348. HTTPResponseMessage httpResponseMessage2 = null;
  349. int k = 1;
  350. while(k <= 5)
  351. {
  352. try
  353. {
  354. HTTPSRequest httpsRequest2 = httpsRequests.createHTTPSRequest(httpRequestMessage1);
  355. httpsRequest2.open();
  356. try
  357. {
  358. httpResponseMessage2 = httpsRequest2.getHTTPResponseMessage();
  359. HTTPMessageHeader httpResponseMessage2Header1 = httpResponseMessage2.getHTTPMessageHeader("Content-Range");
  360. if(httpResponseMessage2Header1 == null)
  361. {
  362. throw new HTTPSServerException("HTTPS_SERVER_WORKER/PROCESS: RESPONSE/CONTENT_RANGE != \"bytes " + i + "-" + j + "/" + httpResponseMessage1Header3Value1 + "\", RESPONSE/CONTENT_RANGE == \"\"");
  363. }
  364. String httpResponseMessage2Header1Value1 = httpResponseMessage2Header1.getValue();
  365. if(!httpResponseMessage2Header1Value1.equalsIgnoreCase("bytes " + i + "-" + j + "/" + httpResponseMessage1Header3Value1))
  366. {
  367. throw new HTTPSServerException("HTTPS_SERVER_WORKER/PROCESS: RESPONSE/CONTENT_RANGE != \"bytes " + i + "-" + j + "/" + httpResponseMessage1Header3Value1 + "\", RESPONSE/CONTENT_RANGE == \"" + httpResponseMessage2Header1Value1 + "\"");
  368. }
  369. httpResponseMessage2.read(inputSSLSocketOutputStream);
  370. }
  371. catch(Exception e2)
  372. {
  373. throw e;
  374. }
  375. finally
  376. {
  377. try
  378. {
  379. httpsRequest2.close();
  380. }
  381. catch(Exception e2)
  382. {
  383. }
  384. }
  385. k = 5;
  386. }
  387. catch(Exception e2)
  388. {
  389. logger.log(2, "HTTPS_SERVER_WORKER/PROCESS: EXCEPTION", e2);
  390. if(k == 5)
  391. {
  392. throw new HTTPSServerException("HTTPS_SERVER_WORKER/PROCESS");
  393. }
  394. }
  395. k = k + 1;
  396. }
  397. i = j + 1;
  398. j = j + 1 + 1048576;
  399. if(j > httpResponseMessage1Header3Value1 - 1)
  400. {
  401. j = httpResponseMessage1Header3Value1 - 1;
  402. }
  403. }
  404. }
  405. }
  406. }
  407. }