summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-11-18 03:47:35 +0800
committerGitHub <noreply@github.com>2021-11-17 14:47:35 -0500
commit81a4fc752833101dd7d6b4f612bccc4b29c98dff (patch)
tree3fc851bbdbebf63a3c684791368c33efaf077005 /services
parentd8a8961b99adc1554c218fee474535d4f302bd11 (diff)
downloadgitea-81a4fc752833101dd7d6b4f612bccc4b29c98dff.tar.gz
gitea-81a4fc752833101dd7d6b4f612bccc4b29c98dff.zip
Return 400 but not 500 when request archive with wrong format (#17691)
Diffstat (limited to 'services')
-rw-r--r--services/archiver/archiver.go18
-rw-r--r--services/archiver/archiver_test.go13
2 files changed, 23 insertions, 8 deletions
diff --git a/services/archiver/archiver.go b/services/archiver/archiver.go
index d602b9ed7f..06686220f1 100644
--- a/services/archiver/archiver.go
+++ b/services/archiver/archiver.go
@@ -39,6 +39,22 @@ type ArchiveRequest struct {
// the way to 64.
var shaRegex = regexp.MustCompile(`^[0-9a-f]{4,64}$`)
+// ErrUnknownArchiveFormat request archive format is not supported
+type ErrUnknownArchiveFormat struct {
+ RequestFormat string
+}
+
+// Error implements error
+func (err ErrUnknownArchiveFormat) Error() string {
+ return fmt.Sprintf("unknown format: %s", err.RequestFormat)
+}
+
+// Is implements error
+func (ErrUnknownArchiveFormat) Is(err error) bool {
+ _, ok := err.(ErrUnknownArchiveFormat)
+ return ok
+}
+
// NewRequest creates an archival request, based on the URI. The
// resulting ArchiveRequest is suitable for being passed to ArchiveRepository()
// if it's determined that the request still needs to be satisfied.
@@ -59,7 +75,7 @@ func NewRequest(repoID int64, repo *git.Repository, uri string) (*ArchiveRequest
ext = ".bundle"
r.Type = git.BUNDLE
default:
- return nil, fmt.Errorf("Unknown format: %s", uri)
+ return nil, ErrUnknownArchiveFormat{RequestFormat: uri}
}
r.refName = strings.TrimSuffix(uri, ext)
diff --git a/services/archiver/archiver_test.go b/services/archiver/archiver_test.go
index 67484fdc78..5417dfb939 100644
--- a/services/archiver/archiver_test.go
+++ b/services/archiver/archiver_test.go
@@ -5,6 +5,7 @@
package archiver
import (
+ "errors"
"path/filepath"
"testing"
"time"
@@ -19,10 +20,6 @@ func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."))
}
-func waitForCount(t *testing.T, num int) {
-
-}
-
func TestArchive_Basic(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
@@ -83,11 +80,8 @@ func TestArchive_Basic(t *testing.T) {
inFlight[2] = secondReq
ArchiveRepository(zipReq)
- waitForCount(t, 1)
ArchiveRepository(tgzReq)
- waitForCount(t, 2)
ArchiveRepository(secondReq)
- waitForCount(t, 3)
// Make sure sending an unprocessed request through doesn't affect the queue
// count.
@@ -132,3 +126,8 @@ func TestArchive_Basic(t *testing.T) {
assert.NotEqual(t, zipReq.GetArchiveName(), tgzReq.GetArchiveName())
assert.NotEqual(t, zipReq.GetArchiveName(), secondReq.GetArchiveName())
}
+
+func TestErrUnknownArchiveFormat(t *testing.T) {
+ var err = ErrUnknownArchiveFormat{RequestFormat: "master"}
+ assert.True(t, errors.Is(err, ErrUnknownArchiveFormat{}))
+}