func (dir *Directory) createVolumeHandler(w http.ResponseWriter, r *http.Request) { // increase volumeID createVolCmd := &CreateVolCommand{ReplicateStr: r.FormValue("replication")} // fmt.Println("replication: ", createVolCmd.ReplicateStr) v, err := dir.raftServer.Do(createVolCmd) if err != nil { helper.WriteJson(w, createVolResult{Error: err.Error()}, http.StatusInternalServerError) return } volidip := v.(VolumeIDIP) for _, ip := range volidip.IP { var b bytes.Buffer bytes, err := json.Marshal(volidip) if err != nil { helper.WriteJson(w, createVolResult{Error: err.Error()}, http.StatusInternalServerError) return } b.Write(bytes) _, err = postAndError(fmt.Sprintf("http://%s/vol/create", ip), "application/json", &b) if err != nil { helper.WriteJson(w, createVolResult{Error: err.Error()}, http.StatusInternalServerError) return } } helper.WriteJson(w, volidip, http.StatusOK) }
// Get retrieves the credentials for a given server url. // The reader must contain the server URL to search. // The writer is used to write the JSON serialization of the credentials. func Get(helper Helper, reader io.Reader, writer io.Writer) error { scanner := bufio.NewScanner(reader) buffer := new(bytes.Buffer) for scanner.Scan() { buffer.Write(scanner.Bytes()) } if err := scanner.Err(); err != nil && err != io.EOF { return err } serverURL := strings.TrimSpace(buffer.String()) username, secret, err := helper.Get(serverURL) if err != nil { return err } resp := Credentials{ Username: username, Secret: secret, } buffer.Reset() if err := json.NewEncoder(buffer).Encode(resp); err != nil { return err } fmt.Fprint(writer, buffer.String()) return nil }
func (options *Html) TitleBlock(out *bytes.Buffer, text []byte) { text = bytes.TrimPrefix(text, []byte("% ")) text = bytes.Replace(text, []byte("\n% "), []byte("\n"), -1) out.WriteString("<h1 class=\"title\">") out.Write(text) out.WriteString("\n</h1>") }
// parse a blockquote fragment func (p *parser) quote(out *bytes.Buffer, data []byte) int { var raw bytes.Buffer beg, end := 0, 0 for beg < len(data) { end = beg for data[end] != '\n' { end++ } end++ if pre := p.quotePrefix(data[beg:]); pre > 0 { // skip the prefix beg += pre } else if p.isEmpty(data[beg:]) > 0 && (end >= len(data) || (p.quotePrefix(data[end:]) == 0 && p.isEmpty(data[end:]) == 0)) { // blockquote ends with at least one blank line // followed by something without a blockquote prefix break } // this line is part of the blockquote raw.Write(data[beg:end]) beg = end } var cooked bytes.Buffer p.block(&cooked, raw.Bytes()) p.r.BlockQuote(out, cooked.Bytes()) return end }
func (pipeline *Pipeline) JSONFeed(since, limit int) ([]byte, error) { pipeline.mutex.RLock() var lineRaw []byte var line *PipelineFeedLine var buffer bytes.Buffer var err error data := pipeline.data[since:] count := 0 for seq, msg := range data { line = &PipelineFeedLine{ Seq: seq + since, Msg: msg.Outgoing, } lineRaw, err = json.Marshal(line) if err != nil { return nil, err } buffer.Write(lineRaw) buffer.WriteString("\n") count++ if limit > 0 && count >= limit { break } } pipeline.mutex.RUnlock() return buffer.Bytes(), nil }
// Write writes data to the connection. // net.Conn Deadlines are ignored. net.Conn concurrency semantics are supported. func (meek *MeekConn) Write(buffer []byte) (n int, err error) { if meek.closed() { return 0, ContextError(errors.New("meek connection is closed")) } // Repeats until all n bytes are written n = len(buffer) for len(buffer) > 0 { // Block until there is capacity in the send buffer var sendBuffer *bytes.Buffer select { case sendBuffer = <-meek.emptySendBuffer: case sendBuffer = <-meek.partialSendBuffer: case <-meek.broadcastClosed: return 0, ContextError(errors.New("meek connection has closed")) } writeLen := MAX_SEND_PAYLOAD_LENGTH - sendBuffer.Len() if writeLen > 0 { if writeLen > len(buffer) { writeLen = len(buffer) } _, err = sendBuffer.Write(buffer[:writeLen]) buffer = buffer[writeLen:] } meek.replaceSendBuffer(sendBuffer) } return n, err }
func (r *roffRenderer) TableRow(out *bytes.Buffer, text []byte) { if out.Len() > 0 { out.WriteString("\n") } out.Write(text) out.WriteString("\n") }
func (options *xml2) ListItem(out *bytes.Buffer, text []byte, flags int) { if flags&_LIST_TYPE_DEFINITION != 0 && flags&_LIST_TYPE_TERM == 0 { out.Write(text) return } if flags&_LIST_TYPE_TERM != 0 { if flags&_LIST_ITEM_BEGINNING_OF_LIST == 0 { out.WriteString("</t>\n") } // close previous one?/ out.WriteString("<t hangText=\"") n := out.Len() writeSanitizeXML(out, text) if n == out.Len() { printf(nil, "no text remained after sanitizing XML for definition term: '"+string(text)+"'") } out.WriteString("\">\n") out.WriteString("<vspace />\n") // Align HTML and XML2 output, but inserting a new line (vspace here) return } out.WriteString("<t>") out.Write(text) out.WriteString("</t>\n") options.paraInList = false }
func initJs(t *testing.T) *otto.Otto { var otto = otto.New() soyutilsFile, err := os.Open("lib/soyutils.js") if err != nil { panic(err) } // remove any non-otto compatible regular expressions var soyutilsBuf bytes.Buffer var scanner = bufio.NewScanner(soyutilsFile) var i = 1 for scanner.Scan() { switch i { case 2565, 2579, 2586: // skip these regexes // soy.esc.$$FILTER_FOR_FILTER_CSS_VALUE_ // soy.esc.$$FILTER_FOR_FILTER_HTML_ATTRIBUTES_ // soy.esc.$$FILTER_FOR_FILTER_HTML_ELEMENT_NAME_ default: soyutilsBuf.Write(scanner.Bytes()) soyutilsBuf.Write([]byte("\n")) } i++ } // load the soyutils library _, err = otto.Run(soyutilsBuf.String()) if err != nil { t.Errorf("soyutils error: %v", err) panic(err) } return otto }
func (t *transportBasicIO) WaitForFunc(f func([]byte) (int, error)) ([]byte, error) { var out bytes.Buffer buf := make([]byte, 4096) for { n, err := t.Read(buf) if n == 0 { return nil, fmt.Errorf("WaitForFunc read no data.") } if err != nil { if err != io.EOF { return nil, err } break } end, err := f(buf) if err != nil { return nil, err } if end > -1 { out.Write(buf[0:end]) return out.Bytes(), nil } out.Write(buf[0:n]) } return nil, fmt.Errorf("WaitForFunc failed") }
func (options *xml2) CommentHtml(out *bytes.Buffer, text []byte) { i := bytes.Index(text, []byte("-->")) if i > 0 { text = text[:i] } // strip, <!-- text = text[4:] var source []byte l := len(text) if l > 20 { l = 20 } for i := 0; i < l; i++ { if text[i] == '-' && text[i+1] == '-' { source = text[:i] text = text[i+2:] break } } // don't output a cref if it is not name -- remark if len(source) != 0 { source = bytes.TrimSpace(source) text = bytes.TrimSpace(text) out.WriteString("<t><cref source=\"") out.Write(source) out.WriteString("\">") out.Write(text) out.WriteString("</cref></t>\n") } return }
func (d *WordDecoder) convert(buf *bytes.Buffer, charset string, content []byte) error { switch { case strings.EqualFold("utf-8", charset): buf.Write(content) case strings.EqualFold("iso-8859-1", charset): for _, c := range content { buf.WriteRune(rune(c)) } case strings.EqualFold("us-ascii", charset): for _, c := range content { if c >= utf8.RuneSelf { buf.WriteRune(unicode.ReplacementChar) } else { buf.WriteByte(c) } } default: if d.CharsetReader == nil { return fmt.Errorf("mime: unhandled charset %q", charset) } r, err := d.CharsetReader(strings.ToLower(charset), bytes.NewReader(content)) if err != nil { return err } if _, err = buf.ReadFrom(r); err != nil { return err } } return nil }
// TODO: This is probably broken func (m *Man) TableCell(out *bytes.Buffer, text []byte, align int) { if out.Len() > 0 { out.WriteString("\t") } out.Write(text) out.WriteString("\t") }
func (m *Man) TableRow(out *bytes.Buffer, text []byte) { if out.Len() > 0 { out.WriteString("\n") } out.Write(text) out.WriteString("\n") }
func (client *Client) HandleLoadGroupOffline(lh *LoadGroupOffline) { messages := storage.LoadGroupOfflineMessage(lh.appid, lh.gid, lh.uid, lh.device_id, GROUP_OFFLINE_LIMIT) result := &MessageResult{status: 0} buffer := new(bytes.Buffer) var count int16 = 0 for _, emsg := range messages { if emsg.msg.cmd == MSG_GROUP_IM { im := emsg.msg.body.(*IMMessage) if im.sender == lh.uid && emsg.device_id == lh.device_id { continue } } count += 1 } binary.Write(buffer, binary.BigEndian, count) for _, emsg := range messages { if emsg.msg.cmd == MSG_GROUP_IM { im := emsg.msg.body.(*IMMessage) if im.sender == lh.uid && emsg.device_id == lh.device_id { continue } } ebuf := client.WriteEMessage(emsg) var size int16 = int16(len(ebuf)) binary.Write(buffer, binary.BigEndian, size) buffer.Write(ebuf) } result.content = buffer.Bytes() msg := &Message{cmd: MSG_RESULT, body: result} SendMessage(client.conn, msg) }
// Image defines how images should be processed to produce corresponding HTML elements. func (r *Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) { prefix := strings.Replace(r.urlPrefix, "/src/", "/raw/", 1) if len(link) > 0 { if isLink(link) { // External link with .svg suffix usually means CI status. // TODO: define a keyword to allow non-svg images render as external link. if bytes.HasSuffix(link, svgSuffix) || bytes.Contains(link, svgSuffixWithMark) { r.Renderer.Image(out, link, title, alt) return } } else { if link[0] != '/' { prefix += "/" } link = bytes.Replace([]byte((prefix + string(link))), spaceBytes, spaceEncodedBytes, -1) fmt.Println(333, string(link)) } } out.WriteString(`<a href="`) out.Write(link) out.WriteString(`">`) r.Renderer.Image(out, link, title, alt) out.WriteString("</a>") }
// Test that the line splitter errors out on a long line. func TestScanLineTooLong(t *testing.T) { const smallMaxTokenSize = 256 // Much smaller for more efficient testing. // Build a buffer of lots of line lengths up to but not exceeding smallMaxTokenSize. tmp := new(bytes.Buffer) buf := new(bytes.Buffer) lineNum := 0 j := 0 for i := 0; i < 2*smallMaxTokenSize; i++ { genLine(tmp, lineNum, j, true) j++ buf.Write(tmp.Bytes()) lineNum++ } s := NewScanner(&slowReader{3, buf}) s.Split(ScanLines) s.MaxTokenSize(smallMaxTokenSize) j = 0 for lineNum := 0; s.Scan(); lineNum++ { genLine(tmp, lineNum, j, false) if j < smallMaxTokenSize { j++ } else { j-- } line := tmp.Bytes() if !bytes.Equal(s.Bytes(), line) { t.Errorf("%d: bad line: %d %d\n%.100q\n%.100q\n", lineNum, len(s.Bytes()), len(line), s.Bytes(), line) } } err := s.Err() if err != ErrTooLong { t.Fatalf("expected ErrTooLong; got %s", err) } }
func (options *Latex) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) { out.WriteString("\\href{") out.Write(link) out.WriteString("}{") out.Write(content) out.WriteString("}") }
// TODO: This might now work func (r *roffRenderer) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) { out.WriteString(".TS\nallbox;\n") out.Write(header) out.Write(body) out.WriteString("\n.TE\n") }
func (g *Braintree) execute(method, path string, xmlObj interface{}) (*Response, error) { var buf bytes.Buffer if xmlObj != nil { xmlBody, err := xml.Marshal(xmlObj) if err != nil { return nil, err } _, err = buf.Write(xmlBody) if err != nil { return nil, err } } url := g.MerchantURL() + "/" + path if g.Logger != nil { g.Logger.Printf("> %s %s\n%s", method, url, buf.String()) } req, err := http.NewRequest(method, url, &buf) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/xml") req.Header.Set("Accept", "application/xml") req.Header.Set("Accept-Encoding", "gzip") req.Header.Set("User-Agent", "Braintree Go 0.3.1") req.Header.Set("X-ApiVersion", "3") req.SetBasicAuth(g.PublicKey, g.PrivateKey) httpClient := g.HttpClient if httpClient == nil { httpClient = http.DefaultClient } resp, err := httpClient.Do(req) if err != nil { return nil, err } defer resp.Body.Close() btr := &Response{ Response: resp, } err = btr.unpackBody() if err != nil { return nil, err } if g.Logger != nil { g.Logger.Printf("<\n%s", string(btr.Body)) } err = btr.apiError() if err != nil { return nil, err } return btr, nil }
// TODO: This is probably broken func (r *roffRenderer) TableCell(out *bytes.Buffer, text []byte, align int) { if out.Len() > 0 { out.WriteString("\t") } out.Write(text) out.WriteString("\t") }
func serializeV1PrecertSCTSignatureInput(timestamp uint64, issuerKeyHash [issuerKeyHashLength]byte, tbs []byte, ext CTExtensions) ([]byte, error) { if err := checkCertificateFormat(tbs); err != nil { return nil, err } if err := checkExtensionsFormat(ext); err != nil { return nil, err } var buf bytes.Buffer if err := binary.Write(&buf, binary.BigEndian, V1); err != nil { return nil, err } if err := binary.Write(&buf, binary.BigEndian, CertificateTimestampSignatureType); err != nil { return nil, err } if err := binary.Write(&buf, binary.BigEndian, timestamp); err != nil { return nil, err } if err := binary.Write(&buf, binary.BigEndian, PrecertLogEntryType); err != nil { return nil, err } if _, err := buf.Write(issuerKeyHash[:]); err != nil { return nil, err } if err := writeVarBytes(&buf, tbs, CertificateLengthBytes); err != nil { return nil, err } if err := writeVarBytes(&buf, ext, ExtensionsLengthBytes); err != nil { return nil, err } return buf.Bytes(), nil }
// joinChart reads chart files and joins them with YAML delimiters func joinChart(chartDir string, files []string) (bytes.Buffer, error) { var output bytes.Buffer for _, f := range files { contents, err := ioutil.ReadFile(f) if err != nil { return output, err } rf, err := filepath.Rel(chartDir, f) if err != nil { log.Warn("Could not find relative path: %s", err) return output, err } delimiter := fmt.Sprintf("--- # %s\n", rf) output.WriteString(delimiter) output.Write(contents) output.WriteString("--- # end\n") } return output, nil }
func (mfsm *MyFsm) Restore(inp io.ReadCloser) error { defer inp.Close() fmt.Printf("Restore......................\n") mfsm.mutex.Lock() defer mfsm.mutex.Unlock() var buffer bytes.Buffer readdata := make([]byte, 1024) for { n, err := inp.Read(readdata) if err != nil { panic(err) } if n < 1024 { if n > 0 { lastbytes := make([]byte, n) copy(readdata, lastbytes) buffer.Write(lastbytes) } break } else { buffer.Write(readdata) } } dec := gob.NewDecoder(&buffer) err := dec.Decode(&mfsm.data) errorOnExit(err) return nil }
func (ss *StoreServer) uploadHandler(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() vars := mux.Vars(r) fileIDStr := vars["fileID"] volID, needleID, cookie, err := newFileID(fileIDStr) if err != nil { helper.WriteJson(w, result{Error: err.Error()}, http.StatusInternalServerError) return } if ss.volumeMap[volID] == nil { helper.WriteJson(w, result{Error: fmt.Sprintf("no volume %d", volID)}, http.StatusInternalServerError) return } data, name, err := parseUpload(r) if err != nil { helper.WriteJson(w, result{Error: err.Error()}, http.StatusInternalServerError) return } n := storage.NewNeedle(cookie, needleID, data, name) if err = ss.volumeMap[volID].AppendNeedle(n); err != nil { helper.WriteJson(w, result{Error: err.Error()}, http.StatusInternalServerError) return } fi, _ := ss.volumeMap[volID].StoreFile.Stat() vi := volumeInfo{ ID: volID, Size: fi.Size(), } viBytes, _ := json.Marshal(vi) for i := range ss.conf.Directories { // send volume information to directory server var b bytes.Buffer b.Write(viBytes) _, err := postAndError("http://"+ss.conf.Directories[i]+"/vol/info", "application/json", &b) if err == nil { break } else { log4go.Warn("send volumeInfo to directory get err: %s", err.Error()) } } for _, localVolIDIP := range ss.localVolIDIPs { if localVolIDIP.ID == volID { for _, ip := range localVolIDIP.IP { if ip != ss.Addr { if err = replicateUpload(fmt.Sprintf("http://%s/replicate/%s", ip, fileIDStr), string(name), data); err != nil { helper.WriteJson(w, result{Error: err.Error()}, http.StatusInternalServerError) return } } } break } } res := result{ Name: string(name), Size: len(data), } helper.WriteJson(w, res, http.StatusOK) }
// Use the codec to convert a string func (cd *Iconv) Conv(input string) (result string, err error) { var buf bytes.Buffer if len(input) == 0 { return "", nil } inbuf := []byte(input) outbuf := make([]byte, bufSize) inbytes := C.size_t(len(inbuf)) inptr := &inbuf[0] for inbytes > 0 { outbytes := C.size_t(len(outbuf)) outptr := &outbuf[0] _, err = C.iconv(cd.pointer, (**C.char)(unsafe.Pointer(&inptr)), &inbytes, (**C.char)(unsafe.Pointer(&outptr)), &outbytes) buf.Write(outbuf[:len(outbuf)-int(outbytes)]) if err != nil && err != syscall.E2BIG { return buf.String(), err } } return buf.String(), nil }
func (options *Html) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) { if options.flags&HTML_SKIP_LINKS != 0 { // write the link text out but don't link it, just mark it with typewriter font out.WriteString("<tt>") options.attrEscape(out, content) out.WriteString("</tt>") return } if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) { // write the link text out but don't link it, just mark it with typewriter font out.WriteString("<tt>") options.attrEscape(out, content) out.WriteString("</tt>") return } out.WriteString("<a href=\"") options.attrEscape(out, link) if len(title) > 0 { out.WriteString("\" title=\"") options.attrEscape(out, title) } out.WriteString("\">") out.Write(content) out.WriteString("</a>") return }
func fieldModelProto(r *http.Request, h http.Header, b *bytes.Buffer) *weft.Result { var err error var rows *sql.Rows if rows, err = dbR.Query(`SELECT modelID FROM field.model`); err != nil { return weft.InternalServerError(err) } var fmr mtrpb.FieldModelResult for rows.Next() { var t mtrpb.FieldModel if err = rows.Scan(&t.ModelID); err != nil { return weft.InternalServerError(err) } fmr.Result = append(fmr.Result, &t) } var by []byte if by, err = proto.Marshal(&fmr); err != nil { return weft.InternalServerError(err) } b.Write(by) return &weft.StatusOK }
func (options *Html) Smartypants(out *bytes.Buffer, text []byte) { smrt := smartypantsData{false, false} // first do normal entity escaping var escaped bytes.Buffer attrEscape(&escaped, text) text = escaped.Bytes() mark := 0 for i := 0; i < len(text); i++ { if action := options.smartypants[text[i]]; action != nil { if i > mark { out.Write(text[mark:i]) } previousChar := byte(0) if i > 0 { previousChar = text[i-1] } i += action(out, &smrt, previousChar, text[i:]) mark = i + 1 } } if mark < len(text) { out.Write(text[mark:]) } }
func TestWriterOffset(t *testing.T) { largeData := make([]byte, 1<<17) for i := range largeData { largeData[i] = byte(rand.Int()) } writeTests[1].Data = largeData defer func() { writeTests[1].Data = nil }() // write a zip file buf := new(bytes.Buffer) existingData := []byte{1, 2, 3, 1, 2, 3, 1, 2, 3} n, _ := buf.Write(existingData) w := NewWriter(buf) w.SetOffset(int64(n)) for _, wt := range writeTests { testCreate(t, w, &wt) } if err := w.Close(); err != nil { t.Fatal(err) } // read it back r, err := NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len())) if err != nil { t.Fatal(err) } for i, wt := range writeTests { testReadFile(t, r.File[i], &wt) } }