func (c *HTTPClient) Send(data []byte) (response []byte, err error) { if c.conn == nil || !c.isAlive() { Debug("Connecting:", c.baseURL) c.Connect() } timeout := time.Now().Add(5 * time.Second) c.conn.SetWriteDeadline(timeout) data = proto.SetHeader(data, []byte("Host"), []byte(c.baseURL.Host)) if c.config.Debug { Debug("Sending:", string(data)) } if _, err = c.conn.Write(data); err != nil { Debug("Write error:", err, c.baseURL) return } c.conn.SetReadDeadline(timeout) n, err := c.conn.Read(c.respBuf) if err != nil { Debug("READ ERRORR!", err, c.conn) return } payload := c.respBuf[:n] if c.config.Debug { Debug("Received:", string(payload)) } if c.config.FollowRedirects > 0 && c.redirectsCount < c.config.FollowRedirects { status := payload[9:12] // 3xx requests if status[0] == '3' { c.redirectsCount += 1 location, _, _, _ := proto.Header(payload, []byte("Location")) redirectPayload := []byte("GET " + string(location) + " HTTP/1.1\r\n\r\n") if c.config.Debug { Debug("Redirecting to: " + string(location)) } return c.Send(redirectPayload) } } c.redirectsCount = 0 return payload, err }
func (i *RAWInput) Read(data []byte) (int, error) { msg := <-i.data buf := msg.Bytes() var header []byte if msg.IsIncoming { header = payloadHeader(RequestPayload, msg.UUID(), msg.Start.UnixNano(), -1) if len(i.realIPHeader) > 0 { buf = proto.SetHeader(buf, i.realIPHeader, []byte(msg.IP().String())) } } else { header = payloadHeader(ResponsePayload, msg.UUID(), msg.AssocMessage.Start.UnixNano(), msg.End.UnixNano()-msg.AssocMessage.Start.UnixNano()) } copy(data[0:len(header)], header) copy(data[len(header):], buf) return len(buf) + len(header), nil }
func (m *HTTPModifier) Rewrite(payload []byte) (response []byte) { if len(m.config.methods) > 0 { method := proto.Method(payload) matched := false for _, m := range m.config.methods { if bytes.Equal(method, m) { matched = true break } } if !matched { return } } if len(m.config.headers) > 0 { for _, header := range m.config.headers { payload = proto.SetHeader(payload, []byte(header.Name), []byte(header.Value)) } } if len(m.config.params) > 0 { for _, param := range m.config.params { payload = proto.SetPathParam(payload, param.Name, param.Value) } } if len(m.config.urlRegexp) > 0 { path := proto.Path(payload) matched := false for _, f := range m.config.urlRegexp { if f.regexp.Match(path) { matched = true break } } if !matched { return } } if len(m.config.urlNegativeRegexp) > 0 { path := proto.Path(payload) for _, f := range m.config.urlNegativeRegexp { if f.regexp.Match(path) { return } } } if len(m.config.headerFilters) > 0 { for _, f := range m.config.headerFilters { value := proto.Header(payload, f.name) if len(value) > 0 && !f.regexp.Match(value) { return } } } if len(m.config.headerNegativeFilters) > 0 { for _, f := range m.config.headerNegativeFilters { value := proto.Header(payload, f.name) if len(value) > 0 && f.regexp.Match(value) { return } } } if len(m.config.headerHashFilters) > 0 { for _, f := range m.config.headerHashFilters { value := proto.Header(payload, f.name) if len(value) > 0 { hasher := fnv.New32a() hasher.Write(value) if (hasher.Sum32() % 100) >= f.percent { return } } } } if len(m.config.paramHashFilters) > 0 { for _, f := range m.config.paramHashFilters { value, s, _ := proto.PathParam(payload, f.name) if s != -1 { hasher := fnv.New32a() hasher.Write(value) if (hasher.Sum32() % 100) >= f.percent { return } } } } if len(m.config.urlRewrite) > 0 { path := proto.Path(payload) for _, f := range m.config.urlRewrite { if f.src.Match(path) { path = f.src.ReplaceAll(path, f.target) payload = proto.SetPath(payload, path) break } } } return payload }
func (c *HTTPClient) Send(data []byte) (response []byte, err error) { // Don't exit on panic defer func() { if r := recover(); r != nil { Debug("[HTTPClient]", r, string(data)) if _, ok := r.(error); !ok { log.Println("[HTTPClient] Failed to send request: ", string(data)) log.Println("PANIC: pkg:", r, debug.Stack()) } } }() if c.conn == nil || !c.isAlive() { Debug("[HTTPClient] Connecting:", c.baseURL) if err = c.Connect(); err != nil { log.Println("[HTTPClient] Connection error:", err) response = errorPayload(HTTP_CONNECTION_ERROR) return } } timeout := time.Now().Add(c.config.Timeout) c.conn.SetWriteDeadline(timeout) if !c.config.OriginalHost { data = proto.SetHost(data, []byte(c.baseURL), []byte(c.host)) } if c.auth != "" { data = proto.SetHeader(data, []byte("Authorization"), []byte(c.auth)) } if c.config.Debug { Debug("[HTTPClient] Sending:", string(data)) } if _, err = c.conn.Write(data); err != nil { Debug("[HTTPClient] Write error:", err, c.baseURL) response = errorPayload(HTTP_TIMEOUT) return } var readBytes, n int var currentChunk []byte timeout = time.Now().Add(c.config.Timeout) chunked := false contentLength := -1 currentContentLength := 0 chunks := 0 for { c.conn.SetReadDeadline(timeout) if readBytes < len(c.respBuf) { n, err = c.conn.Read(c.respBuf[readBytes:]) readBytes += n chunks++ if err != nil { if err == io.EOF { err = nil } break } // First chunk if chunked || contentLength != -1 { currentContentLength += n } else { // If headers are finished if bytes.Contains(c.respBuf[:readBytes], proto.EmptyLine) { if bytes.Equal(proto.Header(c.respBuf, []byte("Transfer-Encoding")), []byte("chunked")) { chunked = true } else { status, _ := strconv.Atoi(string(proto.Status(c.respBuf))) if (status >= 100 && status < 200) || status == 204 || status == 304 { contentLength = 0 } else { l := proto.Header(c.respBuf, []byte("Content-Length")) if len(l) > 0 { contentLength, _ = strconv.Atoi(string(l)) } } } currentContentLength += len(proto.Body(c.respBuf[:readBytes])) } } if chunked { // Check if chunked message finished if bytes.HasSuffix(c.respBuf[:readBytes], chunkedSuffix) { break } } else if contentLength != -1 { if currentContentLength > contentLength { Debug("[HTTPClient] disconnected, wrong length", currentContentLength, contentLength) c.Disconnect() break } else if currentContentLength == contentLength { break } } } else { if currentChunk == nil { currentChunk = make([]byte, readChunkSize) } n, err = c.conn.Read(currentChunk) if err == io.EOF { break } else if err != nil { Debug("[HTTPClient] Read the whole body error:", err, c.baseURL) break } readBytes += int(n) chunks++ currentContentLength += n if chunked { // Check if chunked message finished if bytes.HasSuffix(currentChunk[:n], chunkedSuffix) { break } } else if contentLength != -1 { if currentContentLength > contentLength { Debug("[HTTPClient] disconnected, wrong length", currentContentLength, contentLength) c.Disconnect() break } else if currentContentLength == contentLength { break } } else { Debug("[HTTPClient] disconnected, can't find Content-Length or Chunked") c.Disconnect() break } } if readBytes >= maxResponseSize { Debug("[HTTPClient] Body is more than the max size", maxResponseSize, c.baseURL) break } // For following chunks expect less timeout timeout = time.Now().Add(c.config.Timeout / 5) } if err != nil { Debug("[HTTPClient] Response read error", err, c.conn, readBytes) response = errorPayload(HTTP_TIMEOUT) return } if readBytes > len(c.respBuf) { readBytes = len(c.respBuf) } payload := make([]byte, readBytes) copy(payload, c.respBuf[:readBytes]) if c.config.Debug { Debug("[HTTPClient] Received:", string(payload)) } if c.config.FollowRedirects > 0 && c.redirectsCount < c.config.FollowRedirects { status := payload[9:12] // 3xx requests if status[0] == '3' { c.redirectsCount++ location := proto.Header(payload, []byte("Location")) redirectPayload := []byte("GET " + string(location) + " HTTP/1.1\r\n\r\n") if c.config.Debug { Debug("[HTTPClient] Redirecting to: " + string(location)) } return c.Send(redirectPayload) } } if bytes.Equal(proto.Status(payload), []byte("400")) { c.Disconnect() Debug("[HTTPClient] Closed connection on 400 response") } c.redirectsCount = 0 return payload, err }
func (m *HTTPModifier) Rewrite(payload []byte) (response []byte) { if len(m.config.methods) > 0 && !m.config.methods.Contains(proto.Method(payload)) { return } if m.config.urlRegexp.regexp != nil { host, _, _, _ := proto.Header(payload, []byte("Host")) fullPath := append(host, proto.Path(payload)...) if !m.config.urlRegexp.regexp.Match(fullPath) { return } } if len(m.config.headerFilters) > 0 { for _, f := range m.config.headerFilters { value, s, _, _ := proto.Header(payload, f.name) if s != -1 && !f.regexp.Match(value) { return } } } if len(m.config.headerHashFilters) > 0 { for _, f := range m.config.headerHashFilters { value, s, _, _ := proto.Header(payload, f.name) if s == -1 { return } hasher := fnv.New32a() hasher.Write(value) if (hasher.Sum32() % 100) >= f.percent { return } } } if len(m.config.urlRewrite) > 0 { path := proto.Path(payload) for _, f := range m.config.urlRewrite { if f.src.Match(path) { path = f.src.ReplaceAll(path, f.target) payload = proto.SetPath(payload, path) break } } } if len(m.config.headers) > 0 { for _, header := range m.config.headers { payload = proto.SetHeader(payload, []byte(header.Name), []byte(header.Value)) } } return payload }