ducky/devices
ducky/devices/vendor/bitbucket.org/ww/goautoneg/autoneg.go
Validate device creation. Update our uuid package to the latest, which is now based on the GitHub fork instead of the Google Code. Also, update our api package to its latest version, which now needs the pqarrays package as a dependency. We fleshed out the validateDeviceCreation. We now pass in the scopes we have (for broad access control) and the user ID (for fine-grained access control). This helper returns the first error it encounters, though it should probably return a slice so we can return multiple errors all at once. Before we even decode the request to create a Device, let's check if the user is even logged in. If we can't ascertain that or they're not, there's no point in even consuming the memory necessary to read the request, because we know we're not going to use it anyways. Finally actually validate the devices we're creating, and return an appropriate error for each error we can get. Also, the api.CheckScopes helper function now takes the scopes passed in as a string slice, and we have an api.GetScopes helper function to retrieve the scopes associated with the request. Let's not keep parsing that. We need two new scopes to control access for device creation; ScopeImport lets users import devices in and is pretty much admin access. ScopeCreateOtherUserDevices allows a user to create Devices that are owned by another user.
| paddy@15 | 1 /* |
| paddy@15 | 2 HTTP Content-Type Autonegotiation. |
| paddy@15 | 3 |
| paddy@15 | 4 The functions in this package implement the behaviour specified in |
| paddy@15 | 5 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html |
| paddy@15 | 6 |
| paddy@15 | 7 Copyright (c) 2011, Open Knowledge Foundation Ltd. |
| paddy@15 | 8 All rights reserved. |
| paddy@15 | 9 |
| paddy@15 | 10 Redistribution and use in source and binary forms, with or without |
| paddy@15 | 11 modification, are permitted provided that the following conditions are |
| paddy@15 | 12 met: |
| paddy@15 | 13 |
| paddy@15 | 14 Redistributions of source code must retain the above copyright |
| paddy@15 | 15 notice, this list of conditions and the following disclaimer. |
| paddy@15 | 16 |
| paddy@15 | 17 Redistributions in binary form must reproduce the above copyright |
| paddy@15 | 18 notice, this list of conditions and the following disclaimer in |
| paddy@15 | 19 the documentation and/or other materials provided with the |
| paddy@15 | 20 distribution. |
| paddy@15 | 21 |
| paddy@15 | 22 Neither the name of the Open Knowledge Foundation Ltd. nor the |
| paddy@15 | 23 names of its contributors may be used to endorse or promote |
| paddy@15 | 24 products derived from this software without specific prior written |
| paddy@15 | 25 permission. |
| paddy@15 | 26 |
| paddy@15 | 27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| paddy@15 | 28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| paddy@15 | 29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| paddy@15 | 30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| paddy@15 | 31 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| paddy@15 | 32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| paddy@15 | 33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| paddy@15 | 34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| paddy@15 | 35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| paddy@15 | 36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| paddy@15 | 37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| paddy@15 | 38 |
| paddy@15 | 39 |
| paddy@15 | 40 */ |
| paddy@15 | 41 package goautoneg |
| paddy@15 | 42 |
| paddy@15 | 43 import ( |
| paddy@15 | 44 "sort" |
| paddy@15 | 45 "strconv" |
| paddy@15 | 46 "strings" |
| paddy@15 | 47 ) |
| paddy@15 | 48 |
| paddy@15 | 49 // Structure to represent a clause in an HTTP Accept Header |
| paddy@15 | 50 type Accept struct { |
| paddy@15 | 51 Type, SubType string |
| paddy@15 | 52 Q float64 |
| paddy@15 | 53 Params map[string]string |
| paddy@15 | 54 } |
| paddy@15 | 55 |
| paddy@15 | 56 // For internal use, so that we can use the sort interface |
| paddy@15 | 57 type accept_slice []Accept |
| paddy@15 | 58 |
| paddy@15 | 59 func (accept accept_slice) Len() int { |
| paddy@15 | 60 slice := []Accept(accept) |
| paddy@15 | 61 return len(slice) |
| paddy@15 | 62 } |
| paddy@15 | 63 |
| paddy@15 | 64 func (accept accept_slice) Less(i, j int) bool { |
| paddy@15 | 65 slice := []Accept(accept) |
| paddy@15 | 66 ai, aj := slice[i], slice[j] |
| paddy@15 | 67 if ai.Q > aj.Q { |
| paddy@15 | 68 return true |
| paddy@15 | 69 } |
| paddy@15 | 70 if ai.Type != "*" && aj.Type == "*" { |
| paddy@15 | 71 return true |
| paddy@15 | 72 } |
| paddy@15 | 73 if ai.SubType != "*" && aj.SubType == "*" { |
| paddy@15 | 74 return true |
| paddy@15 | 75 } |
| paddy@15 | 76 return false |
| paddy@15 | 77 } |
| paddy@15 | 78 |
| paddy@15 | 79 func (accept accept_slice) Swap(i, j int) { |
| paddy@15 | 80 slice := []Accept(accept) |
| paddy@15 | 81 slice[i], slice[j] = slice[j], slice[i] |
| paddy@15 | 82 } |
| paddy@15 | 83 |
| paddy@15 | 84 // Parse an Accept Header string returning a sorted list |
| paddy@15 | 85 // of clauses |
| paddy@15 | 86 func ParseAccept(header string) (accept []Accept) { |
| paddy@15 | 87 parts := strings.Split(header, ",") |
| paddy@15 | 88 accept = make([]Accept, 0, len(parts)) |
| paddy@15 | 89 for _, part := range parts { |
| paddy@15 | 90 part := strings.Trim(part, " ") |
| paddy@15 | 91 |
| paddy@15 | 92 a := Accept{} |
| paddy@15 | 93 a.Params = make(map[string]string) |
| paddy@15 | 94 a.Q = 1.0 |
| paddy@15 | 95 |
| paddy@15 | 96 mrp := strings.Split(part, ";") |
| paddy@15 | 97 |
| paddy@15 | 98 media_range := mrp[0] |
| paddy@15 | 99 sp := strings.Split(media_range, "/") |
| paddy@15 | 100 a.Type = strings.Trim(sp[0], " ") |
| paddy@15 | 101 |
| paddy@15 | 102 switch { |
| paddy@15 | 103 case len(sp) == 1 && a.Type == "*": |
| paddy@15 | 104 a.SubType = "*" |
| paddy@15 | 105 case len(sp) == 2: |
| paddy@15 | 106 a.SubType = strings.Trim(sp[1], " ") |
| paddy@15 | 107 default: |
| paddy@15 | 108 continue |
| paddy@15 | 109 } |
| paddy@15 | 110 |
| paddy@15 | 111 if len(mrp) == 1 { |
| paddy@15 | 112 accept = append(accept, a) |
| paddy@15 | 113 continue |
| paddy@15 | 114 } |
| paddy@15 | 115 |
| paddy@15 | 116 for _, param := range mrp[1:] { |
| paddy@15 | 117 sp := strings.SplitN(param, "=", 2) |
| paddy@15 | 118 if len(sp) != 2 { |
| paddy@15 | 119 continue |
| paddy@15 | 120 } |
| paddy@15 | 121 token := strings.Trim(sp[0], " ") |
| paddy@15 | 122 if token == "q" { |
| paddy@15 | 123 a.Q, _ = strconv.ParseFloat(sp[1], 32) |
| paddy@15 | 124 } else { |
| paddy@15 | 125 a.Params[token] = strings.Trim(sp[1], " ") |
| paddy@15 | 126 } |
| paddy@15 | 127 } |
| paddy@15 | 128 |
| paddy@15 | 129 accept = append(accept, a) |
| paddy@15 | 130 } |
| paddy@15 | 131 |
| paddy@15 | 132 slice := accept_slice(accept) |
| paddy@15 | 133 sort.Sort(slice) |
| paddy@15 | 134 |
| paddy@15 | 135 return |
| paddy@15 | 136 } |
| paddy@15 | 137 |
| paddy@15 | 138 // Negotiate the most appropriate content_type given the accept header |
| paddy@15 | 139 // and a list of alternatives. |
| paddy@15 | 140 func Negotiate(header string, alternatives []string) (content_type string) { |
| paddy@15 | 141 asp := make([][]string, 0, len(alternatives)) |
| paddy@15 | 142 for _, ctype := range alternatives { |
| paddy@15 | 143 asp = append(asp, strings.SplitN(ctype, "/", 2)) |
| paddy@15 | 144 } |
| paddy@15 | 145 for _, clause := range ParseAccept(header) { |
| paddy@15 | 146 for i, ctsp := range asp { |
| paddy@15 | 147 if clause.Type == ctsp[0] && clause.SubType == ctsp[1] { |
| paddy@15 | 148 content_type = alternatives[i] |
| paddy@15 | 149 return |
| paddy@15 | 150 } |
| paddy@15 | 151 if clause.Type == ctsp[0] && clause.SubType == "*" { |
| paddy@15 | 152 content_type = alternatives[i] |
| paddy@15 | 153 return |
| paddy@15 | 154 } |
| paddy@15 | 155 if clause.Type == "*" && clause.SubType == "*" { |
| paddy@15 | 156 content_type = alternatives[i] |
| paddy@15 | 157 return |
| paddy@15 | 158 } |
| paddy@15 | 159 } |
| paddy@15 | 160 } |
| paddy@15 | 161 return |
| paddy@15 | 162 } |