摸鱼快报:golang net/http中的雕虫小技

aa8bca4097eef46c14262cf08469e782.gif

以后会开一个板块,摸鱼快报,快速记录这几周开发中雕虫小技, 也算一个错题集。

1. 向开发环境localhost:3000种植cookie

前端使用Create React App脚手架,默认以localhost:3000端口启动;
后端使用golang-gin框架,使用8034端口启动。
登录模块走的是sso,前后端分离,后端需要向前端写入认证cookie

c.SetSameSite(http.SameSiteLaxMode)
c.SetCookie("userInfo", userInfoMapString, 60*60*12, "/", cfg.CookieDomain, false, false)
c.SetCookie("access_token", accessToken.(string), 60*60*12, "/", cfg.CookieDomain, false, false)

若种植cookie时设置domain=localhost:3000,实际会发现该cookie被种为domain=localhost

① golang给出日志提示:2023/01/12 19:10:48 net/http: invalid Cookie.Domain "localhost:3000"; dropping domain attribute, 该cookie domain属性被丢弃:

819139179098dbe9a037c5b1f2573f6f.png

② 浏览器认定该cookie没有domain,dimain属性值被重置当前页面domain,该Cookie为HostOnly Cookie,后续请求只有host与cookie的domain完全相等,才能携带这个cookie。

react配置后端地址,要配置为 localhost:8034,而不能是 127.0.0.1:8034 c1f6340317c5829caf1805478af38320.png

经此一役:

f4618292d449a47733b9d18eed3a347a.png
  • • 源(Origin)是由 URL 中协议、主机名(域名 domain)以及端口共同组成的部分

  • • 本次出现的问题在于两个关键cookie属性 :

    • • cookie domain:cookie被种植到哪个域名下?

    • • cookie samesite:请求时,哪些资源能携带该cookie?

2. httpclient timeout报错经验

golang net/http httpclientTimeout:
Timeout specifies a time limit for requests made by this Client. The timeout includes connection time, any redirects, and reading the response body. The timer remains running after Get, Head, Post, or Do return and will interrupt reading of the Response.Body.

HttpClient Timeout包括连接、重定向(如果有)、从Response Body读取的时间,内置定时器会在Get,Head、Post、Do 方法之后继续运行,并有能力中断读取Response.Body.

331ef7d4f6b7b7f20cc7ad42e7936e2e.png

如果upstream服务器处理超时(upstream_response_time> client设置的timeout),则会返回context deadline exceeded (Client.Timeout exceeded while awaiting headers)

如果客户端使用io.ReadAll读取body超时,则会返回context deadline exceeded (Client.Timeout or context cancellation while reading body)

3. url 大小写敏感

大家使用net/http 建立的http server,默认的请求url path是大小写敏感的:

s.mux.HandleFunc("/leader", func(w http.ResponseWriter, r *http.Request) {

}

s.mux.HandleFunc("/LEADER", func(w http.ResponseWriter, r *http.Request) {

}

以上会被认定为不同的路由path。
探究源码:ServeMux使用map[string]muxEntry 哈希表来存储路由。


这与aspnet core的路由行为是不一样的,/hello、/HELLO都会命中下面的路由。

app.UseEndpoints(endpoints =>
 {  
    endpoints.MapGet("/hello", async context =>
      {
         await context.Response.WriteAsync("Hello!");
      });
}

w3c官方建议:url大小写敏感

URLs in general are case-sensitive (with the exception of machine names). There may be URLs, or parts of URLs, where case doesn't matter, but identifying these may not be easy. Users should always consider that URLs are case-sensitive。
大意是说:除了domain主机名是大小写不敏感,url一般被认为是大小写敏感。

stackoverflow有更清晰的描述:

The scheme and host are case-insensitive and normally provided in lowercase; all other components are compared in a case-sensitive manner.

4. golang statuscode被作为header,一直很费解。

在 Go 语言中,客户端请求信息都封装到了Request对象,但是发送给客户端的响应并不是 Response 对象,而是ResponseWriter

func Home(w http.ResponseWriter, r *http.Request)  {
    io.WriteString(w, "Welcome to my blog site")
}

ResponseWriter是处理器用来创建 HTTP 响应的接口,其源码结构如下所示:

type ResponseWriter interface {
   // 用于设置/获取所有响应头信息
    Header() Header
   // 用于写入数据到响应实体
    Write([]byte) (int, error)
   // 用于设置响应状态码
    WriteHeader(statusCode int)
}

WriteHeader这个方法名有点误导,其实它并不是用来设置响应头的,该方法支持传入一个整型数据用来表示响应状态码,如果不调用该方法的话,默认响应状态码是 200 OK。

在fasthttp中,设置请求谓词:req.Header.SetMethod("POST"), 这种将谓词作为header的行为,我也是服气。

只能设置一次statuscode, 若多次设置statuscode,以前者优先。

例如尝试以如下方式:

http.NotFound(w, r)   # 会调用WriteHeader(404);Write()写入body
w.WriteHeader(http.StatusInternalServerError)

会产生一个告警:2023/01/06 19:19:43 http: superfluous response.WriteHeader call from main.ProxyHandler (proxy.go:25), 同时产生404状态码。

可以采用如下方式清晰定义状态码和body

w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, "404 page not found")

本周摸鱼快报,如有更新,请关注左下角[阅读原文]地址。

阅读时间3min;   难度星: 3(满星5星);价值度: 3(满分5分)。

点“”戳“在看

体现态度很有必要!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有态度的马甲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值