aboutsummaryrefslogtreecommitdiff
path: root/cmd/receivepack9418/request.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-03-08 01:56:58 +0800
committerGravatar Runxi Yu2026-03-08 02:00:11 +0800
commit33fda1b8e4da0ad9d4208a8b8249c8d7b305f4ae (patch)
tree4028b1362b816703c9df011797f060dc8b8ad0d5 /cmd/receivepack9418/request.go
parentreceivepack: Actually test it (diff)
signatureNo signature
cmd/receivepack9418: Init
Diffstat (limited to 'cmd/receivepack9418/request.go')
-rw-r--r--cmd/receivepack9418/request.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/cmd/receivepack9418/request.go b/cmd/receivepack9418/request.go
new file mode 100644
index 00000000..5e392926
--- /dev/null
+++ b/cmd/receivepack9418/request.go
@@ -0,0 +1,60 @@
+package main
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "strings"
+)
+
+type gitProtoRequest struct {
+ Command string
+ Pathname string
+ Host string
+ ExtraParameters []string
+}
+
+func parseGitProtoRequestPayload(payload []byte) (gitProtoRequest, error) {
+ parts := bytes.Split(payload, []byte{0})
+ if len(parts) == 0 || len(parts[0]) == 0 {
+ return gitProtoRequest{}, errors.New("missing command/path segment")
+ }
+
+ commandPath := string(parts[0])
+ command, pathname, ok := strings.Cut(commandPath, " ")
+ if !ok || command == "" || pathname == "" {
+ return gitProtoRequest{}, fmt.Errorf("malformed command/path segment %q", commandPath)
+ }
+
+ req := gitProtoRequest{
+ Command: command,
+ Pathname: pathname,
+ }
+
+ i := 1
+ if i < len(parts) && strings.HasPrefix(string(parts[i]), "host=") {
+ req.Host = strings.TrimPrefix(string(parts[i]), "host=")
+ i++
+ }
+
+ // No tail left.
+ if i >= len(parts) {
+ return req, nil
+ }
+
+ // If there is tail, grammar requires one empty field before extras.
+ if len(parts[i]) != 0 {
+ return gitProtoRequest{}, fmt.Errorf("unexpected token %q after host/path", string(parts[i]))
+ }
+
+ i++
+ for ; i < len(parts); i++ {
+ if len(parts[i]) == 0 {
+ continue
+ }
+
+ req.ExtraParameters = append(req.ExtraParameters, string(parts[i]))
+ }
+
+ return req, nil
+}