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
93713f9e
Commit
93713f9e
authored
Feb 05, 2019
by
knowledge4igor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tiny code improvements
parent
153d9f0c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
33 deletions
+41
-33
include/pistache/client.h
include/pistache/client.h
+3
-4
include/pistache/peer.h
include/pistache/peer.h
+2
-2
src/client/client.cc
src/client/client.cc
+32
-23
src/common/peer.cc
src/common/peer.cc
+4
-4
No files found.
include/pistache/client.h
View file @
93713f9e
...
@@ -30,8 +30,6 @@ namespace Http {
...
@@ -30,8 +30,6 @@ namespace Http {
class
ConnectionPool
;
class
ConnectionPool
;
class
Transport
;
class
Transport
;
std
::
pair
<
StringView
,
StringView
>
splitUrl
(
const
std
::
string
&
url
);
struct
Connection
:
public
std
::
enable_shared_from_this
<
Connection
>
{
struct
Connection
:
public
std
::
enable_shared_from_this
<
Connection
>
{
friend
class
ConnectionPool
;
friend
class
ConnectionPool
;
...
@@ -96,8 +94,7 @@ struct Connection : public std::enable_shared_from_this<Connection> {
...
@@ -96,8 +94,7 @@ struct Connection : public std::enable_shared_from_this<Connection> {
Async
::
Rejection
reject
,
Async
::
Rejection
reject
,
OnDone
onDone
);
OnDone
onDone
);
Fd
fd
;
Fd
fd
()
const
;
void
handleResponsePacket
(
const
char
*
buffer
,
size_t
totalBytes
);
void
handleResponsePacket
(
const
char
*
buffer
,
size_t
totalBytes
);
void
handleError
(
const
char
*
error
);
void
handleError
(
const
char
*
error
);
void
handleTimeout
();
void
handleTimeout
();
...
@@ -124,6 +121,8 @@ private:
...
@@ -124,6 +121,8 @@ private:
OnDone
onDone
;
OnDone
onDone
;
};
};
Fd
fd_
;
struct
sockaddr_in
saddr
;
struct
sockaddr_in
saddr
;
std
::
unique_ptr
<
RequestEntry
>
requestEntry
;
std
::
unique_ptr
<
RequestEntry
>
requestEntry
;
std
::
atomic
<
uint32_t
>
state_
;
std
::
atomic
<
uint32_t
>
state_
;
...
...
include/pistache/peer.h
View file @
93713f9e
...
@@ -40,8 +40,8 @@ public:
...
@@ -40,8 +40,8 @@ public:
Peer
(
const
Address
&
addr
);
Peer
(
const
Address
&
addr
);
~
Peer
()
{}
~
Peer
()
{}
Address
address
()
const
;
const
Address
&
address
()
const
;
std
::
string
hostname
()
const
;
const
std
::
string
&
hostname
()
const
;
void
associateFd
(
Fd
fd
);
void
associateFd
(
Fd
fd
);
Fd
fd
()
const
;
Fd
fd
()
const
;
...
...
src/client/client.cc
View file @
93713f9e
...
@@ -26,22 +26,25 @@ namespace Http {
...
@@ -26,22 +26,25 @@ namespace Http {
static
constexpr
const
char
*
UA
=
"pistache/0.1"
;
static
constexpr
const
char
*
UA
=
"pistache/0.1"
;
std
::
pair
<
StringView
,
StringView
>
namespace
splitUrl
(
const
std
::
string
&
url
)
{
{
RawStreamBuf
<
char
>
buf
(
const_cast
<
char
*>
(
&
url
[
0
]),
url
.
size
());
std
::
pair
<
StringView
,
StringView
>
splitUrl
(
const
std
::
string
&
url
)
StreamCursor
cursor
(
&
buf
);
{
RawStreamBuf
<
char
>
buf
(
const_cast
<
char
*>
(
&
url
[
0
]),
url
.
size
());
StreamCursor
cursor
(
&
buf
);
match_string
(
"http://"
,
std
::
strlen
(
"http://"
),
cursor
);
match_string
(
"http://"
,
std
::
strlen
(
"http://"
),
cursor
);
match_string
(
"www"
,
std
::
strlen
(
"www"
),
cursor
);
match_string
(
"www"
,
std
::
strlen
(
"www"
),
cursor
);
match_literal
(
'.'
,
cursor
);
match_literal
(
'.'
,
cursor
);
StreamCursor
::
Token
hostToken
(
cursor
);
StreamCursor
::
Token
hostToken
(
cursor
);
match_until
({
'?'
,
'/'
},
cursor
);
match_until
({
'?'
,
'/'
},
cursor
);
StringView
host
(
hostToken
.
rawText
(),
hostToken
.
size
());
StringView
host
(
hostToken
.
rawText
(),
hostToken
.
size
());
StringView
page
(
cursor
.
offset
(),
buf
.
endptr
());
StringView
page
(
cursor
.
offset
(),
buf
.
endptr
());
return
std
::
make_pair
(
std
::
move
(
host
),
std
::
move
(
page
));
return
std
::
make_pair
(
std
::
move
(
host
),
std
::
move
(
page
));
}
}
}
struct
ExceptionPrinter
{
struct
ExceptionPrinter
{
...
@@ -174,7 +177,7 @@ Transport::onReady(const Aio::FdSet& fds) {
...
@@ -174,7 +177,7 @@ Transport::onReady(const Aio::FdSet& fds) {
// We are connected, we can start reading data now
// We are connected, we can start reading data now
auto
connection
=
connIt
->
second
.
connection
.
lock
();
auto
connection
=
connIt
->
second
.
connection
.
lock
();
if
(
connection
)
{
if
(
connection
)
{
reactor
()
->
modifyFd
(
key
(),
connection
->
fd
,
NotifyOn
::
Read
);
reactor
()
->
modifyFd
(
key
(),
connection
->
fd
()
,
NotifyOn
::
Read
);
}
else
{
}
else
{
throw
std
::
runtime_error
(
"Connection error"
);
throw
std
::
runtime_error
(
"Connection error"
);
}
}
...
@@ -228,7 +231,7 @@ Transport::asyncSendRequestImpl(
...
@@ -228,7 +231,7 @@ Transport::asyncSendRequestImpl(
if
(
!
conn
)
if
(
!
conn
)
throw
std
::
runtime_error
(
"Send request error"
);
throw
std
::
runtime_error
(
"Send request error"
);
auto
fd
=
conn
->
fd
;
auto
fd
=
conn
->
fd
()
;
ssize_t
totalWritten
=
0
;
ssize_t
totalWritten
=
0
;
for
(;;)
{
for
(;;)
{
...
@@ -284,17 +287,17 @@ Transport::handleConnectionQueue() {
...
@@ -284,17 +287,17 @@ Transport::handleConnectionQueue() {
if
(
!
conn
)
{
if
(
!
conn
)
{
throw
std
::
runtime_error
(
"Connection error"
);
throw
std
::
runtime_error
(
"Connection error"
);
}
}
int
res
=
::
connect
(
conn
->
fd
,
data
->
getAddr
(),
data
->
addr_len
);
int
res
=
::
connect
(
conn
->
fd
()
,
data
->
getAddr
(),
data
->
addr_len
);
if
(
res
==
-
1
)
{
if
(
res
==
-
1
)
{
if
(
errno
==
EINPROGRESS
)
{
if
(
errno
==
EINPROGRESS
)
{
reactor
()
->
registerFdOneShot
(
key
(),
conn
->
fd
,
NotifyOn
::
Write
|
NotifyOn
::
Hangup
|
NotifyOn
::
Shutdown
);
reactor
()
->
registerFdOneShot
(
key
(),
conn
->
fd
()
,
NotifyOn
::
Write
|
NotifyOn
::
Hangup
|
NotifyOn
::
Shutdown
);
}
}
else
{
else
{
data
->
reject
(
Error
::
system
(
"Failed to connect"
));
data
->
reject
(
Error
::
system
(
"Failed to connect"
));
continue
;
continue
;
}
}
}
}
connections
.
insert
(
std
::
make_pair
(
conn
->
fd
,
std
::
move
(
*
data
)));
connections
.
insert
(
std
::
make_pair
(
conn
->
fd
()
,
std
::
move
(
*
data
)));
}
}
}
}
...
@@ -305,7 +308,7 @@ Transport::handleIncoming(std::shared_ptr<Connection> connection) {
...
@@ -305,7 +308,7 @@ Transport::handleIncoming(std::shared_ptr<Connection> connection) {
ssize_t
totalBytes
=
0
;
ssize_t
totalBytes
=
0
;
for
(;;)
{
for
(;;)
{
ssize_t
bytes
=
recv
(
connection
->
fd
,
buffer
+
totalBytes
,
Const
::
MaxBuffer
-
totalBytes
,
0
);
ssize_t
bytes
=
recv
(
connection
->
fd
()
,
buffer
+
totalBytes
,
Const
::
MaxBuffer
-
totalBytes
,
0
);
if
(
bytes
==
-
1
)
{
if
(
bytes
==
-
1
)
{
if
(
errno
==
EAGAIN
||
errno
==
EWOULDBLOCK
)
{
if
(
errno
==
EAGAIN
||
errno
==
EWOULDBLOCK
)
{
if
(
totalBytes
>
0
)
{
if
(
totalBytes
>
0
)
{
...
@@ -322,7 +325,7 @@ Transport::handleIncoming(std::shared_ptr<Connection> connection) {
...
@@ -322,7 +325,7 @@ Transport::handleIncoming(std::shared_ptr<Connection> connection) {
}
else
{
}
else
{
connection
->
handleError
(
"Remote closed connection"
);
connection
->
handleError
(
"Remote closed connection"
);
}
}
connections
.
erase
(
connection
->
fd
);
connections
.
erase
(
connection
->
fd
()
);
connection
->
close
();
connection
->
close
();
break
;
break
;
}
}
...
@@ -348,7 +351,7 @@ Transport::handleTimeout(const std::shared_ptr<Connection>& connection) {
...
@@ -348,7 +351,7 @@ Transport::handleTimeout(const std::shared_ptr<Connection>& connection) {
}
}
Connection
::
Connection
()
Connection
::
Connection
()
:
fd
(
-
1
)
:
fd
_
(
-
1
)
,
requestEntry
(
nullptr
)
,
requestEntry
(
nullptr
)
{
{
state_
.
store
(
static_cast
<
uint32_t
>
(
State
::
Idle
));
state_
.
store
(
static_cast
<
uint32_t
>
(
State
::
Idle
));
...
@@ -382,7 +385,7 @@ Connection::connect(const Address& addr)
...
@@ -382,7 +385,7 @@ Connection::connect(const Address& addr)
make_non_blocking
(
sfd
);
make_non_blocking
(
sfd
);
connectionState_
.
store
(
Connecting
);
connectionState_
.
store
(
Connecting
);
fd
=
sfd
;
fd
_
=
sfd
;
transport_
->
asyncConnect
(
shared_from_this
(),
addr
->
ai_addr
,
addr
->
ai_addrlen
)
transport_
->
asyncConnect
(
shared_from_this
(),
addr
->
ai_addr
,
addr
->
ai_addrlen
)
.
then
([
=
]()
{
.
then
([
=
]()
{
...
@@ -402,7 +405,7 @@ Connection::connect(const Address& addr)
...
@@ -402,7 +405,7 @@ Connection::connect(const Address& addr)
std
::
string
std
::
string
Connection
::
dump
()
const
{
Connection
::
dump
()
const
{
std
::
ostringstream
oss
;
std
::
ostringstream
oss
;
oss
<<
"Connection(fd = "
<<
fd
<<
", src_port = "
;
oss
<<
"Connection(fd = "
<<
fd
_
<<
", src_port = "
;
oss
<<
ntohs
(
saddr
.
sin_port
)
<<
")"
;
oss
<<
ntohs
(
saddr
.
sin_port
)
<<
")"
;
return
oss
.
str
();
return
oss
.
str
();
}
}
...
@@ -420,7 +423,7 @@ Connection::isConnected() const {
...
@@ -420,7 +423,7 @@ Connection::isConnected() const {
void
void
Connection
::
close
()
{
Connection
::
close
()
{
connectionState_
.
store
(
NotConnected
);
connectionState_
.
store
(
NotConnected
);
::
close
(
fd
);
::
close
(
fd
_
);
}
}
void
void
...
@@ -436,6 +439,12 @@ Connection::hasTransport() const {
...
@@ -436,6 +439,12 @@ Connection::hasTransport() const {
return
transport_
!=
nullptr
;
return
transport_
!=
nullptr
;
}
}
Fd
Connection
::
fd
()
const
{
assert
(
fd_
!=
-
1
);
return
fd_
;
}
void
void
Connection
::
handleResponsePacket
(
const
char
*
buffer
,
size_t
totalBytes
)
{
Connection
::
handleResponsePacket
(
const
char
*
buffer
,
size_t
totalBytes
)
{
...
...
src/common/peer.cc
View file @
93713f9e
...
@@ -30,13 +30,13 @@ Peer::Peer(const Address& addr)
...
@@ -30,13 +30,13 @@ Peer::Peer(const Address& addr)
,
ssl_
(
NULL
)
,
ssl_
(
NULL
)
{
}
{
}
Address
const
Address
&
Peer
::
address
()
const
Peer
::
address
()
const
{
{
return
addr
;
return
addr
;
}
}
string
const
std
::
string
&
Peer
::
hostname
()
const
Peer
::
hostname
()
const
{
{
return
hostname_
;
return
hostname_
;
}
}
...
...
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