PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/jetty/modules/jetty/src/test/java/org/mortbay/jetty/HttpServerTestBase.java

https://github.com/derickbailey/qedserver
Java | 889 lines | 623 code | 137 blank | 129 comment | 31 complexity | a15541afde7f912cd70c9547c34cb08c MD5 | raw file
  1. //========================================================================
  2. //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
  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. //http://www.apache.org/licenses/LICENSE-2.0
  8. //Unless required by applicable law or agreed to in writing, software
  9. //distributed under the License is distributed on an "AS IS" BASIS,
  10. //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. //See the License for the specific language governing permissions and
  12. //limitations under the License.
  13. //========================================================================
  14. package org.mortbay.jetty;
  15. import java.io.BufferedReader;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.InputStreamReader;
  20. import java.io.LineNumberReader;
  21. import java.io.OutputStream;
  22. import java.io.PrintWriter;
  23. import java.io.Writer;
  24. import java.net.Socket;
  25. import java.net.URL;
  26. import java.util.Arrays;
  27. import java.util.Random;
  28. import javax.servlet.ServletException;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpServletResponse;
  31. import junit.framework.TestCase;
  32. import org.mortbay.jetty.handler.AbstractHandler;
  33. import org.mortbay.thread.BoundedThreadPool;
  34. import org.mortbay.util.IO;
  35. /**
  36. * HttpServer Tester.
  37. */
  38. public class HttpServerTestBase extends TestCase
  39. {
  40. private static boolean stress=Boolean.getBoolean("STRESS");
  41. // ~ Static fields/initializers
  42. // ---------------------------------------------
  43. /** The request. */
  44. private static final String REQUEST1_HEADER="POST / HTTP/1.0\n"+"Host: localhost\n"+"Content-Type: text/xml; charset=utf-8\n"+"Connection: close\n"+"Content-Length: ";
  45. private static final String REQUEST1_CONTENT="<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
  46. +"<nimbus xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"+" xsi:noNamespaceSchemaLocation=\"nimbus.xsd\" version=\"1.0\">\n"
  47. +"</nimbus>";
  48. private static final String REQUEST1=REQUEST1_HEADER+REQUEST1_CONTENT.getBytes().length+"\n\n"+REQUEST1_CONTENT;
  49. /** The expected response. */
  50. private static final String RESPONSE1="HTTP/1.1 200 OK\n"+"Connection: close\n"+"Server: Jetty(6.1.x)\n"+"\n"+"Hello world\n";
  51. // Break the request up into three pieces, splitting the header.
  52. private static final String FRAGMENT1=REQUEST1.substring(0,16);
  53. private static final String FRAGMENT2=REQUEST1.substring(16,34);
  54. private static final String FRAGMENT3=REQUEST1.substring(34);
  55. /** Second test request. */
  56. private static final String REQUEST2_HEADER="POST / HTTP/1.0\n"+"Host: localhost\n"+"Content-Type: text/xml\n"+"Content-Length: ";
  57. private static final String REQUEST2_CONTENT="<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
  58. +"<nimbus xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"+" xsi:noNamespaceSchemaLocation=\"nimbus.xsd\" version=\"1.0\">\n"
  59. +" <request requestId=\"1\">\n"+" <getJobDetails>\n"+" <jobId>73</jobId>\n"+" </getJobDetails>\n"+" </request>\n"
  60. +"</nimbus>";
  61. private static final String REQUEST2=REQUEST2_HEADER+REQUEST2_CONTENT.getBytes().length+"\n\n"+REQUEST2_CONTENT;
  62. private static final String RESPONSE2_CONTENT=
  63. "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"+
  64. "<nimbus xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"+
  65. " xsi:noNamespaceSchemaLocation=\"nimbus.xsd\" version=\"1.0\">\n"+
  66. " <request requestId=\"1\">\n"+
  67. " <getJobDetails>\n"+
  68. " <jobId>73</jobId>\n"+
  69. " </getJobDetails>\n"+
  70. " </request>\n"
  71. +"</nimbus>\n";
  72. private static final String RESPONSE2=
  73. "HTTP/1.1 200 OK\n"+
  74. "Content-Type: text/xml; charset=iso-8859-1\n"+
  75. "Content-Length: "+RESPONSE2_CONTENT.getBytes().length+"\n"+
  76. "Server: Jetty(6.1.x)\n"+
  77. "\n"+
  78. RESPONSE2_CONTENT;
  79. // Useful constants
  80. private static final long PAUSE=15L;
  81. private static final int LOOPS=stress?250:25;
  82. private static final String HOST="localhost";
  83. private Connector _connector;
  84. private int port=0;
  85. protected void tearDown() throws Exception
  86. {
  87. super.tearDown();
  88. Thread.sleep(100);
  89. }
  90. // ~ Methods
  91. // ----------------------------------------------------------------
  92. /**
  93. * Feed the server the entire request at once.
  94. *
  95. * @throws Exception
  96. * @throws InterruptedException
  97. */
  98. public void testRequest1_jetty() throws Exception, InterruptedException
  99. {
  100. Server server=startServer(new HelloWorldHandler());
  101. Socket client=new Socket(HOST,port);
  102. client.setSoTimeout(10000);
  103. OutputStream os=client.getOutputStream();
  104. os.write(REQUEST1.getBytes());
  105. os.flush();
  106. // Read the response.
  107. String response=readResponse(client);
  108. // Shut down
  109. client.close();
  110. server.stop();
  111. // Check the response
  112. assertEquals("response",RESPONSE1,response);
  113. }
  114. /* --------------------------------------------------------------- */
  115. public void testFragmentedChunk() throws Exception
  116. {
  117. Server server=startServer(new EchoHandler());
  118. Socket client=new Socket(HOST,port);
  119. client.setSoTimeout(10000);
  120. OutputStream os=client.getOutputStream();
  121. os.write(("GET /R2 HTTP/1.1\015\012"+"Host: localhost\015\012"+"Transfer-Encoding: chunked\015\012"+"Content-Type: text/plain\015\012"
  122. +"Connection: close\015\012"+"\015\012").getBytes());
  123. os.flush();
  124. Thread.sleep(PAUSE);
  125. os.write(("5\015\012").getBytes());
  126. os.flush();
  127. Thread.sleep(PAUSE);
  128. os.write(("ABCDE\015\012"+"0;\015\012\015\012").getBytes());
  129. os.flush();
  130. // Read the response.
  131. String response=readResponse(client);
  132. // Shut down
  133. client.close();
  134. server.stop();
  135. assertTrue(true); // nothing checked yet.
  136. }
  137. /**
  138. * Feed the server fragmentary headers and see how it copes with it.
  139. *
  140. * @throws Exception
  141. * @throws InterruptedException
  142. */
  143. public void testRequest1Fragments_jetty() throws Exception, InterruptedException
  144. {
  145. Server server=startServer(new HelloWorldHandler());
  146. String response;
  147. try
  148. {
  149. Socket client=new Socket(HOST,port);
  150. client.setSoTimeout(10000);
  151. OutputStream os=client.getOutputStream();
  152. // Write a fragment, flush, sleep, write the next fragment, etc.
  153. os.write(FRAGMENT1.getBytes());
  154. os.flush();
  155. Thread.sleep(PAUSE);
  156. os.write(FRAGMENT2.getBytes());
  157. os.flush();
  158. Thread.sleep(PAUSE);
  159. os.write(FRAGMENT3.getBytes());
  160. os.flush();
  161. // Read the response
  162. response=readResponse(client);
  163. // Shut down
  164. client.close();
  165. }
  166. finally
  167. {
  168. server.stop();
  169. }
  170. // Check the response
  171. assertEquals("response",RESPONSE1,response);
  172. }
  173. public void testRequest2_jetty() throws Exception
  174. {
  175. byte[] bytes=REQUEST2.getBytes();
  176. Server server=startServer(new EchoHandler());
  177. try
  178. {
  179. for (int i=0; i<LOOPS; i++)
  180. {
  181. Socket client=new Socket(HOST,port);
  182. client.setSoTimeout(10000);
  183. OutputStream os=client.getOutputStream();
  184. os.write(bytes);
  185. os.flush();
  186. // Read the response
  187. String response=readResponse(client);
  188. client.close();
  189. // Check the response
  190. assertEquals("response "+i,RESPONSE2,response);
  191. }
  192. }
  193. finally
  194. {
  195. // Shut down
  196. server.stop();
  197. }
  198. }
  199. /**
  200. * @throws Exception
  201. */
  202. public void testRequest2Fragments_jetty() throws Exception
  203. {
  204. Random random=new Random(System.currentTimeMillis());
  205. byte[] bytes=REQUEST2.getBytes();
  206. final int pointCount=2;
  207. Server server=startServer(new EchoHandler());
  208. try
  209. {
  210. for (int i=0; i<LOOPS; i++)
  211. {
  212. int[] points=new int[pointCount];
  213. StringBuffer message=new StringBuffer();
  214. message.append("iteration #"+(i+1));
  215. // Pick fragment points at random
  216. for (int j=0; j<points.length; ++j)
  217. {
  218. points[j]=random.nextInt(bytes.length);
  219. }
  220. // Sort the list
  221. Arrays.sort(points);
  222. Socket client=new Socket(HOST,port);
  223. client.setSoTimeout(10000);
  224. OutputStream os=client.getOutputStream();
  225. writeFragments(bytes,points,message,os);
  226. // Read the response
  227. String response=readResponse(client);
  228. // Close the client
  229. client.close();
  230. // Check the response
  231. assertEquals("response for "+i+" "+message.toString(),RESPONSE2,response);
  232. }
  233. }
  234. finally
  235. {
  236. // Shut down
  237. server.stop();
  238. }
  239. }
  240. public void testRequest2Iterate_jetty() throws Exception
  241. {
  242. byte[] bytes=REQUEST2.getBytes();
  243. Server server=startServer(new EchoHandler());
  244. try
  245. {
  246. for (int i=0; i<bytes.length; i+=3)
  247. {
  248. int[] points=new int[] { i };
  249. StringBuffer message=new StringBuffer();
  250. message.append("iteration #"+(i+1));
  251. // Sort the list
  252. Arrays.sort(points);
  253. Socket client=new Socket(HOST,port);
  254. client.setSoTimeout(10000);
  255. OutputStream os=client.getOutputStream();
  256. writeFragments(bytes,points,message,os);
  257. // Read the response
  258. String response=readResponse(client);
  259. // Close the client
  260. client.close();
  261. // Check the response
  262. assertEquals("response for "+i+" "+message.toString(),RESPONSE2,response);
  263. }
  264. }
  265. finally
  266. {
  267. // Shut down
  268. server.stop();
  269. }
  270. }
  271. /**
  272. * After several iterations, I generated some known bad fragment points.
  273. *
  274. * @throws Exception
  275. */
  276. public void testRequest2KnownBad_jetty() throws Exception
  277. {
  278. byte[] bytes=REQUEST2.getBytes();
  279. int[][] badPoints=new int[][]
  280. {
  281. { 70 }, // beginning here, drops last line of request
  282. { 71 }, // no response at all
  283. { 72 }, // again starts drops last line of request
  284. { 74 }, // again, no response at all
  285. };
  286. Server server=startServer(new EchoHandler());
  287. try
  288. {
  289. for (int i=0; i<badPoints.length; ++i)
  290. {
  291. Socket client=new Socket(HOST,port);
  292. client.setSoTimeout(10000);
  293. OutputStream os=client.getOutputStream();
  294. StringBuffer message=new StringBuffer();
  295. message.append("iteration #"+(i+1));
  296. writeFragments(bytes,badPoints[i],message,os);
  297. // Read the response
  298. String response=readResponse(client);
  299. // Close the client
  300. client.close();
  301. // Check the response
  302. // TODO - change to equals when code gets fixed
  303. assertNotSame("response for "+message.toString(),RESPONSE2,response);
  304. }
  305. }
  306. finally
  307. {
  308. // Shut down
  309. server.stop();
  310. }
  311. }
  312. /**
  313. * After several iterations, I generated some known bad fragment points.
  314. *
  315. * @throws Exception
  316. */
  317. public void testFlush() throws Exception
  318. {
  319. Server server=startServer(new DataHandler());
  320. try
  321. {
  322. String[] encoding = {"NONE","UTF-8","ISO-8859-1","ISO-8859-2"};
  323. for (int e =0; e<encoding.length;e++)
  324. {
  325. for (int b=1;b<=128;b=b==1?2:b==2?32:b==32?128:129)
  326. {
  327. for (int w=41;w<42;w+=4096)
  328. {
  329. for (int c=0;c<1;c++)
  330. {
  331. String test=encoding[e]+"x"+b+"x"+w+"x"+c;
  332. URL url=new URL("http://"+HOST+":"+port+"/?writes="+w+"&block="+b+ (e==0?"":("&encoding="+encoding[e]))+(c==0?"&chars=true":""));
  333. InputStream in = (InputStream)url.getContent();
  334. String response=IO.toString(in,e==0?null:encoding[e]);
  335. assertEquals(test,b*w,response.length());
  336. }
  337. }
  338. }
  339. }
  340. }
  341. finally
  342. {
  343. // Shut down
  344. server.stop();
  345. Thread.yield();
  346. }
  347. }
  348. /**
  349. * After several iterations, I generated some known bad fragment points.
  350. *
  351. * @throws Exception
  352. */
  353. public void testReadWriteBlocking() throws Exception
  354. {
  355. Server server=startServer(new DataHandler());
  356. try
  357. {
  358. long start=System.currentTimeMillis();
  359. Socket client=new Socket(HOST,port);
  360. client.setSoTimeout(10000);
  361. OutputStream os=client.getOutputStream();
  362. InputStream is=client.getInputStream();
  363. os.write((
  364. "GET /data?writes=1024&block=256 HTTP/1.1\r\n"+
  365. "host: "+HOST+":"+port+"\r\n"+
  366. "connection: close\r\n"+
  367. "content-type: unknown\r\n"+
  368. "content-length: 30\r\n"+
  369. "\r\n"
  370. ).getBytes());
  371. os.flush();
  372. Thread.sleep(200);
  373. os.write((
  374. "\r\n23456890"
  375. ).getBytes());
  376. os.flush();
  377. Thread.sleep(1000);
  378. os.write((
  379. "abcdefghij"
  380. ).getBytes());
  381. os.flush();
  382. Thread.sleep(1000);
  383. os.write((
  384. "0987654321\r\n"
  385. ).getBytes());
  386. os.flush();
  387. int total=0;
  388. int len=0;
  389. byte[] buf=new byte[1024*64];
  390. while(len>=0)
  391. {
  392. Thread.sleep(500);
  393. len=is.read(buf);
  394. if (len>0)
  395. total+=len;
  396. }
  397. assertTrue(total>(1024*256));
  398. assertTrue(30000L>(System.currentTimeMillis()-start));
  399. }
  400. finally
  401. {
  402. // Shut down
  403. server.stop();
  404. Thread.yield();
  405. }
  406. }
  407. /**
  408. * After several iterations, I generated some known bad fragment points.
  409. *
  410. * @throws Exception
  411. */
  412. public void testPipeline() throws Exception
  413. {
  414. Server server=startServer(new HelloWorldHandler());
  415. try
  416. {
  417. for (int pipeline=1;pipeline<32;pipeline++)
  418. {
  419. Socket client=new Socket(HOST,port);
  420. client.setSoTimeout(10000);
  421. OutputStream os=client.getOutputStream();
  422. String request="";
  423. for (int i=1;i<pipeline;i++)
  424. request+=
  425. "GET /data?writes=1&block=16&id="+i+" HTTP/1.1\r\n"+
  426. "host: "+HOST+":"+port+"\r\n"+
  427. "user-agent: testharness/1.0 (blah foo/bar)\r\n"+
  428. "accept-encoding: nothing\r\n"+
  429. "cookie: aaa=1234567890\r\n"+
  430. "\r\n";
  431. request+=
  432. "GET /data?writes=1&block=16 HTTP/1.1\r\n"+
  433. "host: "+HOST+":"+port+"\r\n"+
  434. "user-agent: testharness/1.0 (blah foo/bar)\r\n"+
  435. "accept-encoding: nothing\r\n"+
  436. "cookie: aaa=bbbbbb\r\n"+
  437. "Connection: close\r\n"+
  438. "\r\n";
  439. os.write(request.getBytes());
  440. os.flush();
  441. LineNumberReader in = new LineNumberReader(new InputStreamReader(client.getInputStream()));
  442. String line = in.readLine();
  443. int count=0;
  444. while (line!=null)
  445. {
  446. if ("HTTP/1.1 200 OK".equals(line))
  447. count++;
  448. line = in.readLine();
  449. }
  450. assertEquals(pipeline,count);
  451. }
  452. }
  453. finally
  454. {
  455. // Shut down
  456. server.stop();
  457. Thread.yield();
  458. }
  459. }
  460. public void testStoppable() throws Exception
  461. {
  462. Server server=startServer(null);
  463. server.setThreadPool(new BoundedThreadPool());
  464. server.start();
  465. Thread.sleep(1000);
  466. server.stop();
  467. }
  468. /**
  469. */
  470. public void testRecycledWriters() throws Exception
  471. {
  472. Server server=startServer(new EchoHandler());
  473. try
  474. {
  475. long start=System.currentTimeMillis();
  476. Socket client=new Socket(HOST,port);
  477. client.setSoTimeout(10000);
  478. OutputStream os=client.getOutputStream();
  479. InputStream is=client.getInputStream();
  480. os.write((
  481. "POST /echo?charset=utf-8 HTTP/1.1\r\n"+
  482. "host: "+HOST+":"+port+"\r\n"+
  483. "content-type: text/plain; charset=utf-8\r\n"+
  484. "content-length: 10\r\n"+
  485. "\r\n").getBytes("iso-8859-1"));
  486. os.write((
  487. "123456789\n"
  488. ).getBytes("utf-8"));
  489. os.write((
  490. "POST /echo?charset=utf-8 HTTP/1.1\r\n"+
  491. "host: "+HOST+":"+port+"\r\n"+
  492. "content-type: text/plain; charset=utf-8\r\n"+
  493. "content-length: 10\r\n"+
  494. "\r\n"
  495. ).getBytes("iso-8859-1"));
  496. os.write((
  497. "abcdefghi\n"
  498. ).getBytes("utf-8"));
  499. String content="Wibble";
  500. byte[] contentB=content.getBytes("utf-8");
  501. os.write((
  502. "POST /echo?charset=utf-16 HTTP/1.1\r\n"+
  503. "host: "+HOST+":"+port+"\r\n"+
  504. "content-type: text/plain; charset=utf-8\r\n"+
  505. "content-length: "+contentB.length+"\r\n"+
  506. "connection: close\r\n"+
  507. "\r\n"
  508. ).getBytes("iso-8859-1"));
  509. os.write(contentB);
  510. os.flush();
  511. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  512. IO.copy(is,bout);
  513. byte[] b=bout.toByteArray();
  514. String in = new String(b,0,b.length,"utf-8");
  515. assertTrue(in.indexOf("123456789")>=0);
  516. assertTrue(in.indexOf("abcdefghi")>=0);
  517. assertTrue(in.indexOf("Wibble")<0);
  518. in = new String(b,0,b.length,"utf-16");
  519. assertTrue(in.indexOf("123456789")<0);
  520. assertTrue(in.indexOf("abcdefghi")<0);
  521. assertTrue(in.indexOf("Wibble")>=0);
  522. }
  523. finally
  524. {
  525. // Shut down
  526. server.stop();
  527. Thread.yield();
  528. }
  529. }
  530. /**
  531. */
  532. public void testRecycledReaders() throws Exception
  533. {
  534. Server server=startServer(new EchoHandler());
  535. try
  536. {
  537. long start=System.currentTimeMillis();
  538. Socket client=new Socket(HOST,port);
  539. client.setSoTimeout(10000);
  540. OutputStream os=client.getOutputStream();
  541. InputStream is=client.getInputStream();
  542. os.write((
  543. "POST /echo?charset=utf-8 HTTP/1.1\r\n"+
  544. "host: "+HOST+":"+port+"\r\n"+
  545. "content-type: text/plain; charset=utf-8\r\n"+
  546. "content-length: 10\r\n"+
  547. "\r\n").getBytes("iso-8859-1"));
  548. os.write((
  549. "123456789\n"
  550. ).getBytes("utf-8"));
  551. os.write((
  552. "POST /echo?charset=utf-8 HTTP/1.1\r\n"+
  553. "host: "+HOST+":"+port+"\r\n"+
  554. "content-type: text/plain; charset=utf-8\r\n"+
  555. "content-length: 10\r\n"+
  556. "\r\n"
  557. ).getBytes("iso-8859-1"));
  558. os.write((
  559. "abcdefghi\n"
  560. ).getBytes("utf-8"));
  561. String content="Wibble";
  562. byte[] contentB=content.getBytes("utf-16");
  563. os.write((
  564. "POST /echo?charset=utf-8 HTTP/1.1\r\n"+
  565. "host: "+HOST+":"+port+"\r\n"+
  566. "content-type: text/plain; charset=utf-16\r\n"+
  567. "content-length: "+contentB.length+"\r\n"+
  568. "connection: close\r\n"+
  569. "\r\n"
  570. ).getBytes("iso-8859-1"));
  571. os.write(contentB);
  572. os.flush();
  573. String in = IO.toString(is);
  574. assertTrue(in.indexOf("123456789")>=0);
  575. assertTrue(in.indexOf("abcdefghi")>=0);
  576. assertTrue(in.indexOf("Wibble")>=0);
  577. }
  578. finally
  579. {
  580. // Shut down
  581. server.stop();
  582. Thread.yield();
  583. }
  584. }
  585. /**
  586. * Read entire response from the client. Close the output.
  587. *
  588. * @param client
  589. * Open client socket.
  590. *
  591. * @return The response string.
  592. *
  593. * @throws IOException
  594. */
  595. private static String readResponse(Socket client) throws IOException
  596. {
  597. BufferedReader br=null;
  598. try
  599. {
  600. br=new BufferedReader(new InputStreamReader(client.getInputStream()));
  601. StringBuffer sb=new StringBuffer();
  602. String line;
  603. while ((line=br.readLine())!=null)
  604. {
  605. sb.append(line);
  606. sb.append('\n');
  607. }
  608. return sb.toString();
  609. }
  610. finally
  611. {
  612. if (br!=null)
  613. {
  614. br.close();
  615. }
  616. }
  617. }
  618. protected HttpServerTestBase(Connector connector)
  619. {
  620. _connector=connector;
  621. }
  622. /**
  623. * Create the server.
  624. *
  625. * @param handler
  626. *
  627. * @return Newly created server, ready to start.
  628. *
  629. * @throws Exception
  630. */
  631. private Server startServer(Handler handler) throws Exception
  632. {
  633. Server server=new Server();
  634. _connector.setPort(0);
  635. server.setConnectors(new Connector[]
  636. { _connector });
  637. server.setHandler(handler);
  638. server.start();
  639. port=_connector.getLocalPort();
  640. return server;
  641. }
  642. private void writeFragments(byte[] bytes, int[] points, StringBuffer message, OutputStream os) throws IOException, InterruptedException
  643. {
  644. int last=0;
  645. // Write out the fragments
  646. for (int j=0; j<points.length; ++j)
  647. {
  648. int point=points[j];
  649. os.write(bytes,last,point-last);
  650. last=point;
  651. os.flush();
  652. Thread.sleep(PAUSE);
  653. // Update the log message
  654. message.append(" point #"+(j+1)+": "+point);
  655. }
  656. // Write the last fragment
  657. os.write(bytes,last,bytes.length-last);
  658. os.flush();
  659. Thread.sleep(PAUSE);
  660. }
  661. // ~ Inner Classes
  662. // ----------------------------------------------------------
  663. private static class EchoHandler extends AbstractHandler
  664. {
  665. // ~ Methods
  666. // ------------------------------------------------------------
  667. public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
  668. {
  669. Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest();
  670. base_request.setHandled(true);
  671. if (request.getContentType()!=null)
  672. response.setContentType(request.getContentType());
  673. if (request.getParameter("charset")!=null)
  674. response.setCharacterEncoding(request.getParameter("charset"));
  675. else if (request.getCharacterEncoding()!=null)
  676. response.setCharacterEncoding(request.getCharacterEncoding());
  677. PrintWriter writer=response.getWriter();
  678. BufferedReader reader=request.getReader();
  679. int count=0;
  680. String line;
  681. while ((line=reader.readLine())!=null)
  682. {
  683. writer.print(line);
  684. writer.print("\n");
  685. count+=line.length();
  686. }
  687. if (count==0)
  688. throw new IllegalStateException("no input recieved");
  689. // just to be difficult
  690. reader.close();
  691. writer.close();
  692. if (reader.read()>=0)
  693. throw new IllegalStateException("Not closed");
  694. }
  695. }
  696. // ----------------------------------------------------------
  697. private static class HelloWorldHandler extends AbstractHandler
  698. {
  699. // ------------------------------------------------------------
  700. public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
  701. {
  702. Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest();
  703. base_request.setHandled(true);
  704. response.setStatus(200);
  705. response.getOutputStream().print("Hello world\r\n");
  706. }
  707. }
  708. // ----------------------------------------------------------
  709. private static class DataHandler extends AbstractHandler
  710. {
  711. // ------------------------------------------------------------
  712. public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
  713. {
  714. Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest();
  715. base_request.setHandled(true);
  716. response.setStatus(200);
  717. InputStream in = request.getInputStream();
  718. String input=IO.toString(in);
  719. String tmp = request.getParameter("writes");
  720. int writes=Integer.parseInt(tmp==null?"10":tmp);
  721. tmp = request.getParameter("block");
  722. int block=Integer.parseInt(tmp==null?"10":tmp);
  723. String encoding=request.getParameter("encoding");
  724. String chars=request.getParameter("chars");
  725. String chunk = (input+"\u0a870123456789A\u0a87CDEFGHIJKLMNOPQRSTUVWXYZ\u0250bcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
  726. .substring(0,block);
  727. response.setContentType("text/plain");
  728. if (encoding==null)
  729. {
  730. byte[] bytes=chunk.getBytes("ISO-8859-1");
  731. OutputStream out=response.getOutputStream();
  732. for (int i=0;i<writes;i++)
  733. out.write(bytes);
  734. }
  735. else if ("true".equals(chars))
  736. {
  737. response.setCharacterEncoding(encoding);
  738. Writer out=response.getWriter();
  739. char[] c=chunk.toCharArray();
  740. for (int i=0;i<writes;i++)
  741. out.write(c);
  742. }
  743. else
  744. {
  745. response.setCharacterEncoding(encoding);
  746. Writer out=response.getWriter();
  747. for (int i=0;i<writes;i++)
  748. out.write(chunk);
  749. }
  750. }
  751. }
  752. }