github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/integration/messagebus/glue/options.go (about) 1 /* 2 * Glue - Robust Go and Javascript Socket Library 3 * Copyright (C) 2015 Roland Singer <roland.singer[at]desertbit.com> 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 package glue 20 21 import ( 22 "net/http" 23 "net/url" 24 "strings" 25 ) 26 27 //#################// 28 //### Constants ###// 29 //#################// 30 31 // A HTTPSocketType defines which socket type to use for the HTTP glue server. 32 type HTTPSocketType int 33 34 const ( 35 // HTTPSocketTypeNone defines to not configure and run a HTTP server. 36 HTTPSocketTypeNone HTTPSocketType = 1 << iota 37 38 // HTTPSocketTypeTCP defines to use a TCP server. 39 HTTPSocketTypeTCP HTTPSocketType = 1 << iota 40 41 // HTTPSocketTypeUnix defines to use a Unix socket server. 42 HTTPSocketTypeUnix HTTPSocketType = 1 << iota 43 ) 44 45 //####################// 46 //### Options type ###// 47 //####################// 48 49 // Options holds the glue server options. 50 type Options struct { 51 // HTTPSocketType defines which socket type to use for the HTTP glue server. 52 // Default: HTTPSocketTypeTCP 53 HTTPSocketType HTTPSocketType 54 55 // The HTTP address to listen on. 56 // Default: ":80" 57 HTTPListenAddress string 58 59 // HTTPHandleURL defines the base url to handle glue HTTP socket requests. 60 // This has to be set, even if the none socket type is used. 61 // Default: "/glue/" 62 HTTPHandleURL string 63 64 // CheckOrigin returns true if the request Origin header is acceptable. If 65 // CheckOrigin is nil, the host in the Origin header must not be set or 66 // must match the host of the request. 67 // This method is used by the backend sockets before establishing connections. 68 CheckOrigin func(r *http.Request) bool 69 70 // Enables the Cross-Origin Resource Sharing (CORS) mechanism. 71 // This will set the Access-Control-Allow-Origin HTTP headers. 72 // A resource makes a cross-origin HTTP request when it requests a resource 73 // from a different domain than the one which served itself. 74 EnableCORS bool 75 } 76 77 // SetDefaults sets unset option values to its default value. 78 func (o *Options) SetDefaults() { 79 // Set the socket type. 80 if o.HTTPSocketType != HTTPSocketTypeNone && 81 o.HTTPSocketType != HTTPSocketTypeTCP && 82 o.HTTPSocketType != HTTPSocketTypeUnix { 83 o.HTTPSocketType = HTTPSocketTypeTCP 84 } 85 86 // Set the listen address. 87 if len(o.HTTPListenAddress) == 0 { 88 o.HTTPListenAddress = ":80" 89 } 90 91 // Set the handle URL. 92 if len(o.HTTPHandleURL) == 0 { 93 o.HTTPHandleURL = "/glue/" 94 } 95 96 // Be sure that the handle URL ends with a slash. 97 if !strings.HasSuffix(o.HTTPHandleURL, "/") { 98 o.HTTPHandleURL += "/" 99 } 100 101 // Set the default check origin function if not set. 102 if o.CheckOrigin == nil { 103 o.CheckOrigin = checkSameOrigin 104 } 105 } 106 107 //###############// 108 //### Private ###// 109 //###############// 110 111 // checkSameOrigin returns true if the origin is not set or is equal to the request host. 112 // Source from gorilla websockets. 113 func checkSameOrigin(r *http.Request) bool { 114 origin := r.Header["Origin"] 115 if len(origin) == 0 { 116 return true 117 } 118 u, err := url.Parse(origin[0]) 119 if err != nil { 120 return false 121 } 122 return u.Host == r.Host 123 }