Commit 8750386b2634071c51ef0e94ef28ba2ad8d3dda2
Exists in
master
and in
27 other branches
Merge branch 'ai3186' into 'master'
Fix 400 bad request of varnish with nginx and apache 2.4 http://noosfero.org/Development/ActionItem3186 See merge request !249
Showing
1 changed file
with
19 additions
and
10 deletions
Show diff stats
etc/noosfero/varnish-accept-language.vcl
| @@ -6,7 +6,14 @@ C{ | @@ -6,7 +6,14 @@ C{ | ||
| 6 | /* | 6 | /* |
| 7 | * Accept-language header normalization | 7 | * Accept-language header normalization |
| 8 | * | 8 | * |
| 9 | - * Cosimo, 21/01/2010 | 9 | + * - Parses client Accept-Language HTTP header |
| 10 | + * - Tries to find the best match with the supported languages | ||
| 11 | + * - Writes the best match as req.http.X-Varnish-Accept-Language | ||
| 12 | + * | ||
| 13 | + * First version: Cosimo, 21/Jan/2010 | ||
| 14 | + * Last update: Cosimo, 03/Nov/2011 | ||
| 15 | + * | ||
| 16 | + * http://github.com/cosimo/varnish-accept-language | ||
| 10 | * | 17 | * |
| 11 | */ | 18 | */ |
| 12 | 19 | ||
| @@ -16,11 +23,12 @@ C{ | @@ -16,11 +23,12 @@ C{ | ||
| 16 | #include <string.h> | 23 | #include <string.h> |
| 17 | 24 | ||
| 18 | #define DEFAULT_LANGUAGE "en" | 25 | #define DEFAULT_LANGUAGE "en" |
| 19 | -#define SUPPORTED_LANGUAGES ":de:fr:es:ru:pt:hy:en:" | 26 | +#define SUPPORTED_LANGUAGES ":de:eo:es:fr:hy:it:pt:ru:" |
| 20 | 27 | ||
| 21 | #define vcl_string char | 28 | #define vcl_string char |
| 22 | -#define LANG_LIST_SIZE 16 | ||
| 23 | -#define LANG_MAXLEN 16 | 29 | +#define LANG_LIST_SIZE 16 |
| 30 | +#define HDR_MAXLEN 256 | ||
| 31 | +#define LANG_MAXLEN 8 | ||
| 24 | #define RETURN_LANG(x) { \ | 32 | #define RETURN_LANG(x) { \ |
| 25 | strncpy(lang, x, LANG_MAXLEN); \ | 33 | strncpy(lang, x, LANG_MAXLEN); \ |
| 26 | return; \ | 34 | return; \ |
| @@ -64,9 +72,8 @@ int is_supported(vcl_string *lang) { | @@ -64,9 +72,8 @@ int is_supported(vcl_string *lang) { | ||
| 64 | strncat(match_str, lang, LANG_MAXLEN); | 72 | strncat(match_str, lang, LANG_MAXLEN); |
| 65 | strncat(match_str, ":\0", 2); | 73 | strncat(match_str, ":\0", 2); |
| 66 | 74 | ||
| 67 | - if (strstr(supported_languages, match_str)) { | 75 | + if (strstr(supported_languages, match_str)) |
| 68 | is_supported = 1; | 76 | is_supported = 1; |
| 69 | - } | ||
| 70 | 77 | ||
| 71 | return is_supported; | 78 | return is_supported; |
| 72 | } | 79 | } |
| @@ -90,6 +97,7 @@ void select_language(const vcl_string *incoming_header, char *lang) { | @@ -90,6 +97,7 @@ void select_language(const vcl_string *incoming_header, char *lang) { | ||
| 90 | vcl_string *lang_tok = NULL; | 97 | vcl_string *lang_tok = NULL; |
| 91 | vcl_string root_lang[3]; | 98 | vcl_string root_lang[3]; |
| 92 | vcl_string *header; | 99 | vcl_string *header; |
| 100 | + vcl_string header_copy[HDR_MAXLEN]; | ||
| 93 | vcl_string *pos = NULL; | 101 | vcl_string *pos = NULL; |
| 94 | vcl_string *q_spec = NULL; | 102 | vcl_string *q_spec = NULL; |
| 95 | unsigned int curr_lang = 0, i = 0; | 103 | unsigned int curr_lang = 0, i = 0; |
| @@ -106,7 +114,7 @@ void select_language(const vcl_string *incoming_header, char *lang) { | @@ -106,7 +114,7 @@ void select_language(const vcl_string *incoming_header, char *lang) { | ||
| 106 | RETURN_DEFAULT_LANG; | 114 | RETURN_DEFAULT_LANG; |
| 107 | 115 | ||
| 108 | /* Tokenize Accept-Language */ | 116 | /* Tokenize Accept-Language */ |
| 109 | - header = (vcl_string *) incoming_header; | 117 | + header = strncpy(header_copy, incoming_header, sizeof(header_copy)); |
| 110 | 118 | ||
| 111 | while ((lang_tok = strtok_r(header, " ,", &pos))) { | 119 | while ((lang_tok = strtok_r(header, " ,", &pos))) { |
| 112 | 120 | ||
| @@ -137,7 +145,8 @@ void select_language(const vcl_string *incoming_header, char *lang) { | @@ -137,7 +145,8 @@ void select_language(const vcl_string *incoming_header, char *lang) { | ||
| 137 | header = NULL; | 145 | header = NULL; |
| 138 | 146 | ||
| 139 | /* Break out if stored max no. of languages */ | 147 | /* Break out if stored max no. of languages */ |
| 140 | - if (curr_lang >= LANG_MAXLEN) break; | 148 | + if (curr_lang >= LANG_LIST_SIZE) |
| 149 | + break; | ||
| 141 | } | 150 | } |
| 142 | 151 | ||
| 143 | /* Sort by priority */ | 152 | /* Sort by priority */ |
| @@ -157,12 +166,11 @@ void vcl_rewrite_accept_language(const struct sess *sp) { | @@ -157,12 +166,11 @@ void vcl_rewrite_accept_language(const struct sess *sp) { | ||
| 157 | vcl_string *in_hdr; | 166 | vcl_string *in_hdr; |
| 158 | vcl_string lang[LANG_MAXLEN]; | 167 | vcl_string lang[LANG_MAXLEN]; |
| 159 | 168 | ||
| 160 | - memset(lang, 0, LANG_MAXLEN); | ||
| 161 | - | ||
| 162 | /* Get Accept-Language header from client */ | 169 | /* Get Accept-Language header from client */ |
| 163 | in_hdr = VRT_GetHdr(sp, HDR_REQ, "\020Accept-Language:"); | 170 | in_hdr = VRT_GetHdr(sp, HDR_REQ, "\020Accept-Language:"); |
| 164 | 171 | ||
| 165 | /* Normalize and filter out by list of supported languages */ | 172 | /* Normalize and filter out by list of supported languages */ |
| 173 | + memset(lang, 0, sizeof(lang)); | ||
| 166 | select_language(in_hdr, lang); | 174 | select_language(in_hdr, lang); |
| 167 | 175 | ||
| 168 | /* By default, use a different header name: don't mess with backend logic */ | 176 | /* By default, use a different header name: don't mess with backend logic */ |
| @@ -191,3 +199,4 @@ sub vcl_fetch { | @@ -191,3 +199,4 @@ sub vcl_fetch { | ||
| 191 | set beresp.http.Vary = "X-Varnish-Accept-Language"; | 199 | set beresp.http.Vary = "X-Varnish-Accept-Language"; |
| 192 | } | 200 | } |
| 193 | } | 201 | } |
| 202 | + |