func ExampleScanner() { // A reader is needed as input. input := strings.NewReader("000ahello\n" + "000bworld!\n" + "0000", ) // Create the scanner... s := pktline.NewScanner(input) // and scan every pkt-line found in the input. for s.Scan() { payload := s.Bytes() if len(payload) == 0 { // zero sized payloads correspond to flush-pkts. fmt.Println("FLUSH-PKT DETECTED\n") } else { // otherwise, you will be able to access the full payload. fmt.Printf("PAYLOAD = %q\n", string(payload)) } } // this will catch any error when reading from the input, if any. if s.Err() != nil { fmt.Println(s.Err()) } // Output: // PAYLOAD = "hello\n" // PAYLOAD = "world!\n" // FLUSH-PKT DETECTED }
func (s *SuiteScanner) TestEmptyReader(c *C) { r := strings.NewReader("") sc := pktline.NewScanner(r) hasPayload := sc.Scan() c.Assert(hasPayload, Equals, false) c.Assert(sc.Err(), Equals, nil) }
// Decode reads from the given reader and decodes a report-status message. It // does not read more input than what is needed to fill the report status. func (s *ReportStatus) Decode(r io.Reader) error { scan := pktline.NewScanner(r) if err := s.scanFirstLine(scan); err != nil { return err } if err := s.decodeReportStatus(scan.Bytes()); err != nil { return err } flushed := false for scan.Scan() { b := scan.Bytes() if isFlush(b) { flushed = true break } if err := s.decodeCommandStatus(b); err != nil { return err } } if !flushed { return fmt.Errorf("missing flush") } return scan.Err() }
func (s *SuiteScanner) TestScanAndPayload(c *C) { for _, test := range [...]string{ "a", "a\n", strings.Repeat("a", 100), strings.Repeat("a", 100) + "\n", strings.Repeat("\x00", 100), strings.Repeat("\x00", 100) + "\n", strings.Repeat("a", pktline.MaxPayloadSize), strings.Repeat("a", pktline.MaxPayloadSize-1) + "\n", } { var buf bytes.Buffer e := pktline.NewEncoder(&buf) err := e.EncodeString(test) c.Assert(err, IsNil, Commentf("input len=%x, contents=%.10q\n", len(test), test)) sc := pktline.NewScanner(&buf) c.Assert(sc.Scan(), Equals, true, Commentf("test = %.20q...", test)) obtained := sc.Bytes() c.Assert(obtained, DeepEquals, []byte(test), Commentf("in = %.20q out = %.20q", test, string(obtained))) } }
func (s *SuiteScanner) TestPktLineTooShort(c *C) { r := strings.NewReader("010cfoobar") sc := pktline.NewScanner(r) c.Assert(sc.Scan(), Equals, false) c.Assert(sc.Err(), ErrorMatches, "unexpected EOF") }
func (s *SuiteScanner) TestEOF(c *C) { var buf bytes.Buffer e := pktline.NewEncoder(&buf) err := e.EncodeString("first", "second") c.Assert(err, IsNil) sc := pktline.NewScanner(&buf) for sc.Scan() { } c.Assert(sc.Err(), IsNil) }
// Decode reads the next update-request message form the reader and wr func (req *ReferenceUpdateRequest) Decode(r io.Reader) error { var rc io.ReadCloser var ok bool rc, ok = r.(io.ReadCloser) if !ok { rc = ioutil.NopCloser(r) } d := &updReqDecoder{r: rc, s: pktline.NewScanner(r)} return d.Decode(req) }
func (s *SuiteScanner) TestFlush(c *C) { var buf bytes.Buffer e := pktline.NewEncoder(&buf) err := e.Flush() c.Assert(err, IsNil) sc := pktline.NewScanner(&buf) c.Assert(sc.Scan(), Equals, true) payload := sc.Bytes() c.Assert(len(payload), Equals, 0) }
// NewDemuxer returns a new Demuxer for the given t and read from r func NewDemuxer(t Type, r io.Reader) *Demuxer { max := MaxPackedSize64k if t == Sideband { max = MaxPackedSize } return &Demuxer{ t: t, r: r, max: max, s: pktline.NewScanner(r), } }
func (s *SuiteScanner) TestInvalid(c *C) { for _, test := range [...]string{ "0001", "0002", "0003", "0004", "0001asdfsadf", "0004foo", "fff1", "fff2", "gorka", "0", "003", " 5a", "5 a", "5 \n", "-001", "-000", } { r := strings.NewReader(test) sc := pktline.NewScanner(r) _ = sc.Scan() c.Assert(sc.Err(), ErrorMatches, pktline.ErrInvalidPktLen.Error(), Commentf("data = %q", test)) } }
// A section are several non flush-pkt lines followed by a flush-pkt, which // how the git protocol sends long messages. func (s *SuiteScanner) TestReadSomeSections(c *C) { nSections := 2 nLines := 4 data := sectionsExample(c, nSections, nLines) sc := pktline.NewScanner(data) sectionCounter := 0 lineCounter := 0 for sc.Scan() { if len(sc.Bytes()) == 0 { sectionCounter++ } lineCounter++ } c.Assert(sc.Err(), IsNil) c.Assert(sectionCounter, Equals, nSections) c.Assert(lineCounter, Equals, (1+nLines)*nSections) }
func (s *SuiteScanner) TestSkip(c *C) { for _, test := range [...]struct { input []string n int expected []byte }{ { input: []string{ "first", "second", "third"}, n: 1, expected: []byte("second"), }, { input: []string{ "first", "second", "third"}, n: 2, expected: []byte("third"), }, } { var buf bytes.Buffer e := pktline.NewEncoder(&buf) err := e.EncodeString(test.input...) c.Assert(err, IsNil) sc := pktline.NewScanner(&buf) for i := 0; i < test.n; i++ { c.Assert(sc.Scan(), Equals, true, Commentf("scan error = %s", sc.Err())) } c.Assert(sc.Scan(), Equals, true, Commentf("scan error = %s", sc.Err())) obtained := sc.Bytes() c.Assert(obtained, DeepEquals, test.expected, Commentf("\nin = %.20q\nout = %.20q\nexp = %.20q", test.input, obtained, test.expected)) } }
// Decode decodes the response into the struct, isMultiACK should be true, if // the request was done with multi_ack or multi_ack_detailed capabilities func (r *ServerResponse) Decode(reader io.Reader, isMultiACK bool) error { if isMultiACK { return errors.New("multi_ack and multi_ack_detailed are not supported") } s := pktline.NewScanner(reader) for s.Scan() { line := s.Bytes() if err := r.decodeLine(line); err != nil { return err } if !isMultiACK { break } } return s.Err() }
func (r *ShallowUpdate) Decode(reader io.Reader) error { s := pktline.NewScanner(reader) for s.Scan() { line := s.Bytes() var err error switch { case bytes.HasPrefix(line, shallow): err = r.decodeShallowLine(line) case bytes.HasPrefix(line, unshallow): err = r.decodeUnshallowLine(line) case bytes.Compare(line, pktline.Flush) == 0: return nil } if err != nil { return err } } return s.Err() }
func newAdvRefsDecoder(r io.Reader) *advRefsDecoder { return &advRefsDecoder{ s: pktline.NewScanner(r), } }
func newUlReqDecoder(r io.Reader) *ulReqDecoder { return &ulReqDecoder{ s: pktline.NewScanner(r), } }
func (s *SuiteScanner) TestInternalReadError(c *C) { sc := pktline.NewScanner(&mockReader{}) c.Assert(sc.Scan(), Equals, false) c.Assert(sc.Err(), ErrorMatches, "foo") }