Esempio n. 1
0
func (o *KafkaOutput) Write(data []byte) (n int, err error) {
	headers := make(map[string]string)
	proto.ParseHeaders([][]byte{data}, func(header []byte, value []byte) bool {
		headers[string(header)] = string(value)
		return true
	})

	req := payloadBody(data)

	kafkaMessage := KafkaMessage{
		ReqURL:     string(proto.Path(req)),
		ReqMethod:  string(proto.Method(req)),
		ReqBody:    string(proto.Body(req)),
		ReqHeaders: headers,
	}
	jsonMessage, _ := json.Marshal(&kafkaMessage)
	message := sarama.StringEncoder(jsonMessage)

	o.producer.Input() <- &sarama.ProducerMessage{
		Topic: o.config.topic,
		Value: message,
	}

	return len(message), nil
}
Esempio n. 2
0
func (t *TCPMessage) check100Continue() {
	if t.expectType != httpExpectNotSet || len(t.packets[0].Data) < 25 {
		return
	}

	if t.methodType != httpMethodWithBody {
		return
	}

	if t.seqMissing || t.headerPacket == -1 {
		return
	}

	last := t.packets[len(t.packets)-1]
	// reading last 4 bytes for double CRLF
	if !bytes.HasSuffix(last.Data, bEmptyLine) {
		return
	}

	var expectB []byte
	proto.ParseHeaders(t.packetsData(), func(header, value []byte) bool {
		if proto.HeadersEqual(header, bExpectHeader) {
			expectB = value
			return false
		}

		return true
	})

	if len(expectB) > 0 && bytes.Equal(bExpect100Value, expectB) {
		t.expectType = httpExpect100Continue
		return
	}

	t.expectType = httpExpectEmpty
}
Esempio n. 3
0
func (t *TCPMessage) updateBodyType() {
	// if there is cache
	if t.bodyType != httpBodyNotSet {
		return
	}

	// Headers not received
	if t.headerPacket == -1 {
		return
	}

	var lengthB, encB, connB []byte

	proto.ParseHeaders(t.packetsData(), func(header, value []byte) bool {
		if proto.HeadersEqual(header, []byte("Content-Length")) {
			lengthB = value
			return false
		}

		if proto.HeadersEqual(header, []byte("Transfer-Encoding")) {
			encB = value
			return false
		}

		if proto.HeadersEqual(header, []byte("Connection")) {
			connB = value
		}

		return true
	})

	switch t.methodType {
	case httpMethodNotFound:
		return
	case httpMethodWithoutBody:
		t.bodyType = httpBodyEmpty
		return
	case httpMethodWithBody:
		if len(lengthB) > 0 {
			t.contentLength, _ = strconv.Atoi(string(lengthB))

			if t.contentLength == 0 {
				t.bodyType = httpBodyEmpty
			} else {
				t.bodyType = httpBodyContentLength
			}
			return
		}

		if len(encB) > 0 {
			t.bodyType = httpBodyChunked
			return
		}

		if len(connB) > 0 && bytes.Equal(connB, []byte("close")) {
			t.bodyType = httpBodyConnectionClose
			return
		}
	}

	t.bodyType = httpBodyEmpty
}