Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content

Allow dynamic headers for Rack::Files - #2377

Merged
ioquatix merged 4 commits into
rack:mainfrom
codergeek121:add-proc-support-for-dynamic-headers-in-rack-files
Sep 15, 2025
Merged

ioquatix merged 4 commits into
rack:mainfrom
codergeek121:add-proc-support-for-dynamic-headers-in-rack-files

Conversation

@codergeek121

Copy link
Copy Markdown
Contributor

This PR adds support for dynamic headers in Rack::Files via the headers configuration.

For example the following configuration add the header "x-dynamic" with a random integer between 1 and 10 for each request handled by Rack::Files:

Rack::Files.new(".", {
  "x-dynamic" => ->(request) { rand(1..10).to_s }
})

This feature could be used to provide different cache durations for different files. E.g. a longer duration for files with a contenthash and shorter duration for files without a contenthash. This would allow solving issues like
rails/rails#55249 within Rack::Files directly.

This PR adds support for dynamic headers in Rack::Files via the headers
configuration.

For example the following configuration add the header "x-dynamic" with
a random integer between 1 and 10 for each request handled by Rack::Files:

```
Rack::Files.new(".", {
  "x-dynamic" => ->(request) { rand(1..10).to_s }
})
```

This feature could be used to provide different cache durations
for different files. E.g. a longer duration for files with a
contenthash and shorter duration for files without a contenthash. This
would allow solving issues like
rails/rails#55249 within Rack::Files directly.

@jeremyevans jeremyevans left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm against adding this, as I don't think the complexity is worth the benefit. I'd be OK adding a set_headers method that is passed the base headers and request, and runs headers.merge!(@headers) if @headers by default. You could then subclass Rack::Files and override the method to add the behavior you want.

@codergeek121

Copy link
Copy Markdown
Contributor Author

I think this is a great suggestion. This keeps Rack::Files simple and would still allow to add a similar feature to Rails. I'll adapt this PR 👍

@codergeek121

Copy link
Copy Markdown
Contributor Author

I adapted the PR to provide a public set_headers method and added a simple test for it 👍

@jeremyevans jeremyevans left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

@ioquatix

Copy link
Copy Markdown
Member

This looks good to me but I have a slight concern about the name set_headers, since it sounds like it might set @headers or something similar. Ruby has some similar conventions like Exception#set_backtrace.

Perhaps assign_headers is better? @jeremyevans do you have any thoughts?

@jeremyevans

Copy link
Copy Markdown
Contributor

This looks good to me but I have a slight concern about the name set_headers, since it sounds like it might set @headers or something similar. Ruby has some similar conventions like Exception#set_backtrace.

Perhaps assign_headers is better? @jeremyevans do you have any thoughts?

I'm fine with either, so if you would prefer assign_headers, let's go with that.

@ioquatix

Copy link
Copy Markdown
Member

@codergeek121 wdyt?

@codergeek121

Copy link
Copy Markdown
Contributor Author

I'd be fine with both names as well. I think your points made sense though and I've renamed the method 👍

@ioquatix
ioquatix merged commit 2fface9 into rack:main Sep 15, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants