/* $Revision: 18 $ $Date: 18/08/06 17:57 $ Copyright © 2006-2007, FSL Technologies Limited. Contact "http://fost.3.felspar.com/". */ #include "stdafx.h" #include "FOST.internet.hpp" using namespace FSLib; namespace { const FSLib::Revision c_revision( L"$Archive: $", __DATE__, L"$Revision: 0 $", L"$Date: $" ); const char *c_crlf = "\r\n"; } /* FSLib::Headers */ FSLib::Headers::Headers() { } void FSLib::Headers::parse( const string &headers ) { // This implementation ignores character encodings - assumes UTF-8 which won't work for mail headers for ( std::pair< string, Nullable< string > > lines( partition( headers, c_crlf ) ); !lines.first.empty(); lines = partition( lines.second, c_crlf ) ) { std::pair< string, Nullable< string > > line( partition( lines.first, ": " ) ); if ( line.second.isnull() ) { FSLib::Exceptions::ParseError exc( L"Header has no value" ); exc.info() << widen( line.first ) << std::endl; throw exc; } else m_headers.insert( value( widen( line.first ), widen( line.second.value() ) ) ); } } std::pair< FSLib::wstring, FSLib::Headers::Value > FSLib::Headers::value( const wstring &name, const wstring &value ) { return std::make_pair( name, Value( value ) ); } bool FSLib::Headers::exists( const wstring &n ) const { return m_headers.find( n ) != m_headers.end(); } namespace { // Done here so it is initialised on DLL loading. // Should g'tee no multithreading problem as this isn't going to be used in the DLL initialisation const FSLib::Headers::Value g_stat; } const FSLib::Headers::Value &FSLib::Headers::operator[]( const wstring &n ) const { std::map< wstring, Value >::const_iterator p( m_headers.find( n ) ); if ( p == m_headers.end() ) return g_stat; else return (*p).second; } FSLib::Headers::Value &FSLib::Headers::add( const wstring &n, const Value &v ) { return (*m_headers.insert( std::make_pair( n, v ) ).first).second; } FSLib::Headers::const_iterator FSLib::Headers::begin() const { return m_headers.begin(); } FSLib::Headers::const_iterator FSLib::Headers::end() const { return m_headers.end(); } /* FSLib::Headers::Value */ FSLib::Headers::Value::Value() { } FSLib::Headers::Value::Value( const wchar_t *val ) :m_value( val ) { } FSLib::Headers::Value::Value( const wstring &val ) : m_value( val ) { } FSLib::Headers::Value::Value( const wstring &val, const std::map< wstring, wstring > &args ) : m_value( val ), m_subvalues( args ) { } const wstring &FSLib::Headers::Value::value() const { return m_value; } const wstring &FSLib::Headers::Value::subvalue( const wstring &k, const wstring &v ) { return m_subvalues[ k ] = v; } Nullable< wstring > FSLib::Headers::Value::subvalue( const wstring &k ) const { std::map< wstring, wstring >::const_iterator p( m_subvalues.find( k ) ); if ( p == m_subvalues.end() ) return Null; else return (*p).second; } FSLib::Headers::Value::const_iterator FSLib::Headers::Value::begin() const { return m_subvalues.begin(); } FSLib::Headers::Value::const_iterator FSLib::Headers::Value::end() const { return m_subvalues.end(); }