std::vector<char> StaticRestRequestHandler::handleRequest(const Host & host, const RestRequest & request,
                                                    const std::map<std::string, std::string> & headers, const std::vector<char> & body) {
    UNUSED(host);
    UNUSED(request);
    UNUSED(headers);
    UNUSED(body);
    std::smatch sm;
    if (request.getType() == RestRequestType::RRT_GET && (std::regex_match(request.getUri(), sm, uriRegex) || sm.size() != 2)) {
        std::ostringstream ostr;
        std::string subPath = sm[1];
        if (subPath.empty() || subPath == "/") {
            subPath = defaultFilePath;
        }
        ostr << basePath << "/" << subPath;
        std::string path = ostr.str();
        if (path.find("..") == std::string::npos) {
            std::ifstream inp(path);
            if (inp) {
                inp.seekg(0, inp.end);
                uint32_t length = inp.tellg();
                inp.seekg(0, inp.beg);
                std::stringstream ostr;
                ostr << "HTTP/1.0 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
                std::vector<char> returnValue(length + ostr.str().length());
                memcpy(&returnValue[0], ostr.str().c_str(), ostr.str().length());
                inp.read(&returnValue[ostr.str().length()], length);
                if (inp) {
                    return returnValue;
                }
            }
        }
    }
    return std::vector<char>();
}
Example #2
0
std::vector<char> RestStatus::handleRequest(const Host & host, const RestRequest & request, const std::map<std::string, std::string> & headers, const std::vector<char> & body) {
    UNUSED(host);
    UNUSED(request);
    UNUSED(headers);
    UNUSED(body);
    
    if (request.getType() != RestRequestType::RRT_GET) {
        // not me, return empty
        return std::vector<char>();
    }

    if (request.getUri().substr(0, uriPattern.size()) != uriPattern) {
        // not me, return empty
        return std::vector<char>();
    }
    
    if (request.getUri() == uriPattern) {
        return basicStatus();
    }
    std::string rest = request.getUri().substr(uriPattern.size());
    if (rest == "/supports/protocol") {
        return supportedProtocols();
    }
    if (rest == "/supports/contentmanager") {
        return supportedContentManagers();
    }
    
    return std::vector<char>();
}
void ApiDispatcher::handle(RestRequest& request, RestResponse& response) {
	RestRequest::Method method = request.getMethod();
	if (this->endpoints.count(method) > 0) {
		EndPoint * ep = this->endpoints[method];
		try {
			ep->handle(request,response);
		} catch (NoSuchMethodHandlerException const & err) {
			LOG_INFO << LOGGER_PREFIX << err.what();
			response.setStatus(STATUS_403_FORBIDDEN);
		} catch (ParsingContentException const & err) {
				response.setStatus(STATUS_400_BAD_REQUEST);
		}

	}
	else {

		LOG_INFO << LOGGER_PREFIX << "Not Allowed Method on " << request.getUri();
		response.setStatus(STATUS_405_METHOD_NOT_ALLOWED);
	}
}
Example #4
0
void RestResourceHandler::handleCommon(const RestRequest& request, RestResponse& response)
{
    response.setApiVersion(getApiVersion());
    response.setFullContentType(request.getFullContentType());
}