Encode encodes the request into the writer.
(w io.Writer)
| 44 | |
| 45 | // Encode encodes the request into the writer. |
| 46 | func (g *GitProtoRequest) Encode(w io.Writer) error { |
| 47 | if w == nil { |
| 48 | return ErrNilWriter |
| 49 | } |
| 50 | |
| 51 | if err := g.validate(); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | |
| 55 | p := pktline.NewEncoder(w) |
| 56 | req := fmt.Sprintf("%s %s\x00", g.RequestCommand, g.Pathname) |
| 57 | if host := g.Host; host != "" { |
| 58 | req += fmt.Sprintf("host=%s\x00", host) |
| 59 | } |
| 60 | |
| 61 | if len(g.ExtraParams) > 0 { |
| 62 | req += "\x00" |
| 63 | for _, param := range g.ExtraParams { |
| 64 | req += param + "\x00" |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if err := p.Encode([]byte(req)); err != nil { |
| 69 | return err |
| 70 | } |
| 71 | |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | // Decode decodes the request from the reader. |
| 76 | func (g *GitProtoRequest) Decode(r io.Reader) error { |