Implement HEAD command

This commit is contained in:
ChronosX88 2022-02-05 17:40:38 +03:00
parent 032d238b9f
commit ac390d3945
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
3 changed files with 68 additions and 1 deletions

View File

@ -25,7 +25,7 @@
- :x: `IHAVE`
- :construction: Article retrieving
- :heavy_check_mark: `ARTICLE`
- :x: `HEAD`
- :heavy_check_mark: `HEAD`
- :x: `BODY`
- :x: `STAT`
- :x: Articles overview

View File

@ -57,6 +57,7 @@ const (
CommandPost = "POST"
CommandListGroup = "LISTGROUP"
CommandArticle = "ARTICLE"
CommandHead = "HEAD"
)
const (

View File

@ -36,6 +36,7 @@ func NewHandler(b backend.StorageBackend, serverDomain string) *Handler {
protocol.CommandPost: h.handlePost,
protocol.CommandListGroup: h.handleListgroup,
protocol.CommandArticle: h.handleArticle,
protocol.CommandHead: h.handleHead,
}
h.serverDomain = serverDomain
return h
@ -385,6 +386,7 @@ func (h *Handler) handleListgroup(s *Session, arguments []string, id uint) error
return dw.Close()
}
// TODO refactor this, because it's mostly duplicate of ARTICLE handler function
func (h *Handler) handleArticle(s *Session, arguments []string, id uint) error {
s.tconn.StartResponse(id)
defer s.tconn.EndResponse(id)
@ -455,6 +457,70 @@ func (h *Handler) handleArticle(s *Session, arguments []string, id uint) error {
return dw.Close()
}
func (h *Handler) handleHead(s *Session, arguments []string, id uint) error {
s.tconn.StartResponse(id)
defer s.tconn.EndResponse(id)
if len(arguments) == 0 && s.currentArticle == nil {
return s.tconn.PrintfLine(protocol.NNTPResponse{Code: 420, Message: "No current article selected"}.String())
}
if len(arguments) > 1 {
return s.tconn.PrintfLine(protocol.ErrSyntaxError.String())
}
getByArticleNum := true
num, err := strconv.Atoi(arguments[0])
if err != nil {
getByArticleNum = false
}
if getByArticleNum && s.currentGroup == nil {
return s.tconn.PrintfLine(protocol.NNTPResponse{Code: 412, Message: "No newsgroup selected"}.String())
}
var a models.Article
if getByArticleNum {
a, err = h.backend.GetArticleByNumber(s.currentGroup, num)
if err != nil {
if err == sql.ErrNoRows {
return s.tconn.PrintfLine(protocol.NNTPResponse{Code: 423, Message: "No article with that number"}.String())
} else {
return err
}
}
} else {
a, err = h.backend.GetArticle(arguments[0])
if err != nil {
if err == sql.ErrNoRows {
return s.tconn.PrintfLine(protocol.NNTPResponse{Code: 430, Message: "No Such Article Found"}.String())
} else {
return err
}
}
}
s.currentArticle = &a
m := utils.NewMessage()
for k, v := range a.Header {
m.SetHeader(k, v...)
}
dw := s.tconn.DotWriter()
_, err = dw.Write([]byte(protocol.NNTPResponse{Code: 221, Message: fmt.Sprintf("%d %s", num, a.Header.Get("Message-ID"))}.String() + protocol.CRLF))
if err != nil {
return err
}
_, err = m.WriteTo(dw)
if err != nil {
return err
}
return dw.Close()
}
func (h *Handler) Handle(s *Session, message string, id uint) error {
splittedMessage := strings.Split(message, " ")
for i, v := range splittedMessage {