diff options
author | 6543 <6543@obermui.de> | 2020-11-14 15:05:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-14 14:05:40 +0000 |
commit | 3f3447a1ea8ed0d89ed862b7da506c97030d670e (patch) | |
tree | c01d363dce326efe240c26c8cda2d886a73d28cd /routers/api/v1/utils | |
parent | 374ff60465ace2471d0dde121c6a3e1527ab0bec (diff) | |
download | gitea-3f3447a1ea8ed0d89ed862b7da506c97030d670e.tar.gz gitea-3f3447a1ea8ed0d89ed862b7da506c97030d670e.zip |
API: Fix GetQueryBeforeSince (#13559)
Diffstat (limited to 'routers/api/v1/utils')
-rw-r--r-- | routers/api/v1/utils/utils.go | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/routers/api/v1/utils/utils.go b/routers/api/v1/utils/utils.go index 092ea3dbb6..ad1a136db4 100644 --- a/routers/api/v1/utils/utils.go +++ b/routers/api/v1/utils/utils.go @@ -5,6 +5,7 @@ package utils import ( + "net/url" "strings" "time" @@ -15,28 +16,47 @@ import ( // GetQueryBeforeSince return parsed time (unix format) from URL query's before and since func GetQueryBeforeSince(ctx *context.APIContext) (before, since int64, err error) { - qCreatedBefore := strings.Trim(ctx.Query("before"), " ") - if qCreatedBefore != "" { - createdBefore, err := time.Parse(time.RFC3339, qCreatedBefore) - if err != nil { - return 0, 0, err - } - if !createdBefore.IsZero() { - before = createdBefore.Unix() - } + qCreatedBefore, err := prepareQueryArg(ctx, "before") + if err != nil { + return 0, 0, err + } + + qCreatedSince, err := prepareQueryArg(ctx, "since") + if err != nil { + return 0, 0, err + } + + before, err = parseTime(qCreatedBefore) + if err != nil { + return 0, 0, err } - qCreatedAfter := strings.Trim(ctx.Query("since"), " ") - if qCreatedAfter != "" { - createdAfter, err := time.Parse(time.RFC3339, qCreatedAfter) + since, err = parseTime(qCreatedSince) + if err != nil { + return 0, 0, err + } + return before, since, nil +} + +// parseTime parse time and return unix timestamp +func parseTime(value string) (int64, error) { + if len(value) != 0 { + t, err := time.Parse(time.RFC3339, value) if err != nil { - return 0, 0, err + return 0, err } - if !createdAfter.IsZero() { - since = createdAfter.Unix() + if !t.IsZero() { + return t.Unix(), nil } } - return before, since, nil + return 0, nil +} + +// prepareQueryArg unescape and trim a query arg +func prepareQueryArg(ctx *context.APIContext, name string) (value string, err error) { + value, err = url.PathUnescape(ctx.Query(name)) + value = strings.Trim(value, " ") + return } // GetListOptions returns list options using the page and limit parameters |