doumibi6899 2015-09-23 23:26
浏览 266
已采纳

通过基本身份验证返回HTTP请求,返回401而不是301重定向

Using Go 1.5.1.

When I try to make a request to a site that automatically redirects to HTTPS using Basic Auth I would expect to get a 301 Redirect response, instead I get a 401.

package main

import "net/http"
import "log"

func main() {
    url := "http://aerolith.org/files"
    username := "cesar"
    password := "password"
    req, err := http.NewRequest("GET", url, nil)
    if err != nil {
        log.Println("error", err)
    }
    if username != "" || password != "" {
        req.SetBasicAuth(username, password)
        log.Println("[DEBUG] Set basic auth to", username, password)
    }
    cli := &http.Client{

    }
    resp, err := cli.Do(req)
    if err != nil {
        log.Println("Do error", err)
    }
    log.Println("[DEBUG] resp.Header", resp.Header)
    log.Println("[DEBUG] req.Header", req.Header)
    log.Println("[DEBUG] code", resp.StatusCode)

}

Note that curl returns a 301:

curl -vvv http://aerolith.org/files --user cesar:password

Any idea what could be going wrong?

  • 写回答

1条回答 默认 最新

  • doumubi6784 2015-09-24 05:17
    关注

    A request to http://aerolith.org/files redirects to https://aerolith.org/files (note change from http to https). A request to https://aerolith.org/files redirects to https://aerolith.org/files/ (note addition of trailing /).

    Curl does not follow redirects. Curl prints the 301 status for the redirect from http://aerolith.org/files to https://aerolith.org/files/.

    The Go client follows the two redirects to https://aerolith.org/files/. The request to https://aerolith.org/files/ returns with status 401 because the Go client does not propagate the authorization header through the redirects.

    Requests to https://aerolith.org/files/ from the Go client and Curl return status 200.

    If you want to follow the redirects and auth successfully, set auth header in a CheckRedirect function:

    cli := &http.Client{
        CheckRedirect: func(req *http.Request, via []*http.Request) error {
            if len(via) >= 10 {
                return errors.New("stopped after 10 redirects")
            }
            req.SetBasicAuth(username, password)
            return nil
        }}
    resp, err := cli.Do(req)
    

    If you want to match what Curl does, use a transport directly. The transport does not follow redirects.

    resp, err := http.DefaultTransport.RoundTrip(req)
    

    The application can also use the client CheckRedirect function and a distinguished error to prevent redirects as shown in an answer to How Can I Make the Go HTTP Client NOT Follow Redirects Automatically?. This technique seems to be somewhat popular, but is more complicated than using the transport directly.

    redirectAttemptedError := errors.New("redirect")
    cli := &http.Client{
        CheckRedirect: func(req *http.Request, via []*http.Request) error {
            return redirectAttemptedError
        }}
    resp, err := cli.Do(req)
    if urlError, ok := err.(*url.Error); ok && urlError.Err == redirectAttemptedError {
        // ignore error from check redirect
        err = nil   
    }
    if err != nil {
        log.Println("Do error", err)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试