Skip to content

Configuration Parsing Documentation

NHariman edited this page Sep 14, 2022 · 10 revisions

Getting the Configuration Data

After loading in the nginx-esque configuration file, the data is parsed, tokenized and validated via the various subclasses. Each subclass has their own available public methods for use. Below is a short summary of the available getters per class. For full explanation on how a basic configuration file works see config file

All classes are entirely written in coplien form in order for easy copying into other variables and default constructors are available for easy copying.

TargetConfig Class

TargetConfig is an intermediate class that inherits from parent class LocationContext and grandparent class ConfigValues. It uses a host string, port string, target string and the NginxConfig class to in its setup() method and retrieves only the data relevant to the given host/port/target combination.

If host does not match any given server_name + listen then the first server is given. If target is not matched exactly, the data from the location context with the closest match is given. If no closest match is found it will return the data in the / location.

It is set up using the Setup(NginxConfig *config, std::string host, std::string port, std::string target) method. Example:

NginxConfig input_file("Default.conf");
		
TargetConfig target;
target.Setup(&input_file, "localhost", "80", "/");

The class contains the following getters:

		bool						IsAllowedMethod(std::string method);
		CGIPass						GetCGIPass() const;
		std::string					GetRoot() const;
		std::string					GetAlias() const;
		std::vector<std::string> 			GetIndex() const;
		size_t						GetMaxBodySize() const;
		std::vector<ErrorPage>				GetErrorPage() const;
		bool						GetAutoindex() const;
		ReturnDir 					GetReturn() const;

		std::string					GetFinalPath() const;

		ServerContext					GetServer() const;
		LocationContext					GetLocation() const;

The first few are standard directive getters, the data is obtained either from the LocationContext class or the ServerContext class depending on if the directive was set in LocationContext or not.

GetFinalPath() is a unique getter that returns the final parsed path to the target.

GetServer() and GetLocation() returns the ServerContext or the LocationContext belonging to that instance of the TargetConfig class.

It's important to note for return and CGIPass their respective IsSet() should be called to find out if there is content to use within those classes. Same for ErrorPage vector which should be queried with IsEmpty() before use.

NginxConfig Class

NginxConfig class is the main class which hosts the server contexts and their subsequent location contexts. This will act as the main entry point.

available getters

  • std::string GetConfigFile() const; Gets the string containing the entire config file, without comments.
  • size_t GetServerContextAmount() const; gets the amount of server contexts found
  • std::vector<ServerContext> GetServers() const; gets the vector of server contexts for use. (See ServerContext class header for available getters within this context)

ServerContext

Per server directive, a ServerContext class is created, creating a vector block per server. Each then contains the following variables:

std::vector<LocationContext>	_location_contexts;
std::pair<in_addr_t, int>	_listen;
std::vector<std::string>	_server_name;
std::string			_root;
std::vector<std::string>	_index;
int				_client_max_body_size;
std::map<int, std::string>	_error_page;
bool				_autoindex;
ReturnDir			_return_dir;

These are private variables that cannot be edited. Their data can be acquired for use through the following methods:

Available getters // these getters do not throw as they are always set (except for error_page and return)

  • std::vector<LocationContext> GetLocationContexts() const; - Gets available location contexts (See LocationContext class header for available getters within this context)
  • std::pair<in_addr_t, int> GetListen() const; - Gets listen directive
  • in_addr_t GetIPAddress() const; - Gets IP address
  • int GetPortNumber() const; - Gets Port number
  • std::vector<std::string> GetServerNameVector() const; - Gets vector of server_name
  • std::string GetRoot() const; - gets root directive within server context
  • std::vector<std::string> GetIndex() const; - Get vector of indexes in std::string format.
  • size_t GetClientMaxBodySize() const; - Gets client_max_body_size in size_t form.
  • std::map<int, std::string> GetErrorPage() const; - gets vector of error_page entries in format ErrorPage. (See ErrorPage header for getters)
  • bool IsSet(std::string key) const; - checks if a certain directive indicated by key has been set or not.

LocationContext

Within each server different location contexts can be defined. These are then stored in a vector of LocationContext classes. Each class contains the following private variables:

LocationUri				_location_uri;
bool					_autoindex;
std::string				_root; // cannot be set if alias is set
std::string				_alias; // cannot be set if root is set
std::vector<std::string>		_index;
int					_client_max_body_size;
std::map<int, std::string>		_error_page;
CGIPass					_cgi_pass;
AllowedMethods				_allowed_methods;
ReturnDir				_return_dir;

These can be acquired with the following methods:

Available getters These getters throw exception classes if accessed when they shouldn't be.

  • bool IsSet(std::string key); - can be used to check if a certain directive is set by passing a string of the directive in question (available directives: "autoindex", "root", "index", "client_max_body_size", "error_page", "fastcgi_pass", "allowed_methods", "return"), returns true if set, false if not set
  • LocationUri GetLocationUri() const; - Gets the uri of the location context in format LocationUri (see LocationUri header for more available getters.)
  • bool GetAutoindex() const; - returns true of autoindex is on, false if autoindex is off.
  • std::string GetRoot() const; - returns the root of the location context
  • std::vector<std::string> GetIndex() const; - returns available indexes for the location context as a vector of strings.
  • int GetClientMaxBodySize() const; - returns client_max_body_size in int.
  • CGIPass GetCGIPass() const; - returns of CGIPass class.
  • std::map<int, std::string> GetErrorPage() const; - returns a vector of type ErrorPage (see ErrorPage header for information about getters)
  • AllowedMethods GetAllowedMethods() const; - returns AllowedMethods in type AllowedMethods. (Check AllowedMethods for getters)

Available directive classes

Some variables within the server and location contexts share the same custom datatype for easy use. They also contain private variables and can only be accessed through the following methods.

LocationUri

  • std::string getUri() const; - retrieves parsed URI from URI class
  • URI getURIClass() const; - retrieves entire URI class of that location target
  • std::string getInputURI() const; - returns the UNPARSED URI
  • bool IsDirectory() const; - checks if URI is a directory.

CGIPass

  • std::string GetFileExtension() const; - gets file extension from CGIPass
  • std::string GetExecutablePath() const; - gets path to the executable for the CGI script

AllowedMethods

  • bool GetGET() const; - returns if GET method is allowed
  • bool GetPOST() const; - returns if POST method is allowed
  • bool GetDELETE() const; - returns if DELETE method is allowed

ReturnDir

  • bool GetCode() const; - gets the return code listed in the return directive.
  • std::string GetUrl() const; - gets the url of Return.

Misc information

ConfigValues class

The config values class is the base class of ServerContext and LocationContext, the two share some variables such as: root, index, client_max_body_size, error_page, autoindex, root and return. It also has the appropriate getters for these functions and contains most exception classes for custom messages.

ServerContext has the unique variables: listen and server_name

LocationContext has the unique variables: fastcgi_pass and allowed_methods

Except for the getters, the class also has some pure virtual methods:

virtual void InitChecklist() = 0;
virtual int IsDirective(std::string directive) = 0;
virtual void GetDirectiveValuePairs(std::string data){ static_cast<void>(data); throw MethodNotSetException("GetDirectiveValuePairs()");}
virtual void SetValue(int directive, std::string value) = 0;
virtual void CheckListVerification() = 0;

These have been defined in both the Server and Location Contexts.