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


Golang fasthttp.RequestCtx類代碼示例

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


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

示例1: RoundTrip

// RoundTrip implements http.RoundTripper.RoundTrip.
func (binder FastBinder) RoundTrip(stdreq *http.Request) (*http.Response, error) {
	var fastreq fasthttp.Request

	convertRequest(stdreq, &fastreq)

	var ctx fasthttp.RequestCtx

	ctx.Init(&fastreq, nil, nil)

	if stdreq.ContentLength >= 0 {
		ctx.Request.Header.SetContentLength(int(stdreq.ContentLength))
	} else {
		ctx.Request.Header.Add("Transfer-Encoding", "chunked")
	}

	if stdreq.Body != nil {
		b, err := ioutil.ReadAll(stdreq.Body)
		if err == nil {
			ctx.Request.SetBody(b)
		}
	}

	binder.handler(&ctx)

	return convertResponse(stdreq, &ctx.Response), nil
}
開發者ID:stellar,項目名稱:bridge-server,代碼行數:27,代碼來源:binder.go

示例2: ExpvarHandler

// ExpvarHandler dumps json representation of expvars to http response.
//
// Expvars may be filtered by regexp provided via 'r' query argument.
//
// See https://golang.org/pkg/expvar/ for details.
func ExpvarHandler(ctx *fasthttp.RequestCtx) {
	expvarHandlerCalls.Add(1)

	ctx.Response.Reset()

	r, err := getExpvarRegexp(ctx)
	if err != nil {
		expvarRegexpErrors.Add(1)
		fmt.Fprintf(ctx, "Error when obtaining expvar regexp: %s", err)
		ctx.SetStatusCode(fasthttp.StatusBadRequest)
		return
	}

	fmt.Fprintf(ctx, "{\n")
	first := true
	expvar.Do(func(kv expvar.KeyValue) {
		if r.MatchString(kv.Key) {
			if !first {
				fmt.Fprintf(ctx, ",\n")
			}
			first = false
			fmt.Fprintf(ctx, "\t%q: %s", kv.Key, kv.Value)
		}
	})
	fmt.Fprintf(ctx, "\n}\n")

	ctx.SetContentType("application/json; charset=utf-8")
}
開發者ID:valyala,項目名稱:fasthttp,代碼行數:33,代碼來源:expvar.go

示例3: serveWebsocket

func (rp *FastReverseProxy) serveWebsocket(dstHost string, reqData *RequestData, ctx *fasthttp.RequestCtx) {
	req := &ctx.Request
	uri := req.URI()
	uri.SetHost(dstHost)
	dstConn, err := rp.dialFunc(dstHost)
	if err != nil {
		log.LogError(reqData.String(), string(uri.Path()), err)
		return
	}
	if clientIP, _, err := net.SplitHostPort(ctx.RemoteAddr().String()); err == nil {
		if prior := string(req.Header.Peek("X-Forwarded-For")); len(prior) > 0 {
			clientIP = prior + ", " + clientIP
		}
		req.Header.Set("X-Forwarded-For", clientIP)
	}
	_, err = req.WriteTo(dstConn)
	if err != nil {
		log.LogError(reqData.String(), string(uri.Path()), err)
		return
	}
	ctx.Hijack(func(conn net.Conn) {
		defer dstConn.Close()
		defer conn.Close()
		errc := make(chan error, 2)
		cp := func(dst io.Writer, src io.Reader) {
			_, err := io.Copy(dst, src)
			errc <- err
		}
		go cp(dstConn, conn)
		go cp(conn, dstConn)
		<-errc
	})
}
開發者ID:txrxio,項目名稱:planb,代碼行數:33,代碼來源:fasthttp.go

示例4: SetPost

// SetPost is the API method for creating a new post.
func SetPost(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
	v := &struct {
		Title string
		Body  string
	}{}
	err := json.Unmarshal(ctx.PostBody(), v)
	if err != nil {
		ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
		return
	}
	p := model.NewPost(v.Title, v.Body)
	id, err := p.Set(s)
	if err != nil {
		ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
		return
	}
	ctx.Response.Header.Set("Content-Type", "application/json")
	resp, err := json.MarshalIndent(map[string]string{
		"id": string(id),
	}, "", "  ")
	if err != nil {
		ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
	}
	ctx.Write(resp)
}
開發者ID:bentranter,項目名稱:lazyblog,代碼行數:26,代碼來源:handler.go

示例5: TestRequest

func TestRequest(t *testing.T) {
	ctx := new(fast.RequestCtx)
	url, _ := url.Parse("http://github.com/labstack/echo")
	ctx.Init(&fast.Request{}, fakeAddr{addr: "127.0.0.1"}, nil)
	ctx.Request.Read(bufio.NewReader(bytes.NewBufferString(test.MultipartRequest)))
	ctx.Request.SetRequestURI(url.String())
	test.RequestTest(t, NewRequest(ctx, log.New("echo")))
}
開發者ID:ZloyDyadka,項目名稱:echo,代碼行數:8,代碼來源:request_test.go

示例6: NewRequest

// NewRequest returns `Request` instance.
func NewRequest(c *fasthttp.RequestCtx, l *log.Logger) *Request {
	return &Request{
		RequestCtx: c,
		url:        &URL{URI: c.URI()},
		header:     &RequestHeader{RequestHeader: &c.Request.Header},
		logger:     l,
	}
}
開發者ID:efimovalex,項目名稱:EventKitAPI,代碼行數:9,代碼來源:request.go

