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
a9ba5395
Commit
a9ba5395
authored
Apr 03, 2016
by
octal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some ongoing work on the http client
parent
26938285
Changes
6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
177 additions
and
110 deletions
+177
-110
examples/custom_header.cc
examples/custom_header.cc
+0
-19
examples/http_client.cc
examples/http_client.cc
+4
-9
include/client.h
include/client.h
+21
-15
include/view.h
include/view.h
+4
-0
src/client/client.cc
src/client/client.cc
+144
-67
tests/CMakeLists.txt
tests/CMakeLists.txt
+4
-0
No files found.
examples/custom_header.cc
View file @
a9ba5395
...
...
@@ -61,23 +61,4 @@ private:
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 @
a9ba5395
...
...
@@ -12,19 +12,14 @@ using namespace Net;
using
namespace
Net
::
Http
;
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
<
2
)
{
std
::
cerr
<<
"Usage: http_client
host [page]
"
<<
std
::
endl
;
if
(
argc
<
1
)
{
std
::
cerr
<<
"Usage: http_client
page
"
<<
std
::
endl
;
return
1
;
}
std
::
string
host
=
argv
[
1
];
std
::
string
page
;
if
(
argc
==
3
)
page
=
argv
[
2
];
else
page
=
"/"
;
std
::
string
page
=
argv
[
1
];
Experimental
::
Client
client
(
host
)
;
Experimental
::
Client
client
;
auto
opts
=
Http
::
Experimental
::
Client
::
options
()
.
threads
(
1
)
...
...
include/client.h
View file @
a9ba5395
...
...
@@ -10,6 +10,7 @@
#include "http.h"
#include "io.h"
#include "timer_pool.h"
#include "view.h"
#include <atomic>
#include <sys/types.h>
#include <sys/socket.h>
...
...
@@ -24,6 +25,8 @@ namespace Experimental {
class
ConnectionPool
;
class
Transport
;
std
::
pair
<
StringView
,
StringView
>
splitUrl
(
const
std
::
string
&
url
);
struct
Connection
:
public
std
::
enable_shared_from_this
<
Connection
>
{
friend
class
ConnectionPool
;
...
...
@@ -44,13 +47,11 @@ struct Connection : public std::enable_shared_from_this<Connection> {
RequestData
(
Async
::
Resolver
resolve
,
Async
::
Rejection
reject
,
const
Http
::
Request
&
request
,
std
::
string
host
,
std
::
chrono
::
milliseconds
timeout
,
OnDone
onDone
)
:
resolve
(
std
::
move
(
resolve
))
,
reject
(
std
::
move
(
reject
))
,
request
(
request
)
,
host
(
std
::
move
(
host
))
,
timeout
(
timeout
)
,
onDone
(
std
::
move
(
onDone
))
{
}
...
...
@@ -58,7 +59,6 @@ struct Connection : public std::enable_shared_from_this<Connection> {
Async
::
Rejection
reject
;
Http
::
Request
request
;
std
::
string
host
;
std
::
chrono
::
milliseconds
timeout
;
OnDone
onDone
;
};
...
...
@@ -76,19 +76,18 @@ struct Connection : public std::enable_shared_from_this<Connection> {
void
connect
(
Net
::
Address
addr
);
void
close
();
bool
isIdle
()
const
;
bool
isConnected
()
const
;
bool
hasTransport
()
const
;
void
associateTransport
(
const
std
::
shared_ptr
<
Transport
>&
transport
);
Async
::
Promise
<
Response
>
perform
(
const
Http
::
Request
&
request
,
std
::
string
host
,
std
::
chrono
::
milliseconds
timeout
,
OnDone
onDone
);
void
performImpl
(
const
Http
::
Request
&
request
,
std
::
string
host
,
std
::
chrono
::
milliseconds
timeout
,
Async
::
Resolver
resolve
,
Async
::
Rejection
reject
,
...
...
@@ -138,16 +137,23 @@ private:
};
struct
ConnectionPool
{
void
init
(
size_t
max
=
1
);
std
::
shared_ptr
<
Connection
>
pickConnection
();
std
::
shared_ptr
<
Connection
>
pickConnection
(
const
std
::
string
&
domain
);
void
releaseConnection
(
const
std
::
shared_ptr
<
Connection
>&
connection
);
size_t
availableCount
()
const
;
size_t
usedConnections
(
const
std
::
string
&
domain
)
const
;
size_t
idleConnections
(
const
std
::
string
&
domain
)
const
;
size_t
availableConnections
(
const
std
::
string
&
domain
)
const
;
void
closeIdleConnections
(
const
std
::
string
&
domain
);
private:
std
::
atomic
<
uint32_t
>
usedCount
;
std
::
vector
<
std
::
shared_ptr
<
Connection
>>
connections
;
typedef
std
::
vector
<
std
::
shared_ptr
<
Connection
>>
Connections
;
typedef
std
::
mutex
Lock
;
typedef
std
::
lock_guard
<
Lock
>
Guard
;
mutable
Lock
connsLock
;
std
::
unordered_map
<
std
::
string
,
Connections
>
conns
;
};
class
Transport
:
public
Io
::
Handler
{
...
...
@@ -298,13 +304,16 @@ public:
Options
&
threads
(
int
val
);
Options
&
maxConnections
(
int
val
);
Options
&
keepAlive
(
bool
val
);
Options
&
maxConnectionsPerHost
(
int
val
);
private:
int
threads_
;
int
maxConnections_
;
int
maxConnectionsPerHost_
;
bool
keepAlive_
;
};
Client
(
const
std
::
string
&
base
);
Client
();
static
Options
options
();
void
init
(
const
Options
&
options
);
...
...
@@ -319,9 +328,6 @@ public:
private:
Io
::
ServiceGroup
io_
;
std
::
string
url_
;
std
::
string
host_
;
Net
::
Address
addr_
;
ConnectionPool
pool
;
std
::
shared_ptr
<
Transport
>
transport_
;
...
...
include/view.h
View file @
a9ba5395
...
...
@@ -129,6 +129,10 @@ struct View<std::string> : public ViewBase<char> {
typedef
ViewBase
<
char
>
Base
;
explicit
View
(
std
::
nullptr_t
)
:
Base
(
nullptr
)
{
}
explicit
View
(
const
char
*
begin
,
size_t
end
)
:
Base
(
begin
,
end
)
{
}
...
...
src/client/client.cc
View file @
a9ba5395
This diff is collapsed.
Click to expand it.
tests/CMakeLists.txt
View file @
a9ba5395
...
...
@@ -25,3 +25,7 @@ add_test( cookie_test run_cookie_test )
add_executable
(
run_view_test view_test.cc
)
target_link_libraries
(
run_view_test gtest gtest_main net
)
add_test
(
view_test run_view_test
)
add_executable
(
run_client_test http_client_test.cc
)
target_link_libraries
(
run_client_test gtest gtest_main net
)
add_test
(
client_test run_client_test
)
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