Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-cmds
Commits
fd9e4610
Commit
fd9e4610
authored
Nov 18, 2014
by
Matt Bell
Committed by
Brian Tiger Chow
Nov 25, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands/http: Allow API requests from whitelisted origins
parent
9acedbbb
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
4 deletions
+15
-4
http/handler.go
http/handler.go
+15
-4
No files found.
http/handler.go
View file @
fd9e4610
...
...
@@ -14,6 +14,7 @@ var log = u.Logger("commands/http")
type
Handler
struct
{
ctx
cmds
.
Context
root
*
cmds
.
Command
origin
string
}
var
ErrNotFound
=
errors
.
New
(
"404 page not found"
)
...
...
@@ -29,13 +30,23 @@ var mimeTypes = map[string]string{
cmds
.
Text
:
"text/plain"
,
}
func
NewHandler
(
ctx
cmds
.
Context
,
root
*
cmds
.
Command
)
*
Handler
{
return
&
Handler
{
ctx
,
root
}
func
NewHandler
(
ctx
cmds
.
Context
,
root
*
cmds
.
Command
,
origin
string
)
*
Handler
{
// allow whitelisted origins (so we can make API requests from the browser)
if
len
(
origin
)
>
0
{
log
.
Info
(
"Allowing API requests from origin: "
+
origin
)
}
return
&
Handler
{
ctx
,
root
,
origin
}
}
func
(
i
Handler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
log
.
Debug
(
"Incoming API request: "
,
r
.
URL
)
if
len
(
i
.
origin
)
>
0
{
w
.
Header
()
.
Set
(
"Access-Control-Allow-Origin"
,
i
.
origin
)
}
w
.
Header
()
.
Set
(
"Access-Control-Allow-Headers"
,
"Content-Type"
)
req
,
err
:=
Parse
(
r
,
i
.
root
)
if
err
!=
nil
{
if
err
==
ErrNotFound
{
...
...
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