You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by Willie H Armstrong <wa...@csc.com> on 2005/05/11 16:34:53 UTC

Problem with referer header.





I am having a problem with the Referer header being "distorted" by the
WebRequest.addHeader() method.  The header name and the header value
gets changed into: "~~~~~~~" and "~~~~:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
which I see when examining the Enumeration en in the debugger.

I am attempting to set two headers in a WebRequest.

code pieces as follows:


// the imports
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebResponse;
import org.apache.cactus.WebRequest;

// "setting up" the request
    public void beginProcessRequestNotReseeding(WebRequest request) {
        System.out.print("beginProcessRequestNotReseeding");
        request.addHeader("HTTP_SM_USER", strHttpUser);
        request.addHeader("Referer", strRefUrl);
        System.out.println(".....Completed client side begin.");
    }
....
// sending the request

   public void testProcessRequestNotReseeding() {
        System.out.println("testProcessRequestNotReseeding");
        assertFalse("Credential was set to true",
Credentialer.isReseeding());
        Enumeration en = request.getHeaderNames();
        try {
            servlet.processRequest(request, response);
        } catch (javax.servlet.ServletException jsse) {
            // do nothing
        } catch (IOException jioe) {
            // do nothing
        }
        Credentialer.setReseeding(false);
        System.out.println(".....Completed.");
    }

// adding a Basic Authentication to  header then dispatching it to
Sender servlet

.... called by testProcessRequestNotReseeding() ........

    protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
    throws ServletException, IOException {
.......
            String strUser = request.getHeader("HTTP_SM_USER");
            String strPW = Credentialer.getPassword(strUser);
            // put BasicAuthurization data in request header
            try {
                response.setHeader("Authorization", "Basic " +
makeAuthedString(strUser, strPW));
            } catch (EncoderException acnee){
                acnee.printStackTrace();
                // TODO make an HTML Message of failure);
            }
            // send request to Refer url
            request.getRequestDispatcher("/Sender").forward(request,
response);
    }


// the Sender servlet does redirect to back to the referer.
... the final servlet method called

    protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
    throws ServletException, IOException {
      String strRefURL = request.getHeader("Referer");
      response.sendRedirect(strRefURL);
    }


The String strRefUrl is set to null so the sendRedirect of course fails.

I have checked the W3C HTTP docs and it appears the the Referer name and
value are both Strings.


Any assistance is appreciated.

Best Regards,
Bill





Willie (Bill) Armstrong
Sun Certified Programmer for the Java 2 Platform 1.4


Re: Problem with referer header.

Posted by Willie H Armstrong <wa...@csc.com>.



Thanks for your reply.

I am running Tomcat 5.0.28 which is the default for the Netbeans IDE which
I am using.  I develop the classes in the IDE, launch tomcat from the IDE,
then run the test from a command prompt.

I will run your code and see if it gives me clean results.  It looks like
you have distilled the essence of the problem.

Regards,
Bill


Willie (Bill) Armstrong
Sun Certified Programmer for the Java 2 Platform 1.4



                                                                                                                                      
                      Kazuhito SUGURI                                                                                                 
                      <suguri.kazuhito         To:      cactus-user@jakarta.apache.org, Willie H Armstrong/GTS/CSC@CSC                
                      @lab.ntt.co.jp>          cc:                                                                                    
                                               Subject: Re: Problem with referer header.                                              
                      05/11/2005 01:22                                                                                                
                      PM                                                                                                              
                      Please respond                                                                                                  
                      to "Cactus Users                                                                                                
                      List"                                                                                                           
                                                                                                                                      




Hi Willie,

I have tried to re-produce your problem, but my test-case does not fail.
I'm trying with tomcat-5.0.28. My test codes are appending:
              SampleServlet.java
              SampleServletTest.java

The SampleServlet is mapped to /refersample in my case.


In article
<OF...@csc.com>,
Wed, 11 May 2005 09:34:53 -0500,
Willie H Armstrong <wa...@csc.com> wrote:
warmstr2> I am having a problem with the Referer header being "distorted"
by the
warmstr2> WebRequest.addHeader() method.  The header name and the header
value
warmstr2> gets changed into: "~~~~~~~" and
"~~~~:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
warmstr2> which I see when examining the Enumeration en in the debugger.
warmstr2>
warmstr2> I am attempting to set two headers in a WebRequest.
warmstr2>
warmstr2> code pieces as follows:
warmstr2>
warmstr2>
warmstr2> // the imports
warmstr2> import org.apache.cactus.ServletTestCase;
warmstr2> import org.apache.cactus.WebResponse;
warmstr2> import org.apache.cactus.WebRequest;
warmstr2>
warmstr2> // "setting up" the request
warmstr2>     public void beginProcessRequestNotReseeding(WebRequest
request) {
warmstr2>         System.out.print("beginProcessRequestNotReseeding");
warmstr2>         request.addHeader("HTTP_SM_USER", strHttpUser);
warmstr2>         request.addHeader("Referer", strRefUrl);
warmstr2>         System.out.println(".....Completed client side begin.");
warmstr2>     }

