Branch: Tag:

2016-05-25

2016-05-25 16:19:08 by Henrik Grubbström (Grubba) <grubba@grubba.org>

HTTP: Added id-misc-forwarded.

This field contains any parsed RFC 7230 Forwarded headers.

Fixes some of [bug 7694 (#7694)].

969:    }       TIMER_START(parse_got_2_parse_headers); +  array(int|string) new_forwarded_header = ({});    foreach (request_headers; string linename; array|string contents)    {    if( arrayp(contents) ) contents = contents[0];
1093:    return 2;    }    break; +  +  case "forwarded": +  // RFC 7239 +  if (stringp(contents)) { +  misc->forwarded = MIME.tokenize(contents) / ({ ',' }); +  } else { +  misc->forwarded = map(map(contents, MIME.tokenize), `/, ({ ',' })) * +  ({});    } -  +  break; +  +  // The X-Forwarded-* headers have been obsoleted by RFC 7239. +  // Convert them to the corresponding forwarded header as per +  // RFC 7239 7.4. But only if it hasn't already been done. +  case "x-forwarded-for": +  if (request_headers->forwarded) break; +  foreach(stringp(contents)?({ contents }):contents, string line) { +  foreach(MIME.tokenize(line) / ({ ',' }), array(string|int) segment) { +  string ip; +  if ((sizeof(segment) == 1) && stringp(segment[0])) { +  ip = segment[0]; +  } else { +  ip = MIME.quote(segment);    } -  +  if (has_value(ip, ":") && !has_value(ip, ".") && +  !has_prefix(ip, "[")) { +  // DWIM IPv6 +  ip = "[" + ip + "]"; +  } +  if (sizeof(new_forwarded_header)) { +  new_forwarded_header += ({ ',' }); +  } +  new_forwarded_header += ({ "for", '=', ip }); +  } +  } +  break; +  case "x-forwarded-by": +  case "x-forwarded-host": +  case "x-forwarded-proto": +  if (request_headers->forwarded) break; +  string field = linename[sizeof("x-forwarded-")..]; +  foreach(stringp(contents)?({ contents }):contents, string line) { +  foreach(MIME.tokenize(line) / ({ ',' }), array(string|int) segment) { +  string value; +  if ((sizeof(segment) == 1) && stringp(segment[0])) { +  value = segment[0]; +  } else { +  value = MIME.quote(segment); +  } +  if (sizeof(new_forwarded_header)) { +  new_forwarded_header += ({ ',' }); +  } +  new_forwarded_header += ({ field, '=', value }); +  } +  } +  break; +  } +  } +  if (sizeof(new_forwarded_header)) { +  request_headers->forwarded = MIME.quote(new_forwarded_header); +  misc->forwarded = new_forwarded_header / ({ ',' }); +  }    TIMER_END(parse_got_2_parse_headers);    }