Re: Adding / Modifing Response Headers on mp2
On Thu, 3 Apr 2003, Denis Banovic wrote: > Hi! > > I'm trying to figure out, how to Add / Modify the HTTP Headers like > Content Type, Cache aso... > > on mod_perl 1 I've used: > > $r->send_cgi_header($custom_headers); > > but this won't work mod_perl 2... > > I've searched in Apache::compat all I've found was the function header_out > that internally calls $r->headers_out()... > > Can somebody please tell me how to set the right headers??? TFM is a little hard to find, but when you R http://xrl.us/fix you'll see that all you need to do to call $r->send_cgi_header() is: use Apache::Response(); The docs need work, but if you are porting your code from mp1 to mp2 you really should spend some time reading _all_ the mp2 docs, don't you think? Start at http://perl.apache.org/docs/2.0/devel/porting/porting.html and just follow every link from there. > here is the code from Apache::compat Most of us already have that :) - nick -- Nick Tonkin {|8^)>
Adding / Modifing Response Headers on mp2
Hi! I'm trying to figure out, how to Add / Modify the HTTP Headers like Content Type, Cache aso... on mod_perl 1 I've used: $r->send_cgi_header($custom_headers); but this won't work mod_perl 2... I've searched in Apache::compat all I've found was the function header_out that internally calls $r->headers_out()... Can somebody please tell me how to set the right headers??? Thanks a lot! here is the code from Apache::compat sub table_get_set { my($r, $table) = (shift, shift); my($key, $value) = @_; if (1 == @_) { return wantarray() ? ($table->get($key)) : scalar($table->get($key)); } elsif (2 == @_) { if (defined $value) { return wantarray() ?($table->set($key, $value)) : scalar($table->set($key, $value)); } else { return wantarray() ? ($table->unset($key)) : scalar($table->unset($key)); } } elsif (0 == @_) { return $table; } else { my $name = (caller(1))[3]; warn "Usage: \$r->$name([key [,val]])"; } } sub header_out { my $r = shift; return wantarray() ? ($r->table_get_set(scalar($r->headers_out), @_)) : scalar($r->table_get_set(scalar($r->headers_out), @_)); }
Re: Response Headers
On Fri, 4 Oct 2002, Paul Simon wrote: > On Thu, 3 Oct 2002, Paul Simon wrote: >> I'm still having problems with sending an Expires in the >> header. You mentioned, "Apache will send one for you, based >> on, in particular, the DefaultType setting." >> Can I configure the DefaultType in apache? Yes, you can - look at the Apache documentation for the syntax. "DefaultType" refers to the default mime type that that apache will use for a request if it can't otherwise determine one (such as from a file extension). > Ok. I answered my own question, using mod_expires. A little off OT... Not too OT for mod_perl - see http://perl.apache.org/docs/general/correct_headers/correct_headers.html for a discussion. For sending the Expires header yourself in a script, using mod_perl-2, you can do my $expires = Apache::Util::ht_time(time + 180*24*60*60); print <<"END"; Expires: $expires Content-type: text/html\n\n whatever_else END which would require PerlOptions +ParseHeaders in the relevant section of httpd.conf. -- best regards, randy
Re: Response Headers
Ok. I answered my own question, using mod_expires. A little off OT... Paul Simon <[EMAIL PROTECTED]>wrote: Randy Kobes <[EMAIL PROTECTED]>wrote: On Thu, 3 Oct 2002, Paul Simon wrote:> > How do HTTP headers work under Registery::ModPerl?> set up: windows2000 apache2.0.42 mod_perl/1.99_08-dev Perl/v5.8.0> > I had to comment out the following in the CGI script:> > #print "Expires: " . time2str( time() + 432000 ) . "\n";> #print "Content-type: text/html\n\n";> > because it would print out as content to the browser??> > This is in the conf file:> > Alias /standards/ "C:/Apache2/application/standards2/"> > SetHandler perl-script> PerlResponseHandler ModPerl::Registry> #PerlOptions +ParseHeaders> Options +ExecCGI> > > If somebody could point me in the right direction ->> > Thanks.If you send the headers yourself, as, eg,print "Content-type: text/html\n\n";in the script, then you should havePerlOptions +ParseHeaders in the relevant section. If you don't send the header yourself,Apache will send one for you, based on, in particular, theDefaultType setting. In this case you shouldn't havePerlOptions +ParseHeadersin that section. As you found, other combinations can leadto a double set of headers sent, resulting in one ofthem appearing in the browser.-- best regards,randy kobes I have a pilot web app running with the above set-up (It's working really well, *fingers crossed*). I understand your explanation. I'm still having problems with sending an Expires in the header. You mentioned, "Apache will send one for you, based on, in particular, the DefaultType setting." Can I configure the DefaultType in apache? The way I have it now is:print "Content-type: text/html\n\n";in the CGI scriptAnd in the conf file: SetHandler perl-script PerlResponseHandler ModPerl::Registry PerlOptions +ParseHeaders Options +ExecCGI Here's the response header from the server: HTTP/1.1 200 OKDate: Fri, 04 Oct 2002 13:40:30 GMTServer: Apache/2.0.42 (Win32) mod_perl/1.99_08-dev Perl/v5.8.0Connection: closeContent-Type: text/html; charset=ISO-8859-1 My current goal is to cache the pages on the client browser. Ultimately, I'd like to cache on the server side too ( as you would've guessed :) ) I'm learning as I go... Do you Yahoo!?New DSL Internet Access from SBC & Yahoo!Do you Yahoo!? New DSL Internet Access from SBC & Yahoo!
Re: Response Headers
Randy Kobes <[EMAIL PROTECTED]>wrote: On Thu, 3 Oct 2002, Paul Simon wrote:> > How do HTTP headers work under Registery::ModPerl?> set up: windows2000 apache2.0.42 mod_perl/1.99_08-dev Perl/v5.8.0> > I had to comment out the following in the CGI script:> > #print "Expires: " . time2str( time() + 432000 ) . "\n";> #print "Content-type: text/html\n\n";> > because it would print out as content to the browser??> > This is in the conf file:> > Alias /standards/ "C:/Apache2/application/standards2/"> > SetHandler perl-script> PerlResponseHandler ModPerl::Registry> #PerlOptions +ParseHeaders> Options +ExecCGI> > > If somebody could point me in the right direction ->> > Thanks.If you send the headers yourself, as, eg,print "Content-type: text/html\n\n";in the script, then you should havePerlOptions +ParseHeaders in the relevant section. If you don't send the header yourself,Apache will send one for you, based on, in particular, theDefaultType setting. In this case you shouldn't havePerlOptions +ParseHeadersin that section. As you found, other combinations can leadto a double set of headers sent, resulting in one ofthem appearing in the browser.-- best regards,randy kobes I have a pilot web app running with the above set-up (It's working really well, *fingers crossed*). I understand your explanation. I'm still having problems with sending an Expires in the header. You mentioned, "Apache will send one for you, based on, in particular, the DefaultType setting." Can I configure the DefaultType in apache? The way I have it now is:print "Content-type: text/html\n\n";in the CGI scriptAnd in the conf file: SetHandler perl-script PerlResponseHandler ModPerl::Registry PerlOptions +ParseHeaders Options +ExecCGI Here's the response header from the server: HTTP/1.1 200 OKDate: Fri, 04 Oct 2002 13:40:30 GMTServer: Apache/2.0.42 (Win32) mod_perl/1.99_08-dev Perl/v5.8.0Connection: closeContent-Type: text/html; charset=ISO-8859-1 My current goal is to cache the pages on the client browser. Ultimately, I'd like to cache on the server side too ( as you would've guessed :) ) I'm learning as I go... Do you Yahoo!? New DSL Internet Access from SBC & Yahoo!
Re: Response Headers
On Thu, 3 Oct 2002, Paul Simon wrote: > > How do HTTP headers work under Registery::ModPerl? > set up: windows2000 apache2.0.42 mod_perl/1.99_08-dev Perl/v5.8.0 > > I had to comment out the following in the CGI script: > > #print "Expires: " . time2str( time() + 432000 ) . "\n"; > #print "Content-type: text/html\n\n"; > > because it would print out as content to the browser?? > > This is in the conf file: > > Alias /standards/ "C:/Apache2/application/standards2/" > > SetHandler perl-script > PerlResponseHandler ModPerl::Registry > #PerlOptions +ParseHeaders > Options +ExecCGI > > > If somebody could point me in the right direction -> > > Thanks. If you send the headers yourself, as, eg, print "Content-type: text/html\n\n"; in the script, then you should have PerlOptions +ParseHeaders in the relevant section. If you don't send the header yourself, Apache will send one for you, based on, in particular, the DefaultType setting. In this case you shouldn't have PerlOptions +ParseHeaders in that section. As you found, other combinations can lead to a double set of headers sent, resulting in one of them appearing in the browser. -- best regards, randy kobes
Response Headers
How do HTTP headers work under Registery::ModPerl?set up: windows2000 apache2.0.42 mod_perl/1.99_08-dev Perl/v5.8.0 I had to comment out the following in the CGI script: #print "Expires: " . time2str( time() + 432000 ) . "\n";#print "Content-type: text/html\n\n"; because it would print out as content to the browser?? This is in the conf file: Alias /standards/ "C:/Apache2/application/standards2/" SetHandler perl-script PerlResponseHandler ModPerl::Registry #PerlOptions +ParseHeaders Options +ExecCGI If somebody could point me in the right direction -> Thanks.Do you Yahoo!? New DSL Internet Access from SBC & Yahoo!
Re: Unsetting standard response headers?
On Sun, 13 Jan 2002, Perrin Harkins wrote: > > I have noticed that Yahoo uses Location: header only for redirect > responses and thought > > it may be good to save half of the bandwidth and do the same, as my > particular script/server > > is serving redirects mostly. So my question is how to unset Date:, > Server: and > > Content-Type: response headers? > > Who is setting them in the first place? If they are generated by your > script and you don't set them, Apache will not add them. You may be > seeing them added for redirects that Apache does for you, like sending > http://yoursite to http://yoursite/. You can handle those yourself > instead if you want to. Apache core always sets 'Server' and 'Date' headers. You can not simply overwrite them - you need patch Apache or use low-level Apache API. Igor Sysoev
Re: Unsetting standard response headers?
> I have noticed that Yahoo uses Location: header only for redirect responses and thought > it may be good to save half of the bandwidth and do the same, as my particular script/server > is serving redirects mostly. So my question is how to unset Date:, Server: and > Content-Type: response headers? Who is setting them in the first place? If they are generated by your script and you don't set them, Apache will not add them. You may be seeing them added for redirects that Apache does for you, like sending http://yoursite to http://yoursite/. You can handle those yourself instead if you want to. - Perrin
Re: Unsetting standard response headers?
On Sun, 13 Jan 2002, Richard [utf-8] Čepas wrote: > I have noticed that Yahoo uses Location: header only for redirect responses and >thought it may be good to save half of the bandwidth and do the same, as my >particular script/server is serving redirects mostly. To save bandwidth you can try mod_deflate - it gzips content like mod_gzip does. > So my question is how to unset Date:, Server: and Content-Type: response headers? >mod_headers and 'Header unset' doesn't work for some reason, maybe it is possible to >use some Perl*Handler? mod_headers would not help. There are two ways to omit 'Server', 'Date' and 'Content-Type' headers: 1. to use ap_rwrite()/BUFF API to send response as mod_proxy does; 2. to patch Apache sources. This helps to all Apache modules that use ap_send_http_header()/'return HTTP_MOVED_*' API. Igor Sysoev
Unsetting standard response headers?
Hi, I have noticed that Yahoo uses Location: header only for redirect responses and thought it may be good to save half of the bandwidth and do the same, as my particular script/server is serving redirects mostly. So my question is how to unset Date:, Server: and Content-Type: response headers? mod_headers and 'Header unset' doesn't work for some reason, maybe it is possible to use some Perl*Handler? -- ☻ Ričardas Čepas ☺
RE: which Handler to adjust Response-Headers?
> -Original Message- > From: Jochen Schnapka [mailto:[EMAIL PROTECTED]] > Sent: Thursday, April 19, 2001 12:52 PM > To: [EMAIL PROTECTED] > Subject: Re: which Handler to adjust Response-Headers? > > > On Thu, Apr 19, 2001 at 08:42:40AM -0400, Geoffrey Young wrote: > > > > > $r->headers_out seems to be completly empty. > > > > I think $r->headers_out will be empty until you do something to put > > something in it (like $r->headers_out->add(), $r->no_cache(1), or > > $r->send_http_headers()). Depending on which modules you > have in your > > apache binary (like maybe mod_expires?), there may be no > activity that sets > > headers until you call $r->send_http_headers(). > > but there *is* a bunch of headers, which is sent back, even > if I do not > touch anything. and these headers must be readable somewhere, > let it be > Content-Length, Type, Date, whatever... yes, after you call send_http_header(). Apache sets some of these for you when the response is sent to the client if you don't call it yourself. If you try and find them before the content is actually sent (like in a PerlFixupHandler) you won't see them... [snip] > > sub headers_out { > #- > # dump all of the outbound response headers > #- > > my $self = shift; > > # [...] > > my $r = $self->{request}; > # ^^ $self->{request} is not a method call - what you essentially are getting is $r = $r; so what you think is $r->request->headers_out() is really just $r->headers_out(). ;) --Geoff
Re: which Handler to adjust Response-Headers?
On Thu, Apr 19, 2001 at 08:42:40AM -0400, Geoffrey Young wrote: > > > $r->headers_out seems to be completly empty. > > I think $r->headers_out will be empty until you do something to put > something in it (like $r->headers_out->add(), $r->no_cache(1), or > $r->send_http_headers()). Depending on which modules you have in your > apache binary (like maybe mod_expires?), there may be no activity that sets > headers until you call $r->send_http_headers(). but there *is* a bunch of headers, which is sent back, even if I do not touch anything. and these headers must be readable somewhere, let it be Content-Length, Type, Date, whatever... > if you are interested, you can use Apache::DebugInfo and set up a > Perl*Handler for each phase and see what is added when: [...] > > Hehe, but this works: > > > > $r->request->headers_out ... > > huh? that doesn't make sense to me... But it works. And guess, where I stole the trick :-) Apache::DebugInfo !! -- snip -- # Code from DebugInfo.pm [...] sub headers_out { #----- # dump all of the outbound response headers #- my $self = shift; # [...] my $r = $self->{request}; # ^^ # [...] print $fh "\nDebug headers_out for [$ip] $uri during " . $r->current_callback . "\n"; $r->headers_out->do(sub { # ^^^ my ($field, $value) = @_; if ($field =~ m/Cookie/) { my @values = split /;/, $value; print $fh "\t$field => $values[0]\n"; for (my $i=1;$i < @values; $i++) { print $fh "\t\t=> $values[$i]\n"; } } [...] -- snap -- > > Maybe this should be fixed or documented, otherwise somebody tell me > > please, if I am completly wrong :-) > > that's more likely ;) Are you sure? ;-) Maybe I'm partly wrong... Greetings. ~~~:-Jochen -- The human mind ordinarily operates at only ten percent of its capacity -- the rest is overhead for the operating system.
RE: which Handler to adjust Response-Headers?
> -Original Message- > From: Jochen Schnapka [mailto:[EMAIL PROTECTED]] > Sent: Thursday, April 19, 2001 6:31 AM > To: [EMAIL PROTECTED] > Subject: Re: which Handler to adjust Response-Headers? > > > Hi again. Once again, I found the solution by myself. > > On Wed, Apr 18, 2001 at 05:13:21PM +0200, Jochen Schnapka wrote: > > Hi. > > > > I wonder, which Handler I would have to invoke to mangle or > adjust the > > response headers. I thought, this was the PerlFixupHandler. >> But > > $r->headers_out seems to be completly empty. I think $r->headers_out will be empty until you do something to put something in it (like $r->headers_out->add(), $r->no_cache(1), or $r->send_http_headers()). Depending on which modules you have in your apache binary (like maybe mod_expires?), there may be no activity that sets headers until you call $r->send_http_headers(). if you are interested, you can use Apache::DebugInfo and set up a Perl*Handler for each phase and see what is added when: PerlInitHandler 'sub { my $debug = Apache::DebugInfo->new(shift); $debug->headers_out; };' PerlFixupHandler 'sub { my $debug = Apache::DebugInfo->new(shift); $debug->headers_out; };' PerlCleanupHandler 'sub { my $debug = Apache::DebugInfo->new(shift); $debug->headers_out; };' or whatever... [snip] > > Hehe, but this works: > > $r->request->headers_out ... huh? that doesn't make sense to me... > > So headers_out is located beneath the request sub-object (or > however you > like to call it). Unfortunately this is not clearly > documented (I read the > online book, chapter 7 and 9). > > I feel, that some methods can be accessed directly from top (as > headers_in()), others not (like headers_out) :-( > > Maybe this should be fixed or documented, otherwise somebody tell me > please, if I am completly wrong :-) that's more likely ;) --Geoff
Re: which Handler to adjust Response-Headers?
Hi again. Once again, I found the solution by myself. On Wed, Apr 18, 2001 at 05:13:21PM +0200, Jochen Schnapka wrote: > Hi. > > I wonder, which Handler I would have to invoke to mangle or adjust the > response headers. I thought, this was the PerlFixupHandler. But > $r->headers_out seems to be completly empty. > > > This is my handler: > > use strict; > use Apache::Constants; > use Apache::Table; > > sub handler { > my $r = shift; > > # this does not work, it's empty: > > $r->headers_out->do(sub { > my($key, $value) = @_; > $r->log_error("$key => $value\n"); > 1; > }); Hehe, but this works: $r->request->headers_out ... So headers_out is located beneath the request sub-object (or however you like to call it). Unfortunately this is not clearly documented (I read the online book, chapter 7 and 9). I feel, that some methods can be accessed directly from top (as headers_in()), others not (like headers_out) :-( Maybe this should be fixed or documented, otherwise somebody tell me please, if I am completly wrong :-) Greetings, ~~~:-Jochen -- After a number of decimal places, nobody gives a damn.
Re: which Handler to adjust Response-Headers?
On Wed, Apr 18, 2001 at 12:09:57PM -0700, Rob Bloodgood wrote: > > I wonder, which Handler I would have to invoke to mangle or adjust the > > response headers. I thought, this was the PerlFixupHandler. But > > $r->headers_out seems to be completly empty. > > You can do this at any phase of the request, up to and including the content > handler. this makes no sense for me. Adjusting response field require a server-generated response before. Adjusting meens: Read the fields, and if some condition is true, change the fields. > > use strict; > > use Apache::Constants; > > use Apache::Table; > > > > sub handler { > > my $r = shift; > > $r->header_out( Sample => "value" ); > $r->header_out( Cookie => "$cookie"); > # etc... This is good for setting. If I read them first, they are empty. When are those set by the server? > Use $r->header_out for individual headers. $r->headers_out is an > Apache::Table, which is probably not what you are looking to mess with. This should not be a problem, it is just another way to do it. Besides, $r->header_out is not solving my problem :-) Any more ideas? Thanks, ~~~:-Jochen -- A language that doesn't affect the way you think about programming is not worth knowing.
which Handler to adjust Response-Headers?
Hi. I wonder, which Handler I would have to invoke to mangle or adjust the response headers. I thought, this was the PerlFixupHandler. But $r->headers_out seems to be completly empty. This is my handler: use strict; use Apache::Constants; use Apache::Table; sub handler { my $r = shift; # this does not work, it's empty: $r->headers_out->do(sub { my($key, $value) = @_; $r->log_error("$key => $value\n"); 1; }); # this works fine: my $status = $r->status(); $r->log_error("Status: $status"); } 1; __END__ Also, headers_in works fine. How do I get the Response-Headers, the server would normally send to the client, to modify them. Thanks, ~~~:-Jochen Schnapka -- Tussman's Law: Nothing is as inevitable as a mistake whose time has come.