This repository was archived by the owner on Jan 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspamd_client.hpp
More file actions
91 lines (87 loc) · 2.54 KB
/
spamd_client.hpp
File metadata and controls
91 lines (87 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#pragma once
#include "asio_sync_client.hpp"
#include "spamd_protocol.hpp"
class spamd_client
{
spamd_client()
{
}
spamd_client(boost::asio::ip::tcp::endpoint spamd_endpoint)
{
client_endpoint=spamd_endpoint;
}
bool connect(const std::string& host, const std::string& service)
{
boost::system::error_code ec
return client.connect(host,service,timeouts,ec);
}
bool connect(boost::asio::ip::tcp::endpoint spamd_endpoint)
{
boost::system::error_code ec
client_endpoint=spamd_endpoint;
return client.connect(spamd_endpoint,timeouts,ec);
}
bool connect()
{
boost::system::error_code ec
return client.connect(client_endpoint,timeouts,ec);
}
bool process_spam(const string &str_body,string &ret_response)
{
boost::system::error_code ec
string str_request_body=str_body;
str_request_body.append('\r\n');
string str_request = spamd.process(str_request_body);
if(client.is_open()==false)
{
return false;
}
if(client.write_some(str_request,timeouts,ec)==false)
{
return false;
}
string str_buffer;
string str_response;
SPAMD_PROTOCOL spamd_protocol;
while(true)
{
if(client.read_some(str_buffer,timeouts,ec)==false)
{
return false;
}
str_response+=str_buffer;
if(*str_buffer.rbegin()!='\n')
{
continue;
}
map<string, string> header_map;
string str_response_body;
int n_status = 0;
if(spamd_protocol.response_load(str_response,n_status,header_map,str_response_body)==true)
{
if (n_status != 0)
{
cerr<<"protocol error:"<<str_response<<endl;
return false;
}
if (spamd.verify_body_length(header_map, str_response_body.length()) != true)
{
continue;
}
//pop \r\n
str_response_body.pop_back();
str_response_body.pop_back();
ret_response=str_response_body;
return true;
}
else
{
cerr<<"protocol error:"<<str_response<<endl;
return false;
}
}
}
int timeouts;
boost::asio::ip::tcp::endpoint client_endpoint;
asio_sync_client client;
};