Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package push |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | ) |
| 20 | |
| 21 | // linkResource contains the results of a parsed Link header. |
| 22 | type linkResource struct { |
| 23 | uri string |
| 24 | params map[string]string |
| 25 | } |
| 26 | |
| 27 | // parseLinkHeader is responsible for parsing Link header |
| 28 | // and returning list of found resources. |
| 29 | // |
| 30 | // Accepted formats are: |
| 31 | // |
| 32 | // Link: <resource>; as=script |
| 33 | // Link: <resource>; as=script,<resource>; as=style |
| 34 | // Link: <resource>;<resource2> |
| 35 | // |
| 36 | // where <resource> begins with a forward slash (/). |
| 37 | func parseLinkHeader(header string) []linkResource { |
| 38 | resources := []linkResource{} |
| 39 | |
| 40 | if header == "" { |
| 41 | return resources |
| 42 | } |
| 43 | |
| 44 | for link := range strings.SplitSeq(header, comma) { |
| 45 | l := linkResource{params: make(map[string]string)} |
| 46 | |
| 47 | li, ri := strings.Index(link, "<"), strings.Index(link, ">") |
| 48 | if li == -1 || ri == -1 { |
| 49 | continue |
| 50 | } |
| 51 | |
| 52 | l.uri = strings.TrimSpace(link[li+1 : ri]) |
| 53 | |
| 54 | for param := range strings.SplitSeq(strings.TrimSpace(link[ri+1:]), semicolon) { |
| 55 | before, after, isCut := strings.Cut(strings.TrimSpace(param), equal) |
| 56 | key := strings.TrimSpace(before) |
| 57 | if key == "" { |
| 58 | continue |
| 59 | } |
| 60 | if isCut { |
| 61 | l.params[key] = strings.TrimSpace(after) |
| 62 | } else { |
| 63 | l.params[key] = key |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | resources = append(resources, l) |
| 68 | } |
| 69 | |
| 70 | return resources |
| 71 | } |
| 72 | |
| 73 | const ( |
| 74 | comma = "," |
| 75 | semicolon = ";" |
| 76 | equal = "=" |
| 77 | ) |