Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pistache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
pistache
Commits
1eed0c75
Commit
1eed0c75
authored
May 02, 2021
by
hyperxor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes in server timeouts and more tests
parent
b62a4b37
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
179 additions
and
34 deletions
+179
-34
include/pistache/http.h
include/pistache/http.h
+2
-3
src/common/http.cc
src/common/http.cc
+0
-6
src/server/endpoint.cc
src/server/endpoint.cc
+2
-2
tests/http_server_test.cc
tests/http_server_test.cc
+146
-20
tests/tcp_client.h
tests/tcp_client.h
+29
-3
No files found.
include/pistache/http.h
View file @
1eed0c75
...
...
@@ -548,7 +548,7 @@ namespace Pistache
class
BodyStep
:
public
Step
{
public:
static
constexpr
auto
Id
=
Meta
::
Hash
::
fnv1a
(
"
Headers
"
);
static
constexpr
auto
Id
=
Meta
::
Hash
::
fnv1a
(
"
Body
"
);
explicit
BodyStep
(
Message
*
message_
)
:
Step
(
message_
)
...
...
@@ -611,7 +611,7 @@ namespace Pistache
virtual
~
ParserBase
()
=
default
;
virtual
bool
feed
(
const
char
*
data
,
size_t
len
);
bool
feed
(
const
char
*
data
,
size_t
len
);
virtual
void
reset
();
State
parse
();
...
...
@@ -635,7 +635,6 @@ namespace Pistache
public:
explicit
ParserImpl
(
size_t
maxDataSize
);
bool
feed
(
const
char
*
data
,
size_t
len
)
override
;
void
reset
()
override
;
std
::
chrono
::
steady_clock
::
time_point
time
()
const
...
...
src/common/http.cc
View file @
1eed0c75
...
...
@@ -1053,12 +1053,6 @@ namespace Pistache
time_
=
std
::
chrono
::
steady_clock
::
now
();
}
bool
Private
::
ParserImpl
<
Http
::
Request
>::
feed
(
const
char
*
data
,
size_t
len
)
{
time_
=
std
::
chrono
::
steady_clock
::
now
();
return
ParserBase
::
feed
(
data
,
len
);
}
Private
::
ParserImpl
<
Http
::
Response
>::
ParserImpl
(
size_t
maxDataSize
)
:
ParserBase
(
maxDataSize
)
,
response
()
...
...
src/server/endpoint.cc
View file @
1eed0c75
...
...
@@ -112,12 +112,12 @@ namespace Pistache
auto
elapsed
=
now
-
time
;
auto
*
step
=
parser
->
step
();
if
(
step
->
id
()
==
Private
::
RequestLineStep
::
Id
)
if
(
step
->
id
()
==
Private
::
RequestLineStep
::
Id
||
step
->
id
()
==
Private
::
HeadersStep
::
Id
)
{
if
(
elapsed
>
headerTimeout_
||
elapsed
>
bodyTimeout_
)
idlePeers
.
push_back
(
peer
);
}
else
if
(
step
->
id
()
==
Private
::
Headers
Step
::
Id
)
else
if
(
step
->
id
()
==
Private
::
Body
Step
::
Id
)
{
if
(
elapsed
>
bodyTimeout_
)
idlePeers
.
push_back
(
peer
);
...
...
tests/http_server_test.cc
View file @
1eed0c75
...
...
@@ -163,6 +163,8 @@ struct AddressEchoHandler : public Http::Handler
}
};
constexpr
const
char
*
ExpectedResponseLine
=
"HTTP/1.1 408 Request Timeout"
;
struct
PingHandler
:
public
Http
::
Handler
{
HTTP_PROTOTYPE
(
PingHandler
)
...
...
@@ -534,17 +536,18 @@ TEST(http_server_test, response_size_captured)
ASSERT_EQ
(
rcode
,
Http
::
Code
::
Ok
);
}
TEST
(
http_server_test
,
client_request_
header_timeou
t_raises_http_408
)
TEST
(
http_server_test
,
client_request_
timeout_on_only_connec
t_raises_http_408
)
{
Pistache
::
Address
address
(
"localhost"
,
Pistache
::
Port
(
0
));
auto
t
imeout
=
std
::
chrono
::
seconds
(
2
);
const
auto
headerT
imeout
=
std
::
chrono
::
seconds
(
2
);
Http
::
Endpoint
server
(
address
);
auto
flags
=
Tcp
::
Options
::
ReuseAddr
;
auto
opts
=
Http
::
Endpoint
::
options
()
.
flags
(
flags
)
.
headerTimeout
(
timeout
);
.
headerTimeout
(
headerTimeout
);
server
.
init
(
opts
);
server
.
setHandler
(
Http
::
make_handler
<
PingHandler
>
());
server
.
serveThreaded
();
...
...
@@ -553,24 +556,109 @@ TEST(http_server_test, client_request_header_timeout_raises_http_408)
auto
addr
=
"localhost:"
+
port
.
toString
();
LOGGER
(
"test"
,
"Server address: "
<<
addr
)
char
recvBuf
[
1024
];
std
::
memset
(
recvBuf
,
0
,
sizeof
(
recvBuf
));
TcpClient
client
;
EXPECT_TRUE
(
client
.
connect
(
Pistache
::
Address
(
"localhost"
,
port
)))
<<
client
.
lastError
();
char
recvBuf
[
1024
]
=
{
0
,
};
size_t
bytes
;
EXPECT_TRUE
(
client
.
receive
(
recvBuf
,
sizeof
(
recvBuf
),
&
bytes
,
std
::
chrono
::
seconds
(
5
)))
<<
client
.
lastError
();
EXPECT_EQ
(
0
,
strncmp
(
recvBuf
,
ExpectedResponseLine
,
strlen
(
ExpectedResponseLine
)));
server
.
shutdown
();
}
TEST
(
http_server_test
,
client_request_timeout_on_delay_in_header_send_raises_http_408
)
{
Pistache
::
Address
address
(
"localhost"
,
Pistache
::
Port
(
0
));
const
auto
headerTimeout
=
std
::
chrono
::
seconds
(
1
);
Http
::
Endpoint
server
(
address
);
auto
flags
=
Tcp
::
Options
::
ReuseAddr
;
auto
opts
=
Http
::
Endpoint
::
options
()
.
flags
(
flags
)
.
headerTimeout
(
headerTimeout
);
server
.
init
(
opts
);
server
.
setHandler
(
Http
::
make_handler
<
PingHandler
>
());
server
.
serveThreaded
();
auto
port
=
server
.
getPort
();
auto
addr
=
"localhost:"
+
port
.
toString
();
LOGGER
(
"test"
,
"Server address: "
<<
addr
);
const
std
::
string
reqStr
=
"GET /ping HTTP/1.1
\r\n
"
;
const
std
::
string
headerStr
=
"Host: localhost
\r\n
User-Agent: test
\r\n
"
;
TcpClient
client
;
EXPECT_TRUE
(
client
.
connect
(
Pistache
::
Address
(
"localhost"
,
port
)))
<<
client
.
lastError
();
EXPECT_TRUE
(
client
.
send
(
reqStr
))
<<
client
.
lastError
();
std
::
this_thread
::
sleep_for
(
headerTimeout
/
2
);
EXPECT_TRUE
(
client
.
send
(
headerStr
))
<<
client
.
lastError
();
char
recvBuf
[
1024
]
=
{
0
,
};
size_t
bytes
;
EXPECT_TRUE
(
client
.
receive
(
recvBuf
,
sizeof
(
recvBuf
),
&
bytes
,
std
::
chrono
::
seconds
(
5
)))
<<
client
.
lastError
();
EXPECT_EQ
(
0
,
strncmp
(
recvBuf
,
ExpectedResponseLine
,
strlen
(
ExpectedResponseLine
)));
server
.
shutdown
();
}
TEST
(
http_server_test
,
client_request_timeout_on_delay_in_request_line_send_raises_http_408
)
{
Pistache
::
Address
address
(
"localhost"
,
Pistache
::
Port
(
0
));
const
auto
headerTimeout
=
std
::
chrono
::
seconds
(
2
);
Http
::
Endpoint
server
(
address
);
auto
flags
=
Tcp
::
Options
::
ReuseAddr
;
auto
opts
=
Http
::
Endpoint
::
options
()
.
flags
(
flags
)
.
headerTimeout
(
headerTimeout
);
server
.
init
(
opts
);
server
.
setHandler
(
Http
::
make_handler
<
PingHandler
>
());
server
.
serveThreaded
();
auto
port
=
server
.
getPort
();
auto
addr
=
"localhost:"
+
port
.
toString
();
LOGGER
(
"test"
,
"Server address: "
<<
addr
);
const
std
::
string
reqStr
{
"GET /ping HTTP/1.1
\r\n
"
};
TcpClient
client
;
ASSERT_TRUE
(
client
.
connect
(
Pistache
::
Address
(
"localhost"
,
port
)))
<<
client
.
lastError
();
EXPECT_TRUE
(
client
.
connect
(
Pistache
::
Address
(
"localhost"
,
port
)))
<<
client
.
lastError
();
for
(
size_t
i
=
0
;
i
<
reqStr
.
size
();
++
i
)
{
if
(
!
client
.
send
(
reqStr
.
substr
(
i
,
1
)))
{
break
;
}
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
300
));
}
EXPECT_EQ
(
client
.
lastErrno
(),
EPIPE
)
<<
"Errno: "
<<
client
.
lastErrno
();
ASSERT_TRUE
(
client
.
receive
(
recvBuf
,
sizeof
(
recvBuf
),
&
bytes
,
std
::
chrono
::
seconds
(
5
)))
<<
client
.
lastError
();
char
recvBuf
[
1024
]
=
{
0
,
};
size_t
bytes
;
EXPECT_TRUE
(
client
.
receive
(
recvBuf
,
sizeof
(
recvBuf
),
&
bytes
,
std
::
chrono
::
seconds
(
5
)))
<<
client
.
lastError
();
EXPECT_EQ
(
0
,
strncmp
(
recvBuf
,
ExpectedResponseLine
,
strlen
(
ExpectedResponseLine
)));
server
.
shutdown
();
}
TEST
(
http_server_test
,
client_request_
body_timeout
_raises_http_408
)
TEST
(
http_server_test
,
client_request_
timeout_on_delay_in_body_send
_raises_http_408
)
{
Pistache
::
Address
address
(
"localhost"
,
Pistache
::
Port
(
0
));
auto
headerTimeout
=
std
::
chrono
::
seconds
(
1
);
auto
bodyTimeout
=
std
::
chrono
::
seconds
(
1
);
const
auto
headerTimeout
=
std
::
chrono
::
seconds
(
1
);
const
auto
bodyTimeout
=
std
::
chrono
::
seconds
(
2
);
Http
::
Endpoint
server
(
address
);
auto
flags
=
Tcp
::
Options
::
ReuseAddr
;
...
...
@@ -587,24 +675,62 @@ TEST(http_server_test, client_request_body_timeout_raises_http_408)
auto
addr
=
"localhost:"
+
port
.
toString
();
LOGGER
(
"test"
,
"Server address: "
<<
addr
);
std
::
string
reqStr
=
"GET /ping HTTP/1.1
\r\n
"
;
std
::
string
headerStr
=
"Host: localhost
\r\n
User-Agent: test
\r\n
"
;
const
std
::
string
reqStr
=
"POST /ping HTTP/1.1
\r\n
Host: localhost
\r\n
Content-Type: text/plain
\r\n
Content-Length: 32
\r\n\r\n
abc"
;
char
recvBuf
[
1024
];
std
::
memset
(
recvBuf
,
0
,
sizeof
(
recvBuf
));
TcpClient
client
;
EXPECT_TRUE
(
client
.
connect
(
Pistache
::
Address
(
"localhost"
,
port
)))
<<
client
.
lastError
();
EXPECT_TRUE
(
client
.
send
(
reqStr
))
<<
client
.
lastError
();
char
recvBuf
[
1024
]
=
{
0
,
};
size_t
bytes
;
EXPECT_TRUE
(
client
.
receive
(
recvBuf
,
sizeof
(
recvBuf
),
&
bytes
,
std
::
chrono
::
seconds
(
5
)))
<<
client
.
lastError
();
EXPECT_EQ
(
0
,
strncmp
(
recvBuf
,
ExpectedResponseLine
,
strlen
(
ExpectedResponseLine
)));
server
.
shutdown
();
}
TEST
(
http_server_test
,
client_request_no_timeout
)
{
Pistache
::
Address
address
(
"localhost"
,
Pistache
::
Port
(
0
));
const
auto
headerTimeout
=
std
::
chrono
::
seconds
(
2
);
const
auto
bodyTimeout
=
std
::
chrono
::
seconds
(
4
);
Http
::
Endpoint
server
(
address
);
auto
flags
=
Tcp
::
Options
::
ReuseAddr
;
auto
opts
=
Http
::
Endpoint
::
options
()
.
flags
(
flags
)
.
headerTimeout
(
headerTimeout
)
.
bodyTimeout
(
bodyTimeout
);
server
.
init
(
opts
);
server
.
setHandler
(
Http
::
make_handler
<
PingHandler
>
());
server
.
serveThreaded
();
auto
port
=
server
.
getPort
();
auto
addr
=
"localhost:"
+
port
.
toString
();
LOGGER
(
"test"
,
"Server address: "
<<
addr
);
const
std
::
string
headerStr
=
"POST /ping HTTP/1.1
\r\n
Host: localhost
\r\n
Content-Type: text/plain
\r\n
Content-Length: 8
\r\n\r\n
"
;
const
std
::
string
bodyStr
=
"abcdefgh
\r\n\r\n
"
;
TcpClient
client
;
ASSERT_TRUE
(
client
.
connect
(
Pistache
::
Address
(
"localhost"
,
port
)))
<<
client
.
lastError
();
ASSERT_TRUE
(
client
.
send
(
reqStr
))
<<
client
.
lastError
();
EXPECT_TRUE
(
client
.
connect
(
Pistache
::
Address
(
"localhost"
,
port
)))
<<
client
.
lastError
();
std
::
this_thread
::
sleep_for
(
headerTimeout
/
2
);
ASSER
T_TRUE
(
client
.
send
(
headerStr
))
<<
client
.
lastError
();
EXPEC
T_TRUE
(
client
.
send
(
headerStr
))
<<
client
.
lastError
();
static
constexpr
const
char
*
ExpectedResponseLine
=
"HTTP/1.1 408 Request Timeout"
;
std
::
this_thread
::
sleep_for
(
bodyTimeout
/
2
);
EXPECT_TRUE
(
client
.
send
(
bodyStr
))
<<
client
.
lastError
();
ASSERT_TRUE
(
client
.
receive
(
recvBuf
,
sizeof
(
recvBuf
),
&
bytes
,
std
::
chrono
::
seconds
(
5
)))
<<
client
.
lastError
();
ASSERT_TRUE
(
!
strncmp
(
recvBuf
,
ExpectedResponseLine
,
strlen
(
ExpectedResponseLine
)));
char
recvBuf
[
1024
]
=
{
0
,
};
size_t
bytes
;
EXPECT_TRUE
(
client
.
receive
(
recvBuf
,
sizeof
(
recvBuf
),
&
bytes
,
std
::
chrono
::
seconds
(
5
)))
<<
client
.
lastError
();
EXPECT_NE
(
0
,
strncmp
(
recvBuf
,
ExpectedResponseLine
,
strlen
(
ExpectedResponseLine
)));
server
.
shutdown
();
}
...
...
tests/tcp_client.h
View file @
1eed0c75
...
...
@@ -18,6 +18,7 @@ namespace Pistache
if (ret < 0) \
{ \
lastError_ = strerror(errno); \
lastErrno_ = errno; \
return false; \
} \
} while (0)
...
...
@@ -66,9 +67,28 @@ namespace Pistache
return
send
(
data
.
c_str
(),
data
.
size
());
}
bool
send
(
const
void
*
data
,
size_t
size
)
bool
send
(
const
char
*
data
,
size_t
len
)
{
CLIENT_TRY
(
::
send
(
fd_
,
data
,
size
,
0
));
size_t
total
=
0
;
while
(
total
<
len
)
{
ssize_t
n
=
::
send
(
fd_
,
data
+
total
,
len
-
total
,
MSG_NOSIGNAL
);
if
(
n
==
-
1
)
{
if
(
errno
==
EAGAIN
||
errno
==
EWOULDBLOCK
)
{
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
10
));
}
else
{
CLIENT_TRY
(
n
);
}
}
else
{
total
+=
static_cast
<
size_t
>
(
n
);
}
}
return
true
;
}
...
...
@@ -115,9 +135,15 @@ namespace Pistache
return
lastError_
;
}
int
lastErrno
()
const
{
return
lastErrno_
;
}
private:
int
fd_
;
int
fd_
=
-
1
;
std
::
string
lastError_
;
int
lastErrno_
=
0
;
};
#undef CLIENT_TRY
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment