-
Notifications
You must be signed in to change notification settings - Fork 399
feat: Add Caddy placeholders #1901
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
Open
Starfox64
wants to merge
5
commits into
php:main
Choose a base branch
from
Starfox64:placeholders
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8d71f9b
feat: Add Caddy placeholders
Starfox64 b54b771
fix(docs): Add blank line before placeholders list
Starfox64 651a1d5
refactor: Expose a PHP function instead of using HTTP headers
Starfox64 5812bcd
chore: Fix linter errors
Starfox64 5da88ec
fix: Correct indentation in frankenphp_set_caddy_placeholder function
Starfox64 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -565,6 +565,21 @@ func go_log(message *C.char, level C.int) { | |
| } | ||
| } | ||
|
|
||
| //export go_set_caddy_placeholder | ||
| func go_set_caddy_placeholder(threadIndex C.uintptr_t, key *C.char, value *C.char) { | ||
| fc := phpThreads[threadIndex].getRequestContext() | ||
|
|
||
| placeholders, _ := fc.request.Context().Value(PlaceholdersContextKey).(map[string]string) | ||
|
|
||
| if placeholders == nil { | ||
| logger.LogAttrs(context.Background(), slog.LevelDebug, "frankenphp_set_caddy_placeholder() called in non-HTTP context", slog.String("worker", fc.scriptFilename)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| placeholders[C.GoString(key)] = C.GoString(value) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe these keys should always be prefixed with 'frankenphp', otherwise devs might end up replacing existing placeholders |
||
| } | ||
|
|
||
| //export go_is_context_done | ||
| func go_is_context_done(threadIndex C.uintptr_t) C.bool { | ||
| return C.bool(phpThreads[threadIndex].getRequestContext().isDone) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you defer context creation to
go_set_caddy_placeholder? Otherwise this map will most of the time end up being unusedUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been trying to work on this but I'm stuck and don't know how to go about it since this is only my second time ever working with Go.
I've been able to defer creation to the first call to
go_set_caddy_placeholderbut I can't access the updated request context from caddy inServeHTTPafter the request is handled by FrankenPHP. That is becausefrankenPHPContextis private so I can't access the updated request and it's context.I feel like my only way forward would be to make
frankenPHPContextpublic and store the placeholders there directly instead of the request context.What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that probably makes sense. If you were to use this feature outside Caddy (i.e. frankenphp as a library), then it’d also be useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we plan to expose other things about the request, then making parts of FrankenPHPContext public probably is the way to go. What else would you like to expose @withinboredom ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I go ahead and make FrankenPHPContext public?
If so, is there anything I should be aware of before mass replacing
frankenPHPContextbyFrankenPHPContext? Is it safe to proceed like that? (asking because it's a relatively large diff)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm not sure, since there aren't other obvious use cases, I'd rather not make it public. I think this might still be the best option, I also can't think of a better way right now.