As a prerequisite, would like you to go over the last blog on Pattern Set (/blogs/2011/07/01/string-match-simplified-with-pattern-set/). Now, the question is, why do we need String Maps when we already have Pattern Sets? If you treat Pattern Set as single dimensional array then String maps becomes a 2-D array of strings. They can also be looked as key:value pair within the array. String maps minimize the processing of n-numbers of policies to just a few, making the processing of data faster and efficient.
Consider a use case, where you need to create 10s of responder policies with respective redirect actions based on matching incoming URLs. Every URL is distinct and the redirect URL is also different one thus there is no way using a single mapping you can get this use case working. To resolve this, String Maps were introduced, to turn to more organized; array oriented processing of key:value pairs where every incoming URL is a key and every redirect URL would be the value corresponding to the key.
Let’s understand this, with an example.
- Adding a string map URL_STRING_MAP with an array of list of keys and respective values.
Add stringmap URL_STRING_MAP
Bind stringmap URL_STRING_MAP /article/list/technology/tyu.html /NEWarticle/list/technology/tyu.html
Bind stringmap URL_STRING_MAP /article/list/polls/fgh.html /NEWarticle/list/polls/fgh.html
Bind stringmap URL_STRING_MAP /article/list/world/xyx.html /NEWarticle/list/world/xyx.html
2. Adding a Responder Action ACT_URL_REDIRECT
Add responder action ACT_URL_REDIRECT redirect ‘HTTP.REQ.URL.MAP_STRING(“URL_MAP_STRING”)’ -bypassSafetyCheck yes
This action will return the value from the URL_MAP_STRING string map.
3. Adding a Responder Policy POL_URL_REDIRECT
Add responder policy POL_URL_REDIRECT ‘HTTP.REQ.URL.IS_STRINGMAP_KEY(“URL_MAP_STRING”)’ ACT_URL_REDIRECT
The POL_URL_REDIRECT policy will check if the request has any of the keys from the URL_MAP_STRING string map table and will return TRUE of FALSE respectively. If the policy is TRUE, it will hit the action ACT_URL_REDIRECT.
4. Binding POL_URL_REDIRECT policy globally with priority as 100.
Bind responder global POL_URL_REDIRECT 100 END
After binding the policy globally and tagging with 100 priority, if FALSE it would not process any other policy.
The above diagram describes how the configured string map, policy and action behave when a request hits the NetScaler.
If the request hits the policy TRUE, it’ll leads to the action ACT_URL_REDIRECT otherwise it’ll lead to END and processes no other policies.
In this case, the policy finds a match in the String map table and processes Action ACT_URL_REDIRECT. This action, responds back with the value of the key match, in the URL_STRING_MAP string map table as the HTTP Response.