-
Notifications
You must be signed in to change notification settings - Fork 56
Filter list of acceptable headers; use default if the client accepts everything #81
Conversation
…e list of acceptable headers. getFromHeader will return null if the accept header is either missing, or set to accept everything. This will allow the default to be used instead.
The PSR-15 version of this middeware accept a list of formats in the constructor: |
Hi Oscar, I might just use the PSR-15 one instead. I've just updated this PR to make $priorities a constructor parameter, bringing that newer version's functionality into this version. Do you still want it? |
Quick note: I've given up on trying to retrofit PSR-15 middlewares into Slim (the framework I'm using). Apparently support will be in a later version. So I will need to use this PSR-7 FormatNegotiator. |
Hi, @mlambley Middleware::FormatNegotiator([
'json' => [['json'], ['application/json', 'text/json', 'application/x-json']],
]) This prevent the situation where you customize the desired priorities but the formats are not available, which would be confused. |
…ities. Remove the priorities member variable and functionality.
I figure that you would want to keep the existing addFormat and defaultFormat functions, to not introduce a breaking change, even though the functionality is replicated. I have not set the default format to be the first item in the list, on the assumption that we will not be removing the defaultFormat function. |
Yes, I don't want to introduce breaking changes here.
|
…fore we no longer need to check for "all" in getFromHeader. If the user explicitly sets default format to be null, then the system will generate a 406 Not Acceptable response if no types match. For example: ``` //Code Middleware::FormatNegotiator([ 'json' => [['json'], ['application/json', 'text/json', 'application/x-json']], 'zip' => [['zip'], ['application/zip', 'application/x-zip', 'application/x-zip-compressed']] ]) ->defaultFormat(null) //Header: Accept: application/pdf //Result 406 Not Acceptable ``` Readme updated to reflect.
Hi Oscar, I've introduced an option for it to generate a 406 Not Acceptable response in order for it to work with my server. Please let me know if this, and the other changes you requested, are ok. I'm hoping that you won't consider this change to be breaking, since the behaviour of setting default to null is currently undefined. |
I think it's good. In fact, I have a similar feature in the PSR-15 version |
Added a priorities member variable, which allows the dev to filter the list of acceptable headers.
Usage:
getFromHeader will return null if the accept header is either missing, or set to accept everything. This will allow the default to be used instead.
Usage: