mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
API downloads: a Range request no longer skips the limits
A request with a Range header was checked only for access, not for the day's downloads or the ratio, and was not counted, so Range: bytes=0- fetched a whole file past the limits uncounted. A Range request is now free only when it resumes the user's download of that file counted in the last day (BBS.Resuming, remembered by Downloaded); any other is a download like the rest, checked and counted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
This commit is contained in:
4 files changed
+109
-9
No files matched your search
@@ -987,3 +987,55 @@ func TestForumSessions(t *testing.T) {
|
||||
t.Errorf("after logout %+v", toks)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRangeDownloadLimits: a Range request is a download like any other,
|
||||
// checked against the day's limit and counted, unless it resumes the
|
||||
// user's download of that file counted today, which is neither.
|
||||
func TestRangeDownloadLimits(t *testing.T) {
|
||||
s := setup(t, Settings{})
|
||||
alice := s.login("alice", "alicepass1")
|
||||
for _, name := range []string{"one.txt", "two.txt"} {
|
||||
if code, m, _ := s.do("POST", "/api/v1/fileareas/1/files?name="+name, alice, []byte("hello, world\n")); code != 201 {
|
||||
t.Fatalf("upload %s: %d %v", name, code, m)
|
||||
}
|
||||
}
|
||||
conf := *s.b.Conf()
|
||||
one := bbs.AllowAll(0, "One a day")
|
||||
one.Downloads = 1
|
||||
conf.Levels = []bbs.Level{one}
|
||||
s.b.Reconfigure(conf)
|
||||
|
||||
get := func(id int, rng string) int {
|
||||
t.Helper()
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/api/v1/files/%d/content", s.ts.URL, id), nil)
|
||||
req.Header.Set("Authorization", "Bearer "+alice)
|
||||
if rng != "" {
|
||||
req.Header.Set("Range", rng)
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
resp.Body.Close()
|
||||
return resp.StatusCode
|
||||
}
|
||||
counted := func() int {
|
||||
u, _ := s.b.Store.UserByName("alice")
|
||||
return u.DownloadsToday()
|
||||
}
|
||||
// part of a file, the first time: a download, counted
|
||||
if code := get(1, "bytes=0-"); code != 206 || counted() != 1 {
|
||||
t.Fatalf("first range: %d, %d counted", code, counted())
|
||||
}
|
||||
// the rest of it: a resumption, neither limited nor counted
|
||||
if code := get(1, "bytes=7-"); code != 206 || counted() != 1 {
|
||||
t.Errorf("resume: %d, %d counted", code, counted())
|
||||
}
|
||||
// another file, whole or in part: over the limit
|
||||
for _, rng := range []string{"", "bytes=0-"} {
|
||||
if code := get(2, rng); code != 403 || counted() != 1 {
|
||||
t.Errorf("another file (%q): %d, %d counted", rng, code, counted())
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
-8
@@ -1036,24 +1036,27 @@ func (s *Server) file(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// download: a file's content (Range works), if the user may download it
|
||||
// and has downloads left today; counted as a download unless it is asked
|
||||
// for by Range.
|
||||
// and has downloads left today, counted as a download. A Range request
|
||||
// resuming a download of the file counted in the last day (BBS.Resuming)
|
||||
// needs no downloads left and is not counted again; any other is a new
|
||||
// download, however little of the file it asks for.
|
||||
func (s *Server) download(w http.ResponseWriter, r *http.Request) {
|
||||
f := s.fileByPath(w, r)
|
||||
if f == nil {
|
||||
return
|
||||
}
|
||||
u := whoOf(r).user
|
||||
resume := r.Header.Get("Range") != "" && s.B.Resuming(u, f)
|
||||
if fa, err := s.B.Store.FileArea(f.AreaID); err != nil {
|
||||
fail(w, http.StatusNotFound, "no such file")
|
||||
return
|
||||
} else if r.Header.Get("Range") == "" {
|
||||
if err := s.B.MayDownload(u, fa); err != nil {
|
||||
fail(w, http.StatusForbidden, err.Error())
|
||||
} else if resume {
|
||||
if !s.B.CanDownload(u, fa) {
|
||||
fail(w, http.StatusForbidden, bbs.ErrNotAllowed.Error())
|
||||
return
|
||||
}
|
||||
} else if !s.B.CanDownload(u, fa) {
|
||||
fail(w, http.StatusForbidden, bbs.ErrNotAllowed.Error())
|
||||
} else if err := s.B.MayDownload(u, fa); err != nil {
|
||||
fail(w, http.StatusForbidden, err.Error())
|
||||
return
|
||||
}
|
||||
fh, err := s.B.Files.OpenFile(f)
|
||||
@@ -1065,7 +1068,7 @@ func (s *Server) download(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="`+f.Name+`"`)
|
||||
w.Header().Set("Digest", "sha-256="+f.SHA256)
|
||||
if r.Header.Get("Range") == "" {
|
||||
if !resume {
|
||||
s.B.Downloaded(u, f)
|
||||
}
|
||||
http.ServeContent(w, r, f.Name, f.Created, fh)
|
||||
|
||||
+43
-1
@@ -283,16 +283,58 @@ func (b *BBS) DownloadsLeft(u *store.User) (int, error) {
|
||||
return left, why
|
||||
}
|
||||
|
||||
// Downloaded counts a download of f by u (u nil: anonymous, as gopher's).
|
||||
// Downloaded counts a download of f by u (u nil: anonymous, as gopher's),
|
||||
// and remembers it for a day so that its resumption (Resuming) is not
|
||||
// counted again.
|
||||
func (b *BBS) Downloaded(u *store.User, f *store.File) {
|
||||
b.Store.CountDownload(f.ID)
|
||||
if u != nil && u.ID != 0 {
|
||||
b.Store.CountUserDownload(u.ID)
|
||||
u.DLCount, u.DLDay = u.DownloadsToday()+1, store.Today()
|
||||
u.Downloads++
|
||||
b.dlMu.Lock()
|
||||
if b.recent == nil {
|
||||
b.recent = map[recentDownload]time.Time{}
|
||||
}
|
||||
now := time.Now()
|
||||
if len(b.recent) >= maxRecentDownloads {
|
||||
for k, t := range b.recent {
|
||||
if now.Sub(t) > resumeWithin {
|
||||
delete(b.recent, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(b.recent) < maxRecentDownloads {
|
||||
b.recent[recentDownload{u.ID, f.ID}] = now
|
||||
}
|
||||
b.dlMu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// Resuming reports whether u's download of f was counted in the last day:
|
||||
// a request for part of it (HTTP Range) then resumes that download, needing
|
||||
// neither downloads left nor counting again. Any other request for part of
|
||||
// a file is a new download.
|
||||
func (b *BBS) Resuming(u *store.User, f *store.File) bool {
|
||||
if u == nil || u.ID == 0 {
|
||||
return false
|
||||
}
|
||||
b.dlMu.Lock()
|
||||
defer b.dlMu.Unlock()
|
||||
t, ok := b.recent[recentDownload{u.ID, f.ID}]
|
||||
return ok && time.Since(t) <= resumeWithin
|
||||
}
|
||||
|
||||
// recentDownload is a user's download of a file, for Resuming.
|
||||
type recentDownload struct{ user, file int64 }
|
||||
|
||||
const (
|
||||
// resumeWithin is how long after a download counts its resumption
|
||||
resumeWithin = 24 * time.Hour
|
||||
// maxRecentDownloads bounds the downloads remembered for resuming
|
||||
maxRecentDownloads = 100000
|
||||
)
|
||||
|
||||
// CanDoor reports whether u may open door d.
|
||||
func (b *BBS) CanDoor(u *store.User, d *door.Door) bool {
|
||||
if u == nil {
|
||||
|
||||
@@ -162,6 +162,9 @@ type BBS struct {
|
||||
|
||||
codesMu sync.Mutex
|
||||
codes map[string]signInCode // sign-in codes by their hash
|
||||
|
||||
dlMu sync.Mutex
|
||||
recent map[recentDownload]time.Time // downloads counted, for resuming
|
||||
// Config edits the configuration file, on the sysop's menu (nil: none)
|
||||
Config *ConfigTools
|
||||
// Doors are the door programs (for text callers)
|
||||
|
||||
Reference in new issue
Block a user