cmd/sgblog/http.go | 48 +++++++++++++++++++++++++----------------------- common.go | 2 +- diff --git a/cmd/sgblog/http.go b/cmd/sgblog/http.go index f4db38d18262fddf71b8b90aebbb332de14334ab60449c012cdf323d185d6748..dd97cc8500de7aeac96f7bb5f10845110ce110353247870bf37ea7053f44a296 100644 --- a/cmd/sgblog/http.go +++ b/cmd/sgblog/http.go @@ -30,6 +30,7 @@ "hash" "html" "io" "log" + "net/http" "net/url" "os" "strconv" @@ -134,7 +135,8 @@ lines = append(lines, "") 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 @@ pathInfo = "/" } 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 @@ } headHash, err := initRepo(cfg) if err != nil { - makeErr(err) + makeErr(err, http.StatusInternalServerError) } if notes, err := repo.Notes(); err == nil { @@ -254,20 +256,20 @@ offset := 0 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 @@ if t, exists := queryValues["topic"]; exists { 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 @@ LogEnded: logEnded, 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 @@ } 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 @@ commits = repoLog } 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 @@ }) } 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 @@ Author: &atom.Person{Name: cfg.AtomAuthor}, } 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 @@ break } 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 @@ }) } 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 @@ }) } data, err := xml.MarshalIndent(&feed, "", " ") if err != nil { - makeErr(err) + makeErr(err, http.StatusInternalServerError) } out.Write(data) goto AtomFinish @@ -723,10 +725,10 @@ Comments: comments, 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 c7e437e9ea7561a1f7b427c2058c610c3d5490a95d05d4fa4adda73911511716..378e1a24b32fab325fa1fc934a567b2af7f3ca22a1e24352dec69a1f15ec0434 100644 --- a/common.go +++ b/common.go @@ -15,7 +15,7 @@ "go.cypherpunks.ru/recfile" ) const ( - Version = "0.22.0" + Version = "0.23.0" WhenFmt = "2006-01-02 15:04:05Z07:00" )