示例7: ClientFormFastHandler

// ClientFormFastHandler 客戶端表單信息(基於fasthttp)
func ClientFormFastHandler(ctx *fasthttp.RequestCtx) (clientID, clientSecret string, err error) {
	clientID = string(ctx.FormValue("client_id"))
	clientSecret = string(ctx.FormValue("client_secret"))
	if clientID == "" || clientSecret == "" {
		err = ErrAuthorizationFormInvalid
	}
	return
}
開發者ID:Curtis-ly,項目名稱:oauth2,代碼行數:9,代碼來源:authorize.go

示例8: getQueriesCount

func getQueriesCount(ctx *fasthttp.RequestCtx) int {
	n := ctx.QueryArgs().GetUintOrZero("queries")
	if n < 1 {
		n = 1
	} else if n > 500 {
		n = 500
	}
	return n
}
開發者ID:tussion,項目名稱:FrameworkBenchmarks,代碼行數:9,代碼來源:hello.go

示例9: GetPostJSON

// GetPostJSON is the API method for getting a post's JSON.
func GetPostJSON(ctx *fasthttp.RequestCtx, ps fasthttprouter.Params) {
	id := ps.ByName("id")
	postJSON, err := model.GetJSON([]byte(id), s)
	if err != nil {
		ctx.Error(err.Error(), fasthttp.StatusNotFound)
		return
	}
	ctx.Response.Header.Set("Content-Type", "application/json")
	ctx.Write(postJSON)
}
開發者ID:bentranter,項目名稱:lazyblog,代碼行數:11,代碼來源:handler.go

示例10: CheckHost

// CheckHost Implement a CheckHost method on our new type
func (hs HostSwitch) CheckHost(ctx *fasthttp.RequestCtx) {
	// Check if a http.Handler is registered for the given host.
	// If yes, use it to handle the request.
	if handler := hs[string(ctx.Host())]; handler != nil {
		handler(ctx)
	} else {
		// Handle host names for wich no handler is registered
		ctx.Error("Forbidden", 403) // Or Redirect?
	}
}
開發者ID:Taik,項目名稱:zing-mp3,代碼行數:11,代碼來源:hosts.go

示例11: getExpvarRegexp

func getExpvarRegexp(ctx *fasthttp.RequestCtx) (*regexp.Regexp, error) {
	r := string(ctx.QueryArgs().Peek("r"))
	if len(r) == 0 {
		r = "."
	}
	rr, err := regexp.Compile(r)
	if err != nil {
		return nil, fmt.Errorf("cannot parse r=%q: %s", r, err)
	}
	return rr, nil
}
開發者ID:valyala,項目名稱:fasthttp,代碼行數:11,代碼來源:expvar.go

示例12: requestHandler

// Main request handler
func requestHandler(ctx *fasthttp.RequestCtx) {
	path := ctx.Path()
	switch {
	case bytes.HasPrefix(path, imgPrefix):
		imgHandler(ctx)
	case bytes.HasPrefix(path, cssPrefix):
		cssHandler(ctx)
	default:
		filesHandler(ctx)
	}
}
開發者ID:ty2,項目名稱:fasthttp,代碼行數:12,代碼來源:fs_handler_example_test.go

示例13: TestExpvarHandlerRegexp

func TestExpvarHandlerRegexp(t *testing.T) {
	var ctx fasthttp.RequestCtx
	ctx.QueryArgs().Set("r", "cmd")
	ExpvarHandler(&ctx)
	body := string(ctx.Response.Body())
	if !strings.Contains(body, `"cmdline"`) {
		t.Fatalf("missing 'cmdline' expvar")
	}
	if strings.Contains(body, `"memstats"`) {
		t.Fatalf("unexpected memstats expvar found")
	}
}
開發者ID:stellar,項目名稱:bridge-server,代碼行數:12,代碼來源:expvar_test.go

示例14: ServeHTTP

// ServerHTTP writes a redirect to an HTTP response
func (r *ProxyHandler) ServeHTTP(ctx *fasthttp.RequestCtx) {
	proxyClient := &fasthttp.HostClient{
		Addr: r.url,
		// set other options here if required - most notably timeouts.
	}

	req := &ctx.Request
	resp := &ctx.Response
	if err := proxyClient.Do(req, resp); err != nil {
		ctx.Logger().Printf("error when proxying the request: %s", err)
		ctx.SetStatusCode(fasthttp.StatusServiceUnavailable)
	}
}
開發者ID:tpbowden,項目名稱:swarm-ingress-router,代碼行數:14,代碼來源:proxy_handler.go

示例15: HandleTest

func HandleTest(ctx *fasthttp.RequestCtx) {
	reader, err := os.Open("../testimages/loadimage/test.jpg")
	if err != nil {
		return
	}
	// Remember to free resources after you're done
	defer reader.Close()

	buffer := bytes.NewBuffer([]byte{})
	buffer.ReadFrom(reader)
	blob := buffer.Bytes()

	ctx.Write(blob)
}
開發者ID:phzfi,項目名稱:RIC,代碼行數:14,代碼來源:loadimageweb_test.go


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