go-coap icon indicating copy to clipboard operation
go-coap copied to clipboard

Optional ETags in response from server?

Open JosefWN opened this issue 2 years ago • 5 comments

From my understanding of the RFC ETags are optional: https://datatracker.ietf.org/doc/html/rfc7252#section-5.10.6

However, in go-coap they are always included as there is a fallback, potentially increasing the response message size needlessly: https://github.com/plgd-dev/go-coap/blob/4d466801917705aec7cd97b6b53c7502d4c1f7c5/udp/client/responsewriter.go#L47-L53

For the endpoints that I use ETags with, I calculate them using the hashstructure library (before encoding and compressing the payload). For all other endpoints I don't use ETags, and I have other means of verifying the integrity of the payload (gzip's CRC32 checksum).

Would it make sense to add a boolean option to the server deciding whether to automatically calculate checksums for ETags, or better yet, include an option to set an ETag generator as an option (nullable/pointer or with a "no-op" generator for those that don't want it). This way, the library users can choose not to auto-generate ETags, and if they auto-generate them, they can choose how to do so (everyone might not want to use CRC64)?

JosefWN avatar Jun 22 '22 19:06 JosefWN

Sounds reasonable to me. I think allowing the user to optionally add a custom ETag generator is a useful extension of functionality. @jkralik what do you think?

Danielius1922 avatar Jun 23 '22 07:06 Danielius1922

@Danielius1922 I agree. We can create option to the server that allows to set function - etag generator. And nil will be means that etag option will be not set by server/client.

jkralik avatar Jul 18 '22 09:07 jkralik

I have a related question (less advanced):

I implemented a go server with endpoints that are written similar to the handleA endpoint in: https://github.com/plgd-dev/go-coap/tree/master/examples/simple/server

With w.SetResponse(codes.Content, message.TextPlain, bytes.NewReader([]byte("hello world"))) it automatically sets options which look like: "options": [ "ETag(etag=00 00 90 62 7E 81 4E 6E", "Content-Format: text/plain; charset=utf-8" ]

Since I don't need any Options to be sent, I worked with w.SetResponse(codes.Content, message.TextPlain, bytes.NewReader([]byte("hello world")), message.Option{make([]byte,0), 4}) it sets e.g. only the Etag to 0. But there is still an Etag in the options.

Is it possible not to send any options at all?

MariusMagiera avatar Jul 18 '22 18:07 MariusMagiera

Is it possible not to send any options at all?

Yes. Just follow the code in example: https://github.com/plgd-dev/go-coap/blob/e59fa916285a183e0adbcbf6d0c2e1329ddc448e/examples/simple/server/main.go#L28

jkralik avatar Jul 18 '22 19:07 jkralik

This is addressed in github.com/plgd-dev/go-coap/v3. ETag could be set via options:

https://github.com/plgd-dev/go-coap/blob/master/net/responsewriter/responseWriter.go#L41

jkralik avatar Oct 12 '22 14:10 jkralik

I will close it. Feel free to reopen it. Thx

jkralik avatar Oct 15 '22 15:10 jkralik