diff options
| author | Ryan Mehri <[email protected]> | 2020-05-10 11:31:58 -0600 |
|---|---|---|
| committer | Ryan Mehri <[email protected]> | 2020-05-10 11:31:58 -0600 |
| commit | f3f04a2c09bb42ee0892c235a0facfa3014c5420 (patch) | |
| tree | e15df2eaeb78f2fa52836330cb4c09cb42d152f3 | |
| parent | Merge pull request #8 from jackyzha0/react (diff) | |
| download | ctrl-v-f3f04a2c09bb42ee0892c235a0facfa3014c5420.tar.xz ctrl-v-f3f04a2c09bb42ee0892c235a0facfa3014c5420.zip | |
Check for size of content and expiry time comparison
| -rw-r--r-- | backend/api/routes.go | 1 | ||||
| -rw-r--r-- | backend/db/db.go | 20 |
2 files changed, 19 insertions, 2 deletions
diff --git a/backend/api/routes.go b/backend/api/routes.go index ef8ccf7..a65c886 100644 --- a/backend/api/routes.go +++ b/backend/api/routes.go @@ -32,6 +32,7 @@ func insertFunc(w http.ResponseWriter, r *http.Request) { // insert content err := db.New(ip, content, expiry, title) if err != nil { + w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "got err: %s", err.Error()) } } diff --git a/backend/db/db.go b/backend/db/db.go index 4f638fd..4451f34 100644 --- a/backend/db/db.go +++ b/backend/db/db.go @@ -24,11 +24,27 @@ func init() { initSessions(mUser, mPass, mIP) } +const TitleLimit = 100 +const ContentLimit = 100000 + // creates a new paste with title, content and hash func New(ip, content, expiry, title string) error { // generate hash from ip hash := hashing.GenerateURI(ip) + // check for size of title and content + errs := "" + if len(title) > TitleLimit { + errs += fmt.Sprintf("title is longer than character limit of %d\n", TitleLimit) + } + if len(content) > ContentLimit { + errs += fmt.Sprintf("content is longer than character limit of %d\n", ContentLimit) + } + // if any errors were found + if errs != "" { + return fmt.Errorf(errs) + } + // create new struct new := Paste{ Hash: hash, @@ -46,8 +62,8 @@ func New(ip, content, expiry, title string) error { } // time is in the past - if t.After(time.Now()) { - return fmt.Errorf("err: time %s is in the past", t.String()) + if time.Now().After(t) { + return fmt.Errorf("time %s is in the past", t.String()) } new.Expiry = t |