Branch: Tag:

2003-02-05

2003-02-05 16:25:41 by Jonas Wallden <jonasw@roxen.com>

Moved charset handling code from the HTTP protocol module into the common
RequestID object so that e.g. FTP requests can benefit from it.

Rev: server/base_server/prototypes.pike:1.55
Rev: server/protocols/http.pike:1.391

2:   // Modified by Francesco Chemolli to add throttling capabilities.   // Copyright © 1996 - 2001, Roxen IS.    - constant cvs_version = "$Id: http.pike,v 1.390 2003/02/03 14:54:38 grubba Exp $"; + constant cvs_version = "$Id: http.pike,v 1.391 2003/02/05 16:25:36 jonasw Exp $";   // #define REQUEST_DEBUG   #define MAGIC_ERROR   
135:      array|AuthEmulator auth;    - string charset_name( function|string what ) - { -  switch( what ) -  { -  case string_to_unicode: return "ISO10646-1"; -  case string_to_utf8: return "UTF-8"; -  default: return upper_case((string)what); -  } - } +     - function charset_function( function|string what, int allow_entities ) - { -  switch( what ) -  { -  case "ISO-10646-1": -  case "ISO10646-1": -  case string_to_unicode: -  return string_to_unicode; -  case "UTF-8": -  case string_to_utf8: -  return string_to_utf8; -  default: -  catch { -  // If current file is "text/html" or "text/xml" we'll use an entity -  // encoding fallback instead of empty string subsitution. -  function fallback_func = -  allow_entities && -  (file->type[0..8] == "text/html" || file->type[0..7] == "text/xml") && -  lambda(string char) { -  return sprintf("&#x%x;", char[0]); -  }; -  return -  Roxen._charset_decoder( Locale.Charset.encoder( (string) what, -  "", fallback_func ) ) -  ->decode; -  }; -  } -  return lambda(string what){return what;}; - } -  - static array(string) join_charset( string old, -  function|string add, -  function oldcodec, -  int allow_entities ) - { -  switch( old&&upper_case(old) ) -  { -  case 0: -  return ({ charset_name( add ), charset_function( add, allow_entities ) }); -  case "ISO10646-1": -  case "UTF-8": -  return ({ old, oldcodec }); // Everything goes here. :-) -  case "ISO-2022": -  return ({ old, oldcodec }); // Not really true, but how to know this? -  default: -  // Not true, but there is no easy way to add charsets yet... -  return ({ charset_name( add ), charset_function( add, allow_entities ) }); -  } - } -  - static array(string) output_encode( string what, int|void allow_entities, -  string|void force_charset ) - { -  if( !force_charset ) -  { -  string charset; -  function encoder; -  -  foreach( output_charset, string|function f ) -  [charset,encoder] = join_charset( charset, f, encoder, allow_entities ); -  -  -  if( !encoder ) -  if( String.width( what ) > 8 ) -  { -  charset = "UTF-8"; -  encoder = string_to_utf8; -  } -  if( encoder ) -  what = encoder( what ); -  return ({ charset, what }); -  } -  else -  return ({ -  0, -  Locale.Charset.encoder( (force_charset/"=")[-1] )->feed( what )->drain() -  }); - } -  +    void decode_map( mapping what, function decoder )   {    foreach( indices( what ), mixed q )
1648:    if ((file["type"][0..4] == "text/") ||    (String.width(file->data) > 8))    { -  [charset,file->data] = output_encode( file->data, 1 ); +  int allow_entities = +  has_prefix(file->type, "text/xml") || +  has_prefix(file->type, "text/html"); +  [charset,file->data] = output_encode( file->data, allow_entities );    if( charset && (search(file["type"], "; charset=") == -1))    charset = "; charset="+charset;    else