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
c6fc42b2
Commit
c6fc42b2
authored
Feb 15, 2016
by
octal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added custom header registration example
parent
cff0edd5
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
87 additions
and
2 deletions
+87
-2
examples/CMakeLists.txt
examples/CMakeLists.txt
+3
-0
examples/custom_header.cc
examples/custom_header.cc
+83
-0
examples/http_client.cc
examples/http_client.cc
+0
-1
include/http_header.h
include/http_header.h
+1
-1
No files found.
examples/CMakeLists.txt
View file @
c6fc42b2
...
@@ -7,6 +7,9 @@ target_link_libraries(hello_server net)
...
@@ -7,6 +7,9 @@ target_link_libraries(hello_server net)
add_executable
(
http_client http_client.cc
)
add_executable
(
http_client http_client.cc
)
target_link_libraries
(
http_client net
)
target_link_libraries
(
http_client net
)
add_executable
(
custom_header custom_header.cc
)
target_link_libraries
(
custom_header net
)
add_executable
(
rest_server rest_server.cc
)
add_executable
(
rest_server rest_server.cc
)
target_link_libraries
(
rest_server net
)
target_link_libraries
(
rest_server net
)
examples/custom_header.cc
0 → 100644
View file @
c6fc42b2
/*
Mathieu Stefani, 15 février 2016
Example of custom headers registering
*/
#include "net.h"
#include "http_headers.h"
#include "client.h"
using
namespace
Net
;
using
namespace
Net
::
Http
;
class
XProtocolVersion
:
public
Header
::
Header
{
public:
NAME
(
"X-Protocol-Version"
);
XProtocolVersion
()
:
min
(
0
)
,
maj
(
0
)
{
}
XProtocolVersion
(
uint32_t
major
,
uint32_t
minor
)
:
maj
(
major
)
,
min
(
minor
)
{
}
void
parse
(
const
std
::
string
&
str
)
{
auto
p
=
str
.
find
(
'.'
);
std
::
string
major
,
minor
;
if
(
p
!=
std
::
string
::
npos
)
{
major
=
str
.
substr
(
0
,
p
);
minor
=
str
.
substr
(
p
+
1
);
}
else
{
major
=
str
;
}
maj
=
std
::
stoi
(
major
);
if
(
!
minor
.
empty
())
min
=
std
::
stoi
(
minor
);
}
void
write
(
std
::
ostream
&
os
)
const
{
os
<<
maj
;
os
<<
"."
<<
min
;
}
uint32_t
major
()
const
{
return
maj
;
}
uint32_t
minor
()
const
{
return
min
;
}
private:
uint32_t
min
;
uint32_t
maj
;
};
int
main
()
{
Header
::
Registry
::
registerHeader
<
XProtocolVersion
>
();
Experimental
::
Client
client
(
"http://supnetwork.org:9080"
);
auto
ops
=
Experimental
::
Client
::
options
()
.
threads
(
1
)
.
maxConnections
(
64
);
client
.
init
(
ops
);
auto
resp
=
client
.
get
(
"/ping"
).
header
<
XProtocolVersion
>
(
1
,
0
).
send
();
resp
.
then
([](
Response
response
)
{
std
::
cout
<<
"Response code = "
<<
response
.
code
()
<<
std
::
endl
;
auto
body
=
response
.
body
();
if
(
!
body
.
empty
())
std
::
cout
<<
"Response body = "
<<
body
<<
std
::
endl
;
},
Async
::
NoExcept
);
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
1
));
client
.
shutdown
();
}
examples/http_client.cc
View file @
c6fc42b2
...
@@ -7,7 +7,6 @@
...
@@ -7,7 +7,6 @@
#include "net.h"
#include "net.h"
#include "http.h"
#include "http.h"
#include "client.h"
#include "client.h"
#include <atomic>
using
namespace
Net
;
using
namespace
Net
;
using
namespace
Net
::
Http
;
using
namespace
Net
::
Http
;
...
...
include/http_header.h
View file @
c6fc42b2
...
@@ -45,7 +45,7 @@ constexpr uint64_t hash(const char* str)
...
@@ -45,7 +45,7 @@ constexpr uint64_t hash(const char* str)
#ifdef SAFE_HEADER_CAST
#ifdef SAFE_HEADER_CAST
#define NAME(header_name) \
#define NAME(header_name) \
static constexpr uint64_t Hash = detail::hash(header_name); \
static constexpr uint64_t Hash =
Net::Http::Header::
detail::hash(header_name); \
uint64_t hash() const { return Hash; } \
uint64_t hash() const { return Hash; } \
static constexpr const char *Name = header_name; \
static constexpr const char *Name = header_name; \
const char *name() const { return Name; }
const char *name() const { return Name; }
...
...
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