Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Suyu
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
many-archive
Suyu
Commits
8dc9f35b
There was an error fetching the commit references. Please try again later.
Commit
8dc9f35b
authored
5 years ago
by
bunnei
Browse files
Options
Downloads
Patches
Plain Diff
web-service: Port citra's updated web_backend code.
parent
883eb1a1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/web_service/web_backend.cpp
+34
-18
34 additions, 18 deletions
src/web_service/web_backend.cpp
src/web_service/web_backend.h
+23
-0
23 additions, 0 deletions
src/web_service/web_backend.h
with
57 additions
and
18 deletions
src/web_service/web_backend.cpp
+
34
−
18
View file @
8dc9f35b
...
...
@@ -2,10 +2,12 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include
<array>
#include
<cstdlib>
#include
<mutex>
#include
<string>
#include
<LUrlParser.h>
#include
<fmt/format.h>
#include
<httplib.h>
#include
"common/common_types.h"
#include
"common/logging/log.h"
...
...
@@ -16,10 +18,10 @@ namespace WebService {
constexpr
std
::
array
<
const
char
,
1
>
API_VERSION
{
'1'
};
constexpr
u32
HTTP_PORT
=
80
;
constexpr
u32
HTTPS_PORT
=
443
;
constexpr
int
HTTP_PORT
=
80
;
constexpr
int
HTTPS_PORT
=
443
;
constexpr
u32
TIMEOUT_SECONDS
=
30
;
constexpr
std
::
size_t
TIMEOUT_SECONDS
=
30
;
struct
Client
::
Impl
{
Impl
(
std
::
string
host
,
std
::
string
username
,
std
::
string
token
)
...
...
@@ -31,8 +33,9 @@ struct Client::Impl {
}
/// A generic function handles POST, GET and DELETE request together
Common
::
WebResult
GenericJson
(
const
std
::
string
&
method
,
const
std
::
string
&
path
,
const
std
::
string
&
data
,
bool
allow_anonymous
)
{
Common
::
WebResult
GenericRequest
(
const
std
::
string
&
method
,
const
std
::
string
&
path
,
const
std
::
string
&
data
,
bool
allow_anonymous
,
const
std
::
string
&
accept
)
{
if
(
jwt
.
empty
())
{
UpdateJWT
();
}
...
...
@@ -43,11 +46,11 @@ struct Client::Impl {
"Credentials needed"
};
}
auto
result
=
Generic
Json
(
method
,
path
,
data
,
jwt
);
auto
result
=
Generic
Request
(
method
,
path
,
data
,
accept
,
jwt
);
if
(
result
.
result_string
==
"401"
)
{
// Try again with new JWT
UpdateJWT
();
result
=
Generic
Json
(
method
,
path
,
data
,
jwt
);
result
=
Generic
Request
(
method
,
path
,
data
,
accept
,
jwt
);
}
return
result
;
...
...
@@ -56,12 +59,13 @@ struct Client::Impl {
/**
* A generic function with explicit authentication method specified
* JWT is used if the jwt parameter is not empty
* username + token is used if jwt is empty but username and token are
not empty
* anonymous if all of jwt, username and token are empty
* username + token is used if jwt is empty but username and token are
*
not empty
anonymous if all of jwt, username and token are empty
*/
Common
::
WebResult
GenericJson
(
const
std
::
string
&
method
,
const
std
::
string
&
path
,
const
std
::
string
&
data
,
const
std
::
string
&
jwt
=
""
,
const
std
::
string
&
username
=
""
,
const
std
::
string
&
token
=
""
)
{
Common
::
WebResult
GenericRequest
(
const
std
::
string
&
method
,
const
std
::
string
&
path
,
const
std
::
string
&
data
,
const
std
::
string
&
accept
,
const
std
::
string
&
jwt
=
""
,
const
std
::
string
&
username
=
""
,
const
std
::
string
&
token
=
""
)
{
if
(
cli
==
nullptr
)
{
auto
parsedUrl
=
LUrlParser
::
clParseURL
::
ParseURL
(
host
);
int
port
;
...
...
@@ -132,8 +136,7 @@ struct Client::Impl {
return
Common
::
WebResult
{
Common
::
WebResult
::
Code
::
WrongContent
,
""
};
}
if
(
content_type
->
second
.
find
(
"application/json"
)
==
std
::
string
::
npos
&&
content_type
->
second
.
find
(
"text/html; charset=utf-8"
)
==
std
::
string
::
npos
)
{
if
(
content_type
->
second
.
find
(
accept
)
==
std
::
string
::
npos
)
{
LOG_ERROR
(
WebService
,
"{} to {} returned wrong content: {}"
,
method
,
host
+
path
,
content_type
->
second
);
return
Common
::
WebResult
{
Common
::
WebResult
::
Code
::
WrongContent
,
"Wrong content"
};
...
...
@@ -147,7 +150,7 @@ struct Client::Impl {
return
;
}
auto
result
=
Generic
Json
(
"POST"
,
"/jwt/internal"
,
""
,
""
,
username
,
token
);
auto
result
=
Generic
Request
(
"POST"
,
"/jwt/internal"
,
""
,
"text/html"
,
""
,
username
,
token
);
if
(
result
.
result_code
!=
Common
::
WebResult
::
Code
::
Success
)
{
LOG_ERROR
(
WebService
,
"UpdateJWT failed"
);
}
else
{
...
...
@@ -180,16 +183,29 @@ Client::~Client() = default;
Common
::
WebResult
Client
::
PostJson
(
const
std
::
string
&
path
,
const
std
::
string
&
data
,
bool
allow_anonymous
)
{
return
impl
->
Generic
Json
(
"POST"
,
path
,
data
,
allow_anonymous
);
return
impl
->
Generic
Request
(
"POST"
,
path
,
data
,
allow_anonymous
,
"application/json"
);
}
Common
::
WebResult
Client
::
GetJson
(
const
std
::
string
&
path
,
bool
allow_anonymous
)
{
return
impl
->
Generic
Json
(
"GET"
,
path
,
""
,
allow_anonymous
);
return
impl
->
Generic
Request
(
"GET"
,
path
,
""
,
allow_anonymous
,
"application/json"
);
}
Common
::
WebResult
Client
::
DeleteJson
(
const
std
::
string
&
path
,
const
std
::
string
&
data
,
bool
allow_anonymous
)
{
return
impl
->
GenericJson
(
"DELETE"
,
path
,
data
,
allow_anonymous
);
return
impl
->
GenericRequest
(
"DELETE"
,
path
,
data
,
allow_anonymous
,
"application/json"
);
}
Common
::
WebResult
Client
::
GetPlain
(
const
std
::
string
&
path
,
bool
allow_anonymous
)
{
return
impl
->
GenericRequest
(
"GET"
,
path
,
""
,
allow_anonymous
,
"text/plain"
);
}
Common
::
WebResult
Client
::
GetImage
(
const
std
::
string
&
path
,
bool
allow_anonymous
)
{
return
impl
->
GenericRequest
(
"GET"
,
path
,
""
,
allow_anonymous
,
"image/png"
);
}
Common
::
WebResult
Client
::
GetExternalJWT
(
const
std
::
string
&
audience
)
{
return
impl
->
GenericRequest
(
"POST"
,
fmt
::
format
(
"/jwt/external/{}"
,
audience
),
""
,
false
,
"text/html"
);
}
}
// namespace WebService
This diff is collapsed.
Click to expand it.
src/web_service/web_backend.h
+
23
−
0
View file @
8dc9f35b
...
...
@@ -46,6 +46,29 @@ public:
Common
::
WebResult
DeleteJson
(
const
std
::
string
&
path
,
const
std
::
string
&
data
,
bool
allow_anonymous
);
/**
* Gets a plain string from the specified path.
* @param path the URL segment after the host address.
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
* @return the result of the request.
*/
Common
::
WebResult
GetPlain
(
const
std
::
string
&
path
,
bool
allow_anonymous
);
/**
* Gets an PNG image from the specified path.
* @param path the URL segment after the host address.
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
* @return the result of the request.
*/
Common
::
WebResult
GetImage
(
const
std
::
string
&
path
,
bool
allow_anonymous
);
/**
* Requests an external JWT for the specific audience provided.
* @param audience the audience of the JWT requested.
* @return the result of the request.
*/
Common
::
WebResult
GetExternalJWT
(
const
std
::
string
&
audience
);
private:
struct
Impl
;
std
::
unique_ptr
<
Impl
>
impl
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment