Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cookie handling for http client #2534

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ func (h *Client) ResponseToBatch(res *http.Response) (service.MessageBatch, erro
}
}
}
// Handle cookies returned by the server. Create a Cookie header to be used by the client for subsequent requests
if len(res.Cookies()) > 0 {
var cookie strings.Builder
for _, c := range res.Cookies() {
// We're not checkig expiration or other properties like Domain here
cookie.WriteString(fmt.Sprintf("%s=%s; ", c.Name, c.Value))
}
p.MetaSetMut("Cookie", strings.TrimSuffix(cookie.String(), "; "))
}
}

if res.Body == nil {
Expand Down
43 changes: 43 additions & 0 deletions internal/httpclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,3 +708,46 @@ tls:
require.NoError(t, err)
assert.Equal(t, "HELLO WORLD", string(mBytes))
}

func TestHTTPClientExtensiveCookies(t *testing.T) {
require := require.New(t)
assert := assert.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.Header().Add("x-demo", "Hello")
http.SetCookie(w, &http.Cookie{Name: "a", Value: "b", Path: "/", HttpOnly: true, Secure: true})
http.SetCookie(w, &http.Cookie{Name: "x", Value: "y"})
http.SetCookie(w, &http.Cookie{Name: "foo", Value: "bar"})
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"json": "content"}`))
}))
defer ts.Close()

conf := clientConfig(t, `
url: %v
extract_headers:
include_patterns: [".*"]
`, ts.URL+"/testpost")

h, err := NewClientFromOldConfig(conf, service.MockResources())
require.NoError(err)

resBatch, err := h.Send(context.Background(), service.MessageBatch{
service.NewMessage([]byte("hello world")),
})
require.NoError(err)
require.Len(resBatch, 1)

mBytes, err := resBatch[0].AsBytes()
require.NoError(err)
assert.Equal(`{"json": "content"}`, string(mBytes))

xHeader, exists := resBatch[0].MetaGet("x-demo")
require.True(exists)
assert.Equal("Hello", xHeader)

resCookie, exists := resBatch[0].MetaGet("Cookie")
require.True(exists)
assert.Equal("a=b; x=y; foo=bar", resCookie)
}
5 changes: 5 additions & 0 deletions internal/impl/io/processor_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ If the request returns an error response code this processor sets a metadata fie

Use the field `+"`extract_headers`"+` to specify rules for which other headers should be copied into the resulting message from the response.

### Cookies

If the server returns cookies in the response header, this processor sets a metadata field `+"`Cookie`"+` on the resulting message.
This metadata field can be used in subsequent calls to the same server as http header, containing all received cookies in the format `+"`name=value; name=value`"+`

## Error Handling

When all retry attempts for a message are exhausted the processor cancels the attempt. These failed messages will continue through the pipeline unchanged, but can be dropped or placed in a dead letter queue according to your config, you can read about these patterns [here](/docs/configuration/error_handling).`).
Expand Down
5 changes: 5 additions & 0 deletions website/docs/components/processors/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ If the request returns an error response code this processor sets a metadata fie

Use the field `extract_headers` to specify rules for which other headers should be copied into the resulting message from the response.

### Cookies

If the server returns cookies in the response header, this processor sets a metadata field `Cookie` on the resulting message.
This metadata field can be used in subsequent calls to the same server as http header, containing all received cookies in the format `name=value; name=value`

## Error Handling

When all retry attempts for a message are exhausted the processor cancels the attempt. These failed messages will continue through the pipeline unchanged, but can be dropped or placed in a dead letter queue according to your config, you can read about these patterns [here](/docs/configuration/error_handling).
Expand Down
Loading