PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://apjp.googlecode.com/
Java | 270 lines | 210 code | 48 blank | 12 comment | 9 complexity | ab7810bb55f7bf1c1688b1054eac34d2 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 java.net.InetSocketAddress;
  12. import java.net.Socket;
  13. import APJP.ANDROID.APJP;
  14. import APJP.ANDROID.Logger;
  15. import APJP.ANDROID.HTTP11.HTTPMessageHeader;
  16. import APJP.ANDROID.HTTP11.HTTPRequestMessage;
  17. public class HTTPSProxyServerWorker implements Runnable
  18. {
  19. private static Logger logger;
  20. private Thread thread;
  21. private HTTPSProxyServer httpsProxyServer;
  22. private HTTPSServer httpsServer;
  23. private Socket inputSocket;
  24. static
  25. {
  26. logger = Logger.getLogger(APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_LOGGER_ID);
  27. }
  28. protected HTTPSProxyServerWorker(HTTPSProxyServer httpsProxyServer, Socket inputSocket)
  29. {
  30. this.httpsProxyServer = httpsProxyServer;
  31. httpsServer = httpsProxyServer.getHTTPSServer();;
  32. this.inputSocket = inputSocket;
  33. }
  34. protected synchronized void start() throws HTTPSProxyServerException
  35. {
  36. logger.log(2, "HTTPS_PROXY_SERVER_WORKER/START");
  37. try
  38. {
  39. startHTTPSProxyServerWorker();
  40. }
  41. catch(Exception e)
  42. {
  43. logger.log(2, "HTTPS_PROXY_SERVER_WORKER/START: EXCEPTION", e);
  44. throw new HTTPSProxyServerException("HTTPS_PROXY_SERVER_WORKER/START", e);
  45. }
  46. }
  47. protected synchronized void stop() throws HTTPSProxyServerException
  48. {
  49. logger.log(2, "HTTPS_PROXY_SERVER_WORKER/STOP");
  50. try
  51. {
  52. stopHTTPSProxyServerWorker();
  53. }
  54. catch(Exception e)
  55. {
  56. logger.log(2, "HTTPS_PROXY_SERVER_WORKER/STOP: EXCEPTION", e);
  57. throw new HTTPSProxyServerException("HTTPS_PROXY_SERVER_WORKER/STOP", e);
  58. }
  59. }
  60. protected synchronized void startHTTPSProxyServerWorker() throws HTTPSProxyServerException
  61. {
  62. logger.log(2, "HTTPS_PROXY_SERVER_WORKER/START_HTTPS_PROXY_SERVER_WORKER");
  63. try
  64. {
  65. thread = new Thread(this);
  66. thread.start();
  67. }
  68. catch(Exception e)
  69. {
  70. logger.log(2, "HTTPS_PROXY_SERVER_WORKER/START_HTTPS_PROXY_SERVER_WORKER: EXCEPTION", e);
  71. throw new HTTPSProxyServerException("HTTPS_PROXY_SERVER_WORKER/START_HTTPS_PROXY_SERVER_WORKER", e);
  72. }
  73. }
  74. protected synchronized void stopHTTPSProxyServerWorker() throws HTTPSProxyServerException
  75. {
  76. logger.log(2, "HTTPS_PROXY_SERVER_WORKER/STOP_HTTPS_PROXY_SERVER_WORKER");
  77. try
  78. {
  79. thread = null;
  80. try
  81. {
  82. inputSocket.close();
  83. }
  84. catch(Exception e)
  85. {
  86. }
  87. }
  88. catch(Exception e)
  89. {
  90. logger.log(2, "HTTPS_PROXY_SERVER_WORKER/STOP_HTTPS_PROXY_SERVER_WORKER: EXCEPTION", e);
  91. throw new HTTPSProxyServerException("HTTPS_PROXY_SERVER_WORKER/STOP_HTTPS_PROXY_SERVER_WORKER", e);
  92. }
  93. }
  94. public void run()
  95. {
  96. try
  97. {
  98. final Socket inputSocket = this.inputSocket;
  99. final InputStream inputSocketInputStream = inputSocket.getInputStream();
  100. final OutputStream inputSocketOutputStream = inputSocket.getOutputStream();
  101. HTTPRequestMessage httpRequestMessage1 = new HTTPRequestMessage(inputSocketInputStream);
  102. httpRequestMessage1.read();
  103. HTTPMessageHeader httpRequestMessage1Header1 = httpRequestMessage1.getHTTPMessageHeader("");
  104. if(httpRequestMessage1Header1 != null)
  105. {
  106. // CONNECT 127.0.0.1:443 HTTP/1.1
  107. String httpRequestMessage1Header1Value1 = httpRequestMessage1Header1.getValue();
  108. String[] httpRequestMessage1Header1Values1 = httpRequestMessage1Header1Value1.split(" ");
  109. // CONNECT
  110. String httpRequestMessage1Header1Value2 = httpRequestMessage1Header1Values1[0];
  111. if(httpRequestMessage1Header1Value2.equalsIgnoreCase("CONNECT"))
  112. {
  113. final Socket outputSocket = new Socket();
  114. try
  115. {
  116. try
  117. {
  118. outputSocket.connect(new InetSocketAddress(httpsServer.getLocalAddress(), httpsServer.getLocalPort()));
  119. inputSocketOutputStream.write(("HTTP/1.0 200 OK\r\n").getBytes());
  120. inputSocketOutputStream.write(("Connection: keep-alive\r\n").getBytes());
  121. inputSocketOutputStream.write(("\r\n").getBytes());
  122. }
  123. catch(Exception e)
  124. {
  125. inputSocketOutputStream.write(("HTTP/1.0 500 Internal Server Error\r\n").getBytes());
  126. inputSocketOutputStream.write(("Connection: close\r\n").getBytes());
  127. inputSocketOutputStream.write(("\r\n").getBytes());
  128. throw e;
  129. }
  130. final InputStream outputSocketInputStream = outputSocket.getInputStream();
  131. final OutputStream outputSocketOutputStream = outputSocket.getOutputStream();
  132. Thread thread1 = new Thread()
  133. {
  134. public void run()
  135. {
  136. try
  137. {
  138. byte[] byteArray1;
  139. int byteArray1Length;
  140. byteArray1 = new byte[5120];
  141. byteArray1Length = 0;
  142. while((byteArray1Length = inputSocketInputStream.read(byteArray1)) != -1)
  143. {
  144. outputSocketOutputStream.write(byteArray1, 0, byteArray1Length);
  145. }
  146. }
  147. catch(Exception e)
  148. {
  149. }
  150. }
  151. };
  152. thread1.start();
  153. Thread thread2 = new Thread()
  154. {
  155. public void run()
  156. {
  157. try
  158. {
  159. byte[] byteArray1;
  160. int byteArray1Length;
  161. byteArray1 = new byte[5120];
  162. byteArray1Length = 0;
  163. while((byteArray1Length = outputSocketInputStream.read(byteArray1)) != -1)
  164. {
  165. inputSocketOutputStream.write(byteArray1, 0, byteArray1Length);
  166. }
  167. inputSocket.shutdownInput();
  168. inputSocket.shutdownOutput();
  169. outputSocket.shutdownInput();
  170. outputSocket.shutdownOutput();
  171. }
  172. catch(Exception e)
  173. {
  174. }
  175. }
  176. };
  177. thread2.start();
  178. thread1.join();
  179. thread2.join();
  180. }
  181. catch(Exception e)
  182. {
  183. throw e;
  184. }
  185. finally
  186. {
  187. try
  188. {
  189. outputSocket.close();
  190. }
  191. catch(Exception e)
  192. {
  193. }
  194. }
  195. }
  196. }
  197. }
  198. catch(Exception e)
  199. {
  200. if(thread != null)
  201. {
  202. logger.log(2, "HTTPS_PROXY_SERVER_WORKER: EXCEPTION", e);
  203. }
  204. }
  205. finally
  206. {
  207. if(thread != null)
  208. {
  209. try
  210. {
  211. httpsProxyServer.stopHTTPSProxyServerWorker(this);
  212. }
  213. catch(Exception e)
  214. {
  215. }
  216. }
  217. }
  218. }
  219. }