In my code, corresponds to SampleServletTest#beginReferer().

warmstr2> // sending the request
warmstr2>
warmstr2>    public void testProcessRequestNotReseeding() {
warmstr2>         System.out.println("testProcessRequestNotReseeding");
warmstr2>         assertFalse("Credential was set to true",
warmstr2> Credentialer.isReseeding());
warmstr2>         Enumeration en = request.getHeaderNames();
warmstr2>         try {
warmstr2>             servlet.processRequest(request, response);
warmstr2>         } catch (javax.servlet.ServletException jsse) {
warmstr2>             // do nothing
warmstr2>         } catch (IOException jioe) {
warmstr2>             // do nothing
warmstr2>         }
warmstr2>         Credentialer.setReseeding(false);
warmstr2>         System.out.println(".....Completed.");
warmstr2>     }

In my code, corresponds to SampleServletTest#testReferer().


warmstr2> // adding a Basic Authentication to  header then dispatching it
to
warmstr2> Sender servlet
warmstr2>
warmstr2> .... called by testProcessRequestNotReseeding() ........
warmstr2>
warmstr2>     protected void processRequest(HttpServletRequest request,
warmstr2> HttpServletResponse response)
warmstr2>     throws ServletException, IOException {
warmstr2> .......
warmstr2>             String strUser = request.getHeader("HTTP_SM_USER");
warmstr2>             String strPW = Credentialer.getPassword(strUser);
warmstr2>             // put BasicAuthurization data in request header
warmstr2>             try {
warmstr2>                 response.setHeader("Authorization", "Basic " +
warmstr2> makeAuthedString(strUser, strPW));
warmstr2>             } catch (EncoderException acnee){
warmstr2>                 acnee.printStackTrace();
warmstr2>                 // TODO make an HTML Message of failure);
warmstr2>             }
warmstr2>             // send request to Refer url
warmstr2>
request.getRequestDispatcher("/Sender").forward(request,
warmstr2> response);
warmstr2>     }

In my code, corresponds to SampleServlet#processRequest().
At this method, the request is dispatched to "/refersample",
i.e. I'm expecting SampleServlet#doGet() will be called.


warmstr2> // the Sender servlet does redirect to back to the referer.
warmstr2> ... the final servlet method called
warmstr2>
warmstr2>     protected void processRequest(HttpServletRequest request,
warmstr2> HttpServletResponse response)
warmstr2>     throws ServletException, IOException {
warmstr2>       String strRefURL = request.getHeader("Referer");
warmstr2>       response.sendRedirect(strRefURL);
warmstr2>     }

In my code, corresponds to SampleServlet#doGet(). At this point,
request.getHeader("Referer") of my code has non-null value,
"/test.jsp", and of cource, 302 response code is received at
SampleServletTest#endReferer().


warmstr2> The String strRefUrl is set to null so the sendRedirect of course
fails.

How could we re-produce the problem?

