mirror of
https://github.com/ChronosX88/yans.git
synced 2024-11-09 23:21:01 +00:00
Implement HEAD command
This commit is contained in:
parent
032d238b9f
commit
ac390d3945
@ -25,7 +25,7 @@
|
|||||||
- :x: `IHAVE`
|
- :x: `IHAVE`
|
||||||
- :construction: Article retrieving
|
- :construction: Article retrieving
|
||||||
- :heavy_check_mark: `ARTICLE`
|
- :heavy_check_mark: `ARTICLE`
|
||||||
- :x: `HEAD`
|
- :heavy_check_mark: `HEAD`
|
||||||
- :x: `BODY`
|
- :x: `BODY`
|
||||||
- :x: `STAT`
|
- :x: `STAT`
|
||||||
- :x: Articles overview
|
- :x: Articles overview
|
||||||
|
@ -57,6 +57,7 @@ const (
|
|||||||
CommandPost = "POST"
|
CommandPost = "POST"
|
||||||
CommandListGroup = "LISTGROUP"
|
CommandListGroup = "LISTGROUP"
|
||||||
CommandArticle = "ARTICLE"
|
CommandArticle = "ARTICLE"
|
||||||
|
CommandHead = "HEAD"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -36,6 +36,7 @@ func NewHandler(b backend.StorageBackend, serverDomain string) *Handler {
|
|||||||
protocol.CommandPost: h.handlePost,
|
protocol.CommandPost: h.handlePost,
|
||||||
protocol.CommandListGroup: h.handleListgroup,
|
protocol.CommandListGroup: h.handleListgroup,
|
||||||
protocol.CommandArticle: h.handleArticle,
|
protocol.CommandArticle: h.handleArticle,
|
||||||
|
protocol.CommandHead: h.handleHead,
|
||||||
}
|
}
|
||||||
h.serverDomain = serverDomain
|
h.serverDomain = serverDomain
|
||||||
return h
|
return h
|
||||||
@ -385,6 +386,7 @@ func (h *Handler) handleListgroup(s *Session, arguments []string, id uint) error
|
|||||||
return dw.Close()
|
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 {
|
func (h *Handler) handleArticle(s *Session, arguments []string, id uint) error {
|
||||||
s.tconn.StartResponse(id)
|
s.tconn.StartResponse(id)
|
||||||
defer s.tconn.EndResponse(id)
|
defer s.tconn.EndResponse(id)
|
||||||
@ -455,6 +457,70 @@ func (h *Handler) handleArticle(s *Session, arguments []string, id uint) error {
|
|||||||
return dw.Close()
|
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 {
|
func (h *Handler) Handle(s *Session, message string, id uint) error {
|
||||||
splittedMessage := strings.Split(message, " ")
|
splittedMessage := strings.Split(message, " ")
|
||||||
for i, v := range splittedMessage {
|
for i, v := range splittedMessage {
|
||||||
|
Loading…
Reference in New Issue
Block a user