The parameters in the request holds the path parameters. The current implementation based on unorder_map has a few downside: 1. Because path parameters are sometime used to mark a file path and they can extend to multiple url section (may contain /) the value will always contain the leading slash. Though this is true for files, for the common case, it would be easier to return the value without it. 2. The []operator of the hash map is non const, this mean that it cannot be used in the common case where the request object is passed as a const reference. 3. There is no exists method in an ordered_map - Usually query parameters are mandatory, still it is usefull to have an easy way of testing if a parameter is found. This series wrap the unordered_map implementation in a manner more suitable for the request. The [] operator will be const and retrun a copy of the parameter value without the leading slash. The at method will keep the old meaning. For clearer code, a path method was added to indicate that the return value has a leading slash. A set method is used for assignment. Older code will continue to work without changes. Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
74 lines
1.7 KiB
C++
74 lines
1.7 KiB
C++
/*
|
|
* This file is open source software, licensed to you under the terms
|
|
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
|
|
* distributed with this work for additional information regarding copyright
|
|
* ownership. You may not use this file except in compliance with the License.
|
|
*
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing,
|
|
* software distributed under the License is distributed on an
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
* KIND, either express or implied. See the License for the
|
|
* specific language governing permissions and limitations
|
|
* under the License.
|
|
*/
|
|
/*
|
|
* Copyright 2015 Cloudius Systems
|
|
*/
|
|
|
|
#ifndef COMMON_HH_
|
|
#define COMMON_HH_
|
|
|
|
#include <unordered_map>
|
|
#include "core/sstring.hh"
|
|
|
|
namespace httpd {
|
|
|
|
|
|
class parameters {
|
|
std::unordered_map<sstring, sstring> params;
|
|
public:
|
|
const sstring& path(const sstring& key) const {
|
|
return params.at(key);
|
|
}
|
|
|
|
sstring operator[](const sstring& key) const {
|
|
return params.at(key).substr(1);
|
|
}
|
|
|
|
const sstring& at(const sstring& key) const {
|
|
return path(key);
|
|
}
|
|
|
|
bool exists(const sstring& key) const {
|
|
return params.find(key) != params.end();
|
|
}
|
|
|
|
void set(const sstring& key, const sstring& value) {
|
|
params[key] = value;
|
|
}
|
|
|
|
void clear() {
|
|
params.clear();
|
|
}
|
|
|
|
};
|
|
|
|
enum operation_type {
|
|
GET, POST, PUT, DELETE, NUM_OPERATION
|
|
};
|
|
|
|
/**
|
|
* Translate the string command to operation type
|
|
* @param type the string "GET" or "POST"
|
|
* @return the operation_type
|
|
*/
|
|
operation_type str2type(const sstring& type);
|
|
|
|
}
|
|
|
|
#endif /* COMMON_HH_ */
|