• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang byteutils.Replace函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/buger/gor/byteutils.Replace函数的典型用法代码示例。如果您正苦于以下问题:Golang Replace函数的具体用法?Golang Replace怎么用?Golang Replace使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: SetPathParam

// SetPathParam takes payload and updates path Query attribute
// If query param not found, it will append new
// Returns modified payload
func SetPathParam(payload, name, value []byte) []byte {
	path := Path(payload)
	_, vs, ve := PathParam(payload, name)

	if vs != -1 { // If param found, replace its value and set new Path
		newPath := make([]byte, len(path))
		copy(newPath, path)
		newPath = byteutils.Replace(newPath, vs, ve, value)

		return SetPath(payload, newPath)
	}

	// if param not found append to end of url
	// Adding 2 because of '?' or '&' at start, and '=' in middle
	newParam := make([]byte, len(name)+len(value)+2)

	if bytes.IndexByte(path, '?') == -1 {
		newParam[0] = '?'
	} else {
		newParam[0] = '&'
	}

	// Copy "param=value" into buffer, after it looks like "?param=value"
	copy(newParam[1:], name)
	newParam[1+len(name)] = '='
	copy(newParam[2+len(name):], value)

	// Append param to the end of path
	newPath := make([]byte, len(path)+len(newParam))
	copy(newPath, path)
	copy(newPath[len(path):], newParam)

	return SetPath(payload, newPath)
}
开发者ID:robot0x,项目名称:gor,代码行数:37,代码来源:proto.go


示例2: SetPathParam

func SetPathParam(payload, name, value []byte) []byte {
	path := Path(payload)
	_, vs, ve := PathParam(payload, name)

	if vs != -1 {
		newPath := make([]byte, len(path))
		copy(newPath, path)
		newPath = byteutils.Replace(newPath, vs, ve, value)

		return SetPath(payload, newPath)
	} else { // if param not found append to end of url
		// Adding 2 because of '?' or '&' at start, and '=' in middle
		newParam := make([]byte, len(name)+len(value)+2)

		if bytes.IndexByte(path, '?') == -1 {
			newParam[0] = '?'
		} else {
			newParam[0] = '&'
		}

		copy(newParam[1:], name)
		newParam[1+len(name)] = '='
		copy(newParam[2+len(name):], value)

		newPath := make([]byte, len(path)+len(newParam))
		copy(newPath, path)
		copy(newPath[len(path):], newParam)

		return SetPath(payload, newPath)
	}
}
开发者ID:newyue588cc,项目名称:gor,代码行数:31,代码来源:proto.go


示例3: SetPath

func SetPath(payload, path []byte) []byte {
	start := bytes.IndexByte(payload, ' ')
	start += 1

	end := bytes.IndexByte(payload[start:], ' ')

	return byteutils.Replace(payload, start, start+end, path)
}
开发者ID:newyue588cc,项目名称:gor,代码行数:8,代码来源:proto.go


示例4: SetHeader

// SetHeader sets header value. If header not found it creates new one.
// Returns modified request payload
func SetHeader(payload, name, value []byte) []byte {
	_, hs, vs, he := header(payload, name)

	if hs != -1 {
		// If header found we just repace its value
		return byteutils.Replace(payload, vs, he, value)
	}

	return AddHeader(payload, name, value)
}
开发者ID:robot0x,项目名称:gor,代码行数:12,代码来源:proto.go


示例5: SetHeader

func SetHeader(payload, name, value []byte) []byte {
	_, hs, vs, he := Header(payload, name)

	// If header found
	if hs != -1 {
		return byteutils.Replace(payload, vs, he, value)
	} else {
		return AddHeader(payload, name, value)
	}
}
开发者ID:newyue588cc,项目名称:gor,代码行数:10,代码来源:proto.go


示例6: SetHost

// SetHost updates Host header for HTTP/1.1 or updates host in path for HTTP/1.0 or Proxy requests
// Returns modified payload
func SetHost(payload, url, host []byte) []byte {
	// If this is HTTP 1.0 traffic or proxy traffic it may include host right into path variable, so instead of setting Host header we rewrite Path
	// Fix for https://github.com/buger/gor/issues/156
	if path := Path(payload); bytes.HasPrefix(path, []byte("http")) {
		hostStart := bytes.IndexByte(path, ':') // : position "https?:"
		hostStart += 3                          // Skip 1 ':' and 2 '\'
		hostEnd := hostStart + bytes.IndexByte(path[hostStart:], '/')

		newPath := make([]byte, len(path))
		copy(newPath, path)
		newPath = byteutils.Replace(newPath, 0, hostEnd, url)

		return SetPath(payload, newPath)
	}

	return SetHeader(payload, []byte("Host"), host)
}
开发者ID:robot0x,项目名称:gor,代码行数:19,代码来源:proto.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang proto.Header函数代码示例发布时间:2022-05-24
下一篇:
Golang utils.NewError函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap