douqin7086 2015-11-30 20:02
浏览 37


Short: How can I read the CGI var REMOTE_USER on golang using fastcgi?

Long: I'm trying to write a program in go to work behind a httpd using fcgi over a socket. The httpd does the ssl termination and provides basic auth. I need to read $REMOTE_USER, but I cannot in golang, while I can in perl.

My code is based on this fcgi example. I try

func homeView(w http.ResponseWriter, r *http.Request) {     
    user, pass, authok := r.BasicAuth()

But authok is always false, user and pass remain empty, although I know for sure that the authorization (done by httpd) was OK. To eliminate other errors, I have done it in perl:

my $socket = FCGI::OpenSocket("/run/fcgi-check.sock", 5);
my $q = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, $socket);

while ($q->Accept() >= 0) {
    my $c = CGI::Simple->new;
    my $user_id      = $c->remote_user(); 

and it works fine in perl.

To debug, I printed the output of r.Header and I got:


Am I right that the header that go sees does no hold any information about any authorization? But it does in perl.

Here is a full but minimal golang code example that demonstrates the problem (on OpenBSD 5.8 with go version go1.4.2 openbsd/amd64 and OpenBSDs httpd with 'authenticate "/" with restricted_users' in httpd.conf.

package main

import (

func homeView(w http.ResponseWriter, r *http.Request) {
        headers := w.Header()
        headers.Add("Content-Type", "text/html")
        headers.Add("Cache-Control", "no-cache, no-store, must-revalidate")
        headers.Add("Pragma", "no-cache")
        headers.Add("Expires", "0")

        user, pass, authok := r.BasicAuth()

        if authok {
                io.WriteString(w, fmt.Sprintln("Auth OK"))
                io.WriteString(w, fmt.Sprintln("user is: "+user+", pass is: "+pass))
        } else {
                io.WriteString(w, fmt.Sprintln("Auth NOT OK"))

func main() {

        r := mux.NewRouter()
        r.HandleFunc("/check/", homeView)

    var err error
        listener, err := net.Listen("unix", "/run/fcgi-check.sock")
        if err != nil {
        defer listener.Close()

        err = fcgi.Serve(listener, r)
        if err != nil { log.Fatal(err)}

Help will be appreciated!

Thanks in advance! T.

  • 写回答

4条回答 默认 最新

  • doutao1282 2015-12-02 07:59

    The simple answer (as of go version 1.4.2) is that go currently does not support the transfer of CGI variable REMOTE_USER.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 思科校园网的组建,sos!
  • ¥15 主要进行描述非满管状态下,管路的摩阻系数是怎么变化的,在管路长度方向上是怎么分布的(标签-matlab)
  • ¥15 multisim设计求解如图
  • ¥15 动态多目标优化,求解定义的问题的POF和POS公式
  • ¥30 实验 :UML2.0 行为建模
  • ¥15 设计一种优化调度算法并结合论文中的案例给出调度计划
  • ¥15 py文件打包exe报错TypeError:expected str,bytes or os.PathLike object,not_io.BytesIO
  • ¥15 Java编写学籍信息管理系统
  • ¥15 IPXE网刻客户机找不到服务端
  • ¥15 小贝360-4 配二个 华772S 设置WⅰFi5G 连接