home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 November / PCONLINE_11_99.ISO / filesbbs / OS2 / APCHSSL2.ZIP / OS2HTTPD / servlets / SnoopServlet.java < prev    next >
Encoding:
Java Source  |  1998-12-12  |  5.5 KB  |  178 lines

  1. /*
  2.  * 1.19 02/25/98
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. import java.io.*;
  23. import java.util.*;
  24.  
  25. import javax.servlet.*;
  26. import javax.servlet.http.*;
  27.  
  28.  
  29. /**
  30.  * Snoop servlet. This servlet simply echos back the request line and
  31.  * headers that were sent by the client, plus any HTTPS information
  32.  * which is accessible.
  33.  *
  34.  * @version     1.19 98/02/25
  35.  * @author     David Connelly
  36.  */
  37. public
  38. class SnoopServlet extends HttpServlet {
  39.  
  40.  
  41.     public void doPost (HttpServletRequest req, HttpServletResponse res)
  42.     throws ServletException, IOException
  43.     {
  44.         //value chosen to limit denial of service
  45.         if (req.getContentLength() > 8*1024) {  
  46.         res.setContentType("text/html");
  47.             ServletOutputStream out = res.getOutputStream();
  48.  
  49.         out.println("<html><head><title>Too big</title></head>");
  50.         out.println("<body><h1>Error - content length >8k not ");
  51.         out.println("</h1></body></html>");
  52.         } else {
  53.         doGet(req, res);
  54.         }
  55.     }
  56.     
  57.     public void doGet (HttpServletRequest req, HttpServletResponse res)
  58.     throws ServletException, IOException
  59.     {
  60.     res.setContentType("text/html");
  61.     ServletOutputStream out = res.getOutputStream();
  62.  
  63.     out.println("<html>");
  64.     out.println("<head><title>Snoop Servlet</title></head>");
  65.     out.println("<body>");
  66.  
  67.     out.println("<h1>Requested URL:</h1>");
  68.     out.println("<pre>");
  69.     out.println (HttpUtils.getRequestURL (req).toString ());
  70.     out.println("</pre>");
  71.  
  72.     Enumeration enum = getServletConfig().getInitParameterNames();
  73.         if (enum != null) {
  74.             boolean first = true;
  75.         while (enum.hasMoreElements()) {
  76.         if (first) {
  77.                 out.println("<h1>Init Parameters</h1>");
  78.                 out.println("<pre>");
  79.             first = false;
  80.                 }
  81.         String param = (String) enum.nextElement();
  82.                 out.println(" "+param+": "+getInitParameter(param));
  83.         }
  84.         out.println("</pre>");
  85.         }
  86.  
  87.     out.println("<h1>Request information:</h1>");
  88.     out.println("<pre>");
  89.     print(out, "Request method", req.getMethod());
  90.     print(out, "Request URI", req.getRequestURI());
  91.     print(out, "Request protocol", req.getProtocol());
  92.     print(out, "Servlet path", req.getServletPath());
  93.     print(out, "Path info", req.getPathInfo());
  94.     print(out, "Path translated", req.getPathTranslated());
  95.     print(out, "Query string", req.getQueryString());
  96.     print(out, "Content length", req.getContentLength());
  97.     print(out, "Content type", req.getContentType());
  98.     print(out, "Server name", req.getServerName());
  99.     print(out, "Server port", req.getServerPort());
  100.     print(out, "Remote user", req.getRemoteUser());
  101.     print(out, "Remote address", req.getRemoteAddr());
  102.     print(out, "Remote host", req.getRemoteHost());
  103.     print(out, "Authorization scheme", req.getAuthType());
  104.     out.println("</pre>");
  105.     
  106.     Enumeration e = req.getHeaderNames();
  107.     if (e.hasMoreElements()) {
  108.         out.println("<h1>Request headers:</h1>");
  109.         out.println("<pre>");
  110.         while (e.hasMoreElements()) {
  111.         String name = (String)e.nextElement();
  112.         out.println(" " + name + ": " + req.getHeader(name));
  113.         }
  114.         out.println("</pre>");
  115.     }
  116.  
  117.     e = req.getParameterNames();
  118.     if (e.hasMoreElements()) {
  119.         out.println("<h1>Servlet parameters (Single Value style):</h1>");
  120.         out.println("<pre>");
  121.         while (e.hasMoreElements()) {
  122.         String name = (String)e.nextElement();
  123.         out.println(" " + name + " = " + req.getParameter(name));
  124.         }
  125.         out.println("</pre>");
  126.     }
  127.  
  128.     e = req.getParameterNames();
  129.     if (e.hasMoreElements()) {
  130.         out.println("<h1>Servlet parameters (Multiple Value style):</h1>");
  131.         out.println("<pre>");
  132.         while (e.hasMoreElements()) {
  133.         String name = (String)e.nextElement();
  134.         String vals[] = (String []) req.getParameterValues(name);
  135.         if (vals != null) {
  136.             out.print("<b> " + name + " = </b>"); 
  137.             out.println(vals[0]);
  138.             for (int i = 1; i<vals.length; i++)
  139.             out.println("           " + vals[i]);
  140.         }
  141.         out.println("<p>");
  142.         }
  143.         out.println("</pre>");
  144.     }
  145.  
  146.     String    charset = res.getCharacterEncoding ();
  147.  
  148.     out.println ("<h1>Response Information:</h1>");
  149.     out.println ("<pre>");
  150.     out.println ("MIME character encoding: " + charset);
  151.     out.println ("</pre>");
  152.  
  153.     out.println("</body></html>");
  154.     }
  155.  
  156.     private void print(ServletOutputStream out, String name, String value)
  157.     throws IOException
  158.     {
  159.     out.print(" " + name + ": ");
  160.     out.println(value == null ? "<none>" : value);
  161.     }
  162.  
  163.     private void print(ServletOutputStream out, String name, int value)
  164.     throws IOException
  165.     {
  166.     out.print(" " + name + ": ");
  167.     if (value == -1) {
  168.         out.println("<none>");
  169.     } else {
  170.         out.println(value);
  171.     }
  172.     }
  173.  
  174.     public String getServletInfo() {
  175.     return "A servlet that shows the request headers sent by the client";
  176.     }
  177. }
  178.