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
2ef937c4
Unverified
Commit
2ef937c4
authored
Aug 15, 2019
by
Dennis Jenkins
Committed by
GitHub
Aug 15, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #593 from lnihlen/address-in-request
adds address copying to request, and test
parents
399d04ee
fe9a6f69
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
0 deletions
+69
-0
include/pistache/http.h
include/pistache/http.h
+8
-0
src/common/http.cc
src/common/http.cc
+7
-0
tests/http_server_test.cc
tests/http_server_test.cc
+54
-0
No files found.
include/pistache/http.h
View file @
2ef937c4
...
...
@@ -178,6 +178,12 @@ public:
std
::
shared_ptr
<
Tcp
::
Peer
>
peer
()
const
;
#endif
const
Address
&
address
()
const
;
void
copyAddress
(
const
Address
&
address
)
{
address_
=
address
;
}
private:
#ifdef LIBSTDCPP_SMARTPTR_LOCK_FIXME
void
associatePeer
(
const
std
::
shared_ptr
<
Tcp
::
Peer
>&
peer
)
{
...
...
@@ -188,6 +194,7 @@ private:
}
#endif
Method
method_
;
std
::
string
resource_
;
Uri
::
Query
query_
;
...
...
@@ -195,6 +202,7 @@ private:
#ifdef LIBSTDCPP_SMARTPTR_LOCK_FIXME
std
::
weak_ptr
<
Tcp
::
Peer
>
peer_
;
#endif
Address
address_
;
};
class
Handler
;
...
...
src/common/http.cc
View file @
2ef937c4
...
...
@@ -564,6 +564,11 @@ Request::cookies() const {
return
cookies_
;
}
const
Address
&
Request
::
address
()
const
{
return
address_
;
}
#ifdef LIBSTDCPP_SMARTPTR_LOCK_FIXME
std
::
shared_ptr
<
Tcp
::
Peer
>
Request
::
peer
()
const
{
...
...
@@ -792,6 +797,8 @@ Handler::onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer
#endif
auto
request
=
parser
.
request
;
request
.
copyAddress
(
peer
->
address
());
auto
connection
=
request
.
headers
().
tryGet
<
Header
::
Connection
>
();
if
(
connection
)
{
...
...
tests/http_server_test.cc
View file @
2ef937c4
...
...
@@ -78,6 +78,20 @@ private:
std
::
string
fileName_
;
};
struct
AddressEchoHandler
:
public
Http
::
Handler
{
HTTP_PROTOTYPE
(
AddressEchoHandler
)
AddressEchoHandler
()
{
}
void
onRequest
(
const
Http
::
Request
&
request
,
Http
::
ResponseWriter
writer
)
override
{
std
::
string
requestAddress
=
request
.
address
().
host
();
writer
.
send
(
Http
::
Code
::
Ok
,
requestAddress
);
std
::
cout
<<
"[server] Sent: "
<<
requestAddress
<<
std
::
endl
;
}
};
int
clientLogicFunc
(
int
response_size
,
const
std
::
string
&
server_page
,
int
timeout_seconds
,
...
...
@@ -309,3 +323,43 @@ TEST(http_server_test, server_with_static_file)
ASSERT_EQ
(
data
,
resultData
);
}
TEST
(
http_server_test
,
server_request_copies_address
)
{
const
Pistache
::
Address
address
(
"localhost"
,
Pistache
::
Port
(
0
));
Http
::
Endpoint
server
(
address
);
auto
flags
=
Tcp
::
Options
::
ReuseAddr
;
auto
server_opts
=
Http
::
Endpoint
::
options
().
flags
(
flags
);
server
.
init
(
server_opts
);
server
.
setHandler
(
Http
::
make_handler
<
AddressEchoHandler
>
());
server
.
serveThreaded
();
const
std
::
string
server_address
=
"localhost:"
+
server
.
getPort
().
toString
();
std
::
cout
<<
"Server address: "
<<
server_address
<<
"
\n
"
;
Http
::
Client
client
;
client
.
init
();
auto
rb
=
client
.
get
(
server_address
);
auto
response
=
rb
.
send
();
std
::
string
resultData
;
response
.
then
([
&
resultData
](
Http
::
Response
resp
)
{
std
::
cout
<<
"Response code is "
<<
resp
.
code
()
<<
std
::
endl
;
if
(
resp
.
code
()
==
Http
::
Code
::
Ok
)
{
resultData
=
resp
.
body
();
}
},
Async
::
Throw
);
const
int
WAIT_TIME
=
2
;
Async
::
Barrier
<
Http
::
Response
>
barrier
(
response
);
barrier
.
wait_for
(
std
::
chrono
::
seconds
(
WAIT_TIME
));
client
.
shutdown
();
server
.
shutdown
();
ASSERT_EQ
(
"127.0.0.1"
,
resultData
);
}
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