1
1
#include < string_view>
2
2
#include " HttpConnect.h"
3
3
#include " BasePacket.h"
4
- #include " http_parser.h"
5
4
#include " CommonPool.h"
6
5
7
6
/*
@@ -25,43 +24,23 @@ Content-Length: 10\r\n\r\n
25
24
data......
26
25
*/
27
26
28
- http_parser_settings http_settings;
29
27
HttpConnect::HttpConnect ()
30
28
{
31
- m_readPacket = new BasePacket ();
32
- m_parser = (http_parser *)malloc (sizeof (http_parser));
33
- m_url = (http_parser_url*)malloc (sizeof (http_parser_url));
34
- m_parser->data = this ;
35
-
36
- http_settings.on_message_begin = NULL ;
37
- http_settings.on_url = &HttpConnect::on_url;
38
- http_settings.on_status = NULL ;
39
- http_settings.on_header_field = NULL ;
40
- http_settings.on_header_value = NULL ;
41
- http_settings.on_headers_complete = NULL ;
42
- http_settings.on_body = NULL ;
43
- http_settings.on_message_complete = NULL ;
44
- http_settings.on_chunk_header = NULL ;
45
- http_settings.on_chunk_complete = NULL ;
46
-
47
- http_parser_init (m_parser, HTTP_REQUEST);
48
-
29
+ m_readPacket = createPacket ();
49
30
zero ();
50
31
}
51
32
52
33
HttpConnect::~HttpConnect ()
53
34
{
54
- delete m_readPacket;
55
- free (m_parser);
56
- free (m_url);
35
+ recyclePacket (m_readPacket);
57
36
}
58
37
59
38
void HttpConnect::zero ()
60
39
{
61
40
TcpSocket::zero ();
62
41
m_readPacket->zero ();
42
+ m_parser.zero ();
63
43
m_residue = 0 ;
64
- m_urlp = NULL ;
65
44
m_content = NULL ;
66
45
}
67
46
@@ -105,13 +84,6 @@ const char * HttpConnect::getContentTypeStr(enum http_content_type type)
105
84
return ctype;
106
85
}
107
86
108
- int HttpConnect::on_url (http_parser* _, const char *at, size_t length)
109
- {
110
- HttpConnect * conn = (HttpConnect *)(_->data );
111
- conn->m_urlp = at;
112
- return http_parser_parse_url (at, length, 0 , conn->m_url );
113
- }
114
-
115
87
void HttpConnect::on_msgbuffer (MessageBuffer * buffer)
116
88
{
117
89
// http head end: \r\n\r\n
@@ -128,18 +100,17 @@ void HttpConnect::on_msgbuffer(MessageBuffer * buffer)
128
100
buffer->ReadCompleted (rpos + 4 );
129
101
m_content = (const char *)(m_readPacket->contents () + m_readPacket->wpos ());
130
102
131
- if (!parser ((const char *)(m_readPacket->contents ()), m_readPacket->wpos ()))
103
+ if (!m_parser. parser ((const char *)(m_readPacket->contents ()), m_readPacket->wpos ()))
132
104
{
133
105
close ();
134
106
break ;
135
107
}
136
108
else
137
109
{
138
- if (m_parser-> method == HTTP_POST)
110
+ if (m_parser. method () == HTTP_POST)
139
111
{
140
- m_residue = static_cast <int >(m_parser-> content_length );
112
+ m_residue = static_cast <int >(m_parser. contentLen () );
141
113
}
142
- m_close = (http_should_keep_alive (m_parser) == 0 );
143
114
}
144
115
}
145
116
else
@@ -176,38 +147,27 @@ void HttpConnect::on_msgbuffer(MessageBuffer * buffer)
176
147
}
177
148
}
178
149
179
- bool HttpConnect::parser (const char * buf, int len)
180
- {
181
- http_parser_execute (m_parser, &http_settings, buf, len);
182
- if (m_parser->http_errno != HPE_OK)
183
- {
184
- return false ;
185
- }
186
-
187
- return true ;
188
- }
189
-
190
150
void HttpConnect::complete ()
191
151
{
192
- std::string_view path (m_urlp + m_url-> field_data [UF_PATH]. off , m_url-> field_data [UF_PATH]. len );
152
+ std::string_view path = m_parser. getUrl ()-> getPath ( );
193
153
194
- if (m_parser-> method == HTTP_POST)
154
+ if (m_parser. method () == HTTP_POST)
195
155
{
196
- std::string_view param (m_content, static_cast <int >(m_parser-> content_length ));
197
- if (m_event) m_event->onMsg (this , HTTP_POST , path, param);
156
+ std::string_view param (m_content, static_cast <int >(m_parser. contentLen () ));
157
+ if (m_event) m_event->onPost (this , path, param);
198
158
}
199
- else if (m_parser-> method == HTTP_GET)
159
+ else if (m_parser. method () == HTTP_GET)
200
160
{
201
161
std::string_view param;
202
- if ((m_url-> field_set & ( 1 << UF_QUERY)) != 0 )
162
+ if (m_parser. getUrl ()-> haveParam () )
203
163
{
204
- param = std::string_view (m_urlp + m_url-> field_data [UF_QUERY]. off , m_url-> field_data [UF_QUERY]. len );
164
+ param = m_parser. getUrl ()-> getParam ( );
205
165
}
206
- if (m_event) m_event->onMsg (this , HTTP_GET , path, param);
166
+ if (m_event) m_event->onGet (this , path, param);
207
167
}
208
168
else
209
169
{
210
-
170
+ if (m_event) m_event-> onOther ( this , &m_parser);
211
171
}
212
172
}
213
173
@@ -218,7 +178,7 @@ void HttpConnect::on_clsesocket()
218
178
219
179
void HttpConnect::on_writecomplete ()
220
180
{
221
- if (m_close )
181
+ if (m_parser. isClose () )
222
182
{
223
183
this ->close ();
224
184
}
0 commit comments