import the first version of cmslu
git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@460 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
231
core/httpsimpleparser.cpp
Executable file
231
core/httpsimpleparser.cpp
Executable file
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
* This file is a part of CMSLU -- Content Management System like Unix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2008, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "httpsimpleparser.h"
|
||||
|
||||
|
||||
void HttpSimpleParser::ToLower(std::string & s)
|
||||
{
|
||||
std::string::iterator i;
|
||||
|
||||
for(i=s.begin() ; i!= s.end() ; ++i)
|
||||
{
|
||||
if( *i>='A' && *i<='Z' )
|
||||
*i = *i - 'A' + 'a';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool HttpSimpleParser::IsWhite(int c)
|
||||
{
|
||||
if( c==' ' || c=='\t' )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void HttpSimpleParser::SkipWhiteChars()
|
||||
{
|
||||
while( IsWhite(last_c) )
|
||||
last_c = GetChar();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void HttpSimpleParser::TrimWhiteChars(std::string & s)
|
||||
{
|
||||
if( s.empty() )
|
||||
return;
|
||||
|
||||
std::string::size_type i;
|
||||
|
||||
for(i = 0 ; i<s.size() && IsWhite(s[i]) ; ++i);
|
||||
|
||||
if( i == s.size() )
|
||||
{
|
||||
// all characters are white
|
||||
s.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
if( i > 0 )
|
||||
// there are some white characters at the beginning
|
||||
s.erase(0, i);
|
||||
|
||||
|
||||
// s is not empty now (i was not equal s.size())
|
||||
// and we have some non white characters
|
||||
// we stops at the last non white character
|
||||
for(i = s.size()-1 ; i>0 && IsWhite(s[i]) ; --i);
|
||||
|
||||
if( i != s.size()-1 )
|
||||
// there are some white characters at the end
|
||||
// we're starting from i+1 even when i==0 (there are some non white characters)
|
||||
s.erase(i+1, s.size() - i - 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int HttpSimpleParser::ParseHalfHex(int c)
|
||||
{
|
||||
if( c>='a' && c<='z' )
|
||||
c += 'A' - 'a'; // to upper case
|
||||
|
||||
|
||||
if( c >= 'A' )
|
||||
c = c - 'A' + 10;
|
||||
else
|
||||
c = c - '0';
|
||||
|
||||
c &= 0xf;
|
||||
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void HttpSimpleParser::CheckSpecialChar()
|
||||
{
|
||||
if( last_c == '%' )
|
||||
{
|
||||
int c1 = GetChar();
|
||||
int c2 = GetChar();
|
||||
|
||||
if( c1==-1 || c2==-1 )
|
||||
last_c = -1;
|
||||
|
||||
c1 = ParseHalfHex(c1);
|
||||
c2 = ParseHalfHex(c2);
|
||||
|
||||
last_c = (c1 << 4) + c2;
|
||||
}
|
||||
else
|
||||
if( last_c == '+' )
|
||||
{
|
||||
last_c = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void HttpSimpleParser::ReadName()
|
||||
{
|
||||
// we're checking 'separator' and '=' because the string is allowed not having '=' (the value is optional)
|
||||
|
||||
for( ; last_c!=-1 && last_c!=separator && last_c!='=' ; last_c = GetChar() )
|
||||
{
|
||||
if( recognize_special_chars )
|
||||
CheckSpecialChar();
|
||||
|
||||
if( last_c != -1 )
|
||||
last_name += last_c;
|
||||
}
|
||||
|
||||
|
||||
if( last_c == '=' )
|
||||
last_c = GetChar();
|
||||
}
|
||||
|
||||
|
||||
void HttpSimpleParser::ReadQuotedValue()
|
||||
{
|
||||
// skipping '"'
|
||||
last_c = GetChar();
|
||||
|
||||
|
||||
for( ; last_c!=-1 && last_c!='"' ; last_c = GetChar() )
|
||||
{
|
||||
if( recognize_special_chars )
|
||||
CheckSpecialChar();
|
||||
|
||||
if( last_c != -1 )
|
||||
last_value += last_c;
|
||||
}
|
||||
|
||||
|
||||
if( last_c == '"' )
|
||||
last_c = GetChar();
|
||||
|
||||
// looking for a separator (skipping)
|
||||
for( ; last_c!=-1 && last_c!=separator ; last_c = GetChar() );
|
||||
|
||||
}
|
||||
|
||||
|
||||
void HttpSimpleParser::ReadValue()
|
||||
{
|
||||
if( skip_white_chars )
|
||||
SkipWhiteChars();
|
||||
|
||||
if( value_can_be_quoted && last_c == '"' )
|
||||
{
|
||||
ReadQuotedValue();
|
||||
}
|
||||
else
|
||||
{
|
||||
for( ; last_c!=-1 && last_c!=separator ; last_c = GetChar() )
|
||||
{
|
||||
if( recognize_special_chars )
|
||||
CheckSpecialChar();
|
||||
|
||||
if( last_c != -1 )
|
||||
last_value += last_c;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if( last_c == separator )
|
||||
last_c = GetChar();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void HttpSimpleParser::Parse()
|
||||
{
|
||||
for( last_c = GetChar() ; last_c != -1 ; )
|
||||
{
|
||||
last_name.clear();
|
||||
last_value.clear();
|
||||
|
||||
if( read_name )
|
||||
ReadName();
|
||||
|
||||
ReadValue();
|
||||
|
||||
if( skip_white_chars )
|
||||
{
|
||||
TrimWhiteChars(last_name);
|
||||
TrimWhiteChars(last_value);
|
||||
}
|
||||
|
||||
Parameter(last_name, last_value); // user definied function
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user