]> Sergey Matveev's repositories - sgblog.git/commitdiff
Proper HTTP status codes v0.23.0
authorSergey Matveev <stargrave@stargrave.org>
Wed, 6 Oct 2021 09:21:25 +0000 (12:21 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 6 Oct 2021 09:21:25 +0000 (12:21 +0300)
cmd/sgblog/http.go
common.go

index 6ec97bb4cc48ca1b1610f32e8f56f641f9ac8d31..f81e38eb4075302fcf730473c9a0389a46e7f179 100644 (file)
@@ -30,6 +30,7 @@ import (
        "html"
        "io"
        "log"
+       "net/http"
        "net/url"
        "os"
        "strconv"
@@ -134,7 +135,8 @@ func startHeader(etag hash.Hash, gziped bool) string {
        return strings.Join(lines, "\n")
 }
 
-func makeErr(err error) {
+func makeErr(err error, status int) {
+       fmt.Println("Status:", status)
        fmt.Print("Content-Type: text/plain; charset=UTF-8\n\n")
        fmt.Println(err)
        log.Fatalln(err)
@@ -175,7 +177,7 @@ func serveHTTP() {
        }
        queryValues, err := url.ParseQuery(os.Getenv("QUERY_STRING"))
        if err != nil {
-               makeErr(err)
+               makeErr(err, http.StatusBadRequest)
        }
 
        etagHash, err := blake2b.New256(nil)
@@ -210,7 +212,7 @@ func serveHTTP() {
 
        headHash, err := initRepo(cfg)
        if err != nil {
-               makeErr(err)
+               makeErr(err, http.StatusInternalServerError)
        }
 
        if notes, err := repo.Notes(); err == nil {
@@ -254,20 +256,20 @@ func serveHTTP() {
                if offsetRaw, exists := queryValues["offset"]; exists {
                        offset, err = strconv.Atoi(offsetRaw[0])
                        if err != nil {
-                               makeErr(err)
+                               makeErr(err, http.StatusBadRequest)
                        }
                }
                repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
                if err != nil {
-                       makeErr(err)
+                       makeErr(err, http.StatusInternalServerError)
                }
                topicsCache, err := getTopicsCache(cfg, repoLog)
                if err != nil {
-                       makeErr(err)
+                       makeErr(err, http.StatusInternalServerError)
                }
                repoLog, err = repo.Log(&git.LogOptions{From: *headHash})
                if err != nil {
-                       makeErr(err)
+                       makeErr(err, http.StatusInternalServerError)
                }
 
                commitN := 0
@@ -277,7 +279,7 @@ func serveHTTP() {
                        topic = t[0]
                        hashes := topicsCache[topic]
                        if hashes == nil {
-                               makeErr(errors.New("no posts with that topic"))
+                               makeErr(errors.New("no posts with that topic"), http.StatusBadRequest)
                        }
                        if len(hashes) > offset {
                                hashes = hashes[offset:]
@@ -372,12 +374,12 @@ func serveHTTP() {
                        Entries:          entries,
                })
                if err != nil {
-                       makeErr(err)
+                       makeErr(err, http.StatusInternalServerError)
                }
        } else if pathInfo == "/"+AtomPostsFeed {
                commit, err := repo.CommitObject(*headHash)
                if err != nil {
-                       makeErr(err)
+                       makeErr(err, http.StatusInternalServerError)
                }
 
                var topic string
@@ -415,7 +417,7 @@ func serveHTTP() {
 
                repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
                if err != nil {
-                       makeErr(err)
+                       makeErr(err, http.StatusInternalServerError)
                }
                var commits CommitIterNext
                if topic == "" {
@@ -423,11 +425,11 @@ func serveHTTP() {
                } else {
                        topicsCache, err := getTopicsCache(cfg, repoLog)
                        if err != nil {
-                               makeErr(err)
+                               makeErr(err, http.StatusInternalServerError)
                        }
                        hashes := topicsCache[topic]
                        if hashes == nil {
-                               makeErr(errors.New("no posts with that topic"))
+                               makeErr(errors.New("no posts with that topic"), http.StatusBadRequest)
                        }
                        commits = &HashesIter{hashes}
                }
@@ -467,14 +469,14 @@ func serveHTTP() {
                }
                data, err := xml.MarshalIndent(&feed, "", "  ")
                if err != nil {
-                       makeErr(err)
+                       makeErr(err, http.StatusInternalServerError)
                }
                out.Write(data)
                goto AtomFinish
        } else if pathInfo == "/"+AtomCommentsFeed {
                commit, err := repo.CommitObject(commentsRef.Hash())
                if err != nil {
-                       makeErr(err)
+                       makeErr(err, http.StatusInternalServerError)
                }
                etagHash.Write([]byte("ATOM COMMENTS"))
                etagHash.Write(commit.Hash[:])
@@ -497,7 +499,7 @@ func serveHTTP() {
                }
                repoLog, err := repo.Log(&git.LogOptions{From: commentsRef.Hash()})
                if err != nil {
-                       makeErr(err)
+                       makeErr(err, http.StatusInternalServerError)
                }
                for i := 0; i < PageEntries; i++ {
                        commit, err = repoLog.Next()
@@ -506,11 +508,11 @@ func serveHTTP() {
                        }
                        fileStats, err := commit.Stats()
                        if err != nil {
-                               makeErr(err)
+                               makeErr(err, http.StatusInternalServerError)
                        }
                        t, err := commit.Tree()
                        if err != nil {
-                               makeErr(err)
+                               makeErr(err, http.StatusInternalServerError)
                        }
                        commentedHash := plumbing.NewHash(strings.ReplaceAll(
                                fileStats[0].Name, "/", "",
@@ -561,14 +563,14 @@ func serveHTTP() {
                }
                data, err := xml.MarshalIndent(&feed, "", "  ")
                if err != nil {
-                       makeErr(err)
+                       makeErr(err, http.StatusInternalServerError)
                }
                out.Write(data)
                goto AtomFinish
        } else if sha1DigestRe.MatchString(pathInfo[1:]) {
                commit, err := repo.CommitObject(plumbing.NewHash(pathInfo[1 : 1+sha1.Size*2]))
                if err != nil {
-                       makeErr(err)
+                       makeErr(err, http.StatusBadRequest)
                }
                for _, data := range etagHashForWeb {
                        etagHash.Write([]byte(data))
@@ -667,7 +669,7 @@ func serveHTTP() {
                        }
                        data, err := xml.MarshalIndent(&feed, "", "  ")
                        if err != nil {
-                               makeErr(err)
+                               makeErr(err, http.StatusInternalServerError)
                        }
                        out.Write(data)
                        goto AtomFinish
@@ -723,10 +725,10 @@ func serveHTTP() {
                        Topics:          sgblog.ParseTopics(topicsRaw),
                })
                if err != nil {
-                       makeErr(err)
+                       makeErr(err, http.StatusInternalServerError)
                }
        } else {
-               makeErr(errors.New("unknown URL action"))
+               makeErr(errors.New("unknown URL action"), http.StatusNotFound)
        }
        out.Write([]byte("</body></html>\n"))
        if gzipWriter != nil {
index 9eaaeda29455fb390f6a00db6124bc61b21e9e48..eaafeadb65d173542e2acb17eb394ca2bf2ae036 100644 (file)
--- a/common.go
+++ b/common.go
@@ -15,7 +15,7 @@ import (
 )
 
 const (
-       Version = "0.22.0"
+       Version = "0.23.0"
        WhenFmt = "2006-01-02 15:04:05Z07:00"
 )