diff --git a/internal/server/request_get.go b/internal/server/request_get.go index 25b20e8..c16739f 100644 --- a/internal/server/request_get.go +++ b/internal/server/request_get.go @@ -5,6 +5,7 @@ import ( "github.com/darmiel/yaxc/internal/common" "github.com/gofiber/fiber/v2" "github.com/muesli/termenv" + "net/http" "strings" ) @@ -13,7 +14,7 @@ func (s *yAxCServer) handleGetAnywhere(ctx *fiber.Ctx) (err error) { // validate path if !common.ValidateAnywherePath(path) { - return ctx.Status(400).SendString("ERROR: Invalid path") + return fiber.NewError(http.StatusNotAcceptable, "invalid anywhere-path") } var res string @@ -24,7 +25,7 @@ func (s *yAxCServer) handleGetAnywhere(ctx *fiber.Ctx) (err error) { // Encryption if q := ctx.Query("secret"); q != "" { if !s.EnableEncryption { - return errEncryptionNotEnabled + return fiber.NewError(http.StatusLocked, "encryption is currently not enabled on this server") } // do not fail on error if encrypt, err := common.Decrypt(res, q); err == nil { diff --git a/internal/server/request_post.go b/internal/server/request_post.go index f4c5a40..3fe8916 100644 --- a/internal/server/request_post.go +++ b/internal/server/request_post.go @@ -7,6 +7,7 @@ import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/utils" "github.com/muesli/termenv" + "net/http" "strings" "time" ) @@ -31,13 +32,13 @@ func (s *yAxCServer) handlePostAnywhereWithHash(ctx *fiber.Ctx) (err error) { func (s *yAxCServer) setAnywhereWithHash(ctx *fiber.Ctx, path, hash string) (err error) { // validate path if !common.ValidateAnywherePath(path) { - return ctx.Status(400).SendString("ERROR: Invalid path") + return fiber.NewError(http.StatusNotAcceptable, "invalid anywhere-path") } // Read content bytes := ctx.Body() if s.MaxBodyLength > 0 && len(bytes) > s.MaxBodyLength { - return s.errBodyLen + return fiber.NewError(http.StatusRequestEntityTooLarge, "exceeded max body length") } content := string(bytes) @@ -45,26 +46,26 @@ func (s *yAxCServer) setAnywhereWithHash(ctx *fiber.Ctx, path, hash string) (err ttl := s.DefaultTTL if q := ctx.Query("ttl"); q != "" { if ttl, err = time.ParseDuration(q); err != nil { - return + return fiber.NewError(http.StatusUnprocessableEntity, "invalid ttl. (examples: 10s, 5m, 1h)") } } // Encryption if q := ctx.Query("secret"); q != "" { if !s.EnableEncryption { - return errEncryptionNotEnabled + return fiber.NewError(http.StatusLocked, "encryption is currently not enabled on this server") } // fail on error encrypt, err := common.Encrypt(content, q) if err != nil { - return err + return fiber.NewError(http.StatusInternalServerError, "error encrypting content: "+err.Error()) } content = string(encrypt) } // Check if ttl is valid if (s.MinTTL != 0 && s.MinTTL > ttl) || (s.MaxTTL != 0 && s.MaxTTL < ttl) { - return ctx.Status(400).SendString("ERROR: TTL out of range") + return fiber.NewError(http.StatusRequestedRangeNotSatisfiable, "ttl out of range") } // generate hash diff --git a/internal/server/type.go b/internal/server/type.go index 6557cf8..d65d050 100644 --- a/internal/server/type.go +++ b/internal/server/type.go @@ -45,15 +45,13 @@ type YAxCConfig struct { type yAxCServer struct { *YAxCConfig - App *fiber.App - Backend Backend - errBodyLen error + App *fiber.App + Backend Backend } func NewServer(cfg *YAxCConfig) (s *yAxCServer) { s = &yAxCServer{ YAxCConfig: cfg, - errBodyLen: errors.New("exceeded max body length"), } // backend diff --git a/swagger.yml b/swagger.yml new file mode 100644 index 0000000..925221e --- /dev/null +++ b/swagger.yml @@ -0,0 +1,68 @@ +swagger: "2.0" + +info: + description: "YAxC - Yet Another Cross Clipboard" + version: "1.1.0" + title: "YAxC" +host: "yaxc.d2a.io" +basePath: "/" + +tags: + - name: "{anywhere}" + description: "Public Clipboard Name" + +schemes: + - "https" + - "http" + +paths: + /{anywhere}: + post: + tags: + - "{anywhere}" + summary: Post new clipboard data + consumes: + - 'text/plain' + produces: + - 'text/plain' + parameters: + - in: "secret" + name: "Encryption Password" + description: "Encrypts the content with the password (server-side!)" + required: false + - in: "ttl" + name: "TTL" + description: "Waiting time after which the content is deleted" + responses: + "200": + description: "OK" + "406": + description: "Invalid Path" + "413": + description: "Body too large" + "416": + description: "TTL out of range" + "422": + description: "Invalid TTL. (Examples: 10s, 5m, 1h)" + "423": + description: "Encryption password was specified, but encryption is not enabled on the server" + "500": + description: "Intenral server error; e.g. Encryption failed (server-side)" + get: + tags: + - "{anywhere}" + summary: Get clipboard data + produces: + - 'text/plain' + parameters: + - in: "secret" + name: "Encryption Password" + description: "Decrypts the contents with the password (server-side!)" + required: false + responses: + "200": + description: "OK" + "406": + description: "Invalid Path" + "423": + description: "Encryption password was specified, but encryption is not enabled on the server" \ No newline at end of file