E-MailRelay
grequestclient.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file grequestclient.cpp
19///
20
21#include "gdef.h"
22#include "gstr.h"
23#include "grequestclient.h"
24
25GSmtp::RequestClient::RequestClient( GNet::ExceptionSink es , const std::string & key , const std::string & ok ,
26 const GNet::Location & location , unsigned int connection_timeout , unsigned int response_timeout ,
27 unsigned int idle_timeout ) :
28 GNet::Client(es,location,
30 .set_line_buffer_config(GNet::LineBuffer::Config::newline())
31 .set_connection_timeout(connection_timeout)
32 .set_response_timeout(response_timeout)
33 .set_idle_timeout(idle_timeout)) ,
34 m_eol(1U,'\n') ,
35 m_key(key) ,
36 m_ok(ok) ,
37 m_timer(*this,&RequestClient::onTimeout,es)
38{
39 G_DEBUG( "GSmtp::RequestClient::ctor: " << location.displayString() << ": "
40 << connection_timeout << " " << response_timeout ) ;
41}
42
43void GSmtp::RequestClient::onConnect()
44{
45 G_DEBUG( "GSmtp::RequestClient::onConnect" ) ;
46 if( busy() )
47 send( requestLine(m_request) ) ; // GNet::Client::send()
48}
49
50void GSmtp::RequestClient::request( const std::string & request_payload )
51{
52 G_DEBUG( "GSmtp::RequestClient::request: \"" << request_payload << "\"" ) ;
53 if( busy() )
54 throw ProtocolError() ;
55
56 m_request = request_payload ;
57 m_timer.startTimer( 0U ) ;
58
59 // clear the base-class line buffer of any incomplete line
60 // data from a previous request -- this is racey for servers
61 // that incorrectly reply with more than one line
62 clearInput() ;
63}
64
65void GSmtp::RequestClient::onTimeout()
66{
67 if( connected() )
68 send( requestLine(m_request) ) ; // GNet::Client::send()
69}
70
72{
73 return !m_request.empty() ;
74}
75
76void GSmtp::RequestClient::onDelete( const std::string & reason )
77{
78 if( !reason.empty() )
79 G_WARNING( "GSmtp::RequestClient::onDelete: error: " << reason ) ;
80}
81
82void GSmtp::RequestClient::onSecure( const std::string & , const std::string & , const std::string & )
83{
84}
85
86bool GSmtp::RequestClient::onReceive( const char * line_data , std::size_t line_size , std::size_t ,
87 std::size_t , char )
88{
89 std::string line( line_data , line_size ) ;
90 G_DEBUG( "GSmtp::RequestClient::onReceive: [" << G::Str::printable(line) << "]" ) ;
91 if( busy() )
92 {
93 m_request.erase() ;
94 eventSignal().emit( std::string(m_key) , result(line) , std::string() ) ; // empty string if matching m_ok
95 return false ;
96 }
97 else
98 {
99 return true ;
100 }
101}
102
103void GSmtp::RequestClient::onSendComplete()
104{
105}
106
107std::string GSmtp::RequestClient::requestLine( const std::string & request_payload ) const
108{
109 return request_payload + m_eol ;
110}
111
112std::string GSmtp::RequestClient::result( std::string line ) const
113{
114 G::Str::trimRight( line , "\r" ) ;
115 return !m_ok.empty() && line.find(m_ok) == 0U ? std::string() : line ;
116}
117
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
A class that represents the remote target for out-going client connections.
Definition: glocation.h:71
std::string displayString() const
Returns a string representation for logging and debug.
Definition: glocation.cpp:188
A class which acts as an SMTP client, sending messages to a remote SMTP server.
Definition: gsmtpclient.h:63
A network client class that interacts with a remote server using a stateless line-based request/respo...
RequestClient(GNet::ExceptionSink, const std::string &key, const std::string &ok, const GNet::Location &host_and_service, unsigned int connection_timeout, unsigned int response_timeout, unsigned int idle_timeout)
Constructor.
bool busy() const
Returns true after request() and before the subsequent event signal.
void request(const std::string &)
Issues a request.
static std::string & trimRight(std::string &s, string_view ws, std::size_t limit=0U)
Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
Definition: gstr.cpp:313
static std::string printable(const std::string &in, char escape='\\')
Returns a printable representation of the given input string, using chacter code ranges 0x20 to 0x7e ...
Definition: gstr.cpp:916
Network classes.
Definition: gdef.h:1144
A structure containing GNet::Client configuration parameters.
Definition: gclient.h:85