• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/web/HttpHeader.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/web/HttpHeader.h>
00018 #include <spl/text/StringBuffer.h>
00019 
00020 HttpHeader::HttpHeader()
00021 : m_headers(20), m_headerIdx()
00022 {
00023 }
00024 
00025 HttpHeader::~HttpHeader()
00026 {
00027 }
00028 
00029 String& HttpHeader::Header(const String& _name)
00030 {
00031         StringPtr name = _name.ToUpper();
00032         if ( ! m_headerIdx.ContainsKey(*name) )
00033         {
00034                 int idx = m_headers.Count();
00035                 m_headers.Add( Association<String, String>(*name, String()) );
00036                 m_headerIdx.Set( *name, idx );
00037         }
00038         return m_headers.ElementAtRef( m_headerIdx.Get(*name) ).ValueRef();
00039 }
00040 
00041 void HttpHeader::Write(spl::IStream& stream) const
00042 {
00043         StringPtr header = ToString();
00044         stream.Write( header->ToByteArray() );
00045 }
00046 
00047 void HttpHeader::ParseLine( const String& line )
00048 {
00049         int colonPos = line.IndexOf(':');
00050         StringPtr key = line.Substring(0, colonPos);
00051         StringPtr val = line.Substring(colonPos + 1)->Trim();
00052         Header(*key) = *val->Trim();
00053 }
00054 
00055 StringPtr HttpHeader::ToString() const
00056 {
00057         StringBuffer buf;
00058 
00059         int headcount = m_headers.Count();
00060         for ( int x = 0; x < headcount; x++ )
00061         {
00062                 Association<String, String>& pair = m_headers.ElementAtRef(x);
00063                 buf.Append( pair.KeyRef() );
00064                 buf.Append( " :\t" );
00065                 buf.Append( pair.ValueRef() );
00066                 buf.Append( "\r\n" );   
00067         }
00068 
00069         if ( m_headers.Count() > 0 )
00070         {
00071                 buf.Append( "\r\n" );
00072         }
00073         else
00074         {
00075                 buf.Append( "\r\n\r\n" );
00076         }
00077 
00078         return buf.ToString();
00079 }
00080 
00081 #ifdef DEBUG
00082 void HttpHeader::ValidateMem() const
00083 {
00084         m_headers.ValidateMem();
00085         m_headerIdx.ValidateMem();
00086 }
00087 
00088 void HttpHeader::CheckMem() const
00089 {
00090         m_headers.CheckMem();
00091         m_headerIdx.CheckMem();
00092 }
00093 #endif