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
534fe58e
Commit
534fe58e
authored
Nov 26, 2020
by
hyperxor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code simplification in RequestParser usage
parent
3eb92d86
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
18 additions
and
46 deletions
+18
-46
include/pistache/http.h
include/pistache/http.h
+0
-1
include/pistache/peer.h
include/pistache/peer.h
+3
-5
src/common/http.cc
src/common/http.cc
+11
-18
src/common/peer.cc
src/common/peer.cc
+4
-22
No files found.
include/pistache/http.h
View file @
534fe58e
...
...
@@ -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 @
534fe58e
...
...
@@ -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 @
534fe58e
...
...
@@ -111,8 +111,6 @@ const HttpMethods httpMethods = {
}
// namespace
static
constexpr
const
char
*
ParserData
=
"__Parser"
;
namespace
Private
{
Step
::
Step
(
Message
*
request
)
:
message
(
request
)
{}
...
...
@@ -915,16 +913,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 +942,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 +998,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 @
534fe58e
...
...
@@ -78,30 +78,12 @@ 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
)));
void
Peer
::
setParser
(
std
::
shared_ptr
<
Http
::
RequestParser
>
parser
)
{
parser_
=
parser
;
}
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
;
}
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
)
{
...
...
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