View Full Version : HTTP headers
Mark Johnson
08-15-08, 04:42 AM
A feature that has been heavily requested is the ability to control the HTTP headers that the engine outputs. I've been working on this, but before I actually implement it I'd like to get some feedback on the API that I am proposing.
I plan to implement 2 new built in functions.
get_http_headers( list )
Copies the current set of headers as an array into the variable list. Each header in the array will be a structure with a name element and a value element. Return value will be 0 for failure, 1 for success.
set_http_headers( list )
The array of headers in the variable list will completely replace the current headers. Each header in the array should be a structure with a name element and a value element. Return value will be 0 for failure, 1 for success.
Note:
The "Content-Length" header will not be returned by get_http_headers or accepted by set_http_headers. It will be generated automatically as the last of the http headers.
These routines can be called at any time during the execution of the Miva Script program (i.e. right up to the end).
I'd like to hear whether this is sufficiently flexible and powerful to do what everyone needs.
aGorilla
08-15-08, 05:30 AM
a) It sounds wonderful.
b) Thanks for asking.
c) I'm a fan of short names, I'd probably drop the 'http_' part.
4TheBest.com
08-15-08, 05:46 AM
The two functions, get_http_headers( list ), and set_http_headers( list ), sound like a nice addition to the current miva_output_header(name,value), but it still doesn't address the rather critical issue of status codes. What we need most is a function to let us set the status so that we can force a 301 redirect, rather than the frowned on by Google 302 redirect, that we get currently when setting the header like:
miva_output_header('Location','http://k1e.com/')
We need to be able to do that, or use the new proposed functions, and then use a new function like miva_headerstatus_set(code,reason), that we would use like:
miva_headerstatus_set('301','Moved Permanently')
Though you might want to allow for the reason to be filled in automatically if that parameter is left blank.
That's all, of importance I can think of off the top of my head.
-Jeff Huber
Mark Johnson
08-15-08, 09:25 PM
The two functions, get_http_headers( list ), and set_http_headers( list ), sound like a nice addition to the current miva_output_header(name,value), but it still doesn't address the rather critical issue of status codes. What we need most is a function to let us set the status so that we can force a 301 redirect, rather than the frowned on by Google 302 redirect, that we get currently when setting the header like:
miva_output_header('Location','http://k1e.com/')
We need to be able to do that, or use the new proposed functions, and then use a new function like miva_headerstatus_set(code,reason), that we would use like:
miva_headerstatus_set('301','Moved Permanently')
Though you might want to allow for the reason to be filled in automatically if that parameter is left blank.
That's all, of importance I can think of off the top of my head.
-Jeff Huber
Jeff,
miva_output_header( 'Status', '301 Moved Permanently' )
works for me. Of course it won't fill in the reason automatically, but otherwise fulfills what you want.
eMediaSales
08-16-08, 02:19 AM
Hi Mark,
Actually - the ability to set the actual HTTP/1.x *response* code is I think lacking, not headers. Headers are already there and fully within the control of Miva Script, *even* the Content-Length. Please don't change this. Also check the s.[xyz] variables, I think you'll find all input-headers get set there and we already have access to them.
The status header line being different than the response code itself - where your example above works well for a 301 redirect, it would be rather cool to be able to set the HTTP response line. This would allow for a script to do things like custom 404 errors that get *logged* by the web server as an error, etc.
By default, the output header for any mvc file is:
HTTP/1.1 200 OK
Content-Type: text/html
Set-Cookie: htscallerid=...; expires=... GMT; path=/
....
One *cannot* do this in Miva Script, which if you're up for improvements would be useful:
HTTP/1.1 404 Not Found
Content-Type: text/html
Set-Cookie: htscallerid=...; expires=... GMT; path=/
...
Now, back to content-length. It's important that scripts be able to set this for delivery of non-html content. Currently one CAN set it, please make sure to keep this flexibility. I use this for digital download deliveries. I'm not certain if the built-in calculation would get thrown off by binary content, etc., so if you do take this away from us make sure to test as such.
Best,
James
eMediaSales
08-16-08, 02:24 AM
Following up to my own reply... to summarize: I see no benefit or need for the specific API you're proposing. It's just another way to do things we can already do, and as such, not necessary.
BUT while you're looking at headers and such, allowing different content-type *inputs* without crashing would be worth investigating. ie: xml, etc.
eMediaSales
08-16-08, 02:34 AM
And a step further while you're in there: allow access to the raw post data. PHP does this, and it would be quite useful for finally making PayPal IPN reliable. Consider this - one of the reasons IPN isn't 100% reliable is they expect you to send back *exactly* the same POST they sent IN. But since POST variables simply get made into globals individually before scripts run, it's extremely difficult to determine exactly what variables were POST-ed. An alternative would be to make a $_POST[] and $_GET[] array similar again to PHP so the source of a variable could be easily determined.
One *cannot* do this in Miva Script, which if you're up for improvements would be useful:
HTTP/1.1 404 Not Found
Content-Type: text/html
Set-Cookie: htscallerid=...; expires=... GMT; path=/
This is certainly a problem with some of the SEO methods where it causes the site to fail Google validation using the script technique. If Miva could could return a proper 404 all would be solved.
Mark Johnson
08-16-08, 08:52 AM
Hi Mark,
Actually - the ability to set the actual HTTP/1.x *response* code is I think lacking, not headers.
....
One *cannot* do this in Miva Script, which if you're up for improvements would be useful:
HTTP/1.1 404 Not Found
Content-Type: text/html
Set-Cookie: htscallerid=...; expires=... GMT; path=/
...
Hi James,
You can do that. The Status header is a server parsed header that tells the server what to put on that first line. Try this program:
<MIVA STANDARDOUTPUTLEVEL="">
<MvEVAL EXPR = "{ miva_output_header( 'Status', '404 Cowabunga' ) }">
eMediaSales
08-16-08, 08:01 PM
Hi Mark,
No, not really, though I knew even my clarification wouldn't be that clear. So let me try again.
I believe the "200 OK" server response - the very FIRST line output by the which is actually the response line not a header line (no name: value pair) - is actually built into the engine. Your code produces this response:
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Content-Type: text/html
Set-Cookie: htscallerid=...; expires=... GMT; path=/
Status: 404 Cowabunga
Content-Length: 0
Length: 0 [text/html]
For maximum flexibility, one should be able to output a response line *before* header lines, but today this is impossible. For example, creating this response cannot be done in mivascript, but can be done in other languages:
HTTP request sent, awaiting response...
HTTP/1.1 404 Not Found
Content-Type: text/html
Set-Cookie: htscallerid=...; expires=... GMT; path=/
Status: 404 Cowabunga
Content-Length: ...
This is a custom 404 error message produced by a program...
To summarize, from what I've observed, there is no need for the get_http_headerlist and set_http_headerlist. But there IS the need for the miva_output_header() to accept something like the following to override the built in "200 OK" server response code.
miva_output_header("404 Not Found",l.null)
Best,
Mark Johnson
08-18-08, 08:56 PM
Hi Mark,
No, not really, though I knew even my clarification wouldn't be that clear. So let me try again.
I believe the "200 OK" server response - the very FIRST line output by the which is actually the response line not a header line (no name: value pair) - is actually built into the engine. Your code produces this response:
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Content-Type: text/html
Set-Cookie: htscallerid=...; expires=... GMT; path=/
Status: 404 Cowabunga
Content-Length: 0
Length: 0 [text/html]
For maximum flexibility, one should be able to output a response line *before* header lines, but today this is impossible. For example, creating this response cannot be done in mivascript, but can be done in other languages:
HTTP request sent, awaiting response...
HTTP/1.1 404 Not Found
Content-Type: text/html
Set-Cookie: htscallerid=...; expires=... GMT; path=/
Status: 404 Cowabunga
Content-Length: ...
This is a custom 404 error message produced by a program...
Hi James,
What web server are you running? With Apache 2.2 I get the following output:
HTTP/1.1 404 Cowabunga
Content-Type: text/html
Set-Cookie: htscallerid=...; expires=... GMT; path=/
Content-Length: 1
1
Hi Mark,
To summarize, from what I've observed, there is no need for the get_http_headerlist and set_http_headerlist. But there IS the need for the miva_output_header() to accept something like the following to override the built in "200 OK" server response code.
miva_output_header("404 Not Found",l.null)
Best,
I agree that the get_http_headerlist and set_http_headerlist are not required. Especially if you know that the Content-Type header can be overridden with a miva_output_header call. If you are running ISAPIor Mia, that may be why the Status header is not being processed correctly. I may have to compensate in the engine.
eMediaSales
08-20-08, 07:08 AM
dyuh... whoops.... sorry.... my bad. My tests were run on Miva Mia. Guess I should break out the linux server every now and again. :) I would take a look, though, at the ISAPI and Mia interfaces, it would be nice to be able to test this stuff and have it work the same in those environments.
RayYates
12-19-08, 03:10 PM
I'm trying to follow along but I'm getting confused.
I do get the correct HTTP response header when I use:
<MvASSIGN NAME="l.ok" VALUE="{ miva_output_header( 'Status', '301 Moved Permanently' ) }">
However, this does NOT work. Why?
<MIVA STANDARDOUTPUTLEVEL="">
<MvASSIGN NAME="g.Product_Code" VALUE="{ 20708 }">
<MvASSIGN NAME="l.newlocation" VALUE="{ 'http://mm55.hostasaurus.com/mm5/merchant.mvc?Screen=PROD&Store_Code=pci&Product_Code=' $ g.Product_Code }">
<MvASSIGN NAME="l.ok" VALUE="{ miva_output_header( 'Status', '301 Moved Permanently' ) }">
<MvASSIGN NAME="l.ok" VALUE="{ miva_output_header('Location',l.newlocation) }">
<MvEXIT>
The test program can be found here.
http://mm55.hostasaurus.com/redirect301.mvc
The redirection seems to work BUT I never see HTTP/1.0 301 Moved Permanently in the header.
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.