/* * This file is a part of Winix * and is not publicly distributed * * Copyright (c) 2008-2011, Tomasz Sowa * All rights reserved. * */ #include "request.h" #include "log.h" #include "plugin.h" #include "misc.h" Request::Request() : char_empty(0) { id = 0; } void Request::SetConfig(Config * pconfig) { config = pconfig; } void Request::Clear() { // id is never 0 if( ++id == 0 ) ++id; RemovePostFileTmp(post_file_tab); post_tab.clear(); post_file_tab.clear(); cookie_tab.clear(); method = none; role = responder; headers.Clear(); page.Clear(); debug.Clear(); page_generated = false; use_html_filter = true; env_request_method = &char_empty; env_request_uri = &char_empty; env_http_cookie = &char_empty; env_remote_addr = &char_empty; env_http_host = &char_empty; env_http_user_agent = &char_empty; env_fcgi_role = &char_empty; env_content_type = &char_empty; env_http_accept_encoding = &char_empty; env_https = &char_empty; item_tab.clear(); item.Clear(); dir_tab.clear(); last_item = &item; is_item = false; function = 0; // !! dodac jakas empty funkcje param_tab.clear(); anchor.clear(); status = WINIX_ERR_OK; browser_msie = false; redirect_to.clear(); redirect_type = 303; x_sendfile.clear(); send_as_attachment = false; using_ssl = false; start_time = 0; memset(&start_tm, 0, sizeof(start_tm)); } void Request::RequestStarts() { // clearing it is better to use at the end of a request // so starting is much faster start_time = std::time(0); start_tm = Time(start_time); } // value can be null void Request::SetCookie(const char * name, const char * value, tm * expires) { headers << "Set-Cookie: " << name << "="; if( value && value[0]!=0 ) headers << value; else headers << "\"\""; if( expires ) headers << "; expires=" << DateToStrCookie(expires) << " GMT"; headers << "; path=/; domain=" << config->base_url << "\r\n"; /* don't use '; secure' flag if you are using both sites (with SSL and without SSL) -- with secure flag the cookie is sent only through SSL and if you accidentally open a new window without SSL (http://) then winix will create a new session for you and the previous session (https://) will be lost (cookie is overwritten on the client browser) */ } void Request::SetCookie(const char * name, long value, tm * expires) { headers << "Set-Cookie: " << name << "=" << value; if( expires ) headers << "; expires=" << DateToStrCookie(expires) << " GMT"; headers << "; path=/; domain=" << config->base_url << "\r\n"; } bool Request::IsPostVar(const wchar_t * var) { PostTab::iterator p; p = post_tab.find(var); if( p == post_tab.end() ) return false; return true; } bool Request::IsPostVar(const std::wstring & var) { PostTab::iterator p; p = post_tab.find(var); if( p == post_tab.end() ) return false; return true; } const std::wstring & Request::PostVar(const wchar_t * var) { PostTab::iterator p = post_tab.find(var); if( p == post_tab.end() ) return str_empty; return p->second; } const std::wstring & Request::PostVar(const std::wstring & var) { PostTab::iterator p = post_tab.find(var); if( p == post_tab.end() ) return str_empty; return p->second; } bool Request::PostVar(const wchar_t * var, std::wstring & result) { PostTab::iterator p = post_tab.find(var); if( p == post_tab.end() ) { result.clear(); return false; } result = p->second; return true; } bool Request::PostVar(const std::wstring & var, std::wstring & result) { PostTab::iterator p = post_tab.find(var); if( p == post_tab.end() ) { result.clear(); return false; } result = p->second; return true; } std::wstring * Request::PostVarp(const wchar_t * var) { PostTab::iterator p = post_tab.find(var); if( p == post_tab.end() ) return 0; return &p->second; } bool Request::AllPostVarEmpty() { PostTab::iterator i; for(i=post_tab.begin() ; i!=post_tab.end() ; ++i) if( !i->second.empty() ) return false; return true; } bool Request::IsParam(const wchar_t * param_name) { ParamTab::iterator i; for(i=param_tab.begin() ; i!=param_tab.end() ; ++i) { if( i->name == param_name ) return true; } return false; } bool Request::IsParam(const std::wstring & param_name) { ParamTab::iterator i; for(i=param_tab.begin() ; i!=param_tab.end() ; ++i) { if( i->name == param_name ) return true; } return false; } const std::wstring & Request::ParamValue(const wchar_t * param_name) { ParamTab::iterator i; for(i=param_tab.begin() ; i!=param_tab.end() ; ++i) { if( i->name == param_name ) return i->value; } return str_empty; } const std::wstring & Request::ParamValue(const std::wstring & param_name) { return ParamValue(param_name.c_str()); }