LeftHookRoll
An HTTP/1.0 compliant web server, as specified by RFC1945
Loading...
Searching...
No Matches
ConfigParser.hpp
Go to the documentation of this file.
1/**
2 * @file ConfigParser.hpp
3 * @brief Parses a webserv.conf file and produces a vector of ServerConf objects.
4 *
5 * Uses a simple two-pass approach:
6 * 1. Tokenize the raw file content.
7 * 2. Walk the token stream to populate ServerConf / LocationConf data structures.
8 *
9 * Throws ConfigParser::ConfigException (a FatalException subclass) on any
10 * syntax or semantic error so the caller can exit cleanly via the normal
11 * fatal-exception handler.
12 */
13
14#pragma once
15
16#include <string>
17#include <vector>
18#include "ServerConf.hpp"
19#include "FatalExceptions.hpp"
20
22{
23public:
24
26 {
27 public:
28 explicit ConfigException(const std::string& msg);
29 virtual ~ConfigException() throw();
30 };
31
32 // Canonical form
33 explicit ConfigParser(const std::string& filePath);
35
36 // Public API
37
38 /**
39 * @brief Parses the config file and returns one ServerConf per server block.
40 * @throws ConfigException on any syntax / validation error.
41 */
42 std::vector<ServerConf> parse();
43
44private:
45 std::string _filePath;
46 std::vector<std::string> _tokens;
47 size_t _pos;
48
49 // Tokenizer
50
51 void _tokenize(const std::string& content);
52
53 // Token stream helpers
54
55 const std::string& _peek() const;
56 const std::string& _consume();
57 void _expect(const std::string& token);
58 bool _atEnd() const;
59
60 // Block parsers
61
63 LocationConf _parseLocationBlock(const std::string& path);
64
65 // Server-level directive handlers
66
67 void _parseListen(ServerConf& conf);
68 void _parseServerName(ServerConf& conf);
69 void _parseMaxBodySize(ServerConf& conf);
70 void _parseErrorPage(ServerConf& conf);
71
72 // Location-level directive handlers
73
74 void _parseRoot(LocationConf& loc);
75 void _parseMethods(LocationConf& loc);
77 void _parseIndex(LocationConf& loc);
79 void _parseReturn(LocationConf& loc);
81
82 // Validators / converters
83
84 struct sockaddr_in _parseSockAddr(const std::string& listenValue);
85 size_t _parseBodySize(const std::string& value);
86 HTTPMethod _parseMethodToken(const std::string& token);
87
88 // Non-copyable
89 ConfigParser(const ConfigParser&);
90 ConfigParser& operator=(const ConfigParser&);
91};
HTTPMethod
Represents the HTTP methods supported by the server as a bitmask.
This class will be used to collect fatal exceptions, which are exceptions that should cause the progr...
Stores per-server configuration directives. This class encapsulates the configuration for a specific ...
const std::string & _consume()
struct sockaddr_in _parseSockAddr(const std::string &listenValue)
std::vector< ServerConf > parse()
Parses the config file and returns one ServerConf per server block.
void _parseUploadStore(LocationConf &loc)
void _parseRoot(LocationConf &loc)
ServerConf _parseServerBlock()
LocationConf _parseLocationBlock(const std::string &path)
void _parseMethods(LocationConf &loc)
void _parseMaxBodySize(ServerConf &conf)
void _parseServerName(ServerConf &conf)
void _parseIndex(LocationConf &loc)
bool _atEnd() const
const std::string & _peek() const
void _tokenize(const std::string &content)
void _expect(const std::string &token)
void _parseErrorPage(ServerConf &conf)
void _parseCgiInterpreter(LocationConf &loc)
void _parseListen(ServerConf &conf)
std::vector< std::string > _tokens
HTTPMethod _parseMethodToken(const std::string &token)
void _parseAutoIndex(LocationConf &loc)
std::string _filePath
size_t _parseBodySize(const std::string &value)
void _parseReturn(LocationConf &loc)