From 014b781fb0a4b1858240b346a7910539952e7f9f Mon Sep 17 00:00:00 2001 From: crs Date: Mon, 3 Jun 2002 16:34:22 +0000 Subject: [PATCH] moved case insensitive comparison utility functions into CString from CHTTPProtocol. --- base/CString.cpp | 47 ++++++++++++++++++++++++++++++ base/CString.h | 14 +++++++++ base/Makefile | 1 + base/base.dsp | 4 +++ http/CHTTPProtocol.cpp | 65 ++++++------------------------------------ http/CHTTPProtocol.h | 16 +---------- 6 files changed, 76 insertions(+), 71 deletions(-) create mode 100644 base/CString.cpp diff --git a/base/CString.cpp b/base/CString.cpp new file mode 100644 index 00000000..e174cad0 --- /dev/null +++ b/base/CString.cpp @@ -0,0 +1,47 @@ +#include "CString.h" +#include +#include + +// +// CStringUtil::CaselessCmp +// + +bool CStringUtil::CaselessCmp::cmpEqual( + const CString::value_type& a, + const CString::value_type& b) +{ + // FIXME -- use std::tolower + return tolower(a) == tolower(b); +} + +bool CStringUtil::CaselessCmp::cmpLess( + const CString::value_type& a, + const CString::value_type& b) +{ + // FIXME -- use std::tolower + return tolower(a) < tolower(b); +} + +bool CStringUtil::CaselessCmp::less( + const CString& a, + const CString& b) +{ + return std::lexicographical_compare( + a.begin(), a.end(), + b.begin(), b.end(), + &CStringUtil::CaselessCmp::cmpLess); +} + +bool CStringUtil::CaselessCmp::equal( + const CString& a, + const CString& b) +{ + return !(less(a, b) || less(b, a)); +} + +bool CStringUtil::CaselessCmp::operator()( + const CString& a, + const CString& b) const +{ + return less(a, b); +} diff --git a/base/CString.h b/base/CString.h index dc0a15f4..10664e30 100644 --- a/base/CString.h +++ b/base/CString.h @@ -44,6 +44,20 @@ public: _Myt(_f, _l CSTRING_ALLOC2) { } }; +class CStringUtil { +public: + class CaselessCmp { + public: + bool operator()(const CString&, const CString&) const; + static bool less(const CString&, const CString&); + static bool equal(const CString&, const CString&); + static bool cmpLess(const CString::value_type&, + const CString::value_type&); + static bool cmpEqual(const CString::value_type&, + const CString::value_type&); + }; +}; + #if defined(_MSC_VER) #pragma warning(pop) #endif diff --git a/base/Makefile b/base/Makefile index 8e262bd4..dcb324f4 100644 --- a/base/Makefile +++ b/base/Makefile @@ -16,6 +16,7 @@ CXXFILES = \ CLog.cpp \ CFunctionJob.cpp \ CStopwatch.cpp \ + CString.cpp \ $(NULL) targets: $(LIBTARGET) diff --git a/base/base.dsp b/base/base.dsp index fa7569aa..ef498104 100644 --- a/base/base.dsp +++ b/base/base.dsp @@ -99,6 +99,10 @@ SOURCE=.\CStopwatch.cpp # End Source File # Begin Source File +SOURCE=.\CString.cpp +# End Source File +# Begin Source File + SOURCE=.\XBase.cpp # End Source File # End Group diff --git a/http/CHTTPProtocol.cpp b/http/CHTTPProtocol.cpp index 0d00fb87..d1bf09e3 100644 --- a/http/CHTTPProtocol.cpp +++ b/http/CHTTPProtocol.cpp @@ -5,57 +5,10 @@ #include "IOutputStream.h" #include "stdsstream.h" #include -#include #include #include #include -// -// CHTTPUtil::CaselessCmp -// - -inline -bool CHTTPUtil::CaselessCmp::cmpEqual( - const CString::value_type& a, - const CString::value_type& b) -{ - // FIXME -- use std::tolower - return tolower(a) == tolower(b); -} - -inline -bool CHTTPUtil::CaselessCmp::cmpLess( - const CString::value_type& a, - const CString::value_type& b) -{ - // FIXME -- use std::tolower - return tolower(a) < tolower(b); -} - -bool CHTTPUtil::CaselessCmp::less( - const CString& a, - const CString& b) -{ - return std::lexicographical_compare( - a.begin(), a.end(), - b.begin(), b.end(), - &CHTTPUtil::CaselessCmp::cmpLess); -} - -bool CHTTPUtil::CaselessCmp::equal( - const CString& a, - const CString& b) -{ - return !(less(a, b) || less(b, a)); -} - -bool CHTTPUtil::CaselessCmp::operator()( - const CString& a, - const CString& b) const -{ - return less(a, b); -} - // // CHTTPRequest // @@ -217,7 +170,7 @@ CHTTPRequest* CHTTPProtocol::readRequest( CString header; if (!(header = request->getHeader("Transfer-Encoding")).empty()) { // we only understand "chunked" encodings - if (!CHTTPUtil::CaselessCmp::equal(header, "chunked")) { + if (!CStringUtil::CaselessCmp::equal(header, "chunked")) { log((CLOG_DEBUG1 "unsupported Transfer-Encoding %s", header.c_str())); throw XHTTP(501); } @@ -295,9 +248,9 @@ void CHTTPProtocol::reply( const CString& header = index->first; // remove certain headers - if (CHTTPUtil::CaselessCmp::equal(header, "Content-Length") || - CHTTPUtil::CaselessCmp::equal(header, "Date") || - CHTTPUtil::CaselessCmp::equal(header, "Transfer-Encoding")) { + if (CStringUtil::CaselessCmp::equal(header, "Content-Length") || + CStringUtil::CaselessCmp::equal(header, "Date") || + CStringUtil::CaselessCmp::equal(header, "Transfer-Encoding")) { // FIXME -- Transfer-Encoding should be left as-is if // not "chunked" and if the version is 1.1 or up. index = reply.m_headers.erase(index); @@ -376,7 +329,7 @@ bool CHTTPProtocol::parseFormData( CString::const_iterator index = std::search( contentType.begin(), contentType.end(), formData, formData + sizeof(formData) - 1, - CHTTPUtil::CaselessCmp::cmpEqual); + CStringUtil::CaselessCmp::cmpEqual); if (index == contentType.end()) { // not form-data return false; @@ -384,7 +337,7 @@ bool CHTTPProtocol::parseFormData( index += sizeof(formData) - 1; index = std::search(index, contentType.end(), boundary, boundary + sizeof(boundary) - 1, - CHTTPUtil::CaselessCmp::cmpEqual); + CStringUtil::CaselessCmp::cmpEqual); if (index == contentType.end()) { // no boundary return false; @@ -436,7 +389,7 @@ bool CHTTPProtocol::parseFormData( body.begin() + endOfHeaders, disposition, disposition + sizeof(disposition) - 1, - CHTTPUtil::CaselessCmp::cmpEqual); + CStringUtil::CaselessCmp::cmpEqual); if (index == contentType.begin() + endOfHeaders) { // bad part return false; @@ -451,7 +404,7 @@ bool CHTTPProtocol::parseFormData( } index = std::search(index, body.begin() + endOfHeader, nameAttr, nameAttr + sizeof(nameAttr) - 1, - CHTTPUtil::CaselessCmp::cmpEqual); + CStringUtil::CaselessCmp::cmpEqual); if (index == body.begin() + endOfHeader) { // no name return false; @@ -466,7 +419,7 @@ bool CHTTPProtocol::parseFormData( CString::size_type namePos = index - body.begin(); index = std::search(index, body.begin() + endOfHeader, quote, quote + 1, - CHTTPUtil::CaselessCmp::cmpEqual); + CStringUtil::CaselessCmp::cmpEqual); if (index == body.begin() + endOfHeader) { // missing close quote return false; diff --git a/http/CHTTPProtocol.h b/http/CHTTPProtocol.h index 316d1fd4..19818b95 100644 --- a/http/CHTTPProtocol.h +++ b/http/CHTTPProtocol.h @@ -10,25 +10,11 @@ class IInputStream; class IOutputStream; -class CHTTPUtil { -public: - class CaselessCmp { - public: - bool operator()(const CString&, const CString&) const; - static bool less(const CString&, const CString&); - static bool equal(const CString&, const CString&); - static bool cmpLess(const CString::value_type&, - const CString::value_type&); - static bool cmpEqual(const CString::value_type&, - const CString::value_type&); - }; -}; - class CHTTPRequest { public: typedef std::list > CHeaderList; typedef std::map CHeaderMap; + CStringUtil::CaselessCmp> CHeaderMap; typedef CHeaderList::const_iterator const_iterator; CHTTPRequest();