Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

sqlexpress database file auto-creation error, dbf2mysql parameter, WWWXXXAPC, wwwxxxAPC, How to unsubscrube from dategen spam, docmd.close 2585, WWWXXXDOCO, nu vot, dhcpd lease file "binding state", WWWXXXDOCO

Links

XODOX
Impressum

#1: Edit HTTP response headers

Posted on 2008-04-21 22:30:22 by J Amuse

------=_Part_7974_17139320.1208809822453
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

I wrote a simple module that parses the HTTP response body and updates
certain text. I want the script to be able to parse and manipulate the HTTP
response headers as well. For example add secure and HttpOnly flags to
certain cookies, or add a redirection when a 500 error code is returned. The
code I have so far is:

============================================================ ==========================
package TE::ST;
# $Header:$
use strict;
use warnings;

use Apache2::Filter ();
use Apache2::RequestRec ();
use APR::Table ();

use Apache2::Const -compile => qw(OK);

use constant BUFF_LEN => 1024;
no warnings qw(redefine);

sub handler {
my $f = shift;

while ($f->read(my $buffer, BUFF_LEN)) {
# Disable autocomplete
$buffer =~ s/login method="post"/login method="post"
autocomplete="off" /g;

$f->print($buffer);
}

return Apache2::Const::OK;
}
1;
============================================================ ==========================

when using f->read it appears as though the HTTP headers are not including.
I know err_headers_out should have access to the headers, but is there a way
to force f->read to access the HTTP headers as well? I've seen the set and
add methods for err_headers_out, but can someone point me to a code example
of either editing the HTTP headers or rewriting the server response when
certain response codes are received?

Thanks.

------=_Part_7974_17139320.1208809822453
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

I wrote a simple module that parses the HTTP response body and updates certain text. I want the script to be able to parse and manipulate the HTTP response headers as well. For example add secure and HttpOnly flags to certain cookies, or add a redirection when a 500 error code is returned. The code I have so far is:<br>
<br> ============================================================ ==========================<br>package TE::ST;<br># $Header:$<br>use strict;<br>use warnings;<br><br>use Apache2::Filter ();<br>use Apache2::RequestRec ();<br>
use APR::Table ();<br><br>use Apache2::Const -compile =&gt; qw(OK);<br><br>use constant BUFF_LEN =&gt; 1024;<br>no warnings qw(redefine);<br><br>sub handler {<br>&nbsp;&nbsp;&nbsp; my $f = shift;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; while ($f-&gt;read(my $buffer, BUFF_LEN)) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Disable autocomplete<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $buffer =~ s/login method=&quot;post&quot;/login method=&quot;post&quot; autocomplete=&quot;off&quot; /g;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $f-&gt;print($buffer);<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; return Apache2::Const::OK;<br>
}<br>1;<br> ============================================================ ==========================<br><br>when using f-&gt;read it appears as though the HTTP headers are not including. I know err_headers_out should have access to the headers, but is there a way to force f-&gt;read to access the HTTP headers as well? I&#39;ve seen the set and add methods for err_headers_out, but can someone point me to a code example of either editing the HTTP headers or rewriting the server response when certain response codes are received? <br>
<br>Thanks.<br>

------=_Part_7974_17139320.1208809822453--

Report this message

#2: Re: Edit HTTP response headers

Posted on 2008-04-23 16:28:07 by Perrin Harkins

On Mon, Apr 21, 2008 at 4:30 PM, J Amuse <jamuse@gmail.com> wrote:
> when using f->read it appears as though the HTTP headers are not including.
> I know err_headers_out should have access to the headers, but is there a way
> to force f->read to access the HTTP headers as well?

No, the headers are not part of the response body.

> I've seen the set and
> add methods for err_headers_out, but can someone point me to a code example
> of either editing the HTTP headers or rewriting the server response when
> certain response codes are received?

http://perl.apache.org/docs/2.0/user/coding/coding.html#Gene rating_HTTP_Response_Headers
http://perl.apache.org/docs/2.0/user/handlers/filters.html#I ntroducing_Filters

- Perrin

Report this message

#3: Re: Edit HTTP response headers

Posted on 2008-04-23 17:48:27 by torsten.foertsch

On Mon 21 Apr 2008, J Amuse wrote:
> I wrote a simple module that parses the HTTP response body and updates
> certain text. I want the script to be able to parse and manipulate the HTTP
> response headers as well. For example add secure and HttpOnly flags to
> certain cookies, or add a redirection when a 500 error code is returned.

You can modify outgoing headers up to the time they are sent. So, at the very
first call to your filter you can modify the status code as well as outgoing
headers. See Apache2::POST200 on CPAN.

Torsten

--
Need professional mod_perl support?
Just hire me: torsten.foertsch@gmx.net

Report this message

#4: Re: Edit HTTP response headers

Posted on 2008-04-27 15:18:03 by J Amuse

------=_Part_6446_6455783.1209302283124
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Wed, Apr 23, 2008 at 5:28 PM, Perrin Harkins <perrin@elem.com> wrote:

> On Mon, Apr 21, 2008 at 4:30 PM, J Amuse <jamuse@gmail.com> wrote:
>
>
> http://perl.apache.org/docs/2.0/user/coding/coding.html#Gene rating_HTTP_Response_Headers
> <http://perl.apache.org/docs/2.0/user/handlers/filters.html#Introducing_Filters>


Thanks Perrin.
This link references the set method, which will overwrite the header value.
I want to append text (i.e. append a secure and httponly flag if there is a
Set-Cookie header in the HTTP response). Is this possible with the overlay
method? How can I extract the contents of the Set-Cookie header to modify
it?

