示例#1
0
func testRandBytes(tt testing2.TB, f func(int) ([]byte, error), charset string) {
	s, e := f(32)
	tt.
		Eq(e, nil).
		Eq(32, len(s)).
		True(strings2.IsAllCharsIn(unsafe2.String(s), charset))
}
示例#2
0
func ReadProperties(fname string) (map[string]string, error) {
	props := make(map[string]string)
	err := file.Filter(fname, func(_ int, line []byte) ([]byte, error) {
		p := pair.Parse(unsafe2.String(line), "=").Trim()
		if p.HasKey() {
			props[p.Key] = strings2.TrimAfter(p.Value, "#")
		}

		return line, nil
	})

	return props, err
}
示例#3
0
func (w Writer) Write(bs []byte) (int, error) {
	i, err := w.Writer.Write(unsafe2.Bytes(w.Prefix))
	if err != nil {
		return 0, err
	}

	n, err := w.RenderTo(w.Writer, unsafe2.String(bs))
	if err != nil {
		return 0, err
	}

	return n + i, nil
}
示例#4
0
文件: mail.go 项目: sshitaime/gohper
func (m *Mailer) Send(mail *Mail) (err error) {
	tmpl, has := m.Templates[mail.Type]
	if !has && mail.RawContent == "" {
		return ErrNoTemplate
	}

	from := mail.From
	if from == "" {
		from = m.username
	}

	buffer := bytes.NewBuffer(m.bufferPool.Get(1024, false))

	buffer.WriteString("To:")
	strings2.WriteStringsToBuffer(buffer, mail.To, ";")

	buffer.WriteString("\r\n")
	buffer.WriteString("From:" + from + "\r\n")

	subject := mail.Subject
	if has && subject == "" {
		subject = tmpl.Subject
	}
	buffer.WriteString("Subject:" + subject + "\r\n")

	buffer.WriteString("Content-Type: text/html;charset=UTF-8\r\n\r\n")

	if mail.RawContent != "" {
		buffer.WriteString(mail.RawContent)
	} else {
		err = tmpl.Execute(buffer, mail.Data)
	}

	data := buffer.Bytes()
	if m.PrintMail {
		fmt.Println(unsafe2.String(data))
	}
	if err == nil {
		err = smtp.SendMail(m.addr, m.auth, from, mail.To, data)
	}
	m.bufferPool.Put(data)

	return
}
示例#5
0
func (r *Recovery) Filter(req zerver.Request, resp zerver.Response, chain zerver.FilterChain) {
	defer func() {
		e := recover()
		if e == nil || r.NoStack {
			return
		}

		resp.ReportInternalServerError()

		buffer := bytes.NewBuffer(make([]byte, 0, r.Bufsize))
		fmt.Fprint(buffer, e)
		buf := buffer.Bytes()

		runtime.Stack(buf[len(buf):cap(buf)], false)

		req.Logger().Errorln(unsafe2.String(buf[:cap(buf)]))
	}()

	chain(req, resp)
}
示例#6
0
文件: msq.go 项目: snowsnail/zerver
func (m *Queue) start() {
	defer func(m *Queue) {
		e := recover()
		if e == nil {
			return
		}
		if m.ErrorLogger != nil {
			m.ErrorLogger.Errorln(e)

			buf := m.BytesPool.Get(4096, true)

			index := runtime.Stack(buf, false)
			buf = buf[:index]
			m.ErrorLogger.Errorln(unsafe2.String(buf))

			m.BytesPool.Put(buf)
		}
		if !m.NoRecover {
			go m.start()
		}
	}(m)
	for {
		select {
		case msg, ok := <-m.queue:
			if !ok {
				return
			}
			err := m.Process(msg.Value())
			if err != nil {
				m.ErrorLogger.Errorln(msg.Pattern(), ":", err)
			}

			if len(m.queue) == 0 && m.closeCond != nil {
				m.closeCond.Signal()
			} else {
				break
			}
		}
	}
}
示例#7
0
func (r *Renderer) RenderString(str string) string {
	return unsafe2.String(r.begin) + str + unsafe2.String(r.end)
}