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
a3c5c68e
Unverified
Commit
a3c5c68e
authored
Nov 26, 2020
by
Kip
Committed by
GitHub
Nov 26, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #844 from hyperxor/code_simplification
Code simplification in RequestParser usage
parents
bb4aa2ef
7788a797
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
55 deletions
+27
-55
include/pistache/http.h
include/pistache/http.h
+3
-4
include/pistache/peer.h
include/pistache/peer.h
+3
-5
src/common/http.cc
src/common/http.cc
+18
-23
src/common/peer.cc
src/common/peer.cc
+3
-23
No files found.
include/pistache/http.h
View file @
a3c5c68e
...
...
@@ -416,9 +416,9 @@ public:
ResponseWriter
clone
()
const
;
std
::
shared_ptr
<
Tcp
::
Peer
>
getPeer
()
const
{
if
(
auto
sp
=
peer_
.
lock
())
return
sp
;
return
nullptr
;
if
(
auto
sp
=
peer_
.
lock
())
return
sp
;
return
nullptr
;
}
private:
...
...
@@ -589,7 +589,6 @@ private:
void
onConnection
(
const
std
::
shared_ptr
<
Tcp
::
Peer
>
&
peer
)
override
;
void
onInput
(
const
char
*
buffer
,
size_t
len
,
const
std
::
shared_ptr
<
Tcp
::
Peer
>
&
peer
)
override
;
RequestParser
&
getParser
(
const
std
::
shared_ptr
<
Tcp
::
Peer
>
&
peer
)
const
;
private:
size_t
maxRequestSize_
=
Const
::
DefaultMaxRequestSize
;
...
...
include/pistache/peer.h
View file @
a3c5c68e
...
...
@@ -9,7 +9,6 @@
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
#include <pistache/async.h>
#include <pistache/http.h>
...
...
@@ -43,9 +42,8 @@ public:
void
*
ssl
()
const
;
void
putData
(
std
::
string
name
,
std
::
shared_ptr
<
Http
::
Parser
>
data
);
std
::
shared_ptr
<
Http
::
Parser
>
getData
(
std
::
string
name
)
const
;
std
::
shared_ptr
<
Http
::
Parser
>
tryGetData
(
std
::
string
name
)
const
;
void
setParser
(
std
::
shared_ptr
<
Http
::
RequestParser
>
parser
);
std
::
shared_ptr
<
Http
::
RequestParser
>
getParser
()
const
;
Async
::
Promise
<
ssize_t
>
send
(
const
RawBuffer
&
buffer
,
int
flags
=
0
);
size_t
getID
()
const
;
...
...
@@ -62,7 +60,7 @@ private:
Address
addr
;
std
::
string
hostname_
;
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
Http
::
Parser
>>
data
_
;
std
::
shared_ptr
<
Http
::
RequestParser
>
parser
_
;
void
*
ssl_
=
nullptr
;
const
size_t
id_
;
...
...
src/common/http.cc
View file @
a3c5c68e
...
...
@@ -111,8 +111,6 @@ const HttpMethods httpMethods = {
}
// namespace
static
constexpr
const
char
*
ParserData
=
"__Parser"
;
namespace
Private
{
Step
::
Step
(
Message
*
request
)
:
message
(
request
)
{}
...
...
@@ -425,12 +423,12 @@ BodyStep::Chunk::Result BodyStep::Chunk::parse(StreamCursor &cursor) {
if
(
available
+
alreadyAppendedChunkBytes
<
size
+
2
)
{
cursor
.
advance
(
available
);
message
->
body_
.
append
(
chunkData
.
rawText
(),
available
);
alreadyAppendedChunkBytes
+=
available
;
alreadyAppendedChunkBytes
+=
available
;
return
Incomplete
;
}
cursor
.
advance
(
size
-
alreadyAppendedChunkBytes
);
//trailing EOL
//
trailing EOL
cursor
.
advance
(
2
);
message
->
body_
.
append
(
chunkData
.
rawText
(),
size
-
alreadyAppendedChunkBytes
);
...
...
@@ -800,8 +798,10 @@ Async::Promise<ssize_t> ResponseWriter::putOnWire(const char *data,
.
then
<
std
::
function
<
Async
::
Promise
<
ssize_t
>
(
ssize_t
)
>
,
std
::
function
<
void
(
std
::
exception_ptr
&
)
>>
(
[
=
](
int
/*l*/
)
{
return
Async
::
Promise
<
ssize_t
>
([
=
](
Async
::
Deferred
<
ssize_t
>
/*deferred*/
)
mutable
{
return
;
});
return
Async
::
Promise
<
ssize_t
>
(
[
=
](
Async
::
Deferred
<
ssize_t
>
/*deferred*/
)
mutable
{
return
;
});
},
[
=
](
std
::
exception_ptr
&
eptr
)
{
...
...
@@ -915,16 +915,16 @@ Private::ParserImpl<Http::Response>::ParserImpl(size_t maxDataSize)
void
Handler
::
onInput
(
const
char
*
buffer
,
size_t
len
,
const
std
::
shared_ptr
<
Tcp
::
Peer
>
&
peer
)
{
auto
&
parser
=
getParser
(
peer
);
auto
parser
=
peer
->
getParser
(
);
try
{
if
(
!
parser
.
feed
(
buffer
,
len
))
{
parser
.
reset
();
if
(
!
parser
->
feed
(
buffer
,
len
))
{
parser
->
reset
();
throw
HttpError
(
Code
::
Request_Entity_Too_Large
,
"Request exceeded maximum buffer size"
);
}
auto
state
=
parser
.
parse
();
auto
&
request
=
parser
.
request
;
auto
state
=
parser
->
parse
();
auto
&
request
=
parser
->
request
;
if
(
state
==
Private
::
State
::
Done
)
{
ResponseWriter
response
(
transport
(),
request
,
this
,
peer
);
...
...
@@ -944,24 +944,24 @@ void Handler::onInput(const char *buffer, size_t len,
}
onRequest
(
request
,
std
::
move
(
response
));
parser
.
reset
();
parser
->
reset
();
}
}
catch
(
const
HttpError
&
err
)
{
ResponseWriter
response
(
transport
(),
parser
.
request
,
this
,
peer
);
ResponseWriter
response
(
transport
(),
parser
->
request
,
this
,
peer
);
response
.
send
(
static_cast
<
Code
>
(
err
.
code
()),
err
.
reason
());
parser
.
reset
();
parser
->
reset
();
}
catch
(
const
std
::
exception
&
e
)
{
ResponseWriter
response
(
transport
(),
parser
.
request
,
this
,
peer
);
ResponseWriter
response
(
transport
(),
parser
->
request
,
this
,
peer
);
response
.
send
(
Code
::
Internal_Server_Error
,
e
.
what
());
parser
.
reset
();
parser
->
reset
();
}
}
void
Handler
::
onConnection
(
const
std
::
shared_ptr
<
Tcp
::
Peer
>
&
peer
)
{
peer
->
putData
(
ParserData
,
std
::
make_shared
<
RequestParser
>
(
maxRequestSize_
));
peer
->
setParser
(
std
::
make_shared
<
RequestParser
>
(
maxRequestSize_
));
}
void
Handler
::
onTimeout
(
const
Request
&
/*request*/
,
...
...
@@ -1000,10 +1000,5 @@ void Handler::setMaxResponseSize(size_t value) { maxResponseSize_ = value; }
size_t
Handler
::
getMaxResponseSize
()
const
{
return
maxResponseSize_
;
}
RequestParser
&
Handler
::
getParser
(
const
std
::
shared_ptr
<
Tcp
::
Peer
>
&
peer
)
const
{
return
static_cast
<
RequestParser
&>
(
*
peer
->
getData
(
ParserData
));
}
}
// namespace Http
}
// namespace Pistache
src/common/peer.cc
View file @
a3c5c68e
...
...
@@ -78,31 +78,11 @@ int Peer::fd() const {
return
fd_
;
}
void
Peer
::
putData
(
std
::
string
name
,
std
::
shared_ptr
<
Http
::
Parser
>
data
)
{
auto
it
=
data_
.
find
(
name
);
if
(
it
!=
std
::
end
(
data_
))
{
throw
std
::
runtime_error
(
"The data already exists"
);
}
data_
.
insert
(
std
::
make_pair
(
std
::
move
(
name
),
std
::
move
(
data
)));
}
std
::
shared_ptr
<
Http
::
Parser
>
Peer
::
getData
(
std
::
string
name
)
const
{
auto
data
=
tryGetData
(
std
::
move
(
name
));
if
(
data
==
nullptr
)
{
throw
std
::
runtime_error
(
"The data does not exist"
);
}
return
data
;
void
Peer
::
setParser
(
std
::
shared_ptr
<
Http
::
RequestParser
>
parser
)
{
parser_
=
parser
;
}
std
::
shared_ptr
<
Http
::
Parser
>
Peer
::
tryGetData
(
std
::
string
(
name
))
const
{
auto
it
=
data_
.
find
(
name
);
if
(
it
==
std
::
end
(
data_
))
return
nullptr
;
return
it
->
second
;
}
std
::
shared_ptr
<
Http
::
RequestParser
>
Peer
::
getParser
()
const
{
return
parser_
;
}
Async
::
Promise
<
ssize_t
>
Peer
::
send
(
const
RawBuffer
&
buffer
,
int
flags
)
{
return
transport
()
->
asyncWrite
(
fd_
,
buffer
,
flags
);
...
...
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