Regards,
----
Kazuhito SUGURI
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SampleServlet
    extends HttpServlet
{
    public void doGet(HttpServletRequest request,
                               HttpServletResponse response)
             throws ServletException, IOException
    {
        System.out.println("Referer: " + request.getHeader("Referer"));
        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void processRequest(HttpServletRequest request,
                                  HttpServletResponse response)
             throws ServletException, IOException
    {
        response.setHeader("TEST", "testHeader");
        request.getRequestDispatcher("/refersample").forward(request,
response);
    }
}
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.apache.cactus.WebResponse;

public class SampleServletTest
    extends ServletTestCase
{
    public SampleServletTest(String name)
    {
             super(name);
    }

    public void beginReferer(WebRequest request)
    {
             request.addHeader("Referer", "/test.jsp");
    }

    public void testReferer()
             throws Exception
    {
        assertEquals("/test.jsp", request.getHeader("Referer"));

             SampleServlet servlet = new SampleServlet();
        servlet.processRequest(request, response);
    }

    public void endReferer(WebResponse response)
        throws Exception
    {
        assertEquals(302, response.getStatusCode());
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: cactus-user-help@jakarta.apache.org


Re: Problem with referer header.

Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi Willie,

I have tried to re-produce your problem, but my test-case does not fail.
I'm trying with tomcat-5.0.28. My test codes are appending:
	 SampleServlet.java
	 SampleServletTest.java

The SampleServlet is mapped to /refersample in my case.


In article <OF...@csc.com>,
Wed, 11 May 2005 09:34:53 -0500,
Willie H Armstrong <wa...@csc.com> wrote: 
warmstr2> I am having a problem with the Referer header being "distorted" by the
warmstr2> WebRequest.addHeader() method.  The header name and the header value
warmstr2> gets changed into: "~~~~~~~" and "~~~~:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
warmstr2> which I see when examining the Enumeration en in the debugger.
warmstr2> 
warmstr2> I am attempting to set two headers in a WebRequest.
warmstr2> 
warmstr2> code pieces as follows:
warmstr2> 
warmstr2> 
warmstr2> // the imports
warmstr2> import org.apache.cactus.ServletTestCase;
warmstr2> import org.apache.cactus.WebResponse;
warmstr2> import org.apache.cactus.WebRequest;
warmstr2> 
warmstr2> // "setting up" the request
warmstr2>     public void beginProcessRequestNotReseeding(WebRequest request) {
warmstr2>         System.out.print("beginProcessRequestNotReseeding");
warmstr2>         request.addHeader("HTTP_SM_USER", strHttpUser);
warmstr2>         request.addHeader("Referer", strRefUrl);
warmstr2>         System.out.println(".....Completed client side begin.");
warmstr2>     }

In my code, corresponds to SampleServletTest#beginReferer().

warmstr2> // sending the request
warmstr2> 
warmstr2>    public void testProcessRequestNotReseeding() {
warmstr2>         System.out.println("testProcessRequestNotReseeding");
warmstr2>         assertFalse("Credential was set to true",
warmstr2> Credentialer.isReseeding());
warmstr2>         Enumeration en = request.getHeaderNames();
warmstr2>         try {
warmstr2>             servlet.processRequest(request, response);
warmstr2>         } catch (javax.servlet.ServletException jsse) {
warmstr2>             // do nothing
warmstr2>         } catch (IOException jioe) {
warmstr2>             // do nothing
warmstr2>         }
warmstr2>         Credentialer.setReseeding(false);
warmstr2>         System.out.println(".....Completed.");
warmstr2>     }

In my code, corresponds to SampleServletTest#testReferer().


warmstr2> // adding a Basic Authentication to  header then dispatching it to
warmstr2> Sender servlet
warmstr2> 
warmstr2> .... called by testProcessRequestNotReseeding() ........
warmstr2> 
warmstr2>     protected void processRequest(HttpServletRequest request,
warmstr2> HttpServletResponse response)
warmstr2>     throws ServletException, IOException {
warmstr2> .......
warmstr2>             String strUser = request.getHeader("HTTP_SM_USER");
warmstr2>             String strPW = Credentialer.getPassword(strUser);
warmstr2>             // put BasicAuthurization data in request header
warmstr2>             try {
warmstr2>                 response.setHeader("Authorization", "Basic " +
warmstr2> makeAuthedString(strUser, strPW));
warmstr2>             } catch (EncoderException acnee){
warmstr2>                 acnee.printStackTrace();
warmstr2>                 // TODO make an HTML Message of failure);
warmstr2>             }
warmstr2>             // send request to Refer url
warmstr2>             request.getRequestDispatcher("/Sender").forward(request,
warmstr2> response);
warmstr2>     }

In my code, corresponds to SampleServlet#processRequest().
At this method, the request is dispatched to "/refersample",
i.e. I'm expecting SampleServlet#doGet() will be called.


warmstr2> // the Sender servlet does redirect to back to the referer.
warmstr2> ... the final servlet method called
warmstr2> 
warmstr2>     protected void processRequest(HttpServletRequest request,
warmstr2> HttpServletResponse response)
warmstr2>     throws ServletException, IOException {
warmstr2>       String strRefURL = request.getHeader("Referer");
warmstr2>       response.sendRedirect(strRefURL);
warmstr2>     }

In my code, corresponds to SampleServlet#doGet(). At this point,
request.getHeader("Referer") of my code has non-null value,
"/test.jsp", and of cource, 302 response code is received at
SampleServletTest#endReferer().


warmstr2> The String strRefUrl is set to null so the sendRedirect of course fails.

How could we re-produce the problem?

Regards,
----
Kazuhito SUGURI