// Symbol looks up the program counters listed in the request, // responding with a table mapping program counters to function names. // The package initialization registers it as /debug/pprof/symbol. func Symbol(c *http.Conn, r *http.Request) { c.SetHeader("content-type", "text/plain; charset=utf-8") // We don't know how many symbols we have, but we // do have symbol information. Pprof only cares whether // this number is 0 (no symbols available) or > 0. fmt.Fprintf(c, "num_symbols: 1\n") var b *bufio.Reader if r.Method == "POST" { b = bufio.NewReader(r.Body) } else { b = bufio.NewReader(strings.NewReader(r.URL.RawQuery)) } for { w, err := b.ReadSlice('+') if err == nil { w = w[0 : len(w)-1] // trim + } pc, _ := strconv.Btoui64(string(w), 0) if pc != 0 { f := runtime.FuncForPC(uintptr(pc)) if f != nil { fmt.Fprintf(c, "%#x %s\n", pc, f.Name()) } } // Wait until here to check for err; the last // symbol will have an err because it doesn't end in +. if err != nil { break } } }
func parseInput(r *bufio.Reader) (args []string, err error) { for { buf, err := r.ReadSlice('\n') end := false if err == io.EOF { end = true } else if err != nil { return args, errors.New(fmt.Sprintf("Error reading line '%s': %v\n", buf, err)) } index := bytes.IndexByte(buf, '#') if index != -1 { buf = buf[:index] } buf = bytes.TrimSpace(buf) if len(buf) == 0 { if end { break } continue } args = append(args, string(buf)) if end { break } } return }
func (p *protocol) ReadParam(r *bufio.Reader) ([]byte, bool, error) { data, err := r.ReadSlice('\r') if err != nil { return nil, false, p.sanitizeErr(err) } err = p.assumeByte(r, '\n') if err != nil { return nil, false, err } next, err := r.ReadByte() if err != nil { return nil, false, p.sanitizeErr(err) } hasNext := true if next == '\r' { hasNext = false err = p.assumeByte(r, '\n') if err != nil { return nil, false, p.sanitizeErr(err) } } else { r.UnreadByte() } return data, hasNext, nil }
// parseConfigResponse reads a CONFIG response from r and returns the version // number and byte array of server descriptions. func parseConfigResponse(r *bufio.Reader) (int, []byte, error) { line, err := r.ReadSlice('\n') if err != nil { return 0, nil, err } size, err := scanConfigDataLength(line) if err != nil { return 0, nil, err } value, err := ioutil.ReadAll(io.LimitReader(r, int64(size)+2)) if err != nil { return 0, nil, err } if !bytes.HasSuffix(value, crlf) { return 0, nil, fmt.Errorf("elasticache: corrupt config result read") } // Reduce the value down to its total identified size value = value[:size] // Pull out version and server list, separated by LFs re := regexp.MustCompile("^(\\d+)\n(.*)\n$") configData := re.FindSubmatch(value) if configData == nil { return 0, nil, fmt.Errorf("elasticache: malformed config data: %s", value) } version, _ := strconv.Atoi(string(configData[1])) return version, configData[2], nil }
func readLine(rd *bufio.Reader) ([]byte, error) { // line, isPrefix, err := rd.ReadLine() // if err != nil { // return line, err // } // if isPrefix { // return line, errReaderTooSmall // } // if len(line) < 2 || line[len(line)-2] != '\r' { // \r\n // fmt.Println(line) // return nil, errors.New(fmt.Sprintf("invalid redis packet %v, err:%v", line, err)) // } // return line, nil line, err := rd.ReadSlice('\n') if err != nil { return nil, err } if len(line) < 2 || line[len(line)-2] != '\r' { // \r\n return nil, errors.New(fmt.Sprintf("invalid redis packet %v, err:%v", line, err)) } return line[:len(line)-2], nil }
func parseStatsResponse(r *bufio.Reader, stats *Statistics) error { for { line, err := r.ReadSlice('\n') if err != nil { return err } if bytes.Equal(line, resultEnd) { return nil } if hits, err := parseStatsLine(line, statsGetHits); err == nil { stats.Hits = hits continue } if misses, err := parseStatsLine(line, statsGetMisses); err == nil { stats.Misses = misses continue } if bytesWritten, err := parseStatsLine(line, statsBytesWritten); err == nil { stats.ByteHits = bytesWritten continue } if items, err := parseStatsLine(line, statsItems); err == nil { stats.Items = items continue } if bytes_total, err := parseStatsLine(line, statsBytes); err == nil { stats.Bytes = bytes_total continue } } }
// parseGetResponse reads a GET response from r and calls cb for each // read and allocated Item func parseGetResponse(r *bufio.Reader, cb func(*Item)) error { for { line, err := r.ReadSlice('\n') if err != nil { return err } if bytes.Equal(line, resultEnd) { return nil } it := new(Item) size, err := scanGetResponseLine(line, it) if err != nil { return err } it.Value, err = ioutil.ReadAll(io.LimitReader(r, int64(size)+2)) if err != nil { return err } if !bytes.HasSuffix(it.Value, crlf) { return fmt.Errorf("memcache: corrupt get result read") } it.Value = it.Value[:size] cb(it) } panic("unreached") }
// parseGetResponse reads a GET response from r and calls cb for each // read and allocated Item func parseGetResponse(r *bufio.Reader, cb func(*Item)) error { for { line, err := r.ReadSlice('\n') if err != nil { return err } if bytes.Equal(line, resultEnd) { return nil } it := new(Item) var size int n, err := fmt.Sscanf(string(line), "VALUE %s %d %d %d\r\n", &it.Key, &it.Flags, &size, &it.casid) if err != nil { return err } if n != 4 { return fmt.Errorf("memcache: unexpected line in get response: %q", string(line)) } it.Value, err = ioutil.ReadAll(io.LimitReader(r, int64(size)+2)) if err != nil { return err } if !bytes.HasSuffix(it.Value, crlf) { return fmt.Errorf("memcache: corrupt get result read") } it.Value = it.Value[:size] cb(it) } panic("unreached") }
func parseStatsSettingsResponse(r *bufio.Reader, stats *SettingsStats) error { pattern := "STAT %s %s\r\n" var ( key string value []byte ) for { line, err := r.ReadSlice('\n') if err != nil { return err } if bytes.Equal(line, resultEnd) { return nil } n, err := fmt.Sscanf(string(line), pattern, &key, &value) if err != nil || n != 2 { return fmt.Errorf("memcache: unexpected line in stats response: %q", line) } err = stats.Set(key, value) if err != nil && err != ErrInvalidStatsKey { return err } } panic("unreached") }
func readRequestLine(b *bufio.Reader) (method string, url string, version int, err os.Error) { p, err := b.ReadSlice('\n') if err != nil { if err == bufio.ErrBufferFull { err = web.ErrLineTooLong } return } m := requestLineRegexp.FindSubmatch(p) if m == nil { err = ErrBadRequestLine return } method = string(m[1]) major, err := strconv.Atoi(string(m[3])) if err != nil { return } minor, err := strconv.Atoi(string(m[4])) if err != nil { return } version = web.ProtocolVersion(major, minor) url = string(m[2]) return }
func handleData(rd bufio.Reader, out io.Writer) string { // Stolen from go-smtpd: // http://code.google.com/p/go-smtpd/source/browse/smtpd/smtpd.go fmt.Fprint(out, "354 Go ahead\r\n") buf := new(bytes.Buffer) for { sl, err := rd.ReadSlice('\n') if err != nil { return "" } if bytes.Equal(sl, []byte(".\r\n")) { break } if sl[0] == '.' { sl = sl[1:] } _, err = buf.Write(sl) if err != nil { return "" } } fmt.Fprint(out, "250 2.0.0 Ok: queued\r\n") return buf.String() }
func parseStatsItemsResponse(r *bufio.Reader, slabMap map[int]*ItemStats) error { pattern := "STAT items:%d:%s %s\r\n" var ( slabIndex int key string value []byte ) for { line, err := r.ReadSlice('\n') if err != nil { return err } if bytes.Equal(line, resultEnd) { return nil } n, err := fmt.Sscanf(string(line), pattern, &slabIndex, &key, &value) if err != nil || n != 3 { return fmt.Errorf("memcache: unexpected line in stats items response: %q", line) } _, ok := slabMap[slabIndex] if !ok { slabMap[slabIndex] = new(ItemStats) } err = slabMap[slabIndex].Set(key, value) if err != nil && err != ErrInvalidStatsKey { return err } } panic("unreached") }
// readEvent reads a single binlog event. It can be a single comment line, // or multiple lines terminated by the delimiter. func (bls *BinlogStreamer) readEvent(bufReader *bufio.Reader) (event []byte, err error) { for { fragment, err := bufReader.ReadSlice('\n') event = append(event, fragment...) if err == bufio.ErrBufferFull { continue } if err != nil { if err == io.EOF { return event, err } return event, fmt.Errorf("read error: %v", err) } // Use a different var than event, because you have to keep // the trailing \n if we continue trimmed := bytes.TrimSpace(event) if bytes.HasPrefix(trimmed, HASH_COMMENT) || bytes.HasPrefix(trimmed, SLASH_COMMENT) || bytes.HasPrefix(trimmed, DELIM_STMT) { return trimmed, nil } if bytes.HasSuffix(trimmed, bls.delim) { return bytes.TrimSpace(trimmed[:len(trimmed)-len(bls.delim)]), nil } } }
func (hub *MsgHub) msgProc(id int, reader *bufio.Reader) (err error) { var msgType byte var controlType byte msgType, err = reader.ReadByte() if err != nil { ERROR.Println("read from incoming error, detail:", err) return } switch msgType { case StringMsgType: //drop msg, should be ping var bs []byte bs, err = reader.ReadSlice('\n') if err != nil { return } else { INFO.Println(id, "said:", string(bs[:len(bs)-1])) } case ControlMsgType: //cronntrol msg if controlType, err = reader.ReadByte(); err != nil { return } switch controlType { case AddRouteControlType, RemoveRouteControlType: var msg RouteControlMsg var buf = make([]byte, msg.Len()) buf[0] = ControlMsgType buf[1] = controlType reader.Read(buf[2:]) if err = msg.Unmarshal(buf); err != nil { return } if msg.ControlType == AddRouteControlType { hub.AddRoute(msg.Id, msg.Type, id, nil) } else { hub.RemoveRoute(msg.Id, msg.Type) } case OfflineControlType: if err = hub.OfflineIncomingControlMsg(ControlMsgType, OfflineMsgType, reader); err != nil { return } } case RouteMsgType, TempRouteMsgType: var msg DeliverMsg if msg.To, msg.Carry, err = readRouteMsgBody(reader); err != nil { return } msg.MsgType = msgType hub.LocalDispatch(&msg) case OfflineMsgType: if err = hub.OfflineIncomingMsg(OfflineMsgType, reader); err != nil { return } default: err = UnknownMsg } return nil }
// readJSONErrorLine returns the error line returned by the log and // logsink APIS. func readJSONErrorLine(c *gc.C, reader *bufio.Reader) params.ErrorResult { line, err := reader.ReadSlice('\n') c.Assert(err, jc.ErrorIsNil) var errResult params.ErrorResult err = json.Unmarshal(line, &errResult) c.Assert(err, jc.ErrorIsNil) return errResult }
func (s *debugLogSuite) getErrorResult(c *gc.C, reader *bufio.Reader) params.ErrorResult { line, err := reader.ReadSlice('\n') c.Assert(err, gc.IsNil) var errResult params.ErrorResult err = json.Unmarshal(line, &errResult) c.Assert(err, gc.IsNil) return errResult }
func readFromClient(reader *bufio.Reader) (c ClientMessage, err error) { data, err := reader.ReadSlice('\n') if err != nil { return c, err } err = json.Unmarshal(data, &c) return c, err }
func readTerminatedInt(r *bufio.Reader, term byte) (int64, error) { buf, err := r.ReadSlice(term) if err != nil { return 0, err } else if len(buf) <= 1 { return 0, errors.New("bencode: empty integer field") } return strconv.ParseInt(string(buf[:len(buf)-1]), 10, 64) }
// Learned from net.textproto. One difference is that this one keeps the // ending '\n' in the returned line. Buf if there's only CRLF in the line, // return nil for the line. func readContinuedLineSlice(r *bufio.Reader) ([]byte, error) { // feedly.com request headers contains things like: // "$Authorization.feedly: $FeedlyAuth\r\n", so we must test for only // continuation spaces. isspace := func(b byte) bool { return b == ' ' || b == '\t' } // Read the first line. line, err := r.ReadSlice('\n') if err != nil && err != io.EOF { return nil, err } // There are servers that use \n for line ending, so trim first before check ending. // For example, the 404 page for http://plan9.bell-labs.com/magic/man2html/1/2l trimmed := TrimSpace(line) if len(trimmed) == 0 { if len(line) > 2 { return nil, fmt.Errorf("malformed end of headers, len: %d, %#v", len(line), string(line)) } return nil, nil } if isspace(line[0]) { return nil, fmt.Errorf("malformed header, start with space: %#v", string(line)) } // Optimistically assume that we have started to buffer the next line // and it starts with an ASCII letter (the next header key), so we can // avoid copying that buffered data around in memory and skipping over // non-existent whitespace. if r.Buffered() > 0 { peek, err := r.Peek(1) if err == nil && !isspace(peek[0]) { return line, nil } } var buf []byte buf = append(buf, trimmed...) // Read continuation lines. for skipSpace(r) > 0 { line, err := r.ReadSlice('\n') if err != nil { break } buf = append(buf, ' ') buf = append(buf, TrimTrailingSpace(line)...) } buf = append(buf, '\r', '\n') return buf, nil }
func readLine(r *bufio.Reader) ([]byte, error) { p, err := r.ReadSlice('\n') if err != nil { return nil, err } i := len(p) - 2 if i < 0 || p[i] != '\r' { return nil, protocolErr } return p[:i], nil }
func readLine(r *bufio.Reader) ([]byte, error) { line, err := r.ReadSlice('\n') if err != nil { return nil, errors.Trace(err) } if len(line) < 2 || line[len(line)-2] != '\r' { // \r\n return nil, errors.Errorf("invalid redis packet %v, err:%v", line, err) } return line, nil }
func ReadLine(rb *bufio.Reader) ([]byte, error) { p, err := rb.ReadSlice('\n') if err != nil { return nil, err } i := len(p) - 2 if i < 0 || p[i] != '\r' { return nil, errLineFormat } return p[:i], nil }
// Send response body if header specifies chunked encoding. rdSize specifies // the size of each read on Reader, it should be set to be the buffer size of // the Reader, this parameter is added for testing. func sendBodyChunked(w io.Writer, r *bufio.Reader) (err error) { // debug.Println("Sending chunked body") for { var s []byte // Read chunk size line, ignore chunk extension if any. if s, err = r.ReadSlice('\n'); err != nil { errl.Println("peek chunk size:", err) return } smid := bytes.IndexByte(s, ';') if smid == -1 { smid = len(s) } else { // use error log to find usage of chunk extension errl.Printf("got chunk extension: %s\n", s) } var size int64 if size, err = ParseIntFromBytes(TrimSpace(s[:smid]), 16); err != nil { errl.Println("chunk size invalid:", err) return } /* if debug { // To debug getting malformed response status line with "0\r\n". if c, ok := w.(*clientConn); ok { debug.Printf("cli(%s) chunk size %d %#v\n", c.RemoteAddr(), size, string(s)) } } */ if size == 0 { if err = skipCRLF(r); err != nil { return } if _, err = w.Write([]byte(chunkEnd)); err != nil { debug.Println("send chunk ending:", err) } return } if _, err = w.Write(s); err != nil { debug.Println("copy chunked header:", err) return } // RFC 2616 19.3 only suggest tolerating single LF for // headers, not for chunked encoding. So assume the server will send // CRLF. If not, the following parse int may find errors. total := size + 2 // total data size for this chunk, including ending CRLF // PeekSlice will not advance reader, so we can just copy total sized data. if _, err = io.CopyN(w, r, total); err != nil { debug.Println("copy chunked data:", err) return } } }
func (s *BlogSession) readline(b *bufio.Reader) (p []byte, err os.Error) { if p, err = b.ReadSlice('\n'); err != nil { return nil, err } var i int for i = len(p); i > 0; i-- { if c := p[i-1]; c != ' ' && c != '\r' && c != '\t' && c != '\n' { break } } return p[0:i], nil }
func NewDocument(in *bufio.Reader) *Document { this := &Document{make(map[NGram]int)} for sentance, err := in.ReadSlice('.'); err == nil; sentance, err = in.ReadSlice('.') { for ngram := range this.tokenize(sentance[:len(sentance)-1]) { if _, found := this.occurances[ngram]; !found { this.occurances[ngram] = 0 } this.occurances[ngram] += 1 } } return this }
// Read a line of bytes (up to \n) from b. // Give up if the line exceeds the buffer size. // The returned bytes are a pointer into storage in // the bufio, so they are only valid until the next bufio read. func readLine(b *bufio.Reader) (p []byte, err error) { if p, err = b.ReadSlice('\n'); err != nil { // We always know when EOF is coming. // If the caller asked for a line, there should be a line. if err == io.EOF { err = io.ErrUnexpectedEOF } else if err == bufio.ErrBufferFull { err = ErrLineTooLong } return nil, err } return bytes.TrimRight(p, " \t\n\r"), nil }
// readHeader parses "[sha1-fooooo 1234]" from r and returns the // number of bytes read (including the starting '[' and ending ']'), // the blobref bytes (not necessarily valid) and the number as a // uint32. // The consumed count returned is only valid if err == nil. // The returned digest slice is only valid until the next read from br. func readHeader(br *bufio.Reader) (consumed int, digest []byte, size uint32, err error) { line, err := br.ReadSlice(']') if err != nil { return } const minSize = len("[b-c 0]") sp := bytes.IndexByte(line, ' ') size64, err := strutil.ParseUintBytes(line[sp+1:len(line)-1], 10, 32) if len(line) < minSize || line[0] != '[' || line[len(line)-1] != ']' || sp < 0 || err != nil { return 0, nil, 0, errors.New("diskpacked: invalid header reader") } return len(line), line[1:sp], uint32(size64), nil }
func (fi *FontInfo) init(file *bufio.Reader) (err error) { // Symbolic fonts don't specify ascender and descender, so default to reasonable numbers. fi.ascender = 750 fi.descender = -188 var line []byte line, err = file.ReadSlice('\n') for err == nil { if m := reStartCharMetrics.FindSubmatch(line); m != nil { fi.numGlyphs, _ = strconv.Atoi(string(m[1])) break } else if m := reFontName.FindSubmatch(line); m != nil { fi.fontName = strings.TrimSpace(string(m[1])) } else if m := reAscender.FindSubmatch(line); m != nil { fi.ascender, _ = strconv.Atoi(string(m[1])) } else if m := reFontBBox.FindSubmatch(line); m != nil { sa := strings.Split(strings.TrimSpace(string(m[1])), " ") for i, s := range sa { fi.fontBBox[i], _ = strconv.Atoi(s) } } else if m := reDescender.FindSubmatch(line); m != nil { fi.descender, _ = strconv.Atoi(string(m[1])) } else if m := reFamilyName.FindSubmatch(line); m != nil { fi.familyName = strings.TrimSpace(string(m[1])) } else if m := reIsFixedPitch.FindSubmatch(line); m != nil { fi.isFixedPitch = string(m[1]) == "true" } else if m := reItalicAngle.FindSubmatch(line); m != nil { fi.italicAngle, _ = strconv.ParseFloat(string(m[1]), 64) } else if m := reWeight.FindSubmatch(line); m != nil { fi.weight = strings.TrimSpace(string(m[1])) } else if m := reCapHeight.FindSubmatch(line); m != nil { fi.capHeight, _ = strconv.Atoi(string(m[1])) } else if m := reCopyright.FindSubmatch(line); m != nil { fi.copyright = strings.TrimSpace(string(m[1])) } else if m := reFullName.FindSubmatch(line); m != nil { fi.fullName = strings.TrimSpace(string(m[1])) } else if m := reStdVW.FindSubmatch(line); m != nil { fi.stdVW, _ = strconv.Atoi(string(m[1])) } else if m := reUnderlinePosition.FindSubmatch(line); m != nil { fi.underlinePosition, _ = strconv.Atoi(string(m[1])) } else if m := reUnderlineThickness.FindSubmatch(line); m != nil { fi.underlineThickness, _ = strconv.Atoi(string(m[1])) } else if m := reVersion.FindSubmatch(line); m != nil { fi.version = strings.TrimSpace(string(m[1])) } else if m := reXHeight.FindSubmatch(line); m != nil { fi.xHeight, _ = strconv.Atoi(string(m[1])) } line, err = file.ReadSlice('\n') } return }
func dumpobj(rd *bufio.Reader) { first := true gochar := byte(0) ver := goobj.GO1_1 for { line, err := rd.ReadSlice('\n') if err != nil && err != bufio.ErrBufferFull { log.Fatal(err) } if len(line) == 2 && string(line) == "!\n" { break } if first { first = false // go object GOOS GOARCH VERSION words := strings.Fields(string(line)) arch := words[3] switch arch { case "arm": gochar = '5' case "amd64": gochar = '6' case "386": gochar = '8' default: log.Printf("unrecognized object format %q", line) return } version := words[4] switch version { case "go1", "go1.0.1", "go1.0.2", "go1.0.3": ver = goobj.GO1 } } } switch gochar { case '5': r5 := go5.NewReader(rd) r5.Version = ver dump(Reader5{r5}) case '6': r6 := go6.NewReader(rd) r6.Version = ver dump(Reader6{r6}) case '8': r8 := go8.NewReader(rd) r8.Version = ver dump(Reader8{r8}) } }
func readUntilSeparator(r *bufio.Reader) ([]byte, error) { var content []byte for { line, err := r.ReadSlice('\n') if err != nil && err != bufio.ErrBufferFull { return nil, err } if len(line) == 3 && string(line) == "$$\n" { break } content = append(content, line...) } return content, nil }