// 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) }
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) } }
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) }
// 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) }
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) } }
// 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) }