PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/JAVA/src/APJP/HTTPS/HTTPSProxyServerWorker.java

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