All resources now again uses regex for path, since requests/second were no longer much affected by this.

This commit is contained in:
eidheim 2015-02-22 12:06:30 +01:00
commit 7aae65b1b5
3 changed files with 14 additions and 28 deletions

View file

@ -20,9 +20,9 @@ int main() {
//HTTPS-server at port 8080 using 4 threads
HttpsServer server(8080, 1, "server.crt", "server.key");
//Add resources using path- and method-string, and an anonymous function
//Add resources using path-regex and method-string, and an anonymous function
//POST-example for the path /string, responds the posted string
server.resource["/string"]["POST"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
server.resource["^/string$"]["POST"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
//Retrieve string from istream (request->content)
stringstream ss;
request->content >> ss.rdbuf();
@ -39,7 +39,7 @@ int main() {
// "lastName": "Smith",
// "age": 25
//}
server.resource["/json"]["POST"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
server.resource["^/json$"]["POST"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
try {
ptree pt;
read_json(request->content, pt);
@ -55,7 +55,7 @@ int main() {
//GET-example for the path /info
//Responds with request-information
server.resource["/info"]["GET"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
server.resource["^/info$"]["GET"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
stringstream content_stream;
content_stream << "<h1>Request:</h1>";
content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>";
@ -69,10 +69,9 @@ int main() {
response << "HTTP/1.1 200 OK\r\nContent-Length: " << content_stream.tellp() << "\r\n\r\n" << content_stream.rdbuf();
};
//GET-example for the path /match/[number] using regex, responds with the matched string in path (number)
//The 'r' at the beginning of the path-string indicates that the string is a regular expression
//GET-example for the path /match/[number], responds with the matched string in path (number)
//For instance a request GET /match/123 will receive: 123
server.resource["r^/match/([0-9]+)/?$"]["GET"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
server.resource["^/match/([0-9]+)/?$"]["GET"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
string number=request->path_match[1];
response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
};