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
d5004d77
Commit
d5004d77
authored
Feb 01, 2016
by
octal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Renamed Response to ResponseWriter so that server now uses ResponseWriter and client Response
parent
e2e6f19f
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
82 additions
and
59 deletions
+82
-59
include/client.h
include/client.h
+1
-3
include/http.h
include/http.h
+65
-39
include/router.h
include/router.h
+1
-1
main.cc
main.cc
+6
-7
src/client/client.cc
src/client/client.cc
+1
-1
src/common/http.cc
src/common/http.cc
+7
-7
src/server/router.cc
src/server/router.cc
+1
-1
No files found.
include/client.h
View file @
d5004d77
...
@@ -52,8 +52,6 @@ public:
...
@@ -52,8 +52,6 @@ public:
return
;
return
;
}
}
std
::
cout
<<
"I am in the same thread, wouhou"
<<
std
::
endl
;
asyncWriteImpl
(
fd
,
flags
,
BufferHolder
(
buffer
),
std
::
move
(
resolve
),
std
::
move
(
reject
));
asyncWriteImpl
(
fd
,
flags
,
BufferHolder
(
buffer
),
std
::
move
(
resolve
),
std
::
move
(
reject
));
});
});
...
@@ -317,7 +315,7 @@ public:
...
@@ -317,7 +315,7 @@ public:
static
Options
options
();
static
Options
options
();
void
init
(
const
Options
&
options
);
void
init
(
const
Options
&
options
);
RequestBuilder
newR
equest
(
std
::
string
resource
);
RequestBuilder
r
equest
(
std
::
string
resource
);
Async
::
Promise
<
Response
>
get
(
Async
::
Promise
<
Response
>
get
(
const
Http
::
Request
&
request
,
const
Http
::
Request
&
request
,
...
...
include/http.h
View file @
d5004d77
...
@@ -29,6 +29,7 @@ namespace Private {
...
@@ -29,6 +29,7 @@ namespace Private {
class
ParserBase
;
class
ParserBase
;
template
<
typename
T
>
struct
Parser
;
template
<
typename
T
>
struct
Parser
;
class
RequestLineStep
;
class
RequestLineStep
;
class
ResponseLineStep
;
class
HeadersStep
;
class
HeadersStep
;
class
BodyStep
;
class
BodyStep
;
}
}
...
@@ -44,6 +45,10 @@ std::basic_ostream<CharT, Traits>& crlf(std::basic_ostream<CharT, Traits>& os) {
...
@@ -44,6 +45,10 @@ std::basic_ostream<CharT, Traits>& crlf(std::basic_ostream<CharT, Traits>& os) {
// 4. HTTP Message
// 4. HTTP Message
class
Message
{
class
Message
{
public:
public:
friend
class
Private
::
HeadersStep
;
friend
class
Private
::
BodyStep
;
friend
class
Private
::
ParserBase
;
Message
();
Message
();
Message
(
const
Message
&
other
)
=
default
;
Message
(
const
Message
&
other
)
=
default
;
...
@@ -52,6 +57,7 @@ public:
...
@@ -52,6 +57,7 @@ public:
Message
(
Message
&&
other
)
=
default
;
Message
(
Message
&&
other
)
=
default
;
Message
&
operator
=
(
Message
&&
other
)
=
default
;
Message
&
operator
=
(
Message
&&
other
)
=
default
;
protected:
Version
version_
;
Version
version_
;
Code
code_
;
Code
code_
;
...
@@ -85,12 +91,9 @@ namespace Uri {
...
@@ -85,12 +91,9 @@ namespace Uri {
class
RequestBuilder
;
class
RequestBuilder
;
// 5. Request
// 5. Request
class
Request
:
p
rivate
Message
{
class
Request
:
p
ublic
Message
{
public:
public:
friend
class
Private
::
RequestLineStep
;
friend
class
Private
::
RequestLineStep
;
friend
class
Private
::
HeadersStep
;
friend
class
Private
::
BodyStep
;
friend
class
Private
::
ParserBase
;
friend
class
Private
::
Parser
<
Http
::
Request
>
;
friend
class
Private
::
Parser
<
Http
::
Request
>
;
friend
class
RequestBuilder
;
friend
class
RequestBuilder
;
...
@@ -101,7 +104,6 @@ public:
...
@@ -101,7 +104,6 @@ public:
Request
(
Request
&&
other
)
=
default
;
Request
(
Request
&&
other
)
=
default
;
Request
&
operator
=
(
Request
&&
other
)
=
default
;
Request
&
operator
=
(
Request
&&
other
)
=
default
;
Version
version
()
const
;
Version
version
()
const
;
Method
method
()
const
;
Method
method
()
const
;
std
::
string
resource
()
const
;
std
::
string
resource
()
const
;
...
@@ -171,7 +173,7 @@ private:
...
@@ -171,7 +173,7 @@ private:
};
};
class
Handler
;
class
Handler
;
class
Response
;
class
Response
Write
;
class
Timeout
{
class
Timeout
{
public:
public:
...
@@ -227,9 +229,9 @@ private:
...
@@ -227,9 +229,9 @@ private:
bool
armed
;
bool
armed
;
};
};
class
ResponseStream
:
p
rivate
Message
{
class
ResponseStream
:
p
ublic
Message
{
public:
public:
friend
class
Response
;
friend
class
Response
Writer
;
ResponseStream
(
ResponseStream
&&
other
)
ResponseStream
(
ResponseStream
&&
other
)
:
Message
(
std
::
move
(
other
))
:
Message
(
std
::
move
(
other
))
...
@@ -315,31 +317,13 @@ operator<<(ResponseStream& stream, ResponseStream & (*func)(ResponseStream &)) {
...
@@ -315,31 +317,13 @@ operator<<(ResponseStream& stream, ResponseStream & (*func)(ResponseStream &)) {
// @Investigate public inheritence
// @Investigate public inheritence
class
Response
:
public
Message
{
class
Response
:
public
Message
{
public:
public:
friend
class
Handler
;
friend
class
Private
::
ResponseLineStep
;
friend
class
Timeout
;
friend
class
Private
::
Parser
<
Http
::
Response
>
;
friend
class
Private
::
ParserBase
;
friend
class
Private
::
Parser
<
Response
>
;
friend
Async
::
Promise
<
ssize_t
>
serveFile
(
Response
&
,
const
char
*
,
const
Mime
::
MediaType
&
);
static
constexpr
size_t
DefaultStreamSize
=
512
;
Response
(
const
Response
&
other
)
=
default
;
Response
&
operator
=
(
const
Response
&
other
)
=
default
;
// C++11: std::weak_ptr move constructor is C++14 only so the default
Response
(
Response
&&
other
)
=
default
;
// version of move constructor / assignement operator does not work and we
Response
&
operator
=
(
Response
&&
other
)
=
default
;
// have to define it ourself
Response
(
Response
&&
other
)
:
Message
(
std
::
move
(
other
))
,
peer_
(
other
.
peer_
)
,
buf_
(
std
::
move
(
other
.
buf_
))
,
transport_
(
other
.
transport_
)
{
}
Response
&
operator
=
(
Response
&&
other
)
{
Message
::
operator
=
(
std
::
move
(
other
));
peer_
=
std
::
move
(
other
.
peer_
);
transport_
=
other
.
transport_
;
buf_
=
std
::
move
(
other
.
buf_
);
return
*
this
;
}
const
Header
::
Collection
&
headers
()
const
{
const
Header
::
Collection
&
headers
()
const
{
return
headers_
;
return
headers_
;
...
@@ -361,6 +345,48 @@ public:
...
@@ -361,6 +345,48 @@ public:
return
code_
;
return
code_
;
}
}
std
::
string
body
()
const
{
return
body_
;
}
protected:
Response
()
:
Message
()
{
}
};
class
ResponseWriter
:
public
Response
{
public:
static
constexpr
size_t
DefaultStreamSize
=
512
;
friend
Async
::
Promise
<
ssize_t
>
serveFile
(
ResponseWriter
&
,
const
char
*
,
const
Mime
::
MediaType
&
);
friend
class
Handler
;
friend
class
Timeout
;
friend
class
Private
::
ResponseLineStep
;
friend
class
Private
::
Parser
<
Http
::
Response
>
;
ResponseWriter
(
const
ResponseWriter
&
other
)
=
delete
;
ResponseWriter
&
operator
=
(
const
ResponseWriter
&
other
)
=
delete
;
//
// C++11: std::weak_ptr move constructor is C++14 only so the default
// version of move constructor / assignement operator does not work and we
// have to define it ourself
ResponseWriter
(
ResponseWriter
&&
other
)
:
Response
(
std
::
move
(
other
))
,
peer_
(
other
.
peer_
)
,
buf_
(
std
::
move
(
other
.
buf_
))
,
transport_
(
other
.
transport_
)
{
}
ResponseWriter
&
operator
=
(
ResponseWriter
&&
other
)
{
Response
::
operator
=
(
std
::
move
(
other
));
peer_
=
std
::
move
(
other
.
peer_
);
transport_
=
other
.
transport_
;
buf_
=
std
::
move
(
other
.
buf_
);
return
*
this
;
}
void
setMime
(
const
Mime
::
MediaType
&
mime
)
{
void
setMime
(
const
Mime
::
MediaType
&
mime
)
{
auto
ct
=
headers_
.
tryGet
<
Header
::
ContentType
>
();
auto
ct
=
headers_
.
tryGet
<
Header
::
ContentType
>
();
if
(
ct
)
if
(
ct
)
...
@@ -428,14 +454,14 @@ public:
...
@@ -428,14 +454,14 @@ public:
}
}
private:
private:
Response
()
Response
Writer
()
:
Messag
e
()
:
Respons
e
()
,
buf_
(
0
)
,
buf_
(
0
)
,
transport_
(
nullptr
)
,
transport_
(
nullptr
)
{
}
{
}
Response
(
Tcp
::
Transport
*
transport
)
Response
Writer
(
Tcp
::
Transport
*
transport
)
:
Messag
e
()
:
Respons
e
()
,
buf_
(
DefaultStreamSize
)
,
buf_
(
DefaultStreamSize
)
,
transport_
(
transport
)
,
transport_
(
transport
)
{
}
{
}
...
@@ -463,7 +489,7 @@ private:
...
@@ -463,7 +489,7 @@ private:
};
};
Async
::
Promise
<
ssize_t
>
serveFile
(
Async
::
Promise
<
ssize_t
>
serveFile
(
Response
&
response
,
const
char
*
fileName
,
Response
Writer
&
response
,
const
char
*
fileName
,
const
Mime
::
MediaType
&
contentType
=
Mime
::
MediaType
());
const
Mime
::
MediaType
&
contentType
=
Mime
::
MediaType
());
namespace
Private
{
namespace
Private
{
...
@@ -641,9 +667,9 @@ public:
...
@@ -641,9 +667,9 @@ public:
void
onConnection
(
const
std
::
shared_ptr
<
Tcp
::
Peer
>&
peer
);
void
onConnection
(
const
std
::
shared_ptr
<
Tcp
::
Peer
>&
peer
);
void
onDisconnection
(
const
std
::
shared_ptr
<
Tcp
::
Peer
>&
peer
);
void
onDisconnection
(
const
std
::
shared_ptr
<
Tcp
::
Peer
>&
peer
);
virtual
void
onRequest
(
const
Request
&
request
,
Response
response
,
Timeout
timeout
)
=
0
;
virtual
void
onRequest
(
const
Request
&
request
,
Response
Writer
response
,
Timeout
timeout
)
=
0
;
virtual
void
onTimeout
(
const
Request
&
request
,
Response
response
);
virtual
void
onTimeout
(
const
Request
&
request
,
Response
Writer
response
);
private:
private:
Private
::
Parser
<
Http
::
Request
>&
getParser
(
const
std
::
shared_ptr
<
Tcp
::
Peer
>&
peer
)
const
;
Private
::
Parser
<
Http
::
Request
>&
getParser
(
const
std
::
shared_ptr
<
Tcp
::
Peer
>&
peer
)
const
;
...
...
include/router.h
View file @
d5004d77
...
@@ -135,7 +135,7 @@ namespace Private {
...
@@ -135,7 +135,7 @@ namespace Private {
void
onRequest
(
void
onRequest
(
const
Http
::
Request
&
req
,
const
Http
::
Request
&
req
,
Http
::
Response
response
,
Http
::
Response
Writer
response
,
Http
::
Timeout
timeout
);
Http
::
Timeout
timeout
);
private:
private:
...
...
main.cc
View file @
d5004d77
...
@@ -38,7 +38,7 @@ class MyHandler : public Net::Http::Handler {
...
@@ -38,7 +38,7 @@ class MyHandler : public Net::Http::Handler {
void
onRequest
(
void
onRequest
(
const
Net
::
Http
::
Request
&
req
,
const
Net
::
Http
::
Request
&
req
,
Net
::
Http
::
Response
response
,
Net
::
Http
::
Response
Writer
response
,
Net
::
Http
::
Timeout
timeout
)
{
Net
::
Http
::
Timeout
timeout
)
{
if
(
req
.
resource
()
==
"/ping"
)
{
if
(
req
.
resource
()
==
"/ping"
)
{
...
@@ -99,7 +99,7 @@ class MyHandler : public Net::Http::Handler {
...
@@ -99,7 +99,7 @@ class MyHandler : public Net::Http::Handler {
}
}
void
onTimeout
(
const
Net
::
Http
::
Request
&
req
,
Net
::
Http
::
Response
response
)
{
void
onTimeout
(
const
Net
::
Http
::
Request
&
req
,
Net
::
Http
::
Response
Writer
response
)
{
response
response
.
send
(
Net
::
Http
::
Code
::
Request_Timeout
,
"Timeout"
)
.
send
(
Net
::
Http
::
Code
::
Request_Timeout
,
"Timeout"
)
.
then
([
=
](
ssize_t
)
{
},
ExceptionPrinter
());
.
then
([
=
](
ssize_t
)
{
},
ExceptionPrinter
());
...
@@ -163,7 +163,7 @@ private:
...
@@ -163,7 +163,7 @@ private:
namespace
Generic
{
namespace
Generic
{
void
handleReady
(
const
Rest
::
Request
&
,
Http
::
Response
response
)
{
void
handleReady
(
const
Rest
::
Request
&
,
Http
::
Response
Writer
response
)
{
response
.
send
(
Http
::
Code
::
Ok
,
"1"
);
response
.
send
(
Http
::
Code
::
Ok
,
"1"
);
}
}
...
@@ -347,14 +347,13 @@ int main() {
...
@@ -347,14 +347,13 @@ int main() {
using
namespace
Net
::
Http
;
using
namespace
Net
::
Http
;
client
.
init
(
opts
);
client
.
init
(
opts
);
client
.
get
(
client
.
get
(
client
client
.
request
(
"/off/octal/nask"
)
.
newRequest
(
"/off/octal/nask"
)
.
header
<
Header
::
ContentType
>
(
MIME
(
Text
,
Plain
))
.
header
<
Header
::
ContentType
>
(
MIME
(
Text
,
Plain
))
.
cookie
(
Cookie
(
"FOO"
,
"bar"
)))
.
cookie
(
Cookie
(
"FOO"
,
"bar"
)))
.
then
([](
const
Http
::
Response
&
response
)
{
.
then
([](
const
Http
::
Response
&
response
)
{
std
::
cout
<<
"code = "
<<
response
.
code
()
<<
std
::
endl
;
std
::
cout
<<
"code = "
<<
response
.
code
()
<<
std
::
endl
;
std
::
cout
<<
"body = "
<<
response
.
body
_
<<
std
::
endl
;
std
::
cout
<<
"body = "
<<
response
.
body
()
<<
std
::
endl
;
},
Async
::
NoExcept
);
},
Async
::
NoExcept
);
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
1
));
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
1
));
}
}
...
...
src/client/client.cc
View file @
d5004d77
...
@@ -498,7 +498,7 @@ Client::init(const Client::Options& options) {
...
@@ -498,7 +498,7 @@ Client::init(const Client::Options& options) {
}
}
RequestBuilder
RequestBuilder
Client
::
newR
equest
(
std
::
string
val
)
{
Client
::
r
equest
(
std
::
string
val
)
{
RequestBuilder
builder
;
RequestBuilder
builder
;
builder
.
resource
(
std
::
move
(
val
));
builder
.
resource
(
std
::
move
(
val
));
return
builder
;
return
builder
;
...
...
src/common/http.cc
View file @
d5004d77
...
@@ -638,7 +638,7 @@ ResponseStream::ends() {
...
@@ -638,7 +638,7 @@ ResponseStream::ends() {
}
}
Async
::
Promise
<
ssize_t
>
Async
::
Promise
<
ssize_t
>
Response
::
putOnWire
(
const
char
*
data
,
size_t
len
)
Response
Writer
::
putOnWire
(
const
char
*
data
,
size_t
len
)
{
{
try
{
try
{
std
::
ostream
os
(
&
buf_
);
std
::
ostream
os
(
&
buf_
);
...
@@ -678,7 +678,7 @@ Response::putOnWire(const char* data, size_t len)
...
@@ -678,7 +678,7 @@ Response::putOnWire(const char* data, size_t len)
}
}
Async
::
Promise
<
ssize_t
>
Async
::
Promise
<
ssize_t
>
serveFile
(
Response
&
response
,
const
char
*
fileName
,
const
Mime
::
MediaType
&
contentType
)
serveFile
(
Response
Writer
&
response
,
const
char
*
fileName
,
const
Mime
::
MediaType
&
contentType
)
{
{
struct
stat
sb
;
struct
stat
sb
;
...
@@ -756,7 +756,7 @@ Handler::onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer
...
@@ -756,7 +756,7 @@ Handler::onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer
auto
state
=
parser
.
parse
();
auto
state
=
parser
.
parse
();
if
(
state
==
Private
::
State
::
Done
)
{
if
(
state
==
Private
::
State
::
Done
)
{
Response
response
(
transport
());
Response
Writer
response
(
transport
());
response
.
associatePeer
(
peer
);
response
.
associatePeer
(
peer
);
Timeout
timeout
(
transport
(),
this
,
peer
,
parser
.
request
);
Timeout
timeout
(
transport
(),
this
,
peer
,
parser
.
request
);
...
@@ -768,13 +768,13 @@ Handler::onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer
...
@@ -768,13 +768,13 @@ Handler::onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer
parser
.
reset
();
parser
.
reset
();
}
}
}
catch
(
const
HttpError
&
err
)
{
}
catch
(
const
HttpError
&
err
)
{
Response
response
(
transport
());
Response
Writer
response
(
transport
());
response
.
associatePeer
(
peer
);
response
.
associatePeer
(
peer
);
response
.
send
(
static_cast
<
Code
>
(
err
.
code
()),
err
.
reason
());
response
.
send
(
static_cast
<
Code
>
(
err
.
code
()),
err
.
reason
());
getParser
(
peer
).
reset
();
getParser
(
peer
).
reset
();
}
}
catch
(
const
std
::
exception
&
e
)
{
catch
(
const
std
::
exception
&
e
)
{
Response
response
(
transport
());
Response
Writer
response
(
transport
());
response
.
associatePeer
(
peer
);
response
.
associatePeer
(
peer
);
response
.
send
(
Code
::
Internal_Server_Error
,
e
.
what
());
response
.
send
(
Code
::
Internal_Server_Error
,
e
.
what
());
getParser
(
peer
).
reset
();
getParser
(
peer
).
reset
();
...
@@ -791,14 +791,14 @@ Handler::onDisconnection(const shared_ptr<Tcp::Peer>& peer) {
...
@@ -791,14 +791,14 @@ Handler::onDisconnection(const shared_ptr<Tcp::Peer>& peer) {
}
}
void
void
Handler
::
onTimeout
(
const
Request
&
request
,
Response
response
)
{
Handler
::
onTimeout
(
const
Request
&
request
,
Response
Writer
response
)
{
}
}
void
void
Timeout
::
onTimeout
(
uint64_t
numWakeup
)
{
Timeout
::
onTimeout
(
uint64_t
numWakeup
)
{
if
(
!
peer
.
lock
())
return
;
if
(
!
peer
.
lock
())
return
;
Response
response
(
transport
);
Response
Writer
response
(
transport
);
response
.
associatePeer
(
peer
);
response
.
associatePeer
(
peer
);
handler
->
onTimeout
(
request
,
std
::
move
(
response
));
handler
->
onTimeout
(
request
,
std
::
move
(
response
));
...
...
src/server/router.cc
View file @
d5004d77
...
@@ -218,7 +218,7 @@ HttpHandler::HttpHandler(
...
@@ -218,7 +218,7 @@ HttpHandler::HttpHandler(
void
void
HttpHandler
::
onRequest
(
HttpHandler
::
onRequest
(
const
Http
::
Request
&
req
,
const
Http
::
Request
&
req
,
Http
::
Response
response
,
Http
::
Response
Writer
response
,
Http
::
Timeout
timeout
)
Http
::
Timeout
timeout
)
{
{
auto
&
r
=
routes
[
req
.
method
()];
auto
&
r
=
routes
[
req
.
method
()];
...
...
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