-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocation.hpp
92 lines (82 loc) · 3.86 KB
/
Location.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef LOCATION_HPP_
#define LOCATION_HPP_
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include "Request.hpp"
#include "Log.hpp"
// The location directive data for Server.
// - Member variables
// _route: The route to match with target resource URI.
// _root: The path to replace matching _route in target resource URI.
// _index: The default file to answer if the request is a directory
// _autoindex: The toggle whether turn on or off directory listing.
// _allowedHTTPMethod: The bit flags of accepted HTTP methods for the route.
// _cgiExtension: The file extension of CGI to call.
// _others: Variable for additional data.
// std::string _HTTPRedirection: The URI to redirect.
class Location {
public:
Location();
bool isRouteMatch(const std::string& resourceURI) const;
bool isRequestMethodAllowed(HTTP::RequestMethod requestMethod) const;
void updateRepresentationPath(const std::string& resourceURI, std::string& representationPath) const;
void updateRepresentationCGIPath(const std::string& resourceURI, std::string& representationCGIPath) const;
std::string getRoute() const { return this->_route; };
std::string getRoot() const { return this->_root; };
std::string getIndex() const { return this->_index; };
bool getAutoIndex() const { return this->_autoindex; };
char getAllowedHTTPMethod() const { return this->_allowedHTTPMethod; };
std::vector<std::string> getCGIExtension() const { return this->_cgiExtension; };
std::map<std::string, std::vector<std::string> > const &getOtherDirective() const { return this->_others; };
int getClientMaxBodySize() const;
void setRoute(std::string route) { this->_route = route; };
void setRoot(std::string root) { this->_root = root; };
void setIndex(std::vector<std::string> idx) {
this->_index = idx[0]; // TODO multiple index
};
void setAutoIndex(bool isAutoindex) { this->_autoindex = isAutoindex; };
void setAllowedHTTPMethod(std::vector<std::string> allowedMethod) {
char beAllowed = '\0';
for (std::vector<std::string>::const_iterator itr = allowedMethod.begin(); itr != allowedMethod.end(); itr++) {
if (*itr == "GET")
beAllowed |= HTTP::RM_GET;
else if (*itr == "POST")
beAllowed |= HTTP::RM_POST;
else if (*itr == "DELETE")
beAllowed |= HTTP::RM_DELETE;
else if (*itr == "PUT")
beAllowed |= HTTP::RM_PUT;
else
beAllowed |= HTTP::RM_UNKNOWN;
}
if (beAllowed)
this->_allowedHTTPMethod = beAllowed;
};
void setCGIExtension(std::vector<std::string> cgiExt) { this->_cgiExtension = cgiExt; }
void setOtherDirective(std::string directiveName, std::vector<std::string> directiveValue) {
_others.insert(make_pair(directiveName, directiveValue));
};
private:
std::string _route;
std::string _root;
std::string _index;
bool _autoindex;
char _allowedHTTPMethod;
std::vector<std::string> _cgiExtension;
std::map<std::string, std::vector<std::string> > _others;
};
// Return whether the resource path is match to this->_route.
// - Parameters resourceURI: The path of target resource in request message.
// - Return: Whether the resource path is match to this->_route.
inline bool Location::isRouteMatch(const std::string& resourceURI) const {
return (resourceURI.compare(0, this->_route.length(), this->_route) == 0);
}
// Return whether the 'requestMethod' is allowed in this location.
// - Parameters requestMethod: request method of request.
// - Return: whether the 'requestMethod' is allowed in this location.
inline bool Location::isRequestMethodAllowed(HTTP::RequestMethod requestMethod) const {
return (this->_allowedHTTPMethod & requestMethod);
}
#endif // LOCATION_HPP_