Jay

------=_Part_6446_6455783.1209302283124
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Wed, Apr 23, 2008 at 5:28 PM, Perrin Harkins &lt;<a href="mailto:perrin@elem.com">perrin@elem.com</a>&gt; wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">On Mon, Apr 21, 2008 at 4:30 PM, J Amuse &lt;<a href="mailto:jamuse@gmail.com">jamuse@gmail.com</a>&gt; wrote:<br></div><div class="Ih2E3d"><br>
</div><a href="http://perl.apache.org/docs/2.0/user/coding/coding.html#Generating_HTTP_Response_Headers" target="_blank"> http://perl.apache.org/docs/2.0/user/coding/coding.html#Gene rating_HTTP_Response_Headers</a><br>
<a href="http://perl.apache.org/docs/2.0/user/handlers/filters.html#Introducing_Filters" target="_blank"></a></blockquote><div><br>Thanks Perrin.<br>This link references the set method, which will overwrite the header value. I want to append text (i.e. append a secure and httponly flag if there is a Set-Cookie header in the HTTP response). Is this possible with the overlay method? How can I extract the contents of the Set-Cookie header to modify it?<br>
&nbsp;</div></div>Jay<br>

------=_Part_6446_6455783.1209302283124--

Report this message

#5: Re: Edit HTTP response headers

Posted on 2008-04-27 15:28:44 by Perrin Harkins

On Sun, Apr 27, 2008 at 9:18 AM, J Amuse <jamuse@gmail.com> wrote:
> This link references the set method, which will overwrite the header value.
> I want to append text (i.e. append a secure and httponly flag if there is a
> Set-Cookie header in the HTTP response). Is this possible with the overlay
> method? How can I extract the contents of the Set-Cookie header to modify
> it?

You can read the headers with $r->headers_out->get(). When you call
$r->headers_out() it gives you an APR::Table object which you can read
and write as much as you like:
http://perl.apache.org/docs/2.0/api/APR/Table.html

- Perrin

Report this message

#6: Re: Edit HTTP response headers

Posted on 2008-04-28 11:33:53 by J Amuse

------=_Part_8836_3293175.1209375233106
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Wed, Apr 23, 2008 at 5:28 PM, Perrin Harkins <perrin@elem.com> wrote:

> On Mon, Apr 21, 2008 at 4:30 PM, J Amuse <jamuse@gmail.com> wrote:
> >
> >
> > http://perl.apache.org/docs/2.0/user/coding/coding.html#Gene rating_HTTP_Response_Headers
> > <http://perl.apache.org/docs/2.0/user/handlers/filters.html#Introducing_Filters>
> >
>
> Thanks Perrin.
> This link references the set method, which will overwrite the header
> value. I want to append text (i.e. append a secure and httponly flag if
> there is a Set-Cookie header in the HTTP response). Is this possible with
> the overlay method? How can I extract the contents of the Set-Cookie header
> to modify it?
>
>
For future reference, I was able to manipulate the HTTP headers via the
following code snippet:

sub handler
{
my $f = shift;

unless ($f->ctx)
{
my $SecureFlags = "\; secure\; HttpOnly\;";
my $Cookie = $f->r->headers_out->get("Set-Cookie");

if ($Cookie)
{
$Cookie .= $SecureFlags;
$f->r->headers_out->set("Set-Cookie" => "$Cookie");
}
$f->ctx(1);
}
}

Thanks again to Perrin for all his help.

------=_Part_8836_3293175.1209375233106
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Wed, Apr 23, 2008 at 5:28 PM, Perrin Harkins &lt;<a href="mailto:perrin@elem.com" target="_blank">perrin@elem.com</a>&gt; wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d">
<div>On Mon, Apr 21, 2008 at 4:30 PM, J Amuse &lt;<a href="mailto:jamuse@gmail.com" target="_blank">jamuse@gmail.com</a>&gt; wrote:<br></div><div><br>
</div></div><div class="Ih2E3d"><a href="http://perl.apache.org/docs/2.0/user/coding/coding.html#Generating_HTTP_Response_Headers" target="_blank"> http://perl.apache.org/docs/2.0/user/coding/coding.html#Gene rating_HTTP_Response_Headers</a><br>

<a href="http://perl.apache.org/docs/2.0/user/handlers/filters.html#Introducing_Filters" target="_blank"></a></div></blockquote><div><br>Thanks Perrin.<br>This link references the set method, which will overwrite the header value. I want to append text (i.e. append a secure and httponly flag if there is a Set-Cookie header in the HTTP response). Is this possible with the overlay method? How can I extract the contents of the Set-Cookie header to modify it?<br>
&nbsp;
<br></div></div></blockquote></div>For future reference, I was able to manipulate the HTTP headers via the following code snippet:<br><br>sub handler <br>{<br>&nbsp;&nbsp;&nbsp; my $f = shift;<br><br>&nbsp;&nbsp;&nbsp; unless ($f-&gt;ctx)<br>&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $SecureFlags = &quot;\; secure\; HttpOnly\;&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $Cookie = $f-&gt;r-&gt;headers_out-&gt;get(&quot;Set-Cookie&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($Cookie)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $Cookie .= $SecureFlags;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $f-&gt;r-&gt;headers_out-&gt;set(&quot;Set-Cookie&quot; =&gt; &quot;$Cookie&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $f-&gt;ctx(1);<br>&nbsp;&nbsp;&nbsp; }<br>}<br><br>Thanks again to Perrin for all his help.<br><br>

------=_Part_8836_3293175.1209375233106--

Report this message