dmpserverrequest.cpp 2.4 KB

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

#include "dmpserverrequest.h"
#include <regex>
// #include <boost/regex.hpp>
// #include <boost/make_shared.hpp>
// #include <boost/lexical_cast.hpp>
// #include <boost/algorithm/string.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;
    }
    std::regex reg("(http|https)://([^/ :]+):?([^/ ]*)(/?[^ #?]*)\\x3f?([^ #]*)#?([^ ]*)");
    std::cmatch what;
    if (std::regex_match(url.c_str(), what, reg)) 
    {
        protocol = what[1].str();
        domain   = what[2].str();
        port     = what[3].str();
        path     = what[4].str();
        query_string = what[5].str();
        return true;
    }
    return false;
}

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

const void* DmpServerRequest::GetData() const
{
  return nullptr;
}

int DmpServerRequest::GetDataLength() const
{
   return 0;
}