spamass-milt-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

refactoring(!) retrieve_field


From: Ronan Waide
Subject: refactoring(!) retrieve_field
Date: Sun, 19 Jan 2003 23:56:36 +0000

Okay, I actually debugged that rewritten version of retrieve_field I
posted last week or the week before. It actually works now, rather
than getting stuck in an infinite loop. Note there is still one major
caveat: it will only find the first occurrence of a given header. With
the fairly restricted use that's made of it in spamass-milter, I don't
think this is a major issue.

Enjoy!

Waider.

// retrieve the content of a specific field in the header
// and return it.
string
retrieve_field(const string& header, const string& field)
{
  // Find the field
  string::size_type field_start = string::npos;
  string::size_type field_end = string::npos;
  string::size_type idx = 0;

  while( field_start == string::npos ) {
        idx = find_nocase( header, field + string(":"), idx );

        // no match
        if ( idx == string::npos ) {
          return string( "" );
        }

        // The string we've found needs to be either at the start of the
        // headers string, or immediately following a "\n"
        if ( idx != 0 ) {
          if ( header[ idx - 1 ] != '\n' ) {
                idx++; // so we don't get stuck in an infinite loop
                continue; // loop around again
          }
        }

        field_start = idx;
  }

  // A mail field starts just after the header. Ideally, there's a
  // space, but it's possible that there isn't.
  field_start += field.length() + 1;
  if ( field_start < ( header.length() - 1 ) && header[ field_start ] == ' ' ) {
        field_start++;
  }

  // See if there's anything left, to shortcut the rest of the
  // function.
  if ( field_start == header.length() - 1 ) {
        return string( "" );
  }

  // The field continues to the end of line. If the start of the next
  // line is whitespace, then the field continues.
  idx = field_start;
  while( field_end == string::npos ) {
        idx = header.find( "\n", idx );

        // if we don't find a "\n", gobble everything to the end of the headers
        if ( idx == string::npos ) {
          field_end = header.length();
        } else {
          // check the next character
          if (( idx + 1 ) < header.length() && ( isspace( header[ idx + 1 ] ))) 
{
                idx ++; // whitespace found, so wrap to the next line
          } else {
                field_end = idx;
          }
        }
  }

  //  Maybe remove the whitespace picked up when a header wraps - this
  //  might actually be a requirement
  return header.substr( field_start, field_end - field_start );
};
-- 
address@hidden / Yes, it /is/ very personal of me.

"It's a poor workman who blames his tools, unless they're WINTEL-based."
                                                        - Conor Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]