-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathHttpConnect.cpp
187 lines (167 loc) · 3.83 KB
/
HttpConnect.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <string_view>
#include "HttpConnect.h"
#include "BasePacket.h"
#include "CommonPool.h"
/*
-----------------
GET /login?name=zs&pass=111 HTTP/1.1\r\n
Host: 127.0.0.1\r\n
Connection: keep-alive\r\n\r\n
-----------------
POST /login HTTP/1.1\r\n
Host: 127.0.0.1\r\n
Connection: keep-alive\r\n
Content-Length:10\r\n
\r\n
name=zs&pass=111
-----------------
HTTP/1.1 200 OK\r\n
Content-Type: text/html\r\n
Content-Length: 10\r\n\r\n
data......
*/
HttpConnect::HttpConnect()
{
m_readPacket = createPacket();
zero();
}
HttpConnect::~HttpConnect()
{
recyclePacket(m_readPacket);
}
void HttpConnect::zero()
{
TcpSocket::zero();
m_readPacket->zero();
m_parser.zero();
m_residue = 0;
m_content = NULL;
}
void HttpConnect::sendMsg(const char* msg, int32 len)
{
BasePacket* pack = createPacket();
pack->append(msg, len);
TcpSocket::write(pack);
}
void HttpConnect::sendData(std::string_view sv)
{
sendMsg(sv.data(), sv.size());
}
void HttpConnect::autoMsg(std::string_view sv, enum http_content_type type)
{
static char buff[1024];
sprintf(buff, "HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %d\r\n\r\n", getContentTypeStr(type), sv.size());
BasePacket * pack = createPacket();
pack->append(buff, strlen(buff));
pack->append((const uint8*)sv.data(), sv.size());
TcpSocket::write(pack);
}
const char * HttpConnect::getContentTypeStr(enum http_content_type type)
{
const char *ctype = "text/html";
switch (type)
{
case hct_text_html: ctype = "text/html"; break;
case hct_text_plain: ctype = "text/plain"; break;
case hct_text_xml: ctype = "text/xml"; break;
case hct_text_json: ctype = "text/json"; break;
case hct_image_gif: ctype = "image/gif"; break;
case hct_image_jpeg: ctype = "image/jpeg"; break;
case hct_image_png: ctype = "image/png"; break;
default:
break;
}
return ctype;
}
void HttpConnect::on_msgbuffer(MessageBuffer * buffer)
{
// http head end: \r\n\r\n
while (buffer->GetActiveSize() > 0)
{
// find head end pos
if (m_content == NULL)
{
std::string_view view((char *)buffer->GetReadPointer());
int rpos = view.find("\r\n\r\n");
if (rpos != std::string_view::npos)
{
m_readPacket->append(buffer->GetReadPointer(), rpos + 4);
buffer->ReadCompleted(rpos + 4);
m_content = (const char *)(m_readPacket->contents() + m_readPacket->wpos());
if (!m_parser.parser((const char *)(m_readPacket->contents()), m_readPacket->wpos()))
{
close();
break;
}
else
{
if (m_parser.method() == HTTP_POST)
{
m_residue = static_cast<int>(m_parser.contentLen());
}
}
}
else
{
m_readPacket->append(buffer->GetReadPointer(), buffer->GetActiveSize());
buffer->ReadCompleted(buffer->GetActiveSize());
if (m_readPacket->wpos() >= 0xffff)
{
// Head is too big
close();
break;
}
}
}
if(m_content)
{
int rlen = buffer->GetActiveSize();
if (m_residue > 0)
{
rlen = m_residue > rlen ? rlen : m_residue;
m_residue -= rlen;
m_readPacket->append(buffer->GetReadPointer(), rlen);
buffer->ReadCompleted(rlen);
}
if (m_residue == 0)
{
complete();
zero();
}
}
}
}
void HttpConnect::complete()
{
std::string_view path = m_parser.getUrl()->getPath();
if (m_parser.method() == HTTP_POST)
{
std::string_view param(m_content, static_cast<int>(m_parser.contentLen()));
if (m_event) m_event->onPost(this, path, param);
}
else if (m_parser.method() == HTTP_GET)
{
std::string_view param;
if (m_parser.getUrl()->haveParam())
{
param = m_parser.getUrl()->getParam();
}
if (m_event) m_event->onGet(this, path, param);
}
else
{
if (m_event) m_event->onOther(this, &m_parser);
}
}
void HttpConnect::on_clsesocket()
{
if(m_event) m_event->onClose(this);
}
void HttpConnect::on_writecomplete()
{
if (m_parser.isClose())
{
this->close();
}
TcpSocket::on_writecomplete();
}