Beispiel #1
0
func writeSuccessResponseHeader(w *bufio.Writer, opcode uint8, keyLength, extraLength,
	totalBodyLength int, opaque uint32, flush bool) error {

	header := resHeadPool.Get().(ResponseHeader)

	header.Magic = MagicResponse
	header.Opcode = opcode
	header.KeyLength = uint16(keyLength)
	header.ExtraLength = uint8(extraLength)
	header.DataType = uint8(0)
	header.Status = StatusSuccess
	header.TotalBodyLength = uint32(totalBodyLength)
	header.OpaqueToken = opaque
	header.CASToken = uint64(0)

	if err := writeResponseHeader(w, header); err != nil {
		resHeadPool.Put(header)
		return err
	}

	if flush {
		if err := w.Flush(); err != nil {
			resHeadPool.Put(header)
			return err
		}
	}

	metrics.IncCounterBy(common.MetricBytesWrittenRemote, resHeaderLen)
	resHeadPool.Put(header)

	return nil
}
Beispiel #2
0
func writeBodyFixedSize(w *bufio.Writer, r io.Reader, size int64) error {
	if size > maxSmallFileSize {
		// w buffer must be empty for triggering
		// sendfile path in bufio.Writer.ReadFrom.
		if err := w.Flush(); err != nil {
			return err
		}
	}

	// Unwrap a single limited reader for triggering sendfile path
	// in net.TCPConn.ReadFrom.
	lr, ok := r.(*io.LimitedReader)
	if ok {
		r = lr.R
	}

	n, err := copyZeroAlloc(w, r)

	if ok {
		lr.N -= n
	}

	if n != size && err == nil {
		err = fmt.Errorf("copied %d bytes from body stream instead of %d bytes", n, size)
	}
	return err
}
Beispiel #3
0
func (r Resolver) outputResults(w *bufio.Writer, results gresults) {
	var zip, city, state, stateName, county, country, countryName string
	var latitude, longitude float32

	var comp addressComponent
	var err error
	for _, res := range results.Results {
		if comp, err = res.findAddressComponent(zipType); err == nil {
			zip = comp.LongName
		}
		if comp, err = res.findAddressComponent(cityType); err == nil {
			city = comp.LongName
		}
		if comp, err = res.findAddressComponent(countyType); err == nil {
			county = comp.LongName
		}
		if comp, err = res.findAddressComponent(stateType); err == nil {
			state = strings.ToUpper(comp.ShortName)
			stateName = comp.LongName
		}
		if comp, err = res.findAddressComponent(countryType); err == nil {
			country = strings.ToUpper(comp.ShortName)
			countryName = comp.LongName
		}
		latitude = res.Geometry.Location.Lat
		longitude = res.Geometry.Location.Lng
	}
	w.WriteString(fmt.Sprintf("%v,\"%v\",\"%v\",\"%v\",\"%v\",\"%v\",\"%v\",%v,%v\n", country, zip, city, state, stateName, county, countryName, latitude, longitude))
	w.Flush()
}
// NewStreamReader returns a reader, which replays all the data generated by sw.
//
// The returned reader may be passed to Response.SetBodyStream.
//
// Close must be called on the returned reader after all the required data
// has been read. Otherwise goroutine leak may occur.
//
// See also Response.SetBodyStreamWriter.
func NewStreamReader(sw StreamWriter) io.ReadCloser {
	pc := fasthttputil.NewPipeConns()
	pw := pc.Conn1()
	pr := pc.Conn2()

	var bw *bufio.Writer
	v := streamWriterBufPool.Get()
	if v == nil {
		bw = bufio.NewWriter(pw)
	} else {
		bw = v.(*bufio.Writer)
		bw.Reset(pw)
	}

	go func() {
		defer func() {
			if r := recover(); r != nil {
				defaultLogger.Printf("panic in StreamWriter: %s\nStack trace:\n%s", r, debug.Stack())
			}
		}()

		sw(bw)
		bw.Flush()
		pw.Close()

		streamWriterBufPool.Put(bw)
	}()

	return pr
}
Beispiel #5
0
// authenticate authenticates ourselves with the X server.
// displayStr is the "12" out of ":12.0".
func authenticate(w *bufio.Writer, displayStr string) os.Error {
	key, value, err := readAuth(displayStr)
	if err != nil {
		return err
	}
	// Assume that the authentication protocol is "MIT-MAGIC-COOKIE-1".
	if len(key) != 18 || len(value) != 16 {
		return os.NewError("unsupported Xauth")
	}
	// 0x006c means little-endian. 0x000b, 0x0000 means X major version 11, minor version 0.
	// 0x0012 and 0x0010 means the auth key and value have lengths 18 and 16.
	// The final 0x0000 is padding, so that the string length is a multiple of 4.
	_, err = io.WriteString(w, "\x6c\x00\x0b\x00\x00\x00\x12\x00\x10\x00\x00\x00")
	if err != nil {
		return err
	}
	_, err = io.WriteString(w, key)
	if err != nil {
		return err
	}
	// Again, the 0x0000 is padding.
	_, err = io.WriteString(w, "\x00\x00")
	if err != nil {
		return err
	}
	_, err = io.WriteString(w, value)
	if err != nil {
		return err
	}
	err = w.Flush()
	if err != nil {
		return err
	}
	return nil
}
Beispiel #6
0
func (c *DefaultServerCodec) WriteResponse(wr *bufio.Writer, proto *Proto) (err error) {
	log.Debug("write proto: %v", proto)
	// packlen =header(16) + body
	if err = binary.Write(wr, binary.BigEndian, uint32(rawHeaderLen)+uint32(len(proto.Body))); err != nil {
		log.Error("packLen: binary.Write() error(%v)", err)
		return
	}
	if err = binary.Write(wr, binary.BigEndian, rawHeaderLen); err != nil {
		log.Error("headerLen: binary.Write() error(%v)", err)
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.Ver); err != nil {
		log.Error("protoVer: binary.Write() error(%v)", err)
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.Operation); err != nil {
		log.Error("operation: binary.Write() error(%v)", err)
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.SeqId); err != nil {
		log.Error("seqId: binary.Write() error(%v)", err)
		return
	}
	if proto.Body != nil {
		if _, err = wr.Write(proto.Body); err != nil {
			log.Error("body: wr.Write() error(%v)", err)
			return
		}
	}
	return wr.Flush()
}
Beispiel #7
0
// Write writes response to w.
//
// Write doesn't flush response to w for performance reasons.
func (resp *Response) Write(w *bufio.Writer) error {
	var err error
	if resp.bodyStream != nil {
		contentLength := resp.Header.ContentLength()
		if contentLength >= 0 {
			if err = resp.Header.Write(w); err != nil {
				return err
			}
			if err = writeBodyFixedSize(w, resp.bodyStream, contentLength); err != nil {
				return err
			}
		} else {
			resp.Header.SetContentLength(-1)
			if err = resp.Header.Write(w); err != nil {
				return err
			}
			if err = writeBodyChunked(w, resp.bodyStream); err != nil {
				return err
			}
		}
		if bsc, ok := resp.bodyStream.(io.Closer); ok {
			err = bsc.Close()
		}
		return err
	}

	resp.Header.SetContentLength(len(resp.body))
	if err = resp.Header.Write(w); err != nil {
		return err
	}
	_, err = w.Write(resp.body)
	return err
}
Beispiel #8
0
func doWriteRune(w *bufio.Writer) {
	if written, err := w.WriteRune(rune('o')); err != nil {
		log.Fatalf("failed writing a rune: %s", err)
	} else {
		log.Printf("Wrote rune in %d bytes", written)
	}
}
Beispiel #9
0
func doWriteString(w *bufio.Writer) {
	if written, err := w.WriteString(", The Standard Library\n"); err != nil {
		log.Fatalf("failed writing string: %s", err)
	} else {
		log.Printf("Wrote string in %d bytes", written)
	}
}
func emit_allele_path_item(ofp *bufio.Writer) {

	for k := range g_allele_path_item {

		for i := 0; i < len(g_allele_path_item[k]); i++ {
			s := fmt.Sprintf("%d,%d,%d,%d,%d,%s\n",
				g_allele_path_item[k][i].AlleleId,
				g_allele_path_item[k][i].PathItemIndex,
				g_allele_path_item[k][i].SequenceId,
				g_allele_path_item[k][i].Start,
				g_allele_path_item[k][i].Length,
				g_allele_path_item[k][i].StrandIsForward)
			ofp.Write([]byte(s))
		}
	}

	return

	for allele_name := range g_allele_sequenceid_path {

		allele_id := g_allele_name_id_map[allele_name]

		for i := 0; i < len(g_allele_sequenceid_path[allele_name]); i++ {
			seqid := g_allele_sequenceid_path[allele_name][i]
			s := fmt.Sprintf("%d,%d,%d,%s,%s,%s\n", allele_id, i, seqid, "NULL", "NULL", "'TRUE'")
			ofp.Write([]byte(s))
		}

	}
}
Beispiel #11
0
// Write writes request to w.
//
// Write doesn't flush request to w for performance reasons.
func (req *Request) Write(w *bufio.Writer) error {
	if len(req.Header.Host()) == 0 {
		uri := req.URI()
		host := uri.Host()
		if len(host) == 0 {
			return errRequestHostRequired
		}
		req.Header.SetHostBytes(host)
		req.Header.SetRequestURIBytes(uri.RequestURI())
	}

	body := req.body
	var err error
	if req.multipartForm != nil && len(req.body) == 0 {
		body, err = marshalMultipartForm(req.multipartForm, req.multipartFormBoundary)
		if err != nil {
			return fmt.Errorf("error when marshaling multipart form: %s", err)
		}
		req.Header.SetMultipartFormBoundary(req.multipartFormBoundary)
	}

	hasBody := !req.Header.noBody()
	if hasBody {
		req.Header.SetContentLength(len(body))
	}
	if err = req.Header.Write(w); err != nil {
		return err
	}
	if hasBody {
		_, err = w.Write(body)
	} else if len(body) > 0 {
		return fmt.Errorf("Non-zero body for non-POST request. body=%q", body)
	}
	return err
}
Beispiel #12
0
func writeln(w *bufio.Writer, s string) error {
	_, err := w.WriteString(s)
	if err != nil {
		return err
	}
	return newLine(w)
}
Beispiel #13
0
Datei: ihex.go Projekt: iand/go
// Function IHex.Write writes the representation of the file to the writer `wr`.
func (ix *IHex) Write(wr *bufio.Writer) (err error) {
	for start, data := range ix.areas {
		i := 0

		for i < len(data) {
			var chunk []byte

			if i+16 <= len(data) {
				chunk = data[i : i+16]
			} else {
				chunk = data[i:]
			}

			_, err := wr.Write([]byte(makeLine(0x00, start, chunk)))
			if err != nil {
				return err
			}

			start += 16
		}
	}

	_, err = wr.Write([]byte(makeLine(0x01, 0, []byte{})))
	if err != nil {
		return err
	}

	return nil
}
Beispiel #14
0
func writeErrorResponseHeader(w *bufio.Writer, opcode uint8, status uint16, opaque uint32) error {
	header := resHeadPool.Get().(ResponseHeader)

	header.Magic = MagicResponse
	header.Opcode = opcode
	header.KeyLength = uint16(0)
	header.ExtraLength = uint8(0)
	header.DataType = uint8(0)
	header.Status = status
	header.TotalBodyLength = uint32(0)
	header.OpaqueToken = opaque
	header.CASToken = uint64(0)

	if err := writeResponseHeader(w, header); err != nil {
		resHeadPool.Put(header)
		return err
	}

	if err := w.Flush(); err != nil {
		resHeadPool.Put(header)
		return err
	}

	metrics.IncCounterBy(common.MetricBytesWrittenRemote, resHeaderLen)
	resHeadPool.Put(header)

	return nil
}
Beispiel #15
0
func (t *TcpPlayer) sendUpdates(w *bufio.Writer, wg *sync.WaitGroup) {
	defer wg.Done()

	enc := json.NewEncoder(w)

	lastTurnSent := -1
	for update := range t.update {
		t.stateL.Lock()
		t.state = update
		t.stateL.Unlock()

		if update.Turn == lastTurnSent {
			continue
		}

		if err := enc.Encode(update); err != nil {
			t.l.Errorf("[TCP] sending update to player, %v", err)
			return
		}

		if _, err := w.WriteRune('\n'); err != nil {
			t.l.Errorf("[TCP] sending EOL to player, %v", err)
			return
		}

		if err := w.Flush(); err != nil {
			t.l.Errorf("[TCP] flushing update to player, %v", err)
			return
		}
		lastTurnSent = update.Turn
	}
}
Beispiel #16
0
Datei: sx.go Projekt: hlandau/sx
func writeBase64String(s string, b *bufio.Writer, f *Format) {
  b.WriteRune('|')
  w := base64.NewEncoder(base64.StdEncoding, b)
  w.Write([]byte(s))
  w.Close()
  b.WriteRune('|')
}
Beispiel #17
0
// writeTo serializes the processing instruction to the writer.
func (p *ProcInst) writeTo(w *bufio.Writer) {
	w.WriteString("<?")
	w.WriteString(p.Target)
	w.WriteByte(' ')
	w.WriteString(p.Inst)
	w.WriteString("?>")
}
Beispiel #18
0
// Write Bind Response PDU
func (pdu *PDUBindResp) write(w *bufio.Writer) (err os.Error) {
	// Write Header
	err = pdu.Header.write(w)
	if err != nil {
		err = os.NewError("Bind Response: Error writing Header")
		return
	}
	// Create byte array the size of the PDU
	p := make([]byte, pdu.Header.CmdLength-pdu.OptionalLen-16)
	pos := 0
	// Copy system id
	if len(pdu.SystemId) > 0 {
		copy(p[pos:len(pdu.SystemId)], []byte(pdu.SystemId))
		pos += len(pdu.SystemId)
	}
	pos++ // Null terminator
	// Write to buffer
	_, err = w.Write(p)
	if err != nil {
		err = os.NewError("Bind Response: Error writing to buffer")
		return
	}
	// Flush write buffer
	err = w.Flush()
	if err != nil {
		err = os.NewError("Bind Response: Error flushing write buffer")
	}
	// Optional params
	err = pdu.writeOptional(w)
	if err != nil {
		err = os.NewError("Bind Response: Error writing optional params")
	}
	return
}
Beispiel #19
0
func WriteProto(wr *bufio.Writer, proto *Proto) (err error) {
	// write
	if err = binary.Write(wr, binary.BigEndian, uint32(rawHeaderLen)+uint32(len(proto.Body))); err != nil {
		return
	}
	if err = binary.Write(wr, binary.BigEndian, rawHeaderLen); err != nil {
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.Ver); err != nil {
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.Operation); err != nil {
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.SeqId); err != nil {
		return
	}
	if proto.Body != nil {
		log.Debug("cipher body: %v", proto.Body)
		if err = binary.Write(wr, binary.BigEndian, proto.Body); err != nil {
			return
		}
	}
	err = wr.Flush()
	return
}
Beispiel #20
0
// Write Optional param
func (op *pduOptParam) write(w *bufio.Writer) (err os.Error) {
	// Create byte array
	p := make([]byte, 4+op.length)
	copy(p[0:2], packUint(uint64(op.tag), 2))
	copy(p[2:4], packUint(uint64(op.length), 2))
	// Determine data type of value
	v := reflect.NewValue(op.value)
	switch t := v.(type) {
	case *reflect.StringValue:
		copy(p[4:op.length], []byte(op.value.(string)))
	case *reflect.Uint8Value:
		p[4] = byte(op.value.(uint8))
	case *reflect.Uint16Value:
		copy(p[4:6], packUint(uint64(op.value.(uint16)), 2))
	case *reflect.Uint32Value:
		copy(p[4:8], packUint(uint64(op.value.(uint32)), 4))
	}
	// Write to buffer
	_, err = w.Write(p)
	if err != nil {
		return
	}
	// Flush write buffer
	err = w.Flush()
	return
}
func writeToStderr(msg string, errWriter *bufio.Writer) {
	if !strings.HasSuffix(msg, "\n") {
		msg = msg + "\n"
	}
	errWriter.WriteString(msg)
	errWriter.Flush()
}
func main() {
	flag.Parse()
	checkFlags()

	var csvWriter *bufio.Writer
	if *csvFile != "" {
		f, err := os.Create(*csvFile)
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close()
		csvWriter = bufio.NewWriter(f)
		defer csvWriter.Flush()

	}
	isFirstFile := true
	if *multiple {
		// Process multiple history files
		filepath.Walk(*input, func(filePath string, f os.FileInfo, err error) error {
			if filePath == *input {
				return nil
			}
			fmt.Println("Processing ", filePath, "...")
			result := processFile(filePath, csvWriter, isFirstFile)
			fmt.Println(result)
			isFirstFile = false
			return nil
		})
	} else {
		result := processFile(*input, csvWriter, isFirstFile)
		fmt.Println(result)
	}
}
Beispiel #23
0
func new_line(fw *bufio.Writer, email string, dest string) {
	pad := 40 - len(email)
	if pad <= 0 {
		pad = 1
	}
	fw.WriteString(email + strings.Repeat(" ", pad) + dest + "\n")
}
Beispiel #24
0
func HandshakeSample(c net.Conn, br *bufio.Reader, bw *bufio.Writer, timeout time.Duration) (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()
	// Send C0+C1
	err = bw.WriteByte(0x03)
	c1 := CreateRandomBlock(RTMP_SIG_SIZE)
	for i := 0; i < 8; i++ {
		c1[i] = 0
	}
	bw.Write(c1)
	err = bw.Flush()
	CheckError(err, "Handshake() Flush C0+C1")
	// Read S0+S1+S2
	s0, err := br.ReadByte()
	CheckError(err, "Handshake() Read S0")
	if s0 != 0x03 {
		return errors.New(fmt.Sprintf("Handshake() Got S0: %x", s0))
	}
	s1 := make([]byte, RTMP_SIG_SIZE)
	_, err = io.ReadAtLeast(br, s1, RTMP_SIG_SIZE)
	CheckError(err, "Handshake() Read S1")
	bw.Write(s1)
	err = bw.Flush()
	CheckError(err, "Handshake() Flush C2")
	_, err = io.ReadAtLeast(br, s1, RTMP_SIG_SIZE)
	CheckError(err, "Handshake() Read S2")
	return
}
Beispiel #25
0
func (conn *Connection) writer() {
	var w *bufio.Writer
	var err error
	for {
		var packet []byte
		select {
		case packet = <-conn.packets:
		default:
			if w = conn.w; w != nil {
				if err := w.Flush(); err != nil {
					conn.closeConnection(err)
				}
			}
			select {
			case packet = <-conn.packets:
			case <-conn.control:
				return
			}
		}
		if packet == nil {
			return
		}
		if w = conn.w; w == nil {
			if _, w, err = conn.createConnection(); err != nil {
				return
			}
		}
		if err := write(w, packet); err != nil {
			conn.closeConnection(err)
			continue
		}
	}
}
Beispiel #26
0
func tokenize(bf2 *bufio.Reader, bf3 *bufio.Writer) {
	for s, err := bf2.ReadString('\n'); err == nil; s, err = bf2.ReadString('\n') {
		fs := strings.Split(s, "\t")

		doc, _ := NewDocument(fs[0])
		doc.SetInput("T", fs[1])
		doc.SetInput("A", fs[2])
		doc.Tokenize()
		doc.AssembleSentences()

		var soffs []string
		sents := doc.SectionSentences("T")
		for _, sent := range sents {
			soffs = append(soffs, fmt.Sprintf("%d:%d", sent.Begin(), sent.End()))
		}
		bf3.WriteString(fmt.Sprintf("%s\t%s\t", fs[0], strings.Join(soffs, ",")))

		soffs = soffs[:0]
		sents = doc.SectionSentences("A")
		for _, sent := range sents {
			soffs = append(soffs, fmt.Sprintf("%d:%d", sent.Begin(), sent.End()))
		}
		bf3.WriteString(fmt.Sprintf("%s\n", strings.Join(soffs, ",")))
	}
}
func writeHeader(output *bufio.Writer) {
	if *verbose && !*dryRun {
		fmt.Println("Writing source header")
	}
	licensePath := pathutils.Join(*projectPath, "GPL3-header.txt")
	license, err := os.Open(licensePath)
	if err != nil {
		panic(err)
	}
	defer func() {
		if err := license.Close(); err != nil {
			panic(err)
		}
	}()

	reader := bufio.NewReader(license)
	for {
		if *verbose {
			fmt.Printf("Got line: '%s'\n")
		}
		line, err := reader.ReadString('\n')
		if err != nil {
			if err != io.EOF {
				panic(err)
			}
		}
		fmt.Fprintf(output, "// %s", line)
		if err == io.EOF {
			break
		}
	}

	output.WriteString("\n//\n")
	output.Flush()
}
Beispiel #28
0
func SendHandshakeResponse(writer *bufio.Writer, status byte) (err error) {
	_, err = writer.Write([]byte{0x05, status})
	if err != nil {
		return
	}
	return writer.Flush()
}
Beispiel #29
0
// sendResponse send resp to client, sendResponse must be goroutine safe.
func (server *Server) writeTCPResponse(wr *bufio.Writer, proto *Proto) (err error) {
	if Conf.Debug {
		log.Debug("write proto: %v", proto)
	}
	if err = ioutil.WriteBigEndianInt32(wr, int32(rawHeaderLen)+int32(len(proto.Body))); err != nil {
		return
	}
	if err = ioutil.WriteBigEndianInt16(wr, rawHeaderLen); err != nil {
		return
	}
	if err = ioutil.WriteBigEndianInt16(wr, proto.Ver); err != nil {
		return
	}
	if err = ioutil.WriteBigEndianInt32(wr, proto.Operation); err != nil {
		return
	}
	if err = ioutil.WriteBigEndianInt32(wr, proto.SeqId); err != nil {
		return
	}
	if proto.Body != nil {
		if _, err = wr.Write(proto.Body); err != nil {
			return
		}
	}
	if err = wr.Flush(); err != nil {
		log.Error("tcp wr.Flush() error(%v)", err)
	}
	proto.Reset()
	return
}
Beispiel #30
0
func (l BList) renderBCode(w *bufio.Writer) {
	w.WriteString("l")
	for i := range l {
		l[i].renderBCode(w)
	}
	w.WriteString("e")
}