karlseguin / http.zig

An HTTP/1.1 server for zig
MIT License
454 stars 31 forks source link

How to pass allocated data to response? #41

Closed notramo closed 2 months ago

notramo commented 5 months ago

I want to pass a dynamically formatted Location header URL into the response. Since response.header("Location", url); does not copy the url value (it really should), I have to allocate it, but how to know when to deallocate to avoid memory leak?

karlseguin commented 5 months ago

This is what res.arena is for. It's an std.mem.Allocator.

I would agree with you that the header value should be copied, if there was a way to tell whether or not the value is a constant. But, without this ability, always copying would result in overhead for the common case where the key or value are constants.

notramo commented 5 months ago

Could you add this explanation to the README?

karlseguin commented 5 months ago

It already is: https://github.com/karlseguin/http.zig?tab=readme-ov-file#header-value