29 static const size_t c_line_limit = G::limits::net_line_limit ;
32 m_auto(config.eol().empty()) ,
34 m_warn_limit(config.warn()) ,
35 m_fail_limit(config.fail()?config.fail():c_line_limit) ,
36 m_extra_data(nullptr) ,
41 m_line_data(nullptr) ,
52 m_store.append( data , size ) ;
60 precheck( s.size() ) ;
65 void GNet::LineBuffer::addextra(
const char * data ,
size_t size )
74 m_extra_data = data ; G_ASSERT( m_extra_size == 0U ) ;
78 while( !m_store.empty() && m_extra_size != 0U && !m_eol.empty() &&
79 m_eol.find(m_store.at(m_store.size()-1U)) != std::string::npos &&
80 m_eol.find(*m_extra_data) != std::string::npos )
82 m_store.append( 1U , *m_extra_data++ ) ;
88 void GNet::LineBuffer::precheck(
size_t n )
91 bool arithmetic_overflow = (m_store.size() + n) < m_store.size() ;
92 bool fail_limit_overflow = (m_store.size() + n) > m_fail_limit ;
93 if( arithmetic_overflow || fail_limit_overflow )
94 throw ErrorOverflow() ;
99 const size_t npos = std::string::npos ;
101 const char * p = nullptr ;
102 m_line_data = nullptr ;
104 G_ASSERT( m_pos <= (m_store.size()+m_extra_size) ) ;
105 if( m_pos == (m_store.size()+m_extra_size) )
114 else if( m_expect != 0U )
117 if( (m_pos+m_expect) <= m_store.size() )
122 m_line_data = m_store.data() + m_pos ;
123 m_line_size = m_expect ;
142 else if( m_pos < m_store.size() && (pos=m_store.find(m_eol,m_pos)) != npos )
146 m_eol_size = m_eol.size() ;
147 m_line_data = m_store.data() + m_pos ;
148 m_line_size = pos - m_pos ;
149 linecheck( m_line_size ) ;
150 m_pos = pos + m_eol_size ;
153 else if( m_pos < m_store.size() && m_extra_size == 0U )
157 if( m_pos ) m_store.erase( 0U , m_pos ) ;
161 else if( (p=extraeol()) !=
nullptr )
163 G_ASSERT( m_extra_size != 0U ) ;
164 if( m_pos < m_store.size() )
168 size_t n_lhs = m_store.size() - m_pos ;
169 size_t n_rhs = std::distance(m_extra_data,p) + m_eol.size() ;
170 m_store.append( m_extra_data , n_rhs ) ;
171 m_extra_data += n_rhs ;
172 m_extra_size -= n_rhs ;
173 m_eol_size = m_eol.size() ;
174 m_line_data = m_store.data() + m_pos ;
175 m_line_size = n_lhs + n_rhs - m_eol_size ;
176 linecheck( m_line_size ) ;
177 m_pos += ( n_lhs + n_rhs ) ;
184 m_eol_size = m_eol.size() ;
185 m_line_data = m_extra_data + m_pos - m_store.size() ;
186 m_line_size = std::distance( m_line_data , p ) ;
187 linecheck( m_line_size ) ;
188 m_pos += ( m_line_size + m_eol_size ) ;
192 else if( m_pos < m_store.size() )
196 G_ASSERT( m_extra_size != 0U ) ;
198 if( m_pos ) m_store.erase( 0U , m_pos ) ;
206 G_ASSERT( m_extra_size != 0U ) ;
207 size_t n = m_pos - m_store.size() ;
208 m_store.assign( m_extra_data + n , m_extra_size - n ) ;
215 const char * GNet::LineBuffer::extraeol()
const
217 G_ASSERT( m_store.find(m_eol,m_pos) == std::string::npos ) ;
218 G_ASSERT( m_extra_size != 0U ) ;
220 const char * end = m_extra_data + m_extra_size ;
221 const char * start = m_extra_data ;
222 if( m_pos > m_store.size() )
223 start += ( m_pos-m_store.size() ) ;
225 const char * p = std::search( start , end , m_eol.data() , m_eol.data()+m_eol.size() ) ;
226 return p == end ?
nullptr : p ;
229 void GNet::LineBuffer::consolidate()
233 m_store.append( m_extra_data , m_extra_size ) ;
238 bool GNet::LineBuffer::detect()
240 const size_t npos = std::string::npos ;
244 size_t pos = m_store.find(
'\n' ) ;
247 if( pos > 0U && m_store.at(pos-1U) ==
'\r' )
248 m_eol = std::string(
"\r\n" , 2U ) ;
250 m_eol = std::string( 1U ,
'\n' ) ;
254 return !m_eol.empty() ;
267 void GNet::LineBuffer::linecheck(
size_t n )
269 if( !m_warned && m_warn_limit != 0U && n > m_warn_limit )
271 G_WARNING(
"GNet::LineBuffer::check: very long line detected: " << n <<
" > " << m_warn_limit ) ;
288 G_ASSERT( m_line_data !=
nullptr ) ;
static LineBufferConfig newline()
Convenience factory function.
static LineBufferConfig http()
Convenience factory function.
static LineBufferConfig pop()
Convenience factory function.
void expect(size_t n)
The next 'n' bytes extracted will be extracted in one contiguous block, without regard to line ending...
bool more()
Returns true if there is lineData() to be had.
A configuration structure for GNet::LineBuffer.
static bool enabled()
Returns true if test features are enabled.
static LineBufferConfig autodetect()
Convenience factory function.
LineBufferConfig(const std::string &eol=std::string(1U,'\n'), size_t warn=0U, size_t fail=0U)
Constructor.
LineBuffer(LineBufferConfig)
Constructor.
static LineBufferConfig smtp()
Convenience factory function.
static LineBufferConfig crlf()
Convenience factory function.
void add(const std::string &data)
Adds a data segment.
size_t lineSize() const
Returns the size of the current line, excluding the line ending.
size_t eolSize() const
Returns the size of current line ending.
std::string eol() const
Returns the end-of-line string as passed in to the constructor, or as auto-detected.
const char * lineData() const
Returns a pointer for the current line, including the line ending.