-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
handler_message.go
70 lines (56 loc) · 1.74 KB
/
handler_message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package smtpmock
import (
"bytes"
"errors"
)
// Message handler interface
type handlerMessageInterface interface {
run()
}
// Message handler
type handlerMessage struct {
*handler
}
// Message handler builder. Returns pointer to new handlerMessage structure
func newHandlerMessage(session sessionInterface, message *Message, configuration *configuration) *handlerMessage {
return &handlerMessage{&handler{session: session, message: message, configuration: configuration}}
}
// Message handler methods
// Main message handler runner
func (handler *handlerMessage) run() {
var request string
var msgData []byte
session, configuration := handler.session, handler.configuration
for {
line, err := session.readBytes()
if err != nil {
return
}
// Handles end of data denoted by lone period (\r\n.\r\n)
if bytes.Equal(line, []byte(".\r\n")) {
break
}
// Removes leading period (RFC 5321 section 4.5.2)
if line[0] == '.' {
line = line[1:]
}
// Enforces the maximum message size limit
if len(msgData)+len(line) > configuration.msgSizeLimit {
session.discardBufin()
handler.writeResult(false, request, configuration.msgMsgSizeIsTooBig)
return
}
msgData = append(msgData, line...)
}
handler.writeResult(true, string(msgData), configuration.msgMsgReceived)
}
// Writes handled message result to session, message. Always returns true
func (handler *handlerMessage) writeResult(isSuccessful bool, request, response string) bool {
session, message := handler.session, handler.message
if !isSuccessful {
session.addError(errors.New(response))
}
message.msgRequest, message.msgResponse, message.msg = request, response, isSuccessful
session.writeResponse(response, handler.configuration.responseDelayMessage)
return true
}