-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpresponse.h
95 lines (73 loc) · 2.22 KB
/
httpresponse.h
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
91
92
93
94
95
#ifndef HTTPRESPONSE_H
#define HTTPRESPONSE_H
#include <vector>
#include <string>
#include <printf.h>
#include <algorithm>
#include <functional>
#include "httpserver.h"
#include "supertcpmanager.h"
class HttpServer;
class HttpResponse
{
public:
HttpResponse();
HttpResponse(HttpServer* server, ClientSocket* socket, std::function<void(HttpResponse*, int)> userCallback);
~HttpResponse();
HttpResponse(const HttpResponse& response);
HttpResponse(HttpResponse&& response);
void close();
void addHeader(const std::pair<std::string, std::string>& header);
void setHeaders(const std::vector< std::pair<std::string, std::string> >& headers);
size_t getResponseSize() const
{
return responseBuffer.size();
}
void setResponseSize(size_t responseSize)
{
responseBuffer.resize(responseSize);
client->setBuffer(responseBuffer.data());
client->setBufferSize(responseSize);
}
const std::vector<char>& getResponseBuffer() const
{
return responseBuffer;
}
std::vector<char>& getResponseBuffer()
{
return responseBuffer;
}
void setResponse(const std::vector<char>& response)
{
HttpResponse::responseBuffer.assign(response.begin(), response.end());
}
std::vector<std::pair<std::string, std::string> > &getHeaders()
{
return headers;
}
const std::vector<std::pair<std::string, std::string> > &getHeaders() const
{
return headers;
}
void createNewBuffer(size_t bufferSize);
template<typename ForwardIt>
ForwardIt searchQuote(ForwardIt begin, ForwardIt end, const std::string& str)
{
return std::search(begin, end, str.begin(), str.end());
}
private:
void getHeadersSocket(int count);
void getContent(int count);
void parseResponseHeader();
std::vector< std::pair<std::string, std::string> > headers;
std::vector<char> responseBuffer;
std::vector<char> headerBuffer;
std::function<void(HttpResponse*, int)> userCallback;
ClientSocket* client;
HttpServer* server;
size_t responseGet;
size_t bufSize;
static const size_t DEFAULT_HEADER_BUFFER = 1024;
friend class HttpServer;
};
#endif // HTTPRESPONSE_H