Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
galene
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
nexedi
galene
Commits
cb7a087e
Commit
cb7a087e
authored
Aug 15, 2024
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use mime.ParseMediaType instead of our version.
parent
1bb71725
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
33 deletions
+15
-33
webserver/api.go
webserver/api.go
+11
-12
webserver/api_test.go
webserver/api_test.go
+4
-2
webserver/webserver_test.go
webserver/webserver_test.go
+0
-19
No files found.
webserver/api.go
View file @
cb7a087e
...
@@ -8,6 +8,7 @@ import (
...
@@ -8,6 +8,7 @@ import (
"encoding/json"
"encoding/json"
"errors"
"errors"
"io"
"io"
"mime"
"net/http"
"net/http"
"os"
"os"
"strings"
"strings"
...
@@ -19,10 +20,6 @@ import (
...
@@ -19,10 +20,6 @@ import (
"github.com/jech/galene/token"
"github.com/jech/galene/token"
)
)
func
parseContentType
(
ctype
string
)
string
{
return
strings
.
Trim
(
strings
.
Split
(
ctype
,
";"
)[
0
],
" "
)
}
// checkAdmin checks whether the client authentifies as an administrator
// checkAdmin checks whether the client authentifies as an administrator
func
checkAdmin
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
bool
{
func
checkAdmin
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
bool
{
username
,
password
,
ok
:=
r
.
BasicAuth
()
username
,
password
,
ok
:=
r
.
BasicAuth
()
...
@@ -73,8 +70,8 @@ func sendJSON(w http.ResponseWriter, r *http.Request, v any) {
...
@@ -73,8 +70,8 @@ func sendJSON(w http.ResponseWriter, r *http.Request, v any) {
}
}
func
getText
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
([]
byte
,
bool
)
{
func
getText
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
([]
byte
,
bool
)
{
ctype
:=
parseContent
Type
(
r
.
Header
.
Get
(
"Content-Type"
))
ctype
,
_
,
err
:=
mime
.
ParseMedia
Type
(
r
.
Header
.
Get
(
"Content-Type"
))
if
!
strings
.
EqualFold
(
ctype
,
"text/plain"
)
{
if
err
!=
nil
||
!
strings
.
EqualFold
(
ctype
,
"text/plain"
)
{
w
.
Header
()
.
Set
(
"Accept"
,
"text/plain"
)
w
.
Header
()
.
Set
(
"Accept"
,
"text/plain"
)
http
.
Error
(
w
,
"unsupported content type"
,
http
.
Error
(
w
,
"unsupported content type"
,
http
.
StatusUnsupportedMediaType
)
http
.
StatusUnsupportedMediaType
)
...
@@ -91,8 +88,8 @@ func getText(w http.ResponseWriter, r *http.Request) ([]byte, bool) {
...
@@ -91,8 +88,8 @@ func getText(w http.ResponseWriter, r *http.Request) ([]byte, bool) {
}
}
func
getJSON
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
v
any
)
bool
{
func
getJSON
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
v
any
)
bool
{
ctype
:=
parseContent
Type
(
r
.
Header
.
Get
(
"Content-Type"
))
ctype
,
_
,
err
:=
mime
.
ParseMedia
Type
(
r
.
Header
.
Get
(
"Content-Type"
))
if
!
strings
.
EqualFold
(
ctype
,
"application/json"
)
{
if
err
!=
nil
||
!
strings
.
EqualFold
(
ctype
,
"application/json"
)
{
w
.
Header
()
.
Set
(
"Accept"
,
"application/json"
)
w
.
Header
()
.
Set
(
"Accept"
,
"application/json"
)
http
.
Error
(
w
,
"unsupported content type"
,
http
.
Error
(
w
,
"unsupported content type"
,
http
.
StatusUnsupportedMediaType
)
http
.
StatusUnsupportedMediaType
)
...
@@ -100,7 +97,7 @@ func getJSON(w http.ResponseWriter, r *http.Request, v any) bool {
...
@@ -100,7 +97,7 @@ func getJSON(w http.ResponseWriter, r *http.Request, v any) bool {
}
}
d
:=
json
.
NewDecoder
(
r
.
Body
)
d
:=
json
.
NewDecoder
(
r
.
Body
)
err
:
=
d
.
Decode
(
v
)
err
=
d
.
Decode
(
v
)
if
err
!=
nil
{
if
err
!=
nil
{
httpError
(
w
,
err
)
httpError
(
w
,
err
)
return
true
return
true
...
@@ -458,8 +455,10 @@ func keysHandler(w http.ResponseWriter, r *http.Request, g string) {
...
@@ -458,8 +455,10 @@ func keysHandler(w http.ResponseWriter, r *http.Request, g string) {
if
r
.
Method
==
"PUT"
{
if
r
.
Method
==
"PUT"
{
// cannot use getJSON due to the weird content-type
// cannot use getJSON due to the weird content-type
ctype
:=
parseContentType
(
r
.
Header
.
Get
(
"Content-Type"
))
ctype
,
_
,
err
:=
if
!
strings
.
EqualFold
(
ctype
,
"application/jwk-set+json"
)
{
mime
.
ParseMediaType
(
r
.
Header
.
Get
(
"Content-Type"
))
if
err
!=
nil
||
!
strings
.
EqualFold
(
ctype
,
"application/jwk-set+json"
)
{
w
.
Header
()
.
Set
(
"Accept"
,
"application/jwk-set+json"
)
w
.
Header
()
.
Set
(
"Accept"
,
"application/jwk-set+json"
)
http
.
Error
(
w
,
"unsupported content type"
,
http
.
Error
(
w
,
"unsupported content type"
,
http
.
StatusUnsupportedMediaType
)
http
.
StatusUnsupportedMediaType
)
...
@@ -467,7 +466,7 @@ func keysHandler(w http.ResponseWriter, r *http.Request, g string) {
...
@@ -467,7 +466,7 @@ func keysHandler(w http.ResponseWriter, r *http.Request, g string) {
}
}
d
:=
json
.
NewDecoder
(
r
.
Body
)
d
:=
json
.
NewDecoder
(
r
.
Body
)
var
keys
jwkset
var
keys
jwkset
err
:
=
d
.
Decode
(
&
keys
)
err
=
d
.
Decode
(
&
keys
)
if
err
!=
nil
{
if
err
!=
nil
{
httpError
(
w
,
err
)
httpError
(
w
,
err
)
return
return
...
...
webserver/api_test.go
View file @
cb7a087e
...
@@ -3,6 +3,7 @@ package webserver
...
@@ -3,6 +3,7 @@ package webserver
import
(
import
(
"errors"
"errors"
"fmt"
"fmt"
"mime"
"os"
"os"
"reflect"
"reflect"
"strings"
"strings"
...
@@ -104,8 +105,9 @@ func TestApi(t *testing.T) {
...
@@ -104,8 +105,9 @@ func TestApi(t *testing.T) {
if
resp
.
StatusCode
!=
http
.
StatusOK
{
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
fmt
.
Errorf
(
"Status is %v"
,
resp
.
StatusCode
)
return
fmt
.
Errorf
(
"Status is %v"
,
resp
.
StatusCode
)
}
}
ctype
:=
parseContentType
(
resp
.
Header
.
Get
(
"Content-Type"
))
ctype
,
_
,
err
:=
if
!
strings
.
EqualFold
(
ctype
,
"application/json"
)
{
mime
.
ParseMediaType
(
resp
.
Header
.
Get
(
"Content-Type"
))
if
err
!=
nil
||
!
strings
.
EqualFold
(
ctype
,
"application/json"
)
{
return
errors
.
New
(
"Unexpected content-type"
)
return
errors
.
New
(
"Unexpected content-type"
)
}
}
d
:=
json
.
NewDecoder
(
resp
.
Body
)
d
:=
json
.
NewDecoder
(
resp
.
Body
)
...
...
webserver/webserver_test.go
View file @
cb7a087e
...
@@ -124,25 +124,6 @@ func TestParseSplit(t *testing.T) {
...
@@ -124,25 +124,6 @@ func TestParseSplit(t *testing.T) {
}
}
}
}
func
TestParseContentType
(
t
*
testing
.
T
)
{
a
:=
[]
struct
{
a
,
b
string
}{
{
""
,
""
},
{
"text/plain"
,
"text/plain"
},
{
"text/plain;charset=utf-8"
,
"text/plain"
},
{
"text/plain; charset=utf-8"
,
"text/plain"
},
{
"text/plain ; charset=utf-8"
,
"text/plain"
},
}
for
_
,
ab
:=
range
a
{
b
:=
parseContentType
(
ab
.
a
)
if
b
!=
ab
.
b
{
t
.
Errorf
(
"Content type %v, got %v, expected %v"
,
ab
.
a
,
b
,
ab
.
b
,
)
}
}
}
func
TestParseBearerToken
(
t
*
testing
.
T
)
{
func
TestParseBearerToken
(
t
*
testing
.
T
)
{
a
:=
[]
struct
{
a
,
b
string
}{
a
:=
[]
struct
{
a
,
b
string
}{
{
""
,
""
},
{
""
,
""
},
...
...
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