| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package frontend | 5 package frontend |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "fmt" | 9 "fmt" |
| 10 "html/template" | 10 "html/template" |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 | 218 |
| 219 // shortHash abbriviates a git hash into 6 characters. | 219 // shortHash abbriviates a git hash into 6 characters. |
| 220 func shortHash(s string) string { | 220 func shortHash(s string) string { |
| 221 if len(s) > 6 { | 221 if len(s) > 6 { |
| 222 return s[0:6] | 222 return s[0:6] |
| 223 } | 223 } |
| 224 return s | 224 return s |
| 225 } | 225 } |
| 226 | 226 |
| 227 // GetLimit extracts the "limit", "numbuilds", or "num_builds" http param from | 227 // GetLimit extracts the "limit", "numbuilds", or "num_builds" http param from |
| 228 // the request, or returns "-1" implying no limit was specified. | 228 // the request, or returns def implying no limit was specified. |
| 229 func GetLimit(r *http.Request) (int, error) { | 229 func GetLimit(r *http.Request, def int) int { |
| 230 sLimit := r.FormValue("limit") | 230 sLimit := r.FormValue("limit") |
| 231 if sLimit == "" { | 231 if sLimit == "" { |
| 232 sLimit = r.FormValue("numbuilds") | 232 sLimit = r.FormValue("numbuilds") |
| 233 if sLimit == "" { | 233 if sLimit == "" { |
| 234 sLimit = r.FormValue("num_builds") | 234 sLimit = r.FormValue("num_builds") |
| 235 if sLimit == "" { | 235 if sLimit == "" { |
| 236 » » » » return -1, nil | 236 » » » » return def |
| 237 } | 237 } |
| 238 } | 238 } |
| 239 } | 239 } |
| 240 limit, err := strconv.Atoi(sLimit) | 240 limit, err := strconv.Atoi(sLimit) |
| 241 » if err != nil { | 241 » if err != nil || limit < 0 { |
| 242 » » return -1, fmt.Errorf("limit parameter value %q is not a number:
%s", sLimit, err) | 242 » » return def |
| 243 } | 243 } |
| 244 » if limit < 0 { | 244 » return limit |
| 245 » » return -1, fmt.Errorf("limit parameter value %q is less than 0",
sLimit) | |
| 246 » } | |
| 247 » return limit, nil | |
| 248 } | 245 } |
| 249 | 246 |
| 250 // pagedURL returns a self URL with the given cursor and limit paging options. | 247 // pagedURL returns a self URL with the given cursor and limit paging options. |
| 251 // if limit is set to 0, then inherit whatever limit is set in request. If | 248 // if limit is set to 0, then inherit whatever limit is set in request. If |
| 252 // both are unspecified, then limit is omitted. | 249 // both are unspecified, then limit is omitted. |
| 253 func pagedURL(r *http.Request, limit int, cursor string) string { | 250 func pagedURL(r *http.Request, limit int, cursor string) string { |
| 254 if limit == 0 { | 251 if limit == 0 { |
| 255 » » var err error | 252 » » limit = GetLimit(r, -1) |
| 256 » » limit, err = GetLimit(r) | |
| 257 » » if err != nil { | |
| 258 » » » // This should not happen because the handler should've
already validated the | |
| 259 » » » // limit earlier in the process. | |
| 260 » » » panic(err) | |
| 261 » » } | |
| 262 if limit < 0 { | 253 if limit < 0 { |
| 263 limit = 0 | 254 limit = 0 |
| 264 } | 255 } |
| 265 } | 256 } |
| 266 values := r.URL.Query() | 257 values := r.URL.Query() |
| 267 switch cursor { | 258 switch cursor { |
| 268 case "EMPTY": | 259 case "EMPTY": |
| 269 values.Del("cursor") | 260 values.Del("cursor") |
| 270 case "": | 261 case "": |
| 271 // Do nothing, just leave the cursor in. | 262 // Do nothing, just leave the cursor in. |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 c.Context = withRequest(c.Context, c.Request) | 363 c.Context = withRequest(c.Context, c.Request) |
| 373 next(c) | 364 next(c) |
| 374 } | 365 } |
| 375 | 366 |
| 376 func getRequest(c context.Context) *http.Request { | 367 func getRequest(c context.Context) *http.Request { |
| 377 if req, ok := c.Value(&requestKey).(*http.Request); ok { | 368 if req, ok := c.Value(&requestKey).(*http.Request); ok { |
| 378 return req | 369 return req |
| 379 } | 370 } |
| 380 panic("No http.request found in context") | 371 panic("No http.request found in context") |
| 381 } | 372 } |
| OLD | NEW |