php – G-WAN,从CGI脚本输出标头
我正在尝试通过CGI脚本设置类似Content-Type的HTTP标头.
在PHP中: header('Content-Type: text/plain'); // or echo 'Content-Type: text/plain',"rnrn"; // as first line 或在Go: fmt.Print("Content-Type: text/plainrnrn") // as first line 两者都对输出没有影响. 如何才能做到这一点? 编辑 我还使用CGI包在Go中尝试了以下内容: 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) } 但我得到错误: cgi: failed to parse REQUEST_URI into a URL: 解决方法
问题1:
如果您的脚本返回有效的HTTP返回代码(如200),则G-WAN会构建相应的HTTP标头,除非它们已经存在(此处以“HTTP / 1.x 200 OK”开头). 因此,要使用脚本语言强制给定内容类型(除了那些支持G-WAN API的那些语言,如C,C,D和Objective-C),您必须返回1并定义回复的所有HTTP头. 支持G-WAN API的编程语言可以使用get_env(argv,REPLY_MIME_TYPE); (如fractal.c等中所示)并让G-WAN构建其余的头文件. 问题2: 环境变量REQUEST_URI(虽然有用)不是受支持的CGI v1 specification(RFC-3875)的一部分.我已经请求在将来的版本中添加REQUEST_URI. 随G-WAN提供的脚本示例列出了v3.12支持的变量: // ---------------------------------------------------------------------------- // CGI/1.1 environment variables: // ---------------------------------------------------------------------------- // "AUTH_TYPE",// "" | "Basic" | "Digest" | etc. // "CONTENT_LENGTH",// "" | entity_length // "CONTENT_TYPE",// "" | content_type // "GATEWAY_INTERFACE",// "CGI/1.1" // "PATH_INFO",// "" | ( "/" path ) // "PATH_TRANSLATED",// disk filename for PATH_INFO // "QUERY_STRING",// "" | ?"hellox.c&name=toto" // "REMOTE_ADDR",// client IP address // "REMOTE_HOST",// client DNS name (or IP addr) // "REMOTE_IDENT",// client identity (RFC 1413),opt // "REMOTE_USER",// client identity (if auth) // "REQUEST_METHOD",// "GET" | "HEAD" | "PUT",etc. // "SCRIPT_NAME",// "" | ("/" path "hello.c") // "SERVER_NAME",// "gwan.com" | IP address // "SERVER_PORT",// "80" // "SERVER_PROTOCOL",// "HTTP/1.1" | "HTTP/1.0" | "HTTP/0.9" // "SERVER_SOFTWARE",// "G-WAN" // ---------------------------------------------------------------------------- 请注意,您可以使用以下(和更快)Go代码访问请求和参数(如果有): // args[1] /opt/gwan/10.10.20.80_80/#192.168.200.80/csp/hello.go // args[2] arg1=123 // args[3] arg2=456 for i := 1; i < len(os.Args); i++ { fmt.Printf("args[%d] %s<br>",i,os.Args[i]) } UPDATE 我们通过电子邮件收到了此源代码: package main import "fmt" import "os" func main() { p := "<h1>Hello world!</h1><p>This is dog bla</p>" fmt.Printf("%s 200 OKrn",os.Getenv("SERVER_PROTOCOL")) fmt.Print("Content-Type: text/html; charset=UTF-8rn") fmt.Print("Connection: Keep-Alivern") fmt.Printf("Content-Length: %drn",len(p)) fmt.Print("rn") fmt.Print(p) } 请注意,此代码不正确:它甚至无法编译 – 并且G-WAN报告以下错误: loading. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Error: hell.go ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # command-line-arguments 0.0.0.0_8080/#0.0.0.0/csp/hell.go:7: syntax error: unexpected semicolon or newline before { 0.0.0.0_8080/#0.0.0.0/csp/hell.go:9: non-declaration statement outside function body 0.0.0.0_8080/#0.0.0.0/csp/hell.go:10: non-declaration statement outside function body 0.0.0.0_8080/#0.0.0.0/csp/hell.go:11: non-declaration statement outside function body 0.0.0.0_8080/#0.0.0.0/csp/hell.go:12: non-declaration statement outside function body 0.0.0.0_8080/#0.0.0.0/csp/hell.go:13: non-declaration statement outside function body 0.0.0.0_8080/#0.0.0.0/csp/hell.go:14: non-declaration statement outside function body 0.0.0.0_8080/#0.0.0.0/csp/hell.go:16: syntax error: unexpected } 4|import "os" 5| 6|func main() 7!{ 8| p := "<h1>Hello world!</h1><p>This is dog bla</p>" 9| fmt.Printf("%s 200 OKrn",os.Getenv("SERVER_PROTOCOL")) 10| fmt.Print("Content-Type: text/html; charset=UTF-8rn") 11| fmt.Print("Connection: Keep-Alivern") To run G-WAN,you must fix the error(s) or remove this Servlet. 这很可能是您没有看到程序被“更新”的原因:旧版本(如果有的话)没有被G-WAN运行时更新的故障版本所取代. 在开发(编辑脚本)时,您应该始终查看终端以检查新编辑的代码是否编译. 我建议你看看(工作)hello.go示例,看看对main()和(madatory)返回代码的预期定义有什么要求. 如果没有使用返回代码(如代码中所示),G-WAN将注入默认的HTTP标头(在您的情况下为HTTP / 0.9 200 OK),这将绕过您的HTTP标头(如果有的话),因此Internet浏览器将等待直到它超时,因为它不知道你的回复的长度. 如示例和手册中所述,为了告诉G-WAN不要创建HTTP头,您必须返回1-99范围内的值(0表示关闭连接,200-600保留用于告知G的HTTP返回码-WAN生成对应的HTTP头). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |