Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/ajp/test/TestAjp13.java


1   /*
2    *  Copyright 1999-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package org.apache.ajp.test;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.net.ServerSocket;
23  import java.net.Socket;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  
29  import org.apache.ajp.Ajp13;
30  import org.apache.ajp.Ajp13Packet;
31  import org.apache.ajp.RequestHandler;
32  import org.apache.tomcat.util.http.BaseRequest;
33  import org.apache.tomcat.util.http.MimeHeaders;
34  
35  public class TestAjp13 extends TestCase {
36  
37      private static org.apache.commons.logging.Log log=
38          org.apache.commons.logging.LogFactory.getLog( TestAjp13.class );
39  
40      Ajp13Server server = null;
41  
42      public TestAjp13(String name) {
43          super(name);
44      }
45  
46      public static Test suite() {
47          return new TestSuite(TestAjp13.class);
48      }
49  
50      protected void setUp() {
51          println("setup...");
52  
53          server = new Ajp13Server();
54          server.start();
55      }
56  
57      protected void tearDown() {
58          println("tear down...");
59  
60          server.shutdown();
61      }
62  
63      public void test1() throws Exception {
64          println("running test1");
65  
66          Socket s = new Socket("localhost", 8009);
67  
68          Ajp13Packet p = new Ajp13Packet(Ajp13.MAX_PACKET_SIZE);
69          p.appendInt(0x1234);
70          p.appendInt(0);
71          p.setByteOff(4);
72          p.appendByte(RequestHandler.JK_AJP13_FORWARD_REQUEST);
73          p.appendByte((byte)2);
74          p.appendString("http");
75          p.appendString("/test_uri");
76          p.appendString("remote_addr");
77          p.appendString("remote_host");
78          p.appendString("server_name");
79          p.appendInt(80);
80          p.appendBool(false);
81          p.appendInt(3);
82          p.appendString("my header");
83          p.appendString("my header value");
84          p.appendInt((0xA0 << 8) + RequestHandler.SC_REQ_AUTHORIZATION);
85          p.appendString("some auth string");
86          p.appendInt((0xA0 << 8) + RequestHandler.SC_REQ_USER_AGENT);
87          p.appendString("TestAjp13 User Agent");
88          p.appendByte(RequestHandler.SC_A_ARE_DONE);
89  
90          int len = p.getByteOff() - 4;
91          p.setByteOff(2);
92          p.appendInt(len);
93  
94          OutputStream os = s.getOutputStream();
95          os.write(p.getBuff(), 0, len + 4);
96  
97          InputStream is = s.getInputStream();
98  
99          println("decoding response...");
100         
101         boolean done = false;
102         while (!done) {
103             int b1, b2;
104             // read a packet
105 
106             // first 2 bytes should be AB
107             b1 = is.read();
108             assertTrue("byte 1 was " + (char)b1, b1 == (int)'A');
109             b2 = is.read();
110             assertTrue("byte 2 was " + (char)b2, b2 == (int)'B');
111 
112             println("b1 = " + (char)b1 + "; b2 = " + (char)b2);
113             
114             // next is length
115             b1 = is.read();
116             b1 &= 0xFF;
117             b2 = is.read();
118             b2 &= 0xFF;
119             
120             int l = (b1 << 8) + b2;
121 
122             println("length = " + l);
123 
124             // now get data
125             byte[] buf = new byte[l];
126             int n = 0;
127             int off = 0;
128             int total = 0;
129             while ((n = is.read(buf, off, l - off)) != -1 && (l - off != 0)) {
130                 total += n;
131                 off += n;
132             }
133 
134             println("read " + total);
135 
136             assertTrue("total read was " + total +
137                        ", should have been " + l,
138                        total == l);
139 
140             
141 
142             int code = (int)buf[0];
143 
144             switch (code) {
145             case 3:
146                 println("AJP13_SEND_BODY_CHUNK ");
147                 break;
148             case 4:
149                 println("AJP13_SEND_HEADERS ");
150                 break;
151             case 5:
152                 println("AJP13_END_RESPONSE ");
153                 done = true;
154                 break;
155             case 6:
156                 println("AJP13_GET_BODY_CHUNK ");
157                 break;
158             default:
159                 assertTrue("invalid prefix code:  " + code, false);
160                 break;
161             }
162         }
163 
164         println("shutting down socket...");
165         s.shutdownOutput();
166         s.shutdownInput();
167         s.close();
168 
169         println("done test1...");
170     }
171 
172     protected static void println(String msg) {
173         if (log.isDebugEnabled())
174             log.debug("[TestAjp13] " + msg);
175     }
176 
177     public static void main(String[] args) throws Exception {
178     }
179 }
180 
181 
182 class Ajp13Server extends Thread {
183 
184     boolean shutdown = false;
185 
186     void shutdown() {
187         this.shutdown = true;
188         this.interrupt();
189     }
190 
191     public void run() {
192         try {
193             ServerSocket server = new ServerSocket(8009);
194             TestAjp13.println("Ajp13Server running...");
195             Socket socket = server.accept();
196             Ajp13 ajp13 = new Ajp13();
197             MimeHeaders headers = new MimeHeaders();
198             BaseRequest request = new BaseRequest();
199             ajp13.setSocket(socket);
200 
201             boolean moreRequests = true;
202             while (moreRequests && !shutdown) {
203             
204                 int status = 0;
205                 try {
206                     status = ajp13.receiveNextRequest(request);
207                 } catch (IOException e) {
208                     if (shutdown) {
209                         TestAjp13.println("Ajp13Server told to shutdown");
210                         break;
211                     }
212                     TestAjp13.println("process: ajp13.receiveNextRequest -> " + e);
213                 }
214             
215                 if( status==-2) {
216                     // special case - shutdown
217                     // XXX need better communication, refactor it
218                     //                  if( !doShutdown(socket.getLocalAddress(),
219                     //                                  socket.getInetAddress())) {
220                     //                      moreRequests = false;
221                     //                      continue;
222                     //                  }
223                     break;
224                 }
225             
226               // Special low level request allready handled (ie: PING/PONG)
227               if( status == 999 )
228               {
229           request.recycle();
230                 continue;
231               }
232               
233                 if( status != 200 )
234                     break;
235 
236                 TestAjp13.println(request.toString());
237 
238                 String message =
239                     "<html><body><pre>" +
240                     "hello from ajp13:  " +
241                     System.getProperty("line.separator") +
242                     request.toString() +
243                     "</pre></body></html>";
244 
245                 headers.addValue("content-type").setString( "text/html");
246                 headers.addValue("content-length").setInt(message.length());
247                 headers.addValue("my-header").setString( "my value");
248                 ajp13.sendHeaders(200, headers);
249 
250                 byte[] b = message.getBytes();
251                 ajp13.doWrite(b, 0, b.length);
252 
253                 ajp13.finish();
254 
255                 request.recycle();
256                 headers.recycle();
257             }
258 
259             try {
260                 ajp13.close();
261             } catch (IOException e) {
262                 TestAjp13.println("process: ajp13.close ->" + e);
263             }
264 
265             try {
266                 socket.close();
267             } catch (IOException e) {
268                 TestAjp13.println("process: socket.close ->" + e);
269             }
270             socket = null;
271 
272             TestAjp13.println("process:  done");
273 
274         } catch (Exception e) {
275             e.printStackTrace();
276             throw new RuntimeException(e.toString());
277         }
278     }
279 }
280