-
-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code transforms for HTTP proxy and unidirectional connect (#903)
- Loading branch information
Showing
19 changed files
with
356 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package tools | ||
|
||
import "unicode" | ||
|
||
func IsASCII(s string) bool { | ||
for _, c := range s { | ||
if c > unicode.MaxASCII { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package tools | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/centrifugal/centrifuge" | ||
) | ||
|
||
type ConnectCodeToHTTPStatus struct { | ||
Enabled bool `mapstructure:"enabled" json:"enabled"` | ||
Transforms []ConnectCodeToHTTPStatusTransform `mapstructure:"transforms" json:"transforms"` | ||
} | ||
|
||
type ConnectCodeToHTTPStatusTransform struct { | ||
Code uint32 `mapstructure:"code" json:"code"` | ||
ToResponse TransformedConnectErrorHttpResponse `mapstructure:"to_response" json:"to_response"` | ||
} | ||
|
||
type TransformedConnectErrorHttpResponse struct { | ||
Status int `mapstructure:"status_code" json:"status_code"` | ||
Body string `mapstructure:"body" json:"body"` | ||
} | ||
|
||
func ConnectErrorToToHTTPResponse(err error, transforms []ConnectCodeToHTTPStatusTransform) (TransformedConnectErrorHttpResponse, bool) { | ||
var code uint32 | ||
var body string | ||
switch t := err.(type) { | ||
case *centrifuge.Disconnect: | ||
code = t.Code | ||
body = t.Reason | ||
case centrifuge.Disconnect: | ||
code = t.Code | ||
body = t.Reason | ||
case *centrifuge.Error: | ||
code = t.Code | ||
body = t.Message | ||
default: | ||
} | ||
if code > 0 { | ||
for _, t := range transforms { | ||
if t.Code != code { | ||
continue | ||
} | ||
if t.ToResponse.Body == "" { | ||
t.ToResponse.Body = body | ||
} | ||
return t.ToResponse, true | ||
} | ||
} | ||
return TransformedConnectErrorHttpResponse{ | ||
Status: http.StatusInternalServerError, | ||
Body: http.StatusText(http.StatusInternalServerError), | ||
}, false | ||
} |
Oops, something went wrong.