|
| 1 | +package headers |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strings" |
| 6 | + "unicode" |
| 7 | +) |
| 8 | + |
| 9 | +const FieldLineSeperator = ":" |
| 10 | +const crlf = "\r\n" |
| 11 | + |
| 12 | +type Headers map[string]string |
| 13 | + |
| 14 | +// there can be an unlimited amount of whitespace |
| 15 | +// before and after the field-value (Header value). However, when parsing a field-name, |
| 16 | +// there must be no spaces betwixt the colon and the field-name. In other words, |
| 17 | +// these are valid: |
| 18 | + |
| 19 | +// 'Host: localhost:42069' |
| 20 | +// ' Host: localhost:42069 ' |
| 21 | + |
| 22 | +// But this is not: |
| 23 | + |
| 24 | +// Host : localhost:42069 |
| 25 | + |
| 26 | +func (h Headers) Parse(data []byte) (n int, done bool, err error) { |
| 27 | + endIdx := strings.Index(string(data), crlf) |
| 28 | + if endIdx == -1 { |
| 29 | + return 0, false, nil |
| 30 | + } |
| 31 | + // if encounter \r\n in front of line that means we consumed all the headers/fieldLines |
| 32 | + if endIdx == 0 { |
| 33 | + return endIdx + 2, true, nil |
| 34 | + } |
| 35 | + |
| 36 | + currentLine := string(data[:endIdx]) |
| 37 | + fieldParts := strings.SplitN(currentLine, FieldLineSeperator, 2) |
| 38 | + |
| 39 | + if len(fieldParts) != 2 { |
| 40 | + return 0, false, errors.New("field-line have wrong number of parts") |
| 41 | + } |
| 42 | + |
| 43 | + fieldName := strings.TrimLeft(fieldParts[0], " ") |
| 44 | + if len(fieldName) != len(strings.TrimSpace(fieldName)) { |
| 45 | + return 0, false, errors.New("error in field-name syntax, whitespace unexpected") |
| 46 | + } |
| 47 | + if !isValidFieldName(fieldName) { |
| 48 | + return 0, false, errors.New("invalid characters in field-name") |
| 49 | + } |
| 50 | + |
| 51 | + fieldValue := strings.TrimSpace(strings.Trim(fieldParts[1], crlf)) |
| 52 | + // lowercase the fieldname while adding to the map |
| 53 | + val, exists := h[strings.ToLower(fieldName)] |
| 54 | + if exists { |
| 55 | + h[strings.ToLower(fieldName)] = val + "," + fieldValue |
| 56 | + } else { |
| 57 | + h[strings.ToLower(fieldName)] = fieldValue |
| 58 | + } |
| 59 | + |
| 60 | + return endIdx + 2, false, nil |
| 61 | +} |
| 62 | + |
| 63 | +func isValidFieldName(fieldName string) bool { |
| 64 | + allowedSpecials := map[rune]bool{ |
| 65 | + '!': true, '#': true, '$': true, '%': true, '&': true, '\'': true, |
| 66 | + '*': true, '+': true, '-': true, '.': true, '^': true, '_': true, |
| 67 | + '`': true, '|': true, '~': true, |
| 68 | + } |
| 69 | + |
| 70 | + if len(fieldName) == 0 { |
| 71 | + return false |
| 72 | + } |
| 73 | + |
| 74 | + for _, ch := range fieldName { |
| 75 | + switch { |
| 76 | + case unicode.IsLetter(ch): |
| 77 | + continue |
| 78 | + case unicode.IsDigit(ch): |
| 79 | + continue |
| 80 | + case allowedSpecials[ch]: |
| 81 | + continue |
| 82 | + default: |
| 83 | + return false |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return true |
| 88 | +} |
0 commit comments