2008-12-30 02:05:03 +01:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2008-12-30 02:05:03 +01:00
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
2012-01-12 03:24:08 +01:00
|
|
|
* Copyright (c) 2008-2012, Tomasz Sowa
|
2008-12-30 02:05:03 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "functionparser.h"
|
2010-08-10 18:12:50 +02:00
|
|
|
#include "core/log.h"
|
|
|
|
#include "core/item.h"
|
|
|
|
#include "core/error.h"
|
|
|
|
#include "functions.h"
|
2012-01-12 03:24:08 +01:00
|
|
|
#include "utf8/utf8.h"
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
|
|
|
|
FunctionParser::FunctionParser()
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
utf8 = false;
|
2008-12-30 02:05:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
void FunctionParser::UTF8(bool use_utf8)
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
utf8 = use_utf8;
|
|
|
|
}
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2009-02-06 14:12:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
bool FunctionParser::Parse(Cur * pcur, Db * pdb, Functions * pfunctions, System * psystem)
|
|
|
|
{
|
|
|
|
db = pdb;
|
|
|
|
cur = pcur;
|
|
|
|
system = psystem;
|
|
|
|
functions = pfunctions;
|
|
|
|
last_dir = 0;
|
|
|
|
path = cur->request->env_request_uri;
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
//!! mozna dodac sprawdzanie dlugosci path
|
|
|
|
// jesli wieksza niz np 2048 to zglosic incorrect url
|
|
|
|
// i nie parsowac
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
ParseDirsItemFunction();
|
|
|
|
ParseParams();
|
|
|
|
ParseAnchor();
|
2010-08-14 16:45:25 +02:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
return cur->request->status == WINIX_ERR_OK;
|
2008-12-30 02:05:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
void FunctionParser::ParseDirsItemFunction()
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
if( !AddRootDir() )
|
2008-12-30 02:05:03 +01:00
|
|
|
return;
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
ReadName();
|
|
|
|
|
|
|
|
while( IsDir() )
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
AddDir();
|
|
|
|
ReadName();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( name.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( CheckAddFunction() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( CheckAddItem() )
|
|
|
|
{
|
|
|
|
ReadName();
|
2010-08-14 16:45:25 +02:00
|
|
|
|
2011-09-29 00:24:24 +02:00
|
|
|
if( !name.empty() )
|
2009-12-30 21:46:12 +01:00
|
|
|
{
|
2011-09-29 00:24:24 +02:00
|
|
|
CheckAddFunction();
|
|
|
|
|
|
|
|
if( !cur->request->function )
|
|
|
|
{
|
|
|
|
log << log3 << "FP: unknown function: " << name << logend;
|
|
|
|
cur->request->status = WINIX_ERR_NO_ITEM;
|
|
|
|
}
|
2009-12-30 21:46:12 +01:00
|
|
|
}
|
2011-07-29 00:18:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FunctionParser::AddRootDir()
|
|
|
|
{
|
|
|
|
last_dir = system->dirs.GetRootDir();
|
|
|
|
|
|
|
|
if( !last_dir )
|
|
|
|
{
|
|
|
|
log << log3 << "FP: there is not a root directory" << logend;
|
|
|
|
cur->request->status = WINIX_ERR_INTERNAL_ERROR;
|
|
|
|
return false;
|
|
|
|
}
|
2009-12-30 21:46:12 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
AddDir();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool FunctionParser::IsDir()
|
|
|
|
{
|
|
|
|
// directory names should not be empty
|
|
|
|
if( name.empty() || !last_dir )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
last_dir = system->dirs.GetDir(name, last_dir->id);
|
|
|
|
|
|
|
|
return last_dir != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FunctionParser::CheckAddItem()
|
|
|
|
{
|
|
|
|
// cur->request->dir_tab has at least one element
|
|
|
|
long parent_id = cur->request->dir_tab.back()->id;
|
|
|
|
Error status = db->GetItem(parent_id, name, cur->request->item);
|
|
|
|
|
|
|
|
if( status == WINIX_ERR_OK )
|
|
|
|
{
|
2011-01-23 15:15:30 +01:00
|
|
|
log << log3 << "FP: Item: id: " << cur->request->item.id << ", url: " << cur->request->item.url << logend;
|
2011-07-29 00:18:10 +02:00
|
|
|
cur->request->last_item = &cur->request->item;
|
|
|
|
cur->request->is_item = true;
|
|
|
|
return true;
|
2008-12-30 02:05:03 +01:00
|
|
|
}
|
2011-07-29 00:18:10 +02:00
|
|
|
|
|
|
|
if( status == WINIX_ERR_NO_ITEM )
|
2009-12-30 21:46:12 +01:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
log << log3 << "FP: No Item: url: " << name << logend;
|
|
|
|
cur->request->status = WINIX_ERR_NO_ITEM;
|
|
|
|
return false;
|
2009-12-30 21:46:12 +01:00
|
|
|
}
|
2011-07-29 00:18:10 +02:00
|
|
|
|
|
|
|
log << log1 << "FP: db error" << logend;
|
|
|
|
cur->request->status = WINIX_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
return false;
|
2008-12-30 02:05:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
bool FunctionParser::CheckAddFunction()
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
cur->request->function = functions->Find(name);
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-01-23 15:15:30 +01:00
|
|
|
if( cur->request->function )
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2011-01-23 15:15:30 +01:00
|
|
|
log << log3 << "FP: Function: " << cur->request->function->fun.url << logend;
|
2011-07-29 00:18:10 +02:00
|
|
|
return true;
|
2008-12-30 02:05:03 +01:00
|
|
|
}
|
2011-07-29 00:18:10 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FunctionParser::AddDir()
|
|
|
|
{
|
|
|
|
cur->request->dir_tab.push_back( last_dir );
|
|
|
|
log << log3 << "FP: Directory: ";
|
|
|
|
|
|
|
|
if( last_dir->parent_id == -1 )
|
|
|
|
log << "(root)" << logend;
|
|
|
|
else
|
|
|
|
log << last_dir->url << logend;
|
|
|
|
|
|
|
|
cur->request->last_item = cur->request->dir_tab.back();
|
2008-12-30 02:05:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-04 22:27:14 +02:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
void FunctionParser::ParseParams()
|
2010-07-04 22:27:14 +02:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
SkipSlashes();
|
2010-07-04 22:27:14 +02:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
if( *path == '#' )
|
|
|
|
{
|
|
|
|
// there are not any parameters
|
2010-07-04 22:27:14 +02:00
|
|
|
return;
|
2011-07-29 00:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( *path == '?' )
|
|
|
|
ParseOrdinaryParams();
|
|
|
|
else
|
|
|
|
ParseWinixParams();
|
|
|
|
}
|
|
|
|
|
2010-07-04 22:27:14 +02:00
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
void FunctionParser::ParseOrdinaryParams()
|
|
|
|
{
|
|
|
|
if( *path=='?' )
|
|
|
|
path += 1;
|
|
|
|
|
|
|
|
do
|
2010-07-04 22:27:14 +02:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
ReadOrdinaryParName();
|
|
|
|
|
|
|
|
if( name.empty() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
ReadOrdinaryParValue();
|
|
|
|
|
|
|
|
if( *path == '&' )
|
|
|
|
path += 1;
|
|
|
|
|
|
|
|
AddParam();
|
2010-07-04 22:27:14 +02:00
|
|
|
}
|
2011-07-29 00:18:10 +02:00
|
|
|
while( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void FunctionParser::ParseWinixParams()
|
|
|
|
{
|
|
|
|
do
|
2010-07-04 22:27:14 +02:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
SkipSlashes();
|
|
|
|
ReadWinixParName();
|
2010-07-04 22:27:14 +02:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
if( name.empty() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
ReadWinixParValue();
|
|
|
|
|
|
|
|
AddParam();
|
2010-07-04 22:27:14 +02:00
|
|
|
}
|
2011-07-29 00:18:10 +02:00
|
|
|
while( true );
|
|
|
|
}
|
2010-07-04 22:27:14 +02:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
|
|
|
|
void FunctionParser::AddParam()
|
|
|
|
{
|
|
|
|
param.name = name;
|
|
|
|
param.value = value;
|
2011-01-23 15:15:30 +01:00
|
|
|
cur->request->param_tab.push_back(param);
|
2010-07-04 22:27:14 +02:00
|
|
|
|
2010-07-05 17:21:06 +02:00
|
|
|
log << log3 << "FP: Param: name=" << param.name;
|
|
|
|
|
|
|
|
if( !param.value.empty() )
|
|
|
|
log << ", value=" << param.value;
|
|
|
|
|
|
|
|
log << logend;
|
2010-07-04 22:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
void FunctionParser::ParseAnchor()
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
if( *path == 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( *path != '#' )
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
cur->request->status = WINIX_ERR_INCORRECT_URI;
|
|
|
|
return;
|
2008-12-30 02:05:03 +01:00
|
|
|
}
|
2011-07-29 00:18:10 +02:00
|
|
|
|
|
|
|
path += 1;
|
|
|
|
name_ascii.clear();
|
|
|
|
|
|
|
|
while( *path )
|
|
|
|
name_ascii += GetChar();
|
|
|
|
|
|
|
|
ToWide(name_ascii, cur->request->anchor);
|
|
|
|
|
|
|
|
if( !cur->request->anchor.empty() )
|
|
|
|
log << log3 << "FP: anchor: " << cur->request->anchor << logend;
|
2008-12-30 02:05:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
void FunctionParser::SkipSlashes()
|
|
|
|
{
|
|
|
|
while( *path == '/' )
|
|
|
|
path += 1;
|
|
|
|
}
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
|
|
|
|
int FunctionParser::FromHex(int c)
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
if( c>='0' && c<='9' )
|
|
|
|
return c - '0';
|
|
|
|
else
|
|
|
|
if( c>='a' && c<='f' )
|
|
|
|
return c - 'a' + 10;
|
|
|
|
else
|
|
|
|
if( c>='A' && c<='F' )
|
|
|
|
return c - 'A' + 10;
|
|
|
|
else
|
|
|
|
cur->request->status = WINIX_ERR_INCORRECT_URI;
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
int FunctionParser::GetChar()
|
|
|
|
{
|
|
|
|
int c;
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
if( *path == 0 )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( *path == '%' && *(path+1)!=0 && *(path+2)!=0 )
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2011-07-29 00:18:10 +02:00
|
|
|
c = (FromHex(*(path+1)) << 4) + FromHex(*(path+2));
|
|
|
|
|
|
|
|
if( c == 0 )
|
|
|
|
cur->request->status = WINIX_ERR_INCORRECT_URI;
|
|
|
|
|
|
|
|
path += 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c = *path;
|
|
|
|
path += 1;
|
2008-12-30 02:05:03 +01:00
|
|
|
}
|
2009-12-30 21:46:12 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
return c;
|
2008-12-30 02:05:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
void FunctionParser::ToWide(const std::string & src, std::wstring & dst)
|
|
|
|
{
|
|
|
|
dst.clear();
|
|
|
|
|
|
|
|
if( utf8 )
|
2012-01-12 03:24:08 +01:00
|
|
|
PT::UTF8ToWide(src, dst, false);
|
2011-07-29 00:18:10 +02:00
|
|
|
else
|
|
|
|
AssignString(src, dst, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void FunctionParser::ReadName()
|
|
|
|
{
|
|
|
|
SkipSlashes();
|
|
|
|
name_ascii.clear();
|
|
|
|
|
|
|
|
while( *path && *path!='/' && *path!='?' && *path!='#' )
|
|
|
|
name_ascii += GetChar();
|
|
|
|
|
|
|
|
ToWide(name_ascii, name);
|
|
|
|
}
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
void FunctionParser::ReadOrdinaryParName()
|
|
|
|
{
|
|
|
|
name_ascii.clear();
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
while( *path && *path!='=' && *path!='&' && *path!='#' )
|
|
|
|
name_ascii += GetChar();
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
ToWide(name_ascii, name);
|
|
|
|
}
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
void FunctionParser::ReadOrdinaryParValue()
|
|
|
|
{
|
|
|
|
value_ascii.clear();
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
if( *path=='=' )
|
|
|
|
path += 1;
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
while( *path && *path!='&' && *path!='#' )
|
|
|
|
value_ascii += GetChar();
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
ToWide(value_ascii, value);
|
|
|
|
}
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
void FunctionParser::ReadWinixParName()
|
|
|
|
{
|
|
|
|
name_ascii.clear();
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
while( *path && *path!='/' && *path!=':' && *path!='#' )
|
|
|
|
name_ascii += GetChar();
|
|
|
|
|
|
|
|
ToWide(name_ascii, name);
|
|
|
|
}
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
void FunctionParser::ReadWinixParValue()
|
|
|
|
{
|
|
|
|
value_ascii.clear();
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
if( *path==':' )
|
|
|
|
path += 1;
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
while( *path && *path!='/' && *path!='#' )
|
|
|
|
value_ascii += GetChar();
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
ToWide(value_ascii, value);
|
|
|
|
}
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|