/
t3
/
iam-server
Обзор
Документация
Войти
/
t3
/
iam-server
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
internal/api/version.go
40 строк
1 KB
Ivan Shibkikh
fix P2 medium
20 июл 2026, 18:39
20 июл 2026, 18:39
6c5606a
Код
Авторство
О чём код?
package api import ( "errors" "fmt" "net/http" "strconv" "strings" ) // ErrInvalidIfMatch is returned by parseIfMatch when the If-Match header // value is present but syntactically invalid (e.g. not a non-negative integer). // Handlers should return 400 Bad Request for this error. var ErrInvalidIfMatch = errors.New("invalid If-Match header format") // parseIfMatch extracts a version integer from the If-Match header. // Returns: // - (v, true, nil) — header present with a valid non-negative integer version. // - (0, false, nil) — header absent or wildcard ("*"), treated as "no precondition". // - (0, false, ErrInvalidIfMatch) — header present but malformed; caller should return 400. func parseIfMatch(r *http.Request) (int, bool, error) { val := r.Header.Get("If-Match") if val == "" || val == "*" { return 0, false, nil } // Strip optional quotes val = strings.Trim(val, `"`) v, err := strconv.Atoi(val) if err != nil || v < 0 { return 0, false, ErrInvalidIfMatch } return v, true, nil } // setVersionHeaders writes ETag and X-Resource-Version response headers // with the given version value. func setVersionHeaders(w http.ResponseWriter, version int) { w.Header().Set("ETag", fmt.Sprintf(`"%d"`, version)) w.Header().Set("X-Resource-Version", strconv.Itoa(version)) }