I'm trying to set an HTTP header like Content-Type
over a CGI script.
In PHP
:
header('Content-Type: text/plain');
// or
echo 'Content-Type: text/plain', "\r\n\r\n"; // as first line
or in Go
:
fmt.Print("Content-Type: text/plain\r\n\r\n") // as first line
Both have no effect on the output.
How can this be done?
EDIT
I also tried the following in Go
, using the CGI
package:
package main
import "fmt"
import "os"
import "net/http/cgi"
func main() {
r,e := cgi.Request()
if e != nil {
fmt.Println(e)
os.Exit(200)
}
fmt.Printf("%#v", r)
os.Exit(200)
}
but I get the error:
cgi: failed to parse REQUEST_URI into a URL:
Question 1:
If your script returns a valid HTTP return code (like
200
) then G-WAN builds the corresponding HTTP Headers unless they are already there (starting with"HTTP/1.x 200 OK"
here).So, to force a given
content-type
with scripted languages (other than those which support the G-WAN API like C, C++, D, and Objective-C) you would have toreturn 1
and define ALL the HTTP headers of your reply.The programming languages that support the G-WAN API can use
get_env(argv, REPLY_MIME_TYPE);
(as shown infractal.c
and others) and let G-WAN build the rest of the headers.Question 2:
The environment variable
REQUEST_URI
(while useful) is not part of the supported CGI v1 specification (RFC-3875). I have requested thatREQUEST_URI
is added in a future release.The script examples provided with G-WAN list the supported variables by v3.12:
Note that you can however access both the request and the parameters (if any) by using the following (and faster) Go code:
UPDATE
We received this source code by email:
Please note that this code is incorrect: it does not even compile - and G-WAN reports the following errors:
This is most probably why you haven't seen the program being "updated": the old version, if any, was not replaced by the faulty version updated while G-WAN was running.
When you develop (editing scripts) you should always have a look at the terminal to check if your newly edited code compiles.
I recommend you to look at the (working)
hello.go
example to see what the requirements are for the expected definition ofmain()
and the (madatory)return code
.When no return code is used (like in your code), G-WAN will inject default HTTP headers (
HTTP/0.9 200 OK
in your case) which will bypass your HTTP headers (if any) and as a result the Internet Browser will wait until it times-out because it does not know the length of your reply.As documented in the examples and in the manual, to tell G-WAN not to create HTTP headers you have to return a value in the
1-99
range (0 means close connection
and200-600 is reserved for HTTP return codes
that tell G-WAN to generate the correspondig HTTP headers).