This repository has been archived by the owner on Jun 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
94e329c
commit cadc791
Showing
3 changed files
with
128 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
queueConsumer "github.com/Financial-Times/message-queue-gonsumer/consumer" | ||
"log" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// FT async msg format: | ||
// | ||
// message-version CRLF | ||
// *(message-header CRLF) | ||
// CRLF | ||
// message-body | ||
func parseMessage(raw []byte) (m queueConsumer.Message, err error) { | ||
decoded := string(raw[:]) | ||
doubleNewLineStartIndex := getHeaderSectionEndingIndex(string(decoded[:])) | ||
if m.Headers, err = parseHeaders(string(decoded[:doubleNewLineStartIndex])); err != nil { | ||
return | ||
} | ||
m.Body = strings.TrimSpace(string(decoded[doubleNewLineStartIndex:])) | ||
return | ||
} | ||
|
||
func getHeaderSectionEndingIndex(msg string) int { | ||
//FT msg format uses CRLF for line endings | ||
i := strings.Index(msg, "\r\n\r\n") | ||
if i != -1 { | ||
return i | ||
} | ||
//fallback to UNIX line endings | ||
i = strings.Index(msg, "\n\n") | ||
if i != -1 { | ||
return i | ||
} | ||
log.Printf("WARN - message with no message body: [%s]", msg) | ||
return len(msg) | ||
} | ||
|
||
var re = regexp.MustCompile("[\\w-]*:[\\w\\-:/. ]*") | ||
|
||
var kre = regexp.MustCompile("[\\w-]*:") | ||
var vre = regexp.MustCompile(":[\\w-:/. ]*") | ||
|
||
func parseHeaders(msg string) (map[string]string, error) { | ||
headerLines := re.FindAllString(msg, -1) | ||
|
||
headers := make(map[string]string) | ||
for _, line := range headerLines { | ||
key, value := parseHeader(line) | ||
headers[key] = value | ||
} | ||
return headers, nil | ||
} | ||
|
||
func parseHeader(header string) (string, string) { | ||
key := kre.FindString(header) | ||
value := vre.FindString(header) | ||
return key[:len(key)-1], strings.TrimSpace(value[1:]) | ||
} |