dmpserverrequest.cpp 2.2 KB

/**************************************************************************
 * file:              dmpserverrequest.cpp
 
 * Author:            wanzhongping
 * Date:              2021-01-08 11:10:59
 * Email:             zhongpingw@chinadci.com
 * copyright:         广州城市信息研究所有限公司
 ***************************************************************************/

#include "dmpserverrequest.h"
#include <boost/regex.hpp>
#include <boost/make_shared.hpp>
#include "dmplogger.h"

DmpServerRequest::DmpServerRequest()
{ 
}

DmpServerRequest::DmpServerRequest(const std::string &url, Method method, const Headers &headers)
   :url_(url)
   ,method_(method)
   ,headers_(headers)
{
}

void DmpServerRequest::set_url(const std::string &url)
{
  url_ = url;
  if (!ParseUrl(url_,protocol_,domain_,port_,path_,query_)) {
    LOGGER_ERROR("非法的URL!");
    return;
  }
  set_query(query_);
}

void DmpServerRequest::set_query(const std::string &query_string)
{
  query_ = query_string;
  params_.SetQueryString(query_);
}

void DmpServerRequest::set_method(Method method)
{
  method_ = method;
}

void DmpServerRequest::SetParameter(const std::string &name, const std::string &value)
{
  params_.Add(name, value);
}

bool DmpServerRequest::ParseUrl(const std::string& url, 
                                std::string &protocol, 
                                std::string &domain, 
                                std::string &port, 
                                std::string &path, 
                                std::string &query_string)
{
    if (url.empty()) {  
       return false;
    }
    boost::regex ex("(http|https)://([^/ :]+):?([^/ ]*)(/?[^ #?]*)\\x3f?([^ #]*)#?([^ ]*)");
    boost::cmatch what;
    if (regex_match(url.c_str(), what, ex)) 
    {
        protocol = std::string(what[1].first, what[1].second);
        domain   = std::string(what[2].first, what[2].second);
        port     = std::string(what[3].first, what[3].second);
        path     = std::string(what[4].first, what[4].second);
        query_string    = std::string(what[5].first, what[5].second);
        return true;
    }
    return false;
}

void DmpServerRequest::set_header(const std::string &name, const std::string &value)
{
  headers_[name] = value;
}