成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Golang Net/Http中的雕蟲小技

開發 前端
瀏覽器認定該cookie沒有domain,屬性值被重置當前頁面,該Cookie為HostOnly Cookie,后續請求只有host與cookie的domain完全相等,才能攜帶這個cookie。

以后會開一個板塊,摸魚快報,快速記錄這幾周開發中雕蟲小技, 也算一個錯題集。

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屬性被丟棄:

圖片

② 瀏覽器認定該cookie沒有domain,屬性值被重置當前頁面,該Cookie為HostOnly Cookie,后續請求只有host與cookie的domain完全相等,才能攜帶這個cookie。

react配置后端地址,要配置為localhost:8034?,而不能是127.0.0.1:8034?

圖片

經此一役:

圖片

  • ? 源(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.

圖片

如果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")

責任編輯:武曉燕 來源: 精益碼農
相關推薦

2009-07-23 16:20:48

HTTP協議ASP.NET

2021-09-23 14:55:57

.NETHTTP服務器

2009-07-28 15:29:03

實現HTTP請求ASP.NET

2024-02-05 08:50:57

Golang標準庫客戶端

2024-01-29 08:04:48

Golang標準庫服務端

2023-10-22 20:20:37

FiberGo

2021-07-20 10:30:46

Golanghttp語言

2022-07-20 08:04:06

net包DNScontext

2023-11-13 21:55:12

Go編程

2024-11-07 11:46:41

2022-03-07 16:30:10

數據庫ORM開發人員

2024-09-30 08:43:33

HttpgolangTimeout

2023-08-14 08:34:14

GolangHttp

2011-04-13 15:18:10

.htmHTTP請求處理

2022-04-29 11:52:02

API代碼HTTP

2021-10-18 05:00:38

語言GoRequestHTTP

2015-09-15 13:48:01

網絡協議HTTP Client

2022-08-12 12:23:55

golangmap數據結構

2010-02-24 08:59:50

HTTP報頭狀態碼

2023-10-24 16:03:34

GoGolang
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美日韩在线看 | 在线国产一区 | 91视频观看| 午夜激情国产 | 国产精品日韩一区二区 | 成人片网址 | 一区二区三区高清不卡 | 99久久精品国产一区二区三区 | 久久久网 | 瑟瑟视频在线看 | 在线日韩精品视频 | 午夜免费看视频 | 欧美一级在线观看 | 精品一区二区三区不卡 | 欧美日韩国产在线 | 手机在线一区二区三区 | 日本 欧美 三级 高清 视频 | 狠狠色综合久久婷婷 | 91精品国产综合久久婷婷香蕉 | 99精品免费久久久久久久久日本 | 亚洲视频一区在线观看 | 欧美精品成人 | 九九久久这里只有精品 | 黄网站免费在线观看 | 久久国产成人午夜av影院武则天 | 五月婷婷激情 | 99久久免费观看 | 国产成人av在线播放 | 久久国产亚洲 | 国产一区二区 | 国产99久久久国产精品 | 日韩精品一区二 | 国产91视频播放 | 曰批视频在线观看 | 亚洲一区在线观看视频 | 国产91丝袜 | 在线色网 | 国产精品毛片 | 91精品久久久久久久久久入口 | 亚洲网站免费看 | 成年视频在线观看福利资源 |