예제 #1
0
func requestHandler(ctx *fasthttp.RequestCtx) {
	fmt.Fprintf(ctx, "Hello, world!\n\n")

	fmt.Fprintf(ctx, "Request method is %q\n", ctx.Method())
	fmt.Fprintf(ctx, "RequestURI is %q\n", ctx.RequestURI())
	fmt.Fprintf(ctx, "Requested path is %q\n", ctx.Path())
	fmt.Fprintf(ctx, "Host is %q\n", ctx.Host())
	fmt.Fprintf(ctx, "Query string is %q\n", ctx.QueryArgs())
	fmt.Fprintf(ctx, "User-Agent is %q\n", ctx.UserAgent())
	fmt.Fprintf(ctx, "Connection has been established at %s\n", ctx.ConnTime())
	fmt.Fprintf(ctx, "Request has been started at %s\n", ctx.Time())
	fmt.Fprintf(ctx, "Serial request number for the current connection is %d\n", ctx.ConnRequestNum())
	fmt.Fprintf(ctx, "Your ip is %q\n\n", ctx.RemoteIP())

	fmt.Fprintf(ctx, "Raw request is:\n---CUT---\n%s\n---CUT---", &ctx.Request)

	ctx.SetContentType("text/plain; charset=utf8")

	// Set arbitrary headers
	ctx.Response.Header.Set("X-My-Header", "my-header-value")

	// Set cookies
	var c fasthttp.Cookie
	c.SetKey("cookie-name")
	c.SetValue("cookie-value")
	ctx.Response.Header.SetCookie(&c)
}
예제 #2
0
func (s *Selector) Match(ctx *fasthttp.RequestCtx) (string, bool) {
	var matchSlice []byte
	found := false
	switch s.RequestAttr {
	case "IP":
		matchSlice = []byte(ctx.RemoteIP().String())
	case "Method":
		matchSlice = ctx.Method()
	case "Path":
		matchSlice = ctx.Path()
	case "Host":
		matchSlice = ctx.Host()
	case "POST":
		matchSlice = ctx.PostArgs().Peek(s.SubAttr)
	case "GET":
		matchSlice = ctx.QueryArgs().Peek(s.SubAttr)
	case "Param":
		matchSlice = ctx.PostArgs().Peek(s.SubAttr)
		if matchSlice == nil {
			matchSlice = ctx.QueryArgs().Peek(s.SubAttr)
		}
	case "Header":
		matchSlice = ctx.Request.Header.Peek(s.SubAttr)
	default:
		log.Println("unknown request attribute:", s.RequestAttr)
	}
	if matchSlice != nil && (s.Regexp == nil || s.Regexp.Match(matchSlice)) {
		found = true
	}
	if s.Negate {
		found = !found
	}
	return string(matchSlice), found
}
예제 #3
0
func getQueriesCount(ctx *fasthttp.RequestCtx) int {
	n := ctx.QueryArgs().GetUintOrZero("queries")
	if n < 1 {
		n = 1
	} else if n > 500 {
		n = 500
	}
	return n
}
예제 #4
0
파일: expvar.go 프로젝트: valyala/fasthttp
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
}
예제 #5
0
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")
	}
}
예제 #6
0
파일: main.go 프로젝트: Taik/zing-mp3
func zingAlbumHandler(ctx *fasthttp.RequestCtx, params fasthttprouter.Params) {
	zingURL := string(ctx.QueryArgs().Peek("url"))
	log.Info("Zing-mp3 album request",
		"zing_url", zingURL,
	)

	album, err := zing.ParseAlbumData(zingURL)
	if err != nil {
		ctx.SetStatusCode(fasthttp.StatusBadRequest)
		fmt.Fprint(ctx, err)
		return
	}

	job, err := newAlbumJob(album, ctx.Response.BodyWriter())
	if err != nil {
		ctx.SetStatusCode(fasthttp.StatusInternalServerError)
		fmt.Fprint(ctx, err)
		return
	}

	ctx.SetStatusCode(fasthttp.StatusOK)
	job.Run()
}