From: Sergey Matveev Date: Wed, 6 Oct 2021 09:21:25 +0000 (+0300) Subject: Proper HTTP status codes X-Git-Tag: v0.23.0^0 X-Git-Url: http://www.git.stargrave.org/?p=sgblog.git;a=commitdiff_plain;h=b9b801ec28189b2a21ad5a3a3b18acfeec6d5ec0 Proper HTTP status codes --- diff --git a/cmd/sgblog/http.go b/cmd/sgblog/http.go index 6ec97bb..f81e38e 100644 --- a/cmd/sgblog/http.go +++ b/cmd/sgblog/http.go @@ -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("\n")) if gzipWriter != nil { diff --git a/common.go b/common.go index 9eaaeda..eaafead 100644 --- 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" )