PageRenderTime 27ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/JAVA_APPENGINE/src/APJP/HTTP11/HTTPResponseMessage.java

http://apjp.googlecode.com/
Java | 228 lines | 166 code | 44 blank | 18 comment | 35 complexity | 913a155a288746968f524e6593f5f0e1 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.HTTP11;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import java.util.Arrays;
  12. public class HTTPResponseMessage extends HTTPMessage
  13. {
  14. protected HTTPRequestMessage httpRequestMessage;
  15. protected InputStream inputStream;
  16. public HTTPResponseMessage(HTTPRequestMessage httpRequestMessage, InputStream inputStream)
  17. {
  18. super();
  19. this.httpRequestMessage = httpRequestMessage;
  20. this.inputStream = inputStream;
  21. }
  22. public void read() throws HTTPMessageException
  23. {
  24. try
  25. {
  26. byte[] byteArray1;
  27. byte[] byteArray2;
  28. int byteArray2Length;
  29. byteArray1 = new byte[1];
  30. byteArray2 = new byte[1024];
  31. byteArray2Length = 0;
  32. while((inputStream.read(byteArray1)) > 0)
  33. {
  34. if(byteArray2Length == byteArray2.length)
  35. {
  36. byteArray2 = Arrays.copyOf(byteArray2, byteArray2Length + byteArray2Length);
  37. }
  38. byteArray2[byteArray2Length] = byteArray1[0];
  39. byteArray2Length = byteArray2Length + 1;
  40. if(byteArray2Length >= 4)
  41. {
  42. if
  43. (
  44. byteArray2[byteArray2Length - 4] == '\r' &&
  45. byteArray2[byteArray2Length - 3] == '\n' &&
  46. byteArray2[byteArray2Length - 2] == '\r' &&
  47. byteArray2[byteArray2Length - 1] == '\n'
  48. )
  49. {
  50. break;
  51. }
  52. }
  53. }
  54. String httpResponseMessageHeader1Value1 = new String(byteArray2, 0, byteArray2Length);
  55. String[] httpResponseMessageHeader1Values1 = httpResponseMessageHeader1Value1.split("\r\n");
  56. String httpResponseMessageHeader1Value2 = httpResponseMessageHeader1Values1[0];
  57. addHTTPMessageHeader(new HTTPMessageHeader("", httpResponseMessageHeader1Value2));
  58. for(int i = 1; i < httpResponseMessageHeader1Values1.length; i = i + 1)
  59. {
  60. String httpResponseMessageHeader1Value3 = httpResponseMessageHeader1Values1[i];
  61. String[] httpResponseMessageHeader1Values3 = httpResponseMessageHeader1Value3.split(": ");
  62. String httpResponseMessageHeader1Value4 = httpResponseMessageHeader1Values3[0];
  63. String httpResponseMessageHeader1Value5 = "";
  64. if(httpResponseMessageHeader1Values3.length == 2)
  65. {
  66. httpResponseMessageHeader1Value5 = httpResponseMessageHeader1Values3[1];
  67. }
  68. addHTTPMessageHeader(new HTTPMessageHeader(httpResponseMessageHeader1Value4, httpResponseMessageHeader1Value5));
  69. }
  70. }
  71. catch(Exception e)
  72. {
  73. throw new HTTPMessageException("HTTP_RESPONSE_MESSAGE/READ", e);
  74. }
  75. }
  76. public void read(OutputStream outputStream) throws HTTPMessageException
  77. {
  78. try
  79. {
  80. HTTPMessageHeader httpRequestMessageHeader1 = httpRequestMessage.getHTTPMessageHeader("");
  81. String httpRequestMessageHeader1Value1 = httpRequestMessageHeader1.getValue();
  82. String[] httpRequestMessageHeader1Values1 = httpRequestMessageHeader1Value1.split(" ");
  83. String httpRequestMessageHeader1Value2 = httpRequestMessageHeader1Values1[0];
  84. // 1.1
  85. if(httpRequestMessageHeader1Value2.equalsIgnoreCase("OPTIONS"))
  86. {
  87. }
  88. // 1.0
  89. else if(httpRequestMessageHeader1Value2.equalsIgnoreCase("GET"))
  90. {
  91. }
  92. // 1.0
  93. else if(httpRequestMessageHeader1Value2.equalsIgnoreCase("HEAD"))
  94. {
  95. return;
  96. }
  97. // 1.0
  98. else if(httpRequestMessageHeader1Value2.equalsIgnoreCase("POST"))
  99. {
  100. }
  101. // 1.1
  102. else if(httpRequestMessageHeader1Value2.equalsIgnoreCase("PUT"))
  103. {
  104. }
  105. // 1.1
  106. else if(httpRequestMessageHeader1Value2.equalsIgnoreCase("DELETE"))
  107. {
  108. }
  109. // 1.1
  110. else if(httpRequestMessageHeader1Value2.equalsIgnoreCase("TRACE"))
  111. {
  112. }
  113. // 1.1
  114. else if(httpRequestMessageHeader1Value2.equalsIgnoreCase("CONNECT"))
  115. {
  116. }
  117. HTTPMessageHeader httpResponseMessageHeader1 = getHTTPMessageHeader("Content-Length");
  118. int httpResponseMessageHeader1Value1 = 0;
  119. if(httpResponseMessageHeader1 != null)
  120. {
  121. try
  122. {
  123. httpResponseMessageHeader1Value1 = new Integer(httpResponseMessageHeader1.getValue());
  124. }
  125. catch(Exception e)
  126. {
  127. }
  128. }
  129. else
  130. {
  131. httpResponseMessageHeader1Value1 = -1;
  132. }
  133. byte[] byteArray1;
  134. int byteArray1Length1;
  135. int byteArray1Length2;
  136. int byteArray1Length3;
  137. byteArray1 = new byte[5120];
  138. byteArray1Length1 = 0;
  139. byteArray1Length2 = 0;
  140. byteArray1Length3 = httpResponseMessageHeader1Value1;
  141. if(byteArray1Length3 > 0)
  142. {
  143. if(byteArray1Length3 >= byteArray1.length)
  144. {
  145. byteArray1Length2 = byteArray1.length;
  146. }
  147. else
  148. {
  149. byteArray1Length2 = byteArray1Length3;
  150. }
  151. while(byteArray1Length2 > 0 && (byteArray1Length1 = inputStream.read(byteArray1, 0, byteArray1Length2)) > 0)
  152. {
  153. outputStream.write(byteArray1, 0, byteArray1Length1);
  154. byteArray1Length3 = byteArray1Length3 - byteArray1Length1;
  155. if(byteArray1Length3 >= byteArray1.length)
  156. {
  157. byteArray1Length2 = byteArray1.length;
  158. }
  159. else
  160. {
  161. byteArray1Length2 = byteArray1Length3;
  162. }
  163. }
  164. }
  165. else
  166. {
  167. if(byteArray1Length3 < 0)
  168. {
  169. while((byteArray1Length1 = inputStream.read(byteArray1)) > 0)
  170. {
  171. outputStream.write(byteArray1, 0, byteArray1Length1);
  172. }
  173. }
  174. }
  175. }
  176. catch(Exception e)
  177. {
  178. throw new HTTPMessageException("HTTP_RESPONSE_MESSAGE/READ", e);
  179. }
  180. }
  181. }