当前位置: 首页>>代码示例>>Golang>>正文


Golang Buffer.Contains方法代码示例

本文整理汇总了Golang中github.com/mbenkmann/golib/bytes.Buffer.Contains方法的典型用法代码示例。如果您正苦于以下问题:Golang Buffer.Contains方法的具体用法?Golang Buffer.Contains怎么用?Golang Buffer.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/mbenkmann/golib/bytes.Buffer的用法示例。


在下文中一共展示了Buffer.Contains方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: testBuffer

func testBuffer() {
	var b bytes.Buffer
	check(b.String(), "") // String() on fresh variable
	b.Reset()             // Reset() on fresh variable
	check(b.String(), "") // String() after Reset()
	b.Reset()             // Reset() after Reset()
	check(b.String(), "")
	check(b.Len(), 0)

	// same tests as above with pointer
	b2 := &bytes.Buffer{}
	check(b2.String(), "")
	b2.Reset()
	check(b2.String(), "")
	b2.Reset()
	check(b2.String(), "")
	check(b2.Len(), 0)

	b2.WriteString("Dies ist ein Test!")
	check(b2.String(), "Dies ist ein Test!")
	check(b2.Len(), 18)

	n, err := b.Write(nil)
	check(n, 0)
	check(err, nil)
	check(b.String(), "")

	n, err = b.Write([]byte{})
	check(n, 0)
	check(err, nil)
	check(b.String(), "")
	check(b.Pointer(), nil)
	check(b.Capacity(), 0)
	check(b.Len(), 0)

	func() {
		defer func() {
			check(recover(), bytes.ErrTooLarge)
		}()
		b.Grow(-1)
	}()

	n, err = b.Write([]byte{'a'})
	check(n, 1)
	check(err, nil)
	check(b.String(), "a")
	check(b.Capacity() >= 1, true)
	check(b.Len(), 1)
	check(b.Pointer() != nil, true)

	check(b.Grow(11), 1)
	check(b.Capacity() >= 12, true)
	c := b.Capacity()
	p := b.Pointer()
	check(b.Grow(11), 1) // should not cause actual growth
	check(b.Pointer(), p)
	check(b.Capacity(), c)
	check(b.Len(), 1)
	((*[2]byte)(b.Pointer()))[1] = 'z'
	check(b.Contains("z"), false)

	n, err = b.WriteString("Hallo")
	check(n, 5)
	check(err, nil)
	check(b.String(), "aHallo")
	check(b.Pointer(), p)
	check(b.Capacity(), c)
	check(b.Len(), 6)

	b.Reset()
	check(b.String(), "")
	check(b.Pointer(), nil)
	check(b.Capacity(), 0)
	check(b.Contains(""), true)
	check(b.Contains("a"), false)

	b.WriteString("Hallo")
	b.WriteByte(' ')
	b.Write([]byte{'d', 'i', 'e', 's'})
	b.WriteByte(' ')
	b.WriteString("ist ")
	b.WriteString("ein ")
	b.Write([]byte("Test"))
	check(b.String(), "Hallo dies ist ein Test")
	check(b.Contains("Hallo dies ist ein Test"), true)
	check(b.Contains("Test"), true)
	check(b.Contains("Hallo"), true)
	check(b.Contains("allo"), true)
	check(b.Contains(""), true)

	check(b.Split(" "), []string{"Hallo", "dies", "ist", "ein", "Test"})
	check(b.Split("X"), []string{"Hallo dies ist ein Test"})
	check(b.Split("Hallo dies ist ein Test"), []string{"", ""})
	check(b.Split("H"), []string{"", "allo dies ist ein Test"})
	check(b.Split("Test"), []string{"Hallo dies ist ein ", ""})
	check(b.Split("es"), []string{"Hallo di", " ist ein T", "t"})

	b.Reset()
	b.WriteString("  \n\t Hallo  \t\v\n")
	check(b.Len(), 15)
//.........这里部分代码省略.........
开发者ID:chrlutz,项目名称:limux-gosa,代码行数:101,代码来源:bytes-test.go

示例2: ProcessEncryptedMessage

// Takes a possibly encrypted message in buf and processes it, returning a reply.
// context is the security context.
// Returns:
//  buffer containing the reply to return (MUST BE FREED BY CALLER VIA Reset()!)
//  disconnect == true if connection should be terminated due to error
//
// NOTE: buf IS NOT FREED BY THIS FUNCTION BUT ITS CONTENTS ARE CHANGED!
func ProcessEncryptedMessage(buf *bytes.Buffer, context *security.Context) (reply *bytes.Buffer, disconnect bool) {
	if buf.Len() > 4096 {
		util.Log(2, "DEBUG! Processing LONG message: (truncated)%v\n.\n.\n.\n%v", string(buf.Bytes()[0:2048]), string(buf.Bytes()[buf.Len()-2048:]))
	} else {
		util.Log(2, "DEBUG! Processing message: %v", buf.String())
	}

	for attempt := 0; attempt < 4; attempt++ {
		if attempt != 0 && config.TLSRequired {
			util.Log(1, "INFO! [SECURITY] TLS-only mode => Decryption with old protocol will not be attempted")
			//NOTE: This prevents the last ditch attempt to decrypt with all known
			//      server and client keys. This attempt might still have produced a
			//      result in case the connecting party is pre-TLS and we happen to
			//      have its key in our database (from a time before our server was
			//      configured to be TLS-only). However if the admin configured our
			//      server to be TLS-only (by not putting any keys
			//      in the config) we assume that he does not want pre-TLS
			//      parties to connect.
			break
		}
		var keys_to_try []string

		switch attempt {
		case 0:
			keys_to_try = config.ModuleKeys
		case 1:
			host := context.PeerID.IP.String()
			{
				keys_to_try = append(db.ServerKeys(host), db.ClientKeys(host)...)
				if host == "127.0.0.1" { // make sure we find the key even if registered under our external IP address
					keys_to_try = append(db.ServerKeys(config.IP), db.ClientKeys(config.IP)...)
				}
			}
		case 2:
			util.Log(1, "INFO! Last resort attempt to decrypt message from %v with all server keys", context.PeerID.IP)
			keys_to_try = db.ServerKeysForAllServers()
		case 3:
			util.Log(1, "INFO! Last resort attempt to decrypt message from %v with all client keys", context.PeerID.IP)
			keys_to_try = db.ClientKeysForAllClients()
		}

		for _, key := range keys_to_try {
			if security.GosaDecryptBuffer(buf, key) {
				if buf.Len() > 4096 {
					util.Log(2, "DEBUG! Decrypted LONG message from %v with key %v: (truncated)%v\n.\n.\n.\n%v", context.PeerID.IP, key, string(buf.Bytes()[0:2048]), string(buf.Bytes()[buf.Len()-2048:]))
				} else {
					util.Log(2, "DEBUG! Decrypted message from %v with key %v: %v", context.PeerID.IP, key, buf.String())
				}

				// special case for CLMSG_save_fai_log because this kind of message
				// is so large and parsing it to XML doesn't really gain us anything.
				if buf.Contains("<CLMSG_save_fai_log>") {
					if handleServerMessage() {
						clmsg_save_fai_log(buf)
					}
					return &bytes.Buffer{}, false
				}

				xml, err := xml.StringToHash(buf.String())
				if err != nil {
					util.Log(0, "ERROR! %v", err)
					return ErrorReplyBuffer(err), true
				}

				// At this point we have successfully decrypted and parsed the message
				return ProcessXMLMessage(xml, context, key)
			}
		}
	}

	// This part is only reached if none of the keys opened the message
	util.Log(0, "ERROR! Could not decrypt message from %v", context.PeerID.IP)

	// Maybe we got out of sync with the sender's encryption key
	// (e.g. by missing a new_key message). Try to re-establish communcation.
	ip := context.PeerID.IP.To4()
	if ip == nil {
		util.Log(0, "ERROR! Cannot convert sender address to IPv4 address: %v", context.PeerID.IP)
	} else {
		go tryToReestablishCommunicationWith(ip.String())
	}

	return ErrorReplyBuffer("Could not decrypt message"), true
}
开发者ID:chrlutz,项目名称:limux-gosa,代码行数:91,代码来源:process_msg.go


注:本文中的github.com/mbenkmann/golib/bytes.Buffer.Contains方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。