grrrr
This commit is contained in:
parent
aff3757618
commit
cc8322f221
9 changed files with 136 additions and 38 deletions
61
src/parser/parser.go
Normal file
61
src/parser/parser.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package parser
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"exhq.dev/pas/src/Constants"
|
||||
)
|
||||
|
||||
func ParseIntent(intent string) Constants.Intent {
|
||||
if intent == "" {
|
||||
return Constants.Intent{
|
||||
Method: Constants.UNKNOWN,
|
||||
Path: "",
|
||||
Version: "",
|
||||
Headers: map[string]string{},
|
||||
}
|
||||
}
|
||||
|
||||
lines := strings.Split(intent, "\n")
|
||||
if len(lines) < 1 {
|
||||
return Constants.Intent{
|
||||
Method: Constants.UNKNOWN,
|
||||
Path: "",
|
||||
Version: "",
|
||||
Headers: map[string]string{},
|
||||
}
|
||||
}
|
||||
|
||||
firstLine := lines[0]
|
||||
parts := strings.Fields(firstLine)
|
||||
if len(parts) != 3 {
|
||||
return Constants.Intent{
|
||||
Method: Constants.UNKNOWN,
|
||||
Path: "",
|
||||
Version: "",
|
||||
Headers: map[string]string{},
|
||||
}
|
||||
}
|
||||
|
||||
method := Constants.UNKNOWN
|
||||
if parts[0] == string(Constants.GET) {
|
||||
method = Constants.GET
|
||||
} else if parts[0] == string(Constants.POST) {
|
||||
method = Constants.POST
|
||||
}
|
||||
|
||||
headers := make(map[string]string)
|
||||
for _, line := range lines[1:] {
|
||||
headerParts := strings.SplitN(line, ":", 2)
|
||||
if len(headerParts) == 2 {
|
||||
headers[strings.TrimSpace(headerParts[0])] = strings.TrimSpace(headerParts[1])
|
||||
}
|
||||
}
|
||||
|
||||
return Constants.Intent{
|
||||
Method: method,
|
||||
Path: parts[1],
|
||||
Version: strings.ReplaceAll(parts[2], "PAS/", ""),
|
||||
Headers: headers,
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue