dmphttpbase.cpp 2.9 KB
/**************************************************************************
* file:              dmphttpbase.cpp

* Author:            lijiahuan
* Date:              2021-12-09 17:41:00
* Email:             jiahuanl@chinadci.com
* copyright:         广州城市信息研究所有限公司
***************************************************************************/
#include "dmphttpbase.h"

#define HTTP_JSON_BEGIN ("[")
#define HTTP_JSON_END ("]")

DmpHttp::DmpHttpBase::DmpHttpBase(){}
DmpHttp::DmpHttpBase::~DmpHttpBase(){}
int DmpHttp::DmpHttpBase::parseUrl(const std::string& url, std::string& out_server,
                           std::string& out_port, std::string& out_path){
const std::string& http_ = "http://";
const std::string& https_ = "https://";
std::string temp_data = url;
if (temp_data.find(http_) == 0) {
		temp_data = temp_data.substr(http_.length());
	}
	else if (temp_data.find(https_) == 0) {
		temp_data = temp_data.substr(https_.length());
	}
	else {
		return HTTP_ERROR_HTTP_HEAD;
	}
    // 解析域名
	std::size_t idx = temp_data.find('/');
	// 解析 域名后的page
	if (std::string::npos == idx) {
		out_path = "/";
		idx = temp_data.size();
	}
	else {
		out_path = temp_data.substr(idx);
	}
	// 解析域名
	out_server = temp_data.substr(0, idx);
	// 解析端口
	idx = out_server.find(':');
	if (std::string::npos == idx) {
		out_port = "80";
	}
	else {
		out_port = out_server.substr(idx + 1);
		out_server = out_server.substr(0, idx);
	}
	return HTTP_SUCCESS;
}

int DmpHttp::DmpHttpBase::buildPostRequest(const std::string& server, const std::string& path,
	std::ostream& out_request) {
	// 分割path中的json数据
	std::string temp_path(path), temp_json;
	int json_pos_begin = temp_path.find(HTTP_JSON_BEGIN) + 1;
	int json_pos_end = temp_path.find(HTTP_JSON_END);
	if (json_pos_begin != std::string::npos) {
		// 计算json的长度
		int temp_json_lenth = std::string::npos;
		if (json_pos_end != temp_json_lenth) {
			temp_json_lenth = (json_pos_end - json_pos_begin); 
		}
		temp_json = temp_path.substr(json_pos_begin, temp_json_lenth);
		temp_path = temp_path.substr(0, (json_pos_begin - 1));
	}
	out_request << "POST " << temp_path.c_str() << " HTTP/1.0\r\n";
	out_request << "Host: " << server.c_str() << "\r\n"; 
	out_request << "Content-Length: " << temp_json.length() << "\r\n";
	out_request << "Content-Type: application/x-www-form-urlencoded\r\n";
	out_request << "Accept: */*\r\n";
	out_request << "Connection: close\r\n\r\n";
	out_request << temp_json.c_str();
	return HTTP_SUCCESS;
}

int DmpHttp::DmpHttpBase::buildGetRequest(const std::string& server, const std::string& path,
	std::ostream& out_request) {
	out_request << "GET " << path.c_str() << " HTTP/1.0\r\n";
	out_request << "Host: " << server.c_str() << "\r\n";
	out_request << "Accept: */*\r\n";
	out_request << "Connection: close\r\n\r\n";
	return HTTP_SUCCESS;
}