當前位置: 首頁>>代碼示例>>Golang>>正文


Golang fasthttp.RequestHeader類代碼示例

本文整理匯總了Golang中github.com/valyala/fasthttp.RequestHeader的典型用法代碼示例。如果您正苦於以下問題:Golang RequestHeader類的具體用法?Golang RequestHeader怎麽用?Golang RequestHeader使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了RequestHeader類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestGetFromContextWithGlobalAndPrivateForwardedIps

func TestGetFromContextWithGlobalAndPrivateForwardedIps(t *testing.T) {
	var ctx iris.Context
	ctx.RequestCtx = &fasthttp.RequestCtx{}
	header := fasthttp.RequestHeader{}
	header.Add("X-Forwarded-For", "65.55.37.104, 100.64.0.0, 192.168.0.0, 192.0.0.0")
	ctx.RequestCtx.Request.Header = header
	ip := GetFromContext(&ctx)

	assert.Equal(t, ip, "65.55.37.104", "Should be the first ip, which is the global ip address.")
}
開發者ID:vaot,項目名稱:user_ip,代碼行數:10,代碼來源:user_ip_test.go

示例2: TestGetFromContextWithPrivateForwadedIp

func TestGetFromContextWithPrivateForwadedIp(t *testing.T) {
	var ctx iris.Context
	ctx.RequestCtx = &fasthttp.RequestCtx{}
	header := fasthttp.RequestHeader{}
	header.Add("X-Forwarded-For", "100.64.0.0, 192.168.0.0")
	ctx.RequestCtx.Request.Header = header
	ip := GetFromContext(&ctx)

	assert.Equal(t, ip, "", "Should not find an ip address.")
}
開發者ID:vaot,項目名稱:user_ip,代碼行數:10,代碼來源:user_ip_test.go

示例3: fetchFromUpstream

func fetchFromUpstream(h *fasthttp.RequestHeader, key []byte) *ybc.Item {
	upstreamUrl := fmt.Sprintf("%s://%s%s", *upstreamProtocol, *upstreamHost, h.RequestURI())
	var req fasthttp.Request
	req.SetRequestURI(upstreamUrl)

	var resp fasthttp.Response
	err := upstreamClient.Do(&req, &resp)
	if err != nil {
		logRequestError(h, "Cannot make request for [%s]: [%s]", key, err)
		return nil
	}

	if resp.StatusCode() != fasthttp.StatusOK {
		logRequestError(h, "Unexpected status code=%d for the response [%s]", resp.StatusCode(), key)
		return nil
	}

	contentType := string(resp.Header.ContentType())
	if contentType == "" {
		contentType = "application/octet-stream"
	}
	body := resp.Body()
	contentLength := len(body)
	itemSize := contentLength + len(contentType) + 1
	txn, err := cache.NewSetTxn(key, itemSize, ybc.MaxTtl)
	if err != nil {
		logRequestError(h, "Cannot start set txn for response [%s], itemSize=%d: [%s]", key, itemSize, err)
		return nil
	}

	if err = storeContentType(h, txn, contentType); err != nil {
		txn.Rollback()
		return nil
	}

	n, err := txn.Write(body)
	if err != nil {
		logRequestError(h, "Cannot read response [%s] body with size=%d to cache: [%s]", key, contentLength, err)
		txn.Rollback()
		return nil
	}
	if n != contentLength {
		logRequestError(h, "Unexpected number of bytes copied=%d from response [%s] to cache. Expected %d", n, key, contentLength)
		txn.Rollback()
		return nil
	}
	item, err := txn.CommitItem()
	if err != nil {
		logRequestError(h, "Cannot commit set txn for response [%s], size=%d: [%s]", key, contentLength, err)
		return nil
	}
	atomic.AddInt64(&stats.BytesReadFromUpstream, int64(n))
	return item
}
開發者ID:valyala,項目名稱:ybc,代碼行數:54,代碼來源:main.go

示例4: logRequestError

func logRequestError(h *fasthttp.RequestHeader, format string, args ...interface{}) {
	msg := fmt.Sprintf(format, args...)
	logMessage("%s - %s - %s. %s", h.RequestURI(), h.Referer(), h.UserAgent(), msg)
}
開發者ID:valyala,項目名稱:ybc,代碼行數:4,代碼來源:main.go

示例5: getRequestHost

func getRequestHost(h *fasthttp.RequestHeader) []byte {
	if *useClientRequestHost {
		return h.Host()
	}
	return upstreamHostBytes
}
開發者ID:valyala,項目名稱:ybc,代碼行數:6,代碼來源:main.go


注:本文中的github.com/valyala/fasthttp.RequestHeader類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。