blob: 338ed2a2ff3774eebc34ab7df0144e5bb23733f7 (
plain)
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
|
/** @file httpreader.hxx
*
* @copyright (C) 2016
* @date 2016.05.26
* @version 1.0.0
* @author amir zamani <azadkuh@live.com>
*
*/
#ifndef __QHTTP_HTTPREADER_HXX__
#define __QHTTP_HTTPREADER_HXX__
#include "qhttpbase.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace qhttp {
namespace details {
///////////////////////////////////////////////////////////////////////////////
// usage in client::QHttpResponse, server::QHttpRequest
template<class TBase>
class HttpReader : public TBase
{
public:
enum TReadState {
EEmpty,
EPartial,
EComplete,
ESent
};
public:
void collectData(int atMost) {
icollectRequired = true;
icollectCapacity = atMost;
icollectedData.clear();
if ( atMost > 0 )
icollectedData.reserve(atMost);
}
bool append(const char* data, size_t length) {
if ( !icollectRequired ) // not allowed to collect data
return false;
int newLength = icollectedData.length() + static_cast<int>(length);
if ( icollectCapacity > 0 && newLength > icollectCapacity )
return false; // the capacity is full
icollectedData.append(data, length);
return true;
}
// call cb if the message is not finalized yet
template<class Func>
void finalizeSending(Func cb) {
if ( ireadState != EComplete ) {
ireadState = EComplete;
isuccessful = true;
cb();
}
}
public:
bool isuccessful = false;
TReadState ireadState = EEmpty;
/// shall I collect incoming body data by myself?
bool icollectRequired = false;
int icollectCapacity = 0;
QByteArray icollectedData;
};
///////////////////////////////////////////////////////////////////////////////
} // namespace details
} // namespace qhttp
///////////////////////////////////////////////////////////////////////////////
#endif // __QHTTP_HTTPREADER_HXX__
|