311 lines
9.2 KiB
C++
Executable File
311 lines
9.2 KiB
C++
Executable File
/*
|
|
* This file is a part of Winix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2008-2013, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#ifndef headerfile_winix_core_request
|
|
#define headerfile_winix_core_request
|
|
|
|
#include <fcgiapp.h>
|
|
#include <sstream>
|
|
#include <vector>
|
|
#include "requesttypes.h"
|
|
#include "item.h"
|
|
#include "error.h"
|
|
#include "config.h"
|
|
#include "textstream.h"
|
|
#include "templates/htmltextstream.h"
|
|
#include "date/date.h"
|
|
#include "space/space.h"
|
|
#include "space/spacetojson.h"
|
|
|
|
|
|
|
|
class FunctionBase;
|
|
|
|
|
|
|
|
|
|
struct Request
|
|
{
|
|
// request id
|
|
// is incremented for each request and is never 0
|
|
// (from -1 will be incremented twice)
|
|
// it's used for some optimalizations e.g. in templates
|
|
size_t id;
|
|
|
|
// !! moze pozbyc sie tego none?
|
|
enum Method { get, post, head, none } method;
|
|
enum Role { responder, authorizer } role;
|
|
|
|
// headers, page and debug
|
|
//std::ostringstream headers, page, debug;
|
|
// !! IMPROVE ME change headers to some kind of a map, may PT::Space ?
|
|
TextStream<std::string> headers;
|
|
HtmlTextStream debug;
|
|
|
|
|
|
// winix can return either a text answer or a binary answer
|
|
// if send_bin_stream is true then the binary answer is sent (out_bin_stream)
|
|
// or if send_bin_stream is false then the text answer is sent
|
|
// default: false
|
|
//
|
|
//
|
|
//
|
|
// winix answer send to the client's browser
|
|
// |
|
|
// |
|
|
// depending on send_bin_stream
|
|
// -------------------------------------------------
|
|
// | |
|
|
// text answer binary answer
|
|
// | |
|
|
// depending on return_json sending out_bin_stream
|
|
// ------------------------------------
|
|
// | |
|
|
// normal request ajax request
|
|
// | |
|
|
// sending out_streams[0] depending on return_info_only
|
|
// ------------------------------------------------------
|
|
// | |
|
|
// generating JSON object from: generating JSON object only from info
|
|
// out_streams and info, e.g.: e.g.:
|
|
// { { info object serialized here }
|
|
// "stream_1": "some html content",
|
|
// "stream_2": "some other html content",
|
|
// "info": { info object serialized here }
|
|
// }
|
|
// note that out_streams[0] is not sent
|
|
// in JSON answers
|
|
//
|
|
//
|
|
bool send_bin_stream;
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------
|
|
// binary answer
|
|
//
|
|
|
|
// binary page sent to the client if send_bin_stream is true
|
|
BinaryPage out_bin_stream;
|
|
|
|
//
|
|
// -------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------
|
|
// text answer
|
|
//
|
|
|
|
// when returning the text answer we can either return the whole html page (normal requests)
|
|
// or a JSON object (for requests generated from AJAX)
|
|
// if return_json is false then we return the whole html page (which is in out_streams[0])
|
|
// if return_json is true we are creating an JSON object from out_streams
|
|
// (zero stream is ignored) and from info space (see above picture)
|
|
// (or just only from info if return_info_only is true)
|
|
// default: false
|
|
// return_json is set to true by App at the beginning of a request
|
|
// if reqtype:json parameter is present (in the url)
|
|
// note: return_json is only valid if send_bin_stream is false
|
|
bool return_json;
|
|
|
|
// main text output streams where the html otput is generated from ezc templates
|
|
// the zero stream (out_streams[0]) is used as the main stream
|
|
// to which the whole html page (with doctype, head, body) is generated
|
|
// the rest streams can be only used in ajax requests (send in JSON format to the client)
|
|
// in ezc templates you can use [ezc stream ...] keyword
|
|
// to switch between streams e.g. [ezc stream "0" "2"]
|
|
std::vector<HtmlTextStream> out_streams;
|
|
|
|
// if true the JSON object is generated only from info (out_streams are not used)
|
|
// default: false
|
|
bool return_info_only;
|
|
|
|
// additional info added when sending the JSON answer
|
|
PT::Space info;
|
|
|
|
// info serializer
|
|
// if not set then the json_generic_serializer from App will be used
|
|
// default: null (json_generic_serializer used)
|
|
PT::SpaceToJSON * info_serializer;
|
|
|
|
//
|
|
// -------------------------------------------------------------------------------------
|
|
|
|
|
|
// if set to true then the standard template system will not be generated
|
|
// default: false
|
|
bool page_generated;
|
|
|
|
// whether or not the html filter should be used
|
|
// default: true
|
|
bool use_html_filter;
|
|
|
|
|
|
|
|
|
|
|
|
// raw parameters
|
|
PostTab post_tab;
|
|
PostFileTab post_file_tab;
|
|
CookieTab cookie_tab;
|
|
|
|
// html anchor (those part of URI after '#' character)
|
|
std::wstring anchor;
|
|
|
|
// environment variables
|
|
// they are not null -- when the server doesn't have such a variable
|
|
// it will be pointing into 'char_empty' which is default '\0'
|
|
const char * env_request_method;
|
|
const char * env_request_uri;
|
|
const char * env_http_cookie;
|
|
const char * env_remote_addr;
|
|
const char * env_http_host;
|
|
const char * env_http_user_agent;
|
|
const char * env_http_accept_encoding;
|
|
const char * env_fcgi_role;
|
|
const char * env_content_type;
|
|
const char * env_https;
|
|
|
|
// current IP address of the remote host (read from REMOTE_ADDR environment variable)
|
|
// (at the moment only IPv4 are supported)
|
|
int ip;
|
|
|
|
// true if the browser is Microsoft Internet Explorer
|
|
bool browser_msie;
|
|
|
|
// true if the browser is Konqueror
|
|
bool browser_konqueror;
|
|
|
|
// true if we are using encrypted connection (SSL)
|
|
bool using_ssl;
|
|
|
|
// current directory
|
|
std::vector<Item*> dir_tab;
|
|
|
|
// true if a file exists
|
|
bool is_item;
|
|
|
|
// current file (if exists)
|
|
Item item;
|
|
|
|
// current winix function
|
|
// null if there is no a function
|
|
FunctionBase * function;
|
|
|
|
// parameters (name:value)
|
|
ParamTab param_tab;
|
|
|
|
// request status
|
|
Error status;
|
|
|
|
// usually items in the current directory (depends on the function)
|
|
std::vector<Item> item_tab;
|
|
|
|
// if not empty means an address for redirecting to
|
|
// it should be url-encoded
|
|
std::wstring redirect_to;
|
|
std::string aredirect_to;
|
|
|
|
// a redirect type
|
|
// following redirect types are supported:
|
|
// 300 Multiple Choices
|
|
// 301 Moved Permanently
|
|
// 302 Found
|
|
// 303 See Other (default)
|
|
// 307 Temporary Redirect
|
|
int redirect_type;
|
|
|
|
// send header X-LIGHTTPD-send-file with path to a file
|
|
std::wstring x_sendfile;
|
|
|
|
// send as attachment (causes header: content-disposition: attachment)
|
|
bool send_as_attachment;
|
|
|
|
// this is a pointer either to the item (if exists) or to the last directory
|
|
Item * last_item;
|
|
|
|
// request start time
|
|
// Time() methods are very slow so it is better to directly use those two values
|
|
// they are set when a request starts
|
|
time_t start_time;
|
|
PT::Date start_date;
|
|
|
|
// a subdomain
|
|
// subdomain = HTTP_HOST environment variable - config->base_url
|
|
std::wstring subdomain;
|
|
|
|
// if this variable is true then winix always return 200 OK header
|
|
// when the status would be 404 (not found) or 403 (permission denied)
|
|
// default: false
|
|
bool use_200_status_for_not_found_and_permission_denied;
|
|
|
|
// options used by ezc generators
|
|
bool gen_trim_white;
|
|
bool gen_skip_new_line;
|
|
bool gen_use_special_chars;
|
|
|
|
|
|
Request();
|
|
void SetConfig(Config * pconfig);
|
|
void RequestStarts();
|
|
void Clear();
|
|
|
|
|
|
|
|
bool IsParam(const wchar_t * param_name);
|
|
bool IsParam(const std::wstring & param_name);
|
|
|
|
const std::wstring & ParamValue(const wchar_t * param_name); // returns an empty string if there is no such a parameter
|
|
const std::wstring & ParamValue(const std::wstring & param_name); // returns an empty string if there is no such a parameter
|
|
|
|
void SetCookie(const char * name, const char * value, PT::Date * expires = 0);
|
|
void SetCookie(const char * name, long value, PT::Date * expires = 0);
|
|
|
|
bool IsPostVar(const wchar_t * var);
|
|
bool IsPostVar(const std::wstring & var);
|
|
|
|
const std::wstring & PostVar(const wchar_t * var); // returns an empty string if there is no such a parameter
|
|
const std::wstring & PostVar(const std::wstring & var); // returns an empty string if there is no such a parameter
|
|
|
|
|
|
bool PostVar(const wchar_t * var, std::wstring & result);
|
|
bool PostVar(const std::wstring & var, std::wstring & result);
|
|
|
|
std::wstring * PostVarp(const wchar_t * var);
|
|
std::wstring * PostVarp(const std::wstring & var);
|
|
|
|
bool AllPostVarEmpty(); // returning true if all post vars are empty
|
|
|
|
void SendAll();
|
|
|
|
|
|
private:
|
|
|
|
Config * config;
|
|
|
|
// contains '\0'
|
|
// used to set env_* pointers to the empty value
|
|
const char char_empty;
|
|
|
|
// used in ParamValue() and PostVar() when there is no such a param
|
|
const std::wstring str_empty;
|
|
|
|
void ClearOutputStreams();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|