mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #320 from philips/add-cors-options
feat(server): introduce a cors handler
This commit is contained in:
commit
4520705bdf
@ -2,10 +2,11 @@
|
||||
|
||||
angular.module('etcd', [])
|
||||
|
||||
.factory('EtcdV2', ['$http', function($http) {
|
||||
.factory('EtcdV2', ['$http', '$q', function($http, $q) {
|
||||
var keyPrefix = '/v2/keys/'
|
||||
var statsPrefix = '/v2/stats/'
|
||||
var baseURL = '/v2/'
|
||||
var leaderURL = ''
|
||||
|
||||
delete $http.defaults.headers.common['X-Requested-With'];
|
||||
|
||||
@ -45,20 +46,24 @@ angular.module('etcd', [])
|
||||
};
|
||||
|
||||
self.set = function(keyValue) {
|
||||
return getLeader().then(function(leader) {
|
||||
return $http({
|
||||
url: self.path(),
|
||||
url: leader + self.path(),
|
||||
data: $.param({value: keyValue}),
|
||||
method: 'PUT',
|
||||
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
self.deleteKey = function(keyValue) {
|
||||
return getLeader().then(function(leader) {
|
||||
return $http({
|
||||
url: self.path(),
|
||||
url: leader + self.path(),
|
||||
method: 'DELETE',
|
||||
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return self;
|
||||
@ -79,8 +84,18 @@ angular.module('etcd', [])
|
||||
return self
|
||||
}
|
||||
|
||||
function getLeader() {
|
||||
return newStat('leader').get().then(function(response) {
|
||||
return newKey('/_etcd/machines/' + response.data.leader).get().then(function(response) {
|
||||
// TODO: do something better here p.s. I hate javascript
|
||||
var data = JSON.parse('{"' + decodeURI(response.data.value.replace(/&/g, "\",\"").replace(/=/g,"\":\"")) + '"}');
|
||||
return data.etcd;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
getStat: newStat,
|
||||
getKey: newKey
|
||||
getKey: newKey,
|
||||
}
|
||||
}]);
|
||||
|
@ -94,23 +94,23 @@ angular.module('etcdBrowser', ['ngRoute', 'etcd', 'timeRelative'])
|
||||
|
||||
$scope.saveData = function() {
|
||||
// TODO: fixup etcd to allow for empty values
|
||||
$scope.key.set($scope.singleValue || ' ').success(function (data, status, headers, config) {
|
||||
$scope.key.set($scope.singleValue || ' ').then(function(response) {
|
||||
$scope.save = 'etcd-save-hide';
|
||||
$scope.preview = 'etcd-preview-hide';
|
||||
$scope.back();
|
||||
$scope.writingNew = false;
|
||||
}).error(function (data, status, headers, config) {
|
||||
}, function (response) {
|
||||
$scope.showSaveError(data.message);
|
||||
});
|
||||
};
|
||||
|
||||
$scope.deleteKey = function() {
|
||||
$scope.key.deleteKey().success(function (data, status, headers, config) {
|
||||
$scope.key.deleteKey().then(function(response) {
|
||||
//TODO: remove loader
|
||||
$scope.save = 'etcd-save-hide';
|
||||
$scope.preview = 'etcd-preview-hide';
|
||||
$scope.back();
|
||||
}).error(function (data, status, headers, config) {
|
||||
}, function (response) {
|
||||
//TODO: remove loader
|
||||
//show errors
|
||||
$scope.showBrowseError('Could not delete the key');
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -8,7 +8,7 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var _views_stats_html = "\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xd4\x56\x4b\x8f\xdb\x36\x10\xbe\xe7\x57\x10\xec\xc1\x09\x20\xc9\x7a\xf8\xb1\x32\x2c\x03\x4d\x0b\xf4\xd2\xed\xa1\x29\x0a\x14\x45\x0f\x34\x45\x5b\xc4\xd2\x94\x4a\xd2\xd6\x3a\xae\xff\x7b\x87\xa2\xec\xae\x64\xc5\xdd\xa0\x7b\xe9\x3e\x40\x71\x38\xf3\xcd\xa7\xe1\xc7\xa1\x96\x39\x3f\x20\x2a\x88\xd6\x19\x66\x86\xe6\x3e\x2d\xa5\x21\x5c\x32\x85\x9a\xa9\x36\xc4\x68\x74\x3a\xd1\x52\xec\x77\x52\x9f\xcf\xf0\x6c\xc8\x5a\xb0\x5f\xb9\xe6\x6b\x2e\xb8\x39\x9e\xcf\x78\xf5\x0e\xc1\xcf\x0d\xd6\xba\xcc\x8f\xed\xda\xe0\xfa\xa6\x54\x3b\x62\x7c\xcd\x04\xa3\xa6\x54\x2f\x5c\x07\xdd\x2f\x7e\x3e\x37\x6c\x87\xba\xa6\xad\x22\x55\x81\x91\xdc\xfa\x54\x70\xfa\x94\x61\x5d\x94\xf5\x0f\xd6\xf8\xfe\x43\x0f\xb7\xc1\xd6\x87\x2d\x3a\x30\xa5\x79\x29\x33\x1c\x05\x11\x46\xcf\x3b\x21\x21\x53\x61\x4c\xb5\x18\x8f\xeb\xba\x0e\xea\x24\x28\xd5\x76\x1c\x87\x61\x38\x06\xff\xd6\x65\xf1\x2c\xb8\x7c\x1a\x72\x8c\xd2\x34\x1d\x37\xab\xe0\x9a\xe1\xb0\x7a\xc6\xe8\xe8\xc6\x1b\x02\x95\x62\x9a\xa9\x03\xfb\x56\x57\xf0\x02\x3f\x13\xc3\xcb\x0c\x3f\x3f\x72\xf9\x1b\xfc\x63\x74\xe0\xac\xfe\x58\x5a\x10\x14\xa2\x99\xfd\x0b\xc2\x70\x8e\x11\x93\xb6\xf6\xfe\x9a\xd0\xa7\xad\x2a\xf7\x32\xcf\xb0\x64\x35\xea\x79\x01\xcf\x85\xae\x08\x65\x19\xbe\xe4\x19\xaa\x41\x45\x4c\x81\x36\x5c\x88\x0c\x7f\x13\x7d\x0f\xbf\x1f\x31\x02\xc0\xc7\x24\xf4\x01\x28\x8d\xa8\x1f\xcd\x82\x59\x1c\x79\xa1\x9f\x58\xc3\xc4\x8b\x92\x60\x32\x9f\x5c\x66\x6e\xa0\xa1\xd7\xba\xb9\x55\xaf\xb3\xda\x0e\x37\xc9\xb5\xb3\xfb\x9d\x98\x16\xf8\xbb\xd9\x35\x5b\xf2\x90\x78\x93\x06\xdd\x51\xf2\x2e\xdc\x3e\x23\xa0\xe9\x4d\xe6\x41\x9c\xa4\xd4\x4f\x83\x69\x94\x02\xcd\xc8\xce\xa7\xfe\x3c\x98\x47\xb3\xcb\xc4\x0d\x37\x04\x3e\xc5\x61\x30\x79\x00\xd2\x71\x30\x9f\x3d\x00\x6e\xfb\x44\x5b\x2c\xcf\xc5\x79\x0d\xd6\x65\xe2\x86\x4f\x89\xf3\x71\xd9\xbd\x2b\x8f\xcf\x78\x3c\x50\x65\x2b\x9d\xd5\xbb\xae\xb2\xc7\x20\xed\xff\x20\xf6\xe6\x00\xf6\xc5\xfe\x8b\x35\xfe\xff\xc5\x3e\x4d\x83\x49\x3a\xb3\xc3\x34\x4c\xee\x0a\xbe\xe7\xf9\x4a\xd1\x2b\x60\xd0\x10\x76\x74\x71\xff\x00\xd4\x3c\x37\x45\x86\x1d\x38\x46\x05\xe3\xdb\xc2\x40\xd5\x92\x20\x8a\xa2\xc1\x0d\xee\x22\xc6\xe0\x68\x03\xdf\x18\x16\x0e\x41\x92\xc6\x6f\x00\xeb\xe4\x78\x47\x8d\xfd\x69\x5f\x98\xae\xd3\xf6\x20\x8a\x78\xf5\x48\x68\x01\xf7\x06\xfa\x91\x18\x26\xe9\x71\x39\x06\xdb\x7d\x89\x37\x48\xff\x5c\x38\x18\x71\xd8\x5d\xe1\xc2\xfb\x09\xbe\x8e\xa3\xe0\xda\xdc\xa3\x08\xcb\x03\xfc\x9a\x63\x85\x28\x13\xa2\x22\x79\xce\xe5\xb6\x29\xbe\x9d\x5b\x5d\xb5\xf3\x7e\x4c\xc1\x48\x3e\x50\x66\x93\x77\x08\x49\xb2\x63\xbe\x75\x85\xd7\xbc\xd2\xf8\x09\x8c\xcb\xb1\x79\x45\xf8\xb5\x28\xd7\xe2\xf6\xa3\xc0\x72\xcb\x64\x69\xec\x0d\x3c\x04\xaf\x6c\xf3\x50\xac\x62\x04\xd4\xb2\x6b\xf9\x70\x89\xda\x47\x3d\x70\x72\x2e\xbc\x20\x50\xd7\xdc\xd0\x02\xd9\x6e\x72\x32\x6a\xcf\x16\x23\xd1\xbc\xda\xc8\x43\x1b\x22\x34\x5b\xa0\xd1\xa6\x14\xa2\xac\xc1\x74\xfe\xdd\xad\xd9\x97\x45\x59\x76\xc9\x10\xd8\x8a\xfc\xf1\x85\x34\x4d\x2a\xbb\xa7\xd7\x5c\x7e\x5d\x30\xc8\x26\xda\x0a\x9e\x4e\x2f\x61\xce\xe7\x25\x6c\x90\xec\x54\xac\x5d\xf7\xcd\xb1\x82\x36\xf0\xde\x05\x7e\x00\xf5\x83\xe3\x6a\xa0\xff\xde\x49\x9d\xb3\x0d\xd9\x0b\x73\x9b\xf4\xcb\x28\xc3\xbb\xda\x56\xf0\x5f\x12\x77\x2e\x81\x3f\xf7\x44\x31\xd7\xe8\xc1\xba\x40\xa7\xd1\x0b\x3b\x1c\x20\xc6\xe4\x68\x71\xad\x69\x2b\x93\x80\xee\x95\x62\xd2\xa0\x25\x8a\xa7\x1e\xea\x84\x94\x8a\xc8\x2d\xbb\x1b\x33\x0b\x7b\x31\x8a\xe5\x77\x02\x56\x19\x44\xc0\x27\xe0\x6b\x8a\x3a\xa0\x69\xff\x40\xc4\x9e\xbd\xdc\xd2\x7e\x82\xbf\x90\xdc\xef\xd6\x4c\x2d\x22\x04\x9f\x9f\x3b\xfd\xd5\x75\x07\xab\xba\x39\x2d\xb7\x27\x03\x8c\xb6\x01\x0c\x76\x99\xf6\xb1\x1d\xfe\x0e\x00\x00\xff\xff\xe8\x36\xb1\x2a\x35\x0b\x00\x00"
|
||||
var _views_stats_html = "\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xd4\x56\x4b\x8f\xdb\x36\x10\xbe\xe7\x57\x10\xec\xc1\x09\x20\xc9\x7a\xf8\xb1\x32\x2c\x03\x4d\x0b\xf4\xd2\x14\x45\x53\x14\x28\x8a\x1e\x68\x8a\xb6\x88\xa5\x29\x95\xa4\xad\x75\x5c\xff\xf7\x0e\x45\xd9\x8d\x64\x79\xbb\x41\xf7\x92\x7d\x80\xe2\x70\xe6\x9b\x8f\xc3\x6f\x28\x2d\x73\x7e\x40\x54\x10\xad\x33\xcc\x0c\xcd\x7d\x5a\x4a\x43\xb8\x64\x0a\x35\x53\x6d\x88\xd1\xe8\x74\xa2\xa5\xd8\xef\xa4\x3e\x9f\xe1\xd9\x90\xb5\x60\xbf\x71\xcd\xd7\x5c\x70\x73\x3c\x9f\xf1\xea\x0d\x82\x9f\x1b\xac\x75\x99\x1f\xdb\xb5\xc1\xf5\x4d\xa9\x76\xc4\xf8\x9a\x09\x46\x4d\xa9\x3e\x73\x1d\x74\xbf\xf8\xf9\xdc\xb0\x1d\xea\x9a\xb6\x8a\x54\x05\x46\x72\xeb\x53\xc1\xe9\x63\x86\x75\x51\xd6\x3f\x58\xe3\xdb\x77\x3d\xdc\x06\x5b\x1f\xb6\xe8\xc0\x94\xe6\xa5\xcc\x70\x14\x44\x18\x3d\xed\x84\x84\x4c\x85\x31\xd5\x62\x3c\xae\xeb\x3a\xa8\x93\xa0\x54\xdb\x71\x1c\x86\xe1\x18\xfc\x5b\x97\xc5\x93\xe0\xf2\x71\xc8\x31\x4a\xd3\x74\xdc\xac\x82\x6b\x86\xc3\xea\x09\xa3\xa3\x1b\x6f\x08\x54\x8a\x69\xa6\x0e\xec\x5b\x5d\xc1\x06\x7e\x21\x86\x97\x19\x7e\xfa\xc0\xe5\xef\xf0\x8f\xd1\x81\xb3\xfa\x7d\x69\x41\x50\x88\x66\xf6\x2f\x08\xc3\x39\x46\x4c\xda\xda\xfb\x6b\x42\x1f\xb7\xaa\xdc\xcb\x3c\xc3\x92\xd5\xa8\xe7\x05\x3c\x17\xba\x22\x94\x65\xf8\x92\x67\xa8\x06\x15\x31\x05\xda\x70\x21\x32\xfc\x4d\xf4\x3d\xfc\xbe\xc7\x08\x00\x3f\x24\xa1\x0f\x40\x69\x44\xfd\x68\x16\xcc\xe2\xc8\x0b\xfd\xc4\x1a\x26\x5e\x94\x04\x93\xf9\xe4\x32\x73\x03\x0d\xbd\xd6\xcd\xad\x7a\x9d\xd5\x76\xb8\x49\xae\x9d\xdd\xef\xc4\xb4\xc0\xdf\xcd\xae\xd9\x92\x87\xc4\x9b\x34\xe8\x8e\x92\x77\xe1\xf6\x09\x01\x4d\x6f\x32\x0f\xe2\x24\xa5\x7e\x1a\x4c\xa3\x14\x68\x46\x76\x3e\xf5\xe7\xc1\x3c\x9a\x5d\x26\x6e\xb8\x21\xf0\x31\x0e\x83\xc9\x03\x90\x8e\x83\xf9\xec\x01\x70\xdb\x27\xda\x62\x79\x2e\xce\x6b\xb0\x2e\x13\x37\x7c\x4c\x9c\x8f\xcb\xee\x5d\x79\x7c\xc2\xe3\x81\x2a\x5b\xe9\xac\xde\x74\x95\x3d\x06\x69\xff\x0f\xb1\x37\x0d\xd8\x17\xfb\xaf\xd6\xf8\xf5\x8b\x7d\x9a\x06\x93\x74\x66\x87\x69\x98\x3c\x2b\xf8\x9e\xe7\x0b\x45\xaf\x80\x41\x43\xd8\xd1\xc5\xfd\x06\xa8\x79\x6e\x8a\x0c\x3b\x70\x8c\x0a\xc6\xb7\x85\x81\xaa\x25\x41\x14\x45\x83\x07\xdc\x45\x8c\xc1\xd1\x06\xbe\x32\x2c\x34\x41\x92\xc6\xaf\x00\xeb\xe4\xf8\x8c\x1a\xfb\xd3\xbe\x30\xdd\x4d\xdb\x83\x28\xe2\xd5\xcf\x0c\xde\x19\x3f\x12\xc3\x24\x3d\x2e\xc7\x60\x78\x5e\xdf\x0d\xcc\xbf\x6f\x1b\x8c\x38\x1c\xad\x70\xe1\x7d\xf4\x2f\x23\x28\xb8\x36\x77\xf9\xc1\xda\x00\xb9\xa6\xa1\x10\x65\x42\x54\x24\xcf\xb9\xdc\x36\x65\xb7\x73\xab\xa8\x76\xde\x8f\x29\x18\xc9\x07\x0a\x6c\xf2\x0e\x1b\x49\x76\xcc\xb7\xae\xb0\x47\xc7\xe1\x27\xb0\x2c\xc7\xe6\x05\xb1\xd7\x72\x5c\xcb\xda\x8f\x02\xcb\x2d\x8d\xa5\xb1\x2f\xde\x21\x78\x65\xef\x0c\xc5\x2a\x46\x40\x24\x95\x25\xc3\x25\xb2\xa3\x1e\x68\x95\x0b\x23\x08\xd1\x35\x37\xb4\x40\xf6\xfa\x38\x19\xb5\x67\x8b\x91\x68\x76\x34\xf2\xd0\x86\x08\xcd\x16\x68\xb4\x29\x85\x28\x6b\x30\x9d\xff\x70\x6b\x76\x9b\x28\xcb\x1a\xf8\xc0\x56\xe1\xcf\x3b\x39\x9a\x3c\xf6\x10\xaf\x89\xfc\xba\x60\x90\x4a\xb4\x55\x3b\x9d\xae\x18\xe7\xf3\x12\x4e\x44\x76\xaa\x64\x17\x7d\x73\xac\xa0\xdd\xdf\xba\x90\x77\xa0\x72\xf0\x5a\x0d\xdc\xb3\xcf\x24\xcd\xd9\x86\xec\x85\xe9\xa5\xbb\x0f\x31\x7c\x86\x6d\xd5\xfe\x23\x6b\xe7\xa6\xff\x6b\x4f\x14\x73\xb7\x39\x58\x17\xe8\x34\xfa\xcc\x0e\x8d\xc2\x98\x1c\x2d\x5c\x1d\x5b\x45\x04\x74\xaf\x14\x93\x06\x2d\x51\x3c\xf5\x50\xc7\xbf\x54\x44\x6e\xd9\xfd\x80\x59\xd8\x0b\x50\x2c\xbf\xe7\xbd\xca\xc0\x1d\x3e\xef\x5e\x52\xc8\x01\xe1\xfa\x07\x22\xf6\xec\x7a\x80\x7d\xf4\xbf\x91\xdc\xef\xd6\x4c\x2d\x22\x04\xdf\x95\x3b\xfd\xc5\xb5\x06\xab\xba\xe9\x87\x5b\xed\x83\xd1\xf6\xf7\xe0\x0d\xd2\x3e\xb6\xc3\x3f\x01\x00\x00\xff\xff\x0a\x0c\x99\x1a\x0e\x0b\x00\x00"
|
||||
|
||||
// views_stats_html returns raw, uncompressed file data.
|
||||
func views_stats_html() []byte {
|
||||
|
78
server/cors_handler.go
Normal file
78
server/cors_handler.go
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
Copyright 2013 CoreOS Inc.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type corsHandler struct {
|
||||
router *mux.Router
|
||||
corsOrigins map[string]bool
|
||||
}
|
||||
|
||||
// AllowOrigins sets a comma-delimited list of origins that are allowed.
|
||||
func (s *corsHandler) AllowOrigins(origins []string) error {
|
||||
// Construct a lookup of all origins.
|
||||
m := make(map[string]bool)
|
||||
for _, v := range origins {
|
||||
if v != "*" {
|
||||
if _, err := url.Parse(v); err != nil {
|
||||
return fmt.Errorf("Invalid CORS origin: %s", err)
|
||||
}
|
||||
}
|
||||
m[v] = true
|
||||
}
|
||||
s.corsOrigins = m
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// OriginAllowed determines whether the server will allow a given CORS origin.
|
||||
func (c *corsHandler) OriginAllowed(origin string) bool {
|
||||
return c.corsOrigins["*"] || c.corsOrigins[origin]
|
||||
}
|
||||
|
||||
// addHeader adds the correct cors headers given an origin
|
||||
func (h *corsHandler) addHeader(w http.ResponseWriter, origin string) {
|
||||
w.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||||
w.Header().Add("Access-Control-Allow-Origin", origin)
|
||||
}
|
||||
|
||||
// ServeHTTP adds the correct CORS headers based on the origin and returns immediatly
|
||||
// with a 200 OK if the method is OPTIONS.
|
||||
func (h *corsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
// Write CORS header.
|
||||
if h.OriginAllowed("*") {
|
||||
h.addHeader(w, "*")
|
||||
} else if origin := req.Header.Get("Origin"); h.OriginAllowed(origin) {
|
||||
h.addHeader(w, origin)
|
||||
}
|
||||
|
||||
if req.Method == "OPTIONS" {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
h.router.ServeHTTP(w, req)
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -32,14 +31,18 @@ type Server struct {
|
||||
url string
|
||||
tlsConf *TLSConfig
|
||||
tlsInfo *TLSInfo
|
||||
corsOrigins map[string]bool
|
||||
router *mux.Router
|
||||
corsHandler *corsHandler
|
||||
}
|
||||
|
||||
// Creates a new Server.
|
||||
func New(name string, urlStr string, bindAddr string, tlsConf *TLSConfig, tlsInfo *TLSInfo, peerServer *PeerServer, registry *Registry, store store.Store) *Server {
|
||||
r := mux.NewRouter()
|
||||
cors := &corsHandler{router: r}
|
||||
|
||||
s := &Server{
|
||||
Server: http.Server{
|
||||
Handler: mux.NewRouter(),
|
||||
Handler: cors,
|
||||
TLSConfig: &tlsConf.Server,
|
||||
Addr: bindAddr,
|
||||
},
|
||||
@ -50,6 +53,8 @@ func New(name string, urlStr string, bindAddr string, tlsConf *TLSConfig, tlsInf
|
||||
tlsConf: tlsConf,
|
||||
tlsInfo: tlsInfo,
|
||||
peerServer: peerServer,
|
||||
router: r,
|
||||
corsHandler: cors,
|
||||
}
|
||||
|
||||
// Install the routes.
|
||||
@ -124,7 +129,7 @@ func (s *Server) installV2() {
|
||||
}
|
||||
|
||||
func (s *Server) installMod() {
|
||||
r := s.Handler.(*mux.Router)
|
||||
r := s.router
|
||||
r.PathPrefix("/mod").Handler(http.StripPrefix("/mod", mod.HttpHandler()))
|
||||
}
|
||||
|
||||
@ -144,20 +149,13 @@ func (s *Server) handleFuncV2(path string, f func(http.ResponseWriter, *http.Req
|
||||
|
||||
// Adds a server handler to the router.
|
||||
func (s *Server) handleFunc(path string, f func(http.ResponseWriter, *http.Request) error) *mux.Route {
|
||||
r := s.Handler.(*mux.Router)
|
||||
r := s.router
|
||||
|
||||
// Wrap the standard HandleFunc interface to pass in the server reference.
|
||||
return r.HandleFunc(path, func(w http.ResponseWriter, req *http.Request) {
|
||||
// Log request.
|
||||
log.Debugf("[recv] %s %s %s [%s]", req.Method, s.url, req.URL.Path, req.RemoteAddr)
|
||||
|
||||
// Write CORS header.
|
||||
if s.OriginAllowed("*") {
|
||||
w.Header().Add("Access-Control-Allow-Origin", "*")
|
||||
} else if origin := req.Header.Get("Origin"); s.OriginAllowed(origin) {
|
||||
w.Header().Add("Access-Control-Allow-Origin", origin)
|
||||
}
|
||||
|
||||
// Execute handler function and return error if necessary.
|
||||
if err := f(w, req); err != nil {
|
||||
if etcdErr, ok := err.(*etcdErr.Error); ok {
|
||||
@ -302,26 +300,14 @@ func (s *Server) Dispatch(c raft.Command, w http.ResponseWriter, req *http.Reque
|
||||
}
|
||||
}
|
||||
|
||||
// Sets a comma-delimited list of origins that are allowed.
|
||||
func (s *Server) AllowOrigins(origins []string) error {
|
||||
// Construct a lookup of all origins.
|
||||
m := make(map[string]bool)
|
||||
for _, v := range origins {
|
||||
if v != "*" {
|
||||
if _, err := url.Parse(v); err != nil {
|
||||
return fmt.Errorf("Invalid CORS origin: %s", err)
|
||||
}
|
||||
}
|
||||
m[v] = true
|
||||
}
|
||||
s.corsOrigins = m
|
||||
|
||||
return nil
|
||||
// OriginAllowed determines whether the server will allow a given CORS origin.
|
||||
func (s *Server) OriginAllowed(origin string) bool {
|
||||
return s.corsHandler.OriginAllowed(origin)
|
||||
}
|
||||
|
||||
// Determines whether the server will allow a given CORS origin.
|
||||
func (s *Server) OriginAllowed(origin string) bool {
|
||||
return s.corsOrigins["*"] || s.corsOrigins[origin]
|
||||
// AllowOrigins sets a comma-delimited list of origins that are allowed.
|
||||
func (s *Server) AllowOrigins(origins []string) error {
|
||||
return s.corsHandler.AllowOrigins(origins)
|
||||
}
|
||||
|
||||
// Handler to return the current version of etcd.
|
||||
|
Loading…
x
Reference in New Issue
Block a user