Vendor main dependencies.
This commit is contained in:
parent
49a09ab7dd
commit
dd5e3fba01
2738 changed files with 1045689 additions and 0 deletions
41
vendor/github.com/vulcand/oxy/utils/auth.go
generated
vendored
Normal file
41
vendor/github.com/vulcand/oxy/utils/auth.go
generated
vendored
Normal file
|
@ -0,0 +1,41 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type BasicAuth struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (ba *BasicAuth) String() string {
|
||||
encoded := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", ba.Username, ba.Password)))
|
||||
return fmt.Sprintf("Basic %s", encoded)
|
||||
}
|
||||
|
||||
func ParseAuthHeader(header string) (*BasicAuth, error) {
|
||||
values := strings.Fields(header)
|
||||
if len(values) != 2 {
|
||||
return nil, fmt.Errorf(fmt.Sprintf("Failed to parse header '%s'", header))
|
||||
}
|
||||
|
||||
auth_type := strings.ToLower(values[0])
|
||||
if auth_type != "basic" {
|
||||
return nil, fmt.Errorf("Expected basic auth type, got '%s'", auth_type)
|
||||
}
|
||||
|
||||
encoded_string := values[1]
|
||||
decoded_string, err := base64.StdEncoding.DecodeString(encoded_string)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to parse header '%s', base64 failed: %s", header, err)
|
||||
}
|
||||
|
||||
values = strings.SplitN(string(decoded_string), ":", 2)
|
||||
if len(values) != 2 {
|
||||
return nil, fmt.Errorf("Failed to parse header '%s', expected separator ':'", header)
|
||||
}
|
||||
return &BasicAuth{Username: values[0], Password: values[1]}, nil
|
||||
}
|
38
vendor/github.com/vulcand/oxy/utils/handler.go
generated
vendored
Normal file
38
vendor/github.com/vulcand/oxy/utils/handler.go
generated
vendored
Normal file
|
@ -0,0 +1,38 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ErrorHandler interface {
|
||||
ServeHTTP(w http.ResponseWriter, req *http.Request, err error)
|
||||
}
|
||||
|
||||
var DefaultHandler ErrorHandler = &StdHandler{}
|
||||
|
||||
type StdHandler struct {
|
||||
}
|
||||
|
||||
func (e *StdHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error) {
|
||||
statusCode := http.StatusInternalServerError
|
||||
if e, ok := err.(net.Error); ok {
|
||||
if e.Timeout() {
|
||||
statusCode = http.StatusGatewayTimeout
|
||||
} else {
|
||||
statusCode = http.StatusBadGateway
|
||||
}
|
||||
} else if err == io.EOF {
|
||||
statusCode = http.StatusBadGateway
|
||||
}
|
||||
w.WriteHeader(statusCode)
|
||||
w.Write([]byte(http.StatusText(statusCode)))
|
||||
}
|
||||
|
||||
type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, error)
|
||||
|
||||
// ServeHTTP calls f(w, r).
|
||||
func (f ErrorHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request, err error) {
|
||||
f(w, r, err)
|
||||
}
|
86
vendor/github.com/vulcand/oxy/utils/logging.go
generated
vendored
Normal file
86
vendor/github.com/vulcand/oxy/utils/logging.go
generated
vendored
Normal file
|
@ -0,0 +1,86 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
||||
var NullLogger Logger = &NOPLogger{}
|
||||
|
||||
// Logger defines a simple logging interface
|
||||
type Logger interface {
|
||||
Infof(format string, args ...interface{})
|
||||
Warningf(format string, args ...interface{})
|
||||
Errorf(format string, args ...interface{})
|
||||
}
|
||||
|
||||
type FileLogger struct {
|
||||
info *log.Logger
|
||||
warn *log.Logger
|
||||
error *log.Logger
|
||||
}
|
||||
|
||||
func NewFileLogger(w io.Writer, lvl LogLevel) *FileLogger {
|
||||
l := &FileLogger{}
|
||||
flag := log.Ldate | log.Ltime | log.Lmicroseconds
|
||||
if lvl <= INFO {
|
||||
l.info = log.New(w, "INFO: ", flag)
|
||||
}
|
||||
if lvl <= WARN {
|
||||
l.warn = log.New(w, "WARN: ", flag)
|
||||
}
|
||||
if lvl <= ERROR {
|
||||
l.error = log.New(w, "ERR: ", flag)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func (f *FileLogger) Infof(format string, args ...interface{}) {
|
||||
if f.info == nil {
|
||||
return
|
||||
}
|
||||
f.info.Printf(format, args...)
|
||||
}
|
||||
|
||||
func (f *FileLogger) Warningf(format string, args ...interface{}) {
|
||||
if f.warn == nil {
|
||||
return
|
||||
}
|
||||
f.warn.Printf(format, args...)
|
||||
}
|
||||
|
||||
func (f *FileLogger) Errorf(format string, args ...interface{}) {
|
||||
if f.error == nil {
|
||||
return
|
||||
}
|
||||
f.error.Printf(format, args...)
|
||||
}
|
||||
|
||||
type NOPLogger struct {
|
||||
}
|
||||
|
||||
func (*NOPLogger) Infof(format string, args ...interface{}) {
|
||||
|
||||
}
|
||||
func (*NOPLogger) Warningf(format string, args ...interface{}) {
|
||||
}
|
||||
|
||||
func (*NOPLogger) Errorf(format string, args ...interface{}) {
|
||||
}
|
||||
|
||||
func (*NOPLogger) Info(string) {
|
||||
|
||||
}
|
||||
func (*NOPLogger) Warning(string) {
|
||||
}
|
||||
|
||||
func (*NOPLogger) Error(string) {
|
||||
}
|
||||
|
||||
type LogLevel int
|
||||
|
||||
const (
|
||||
INFO = iota
|
||||
WARN
|
||||
ERROR
|
||||
)
|
138
vendor/github.com/vulcand/oxy/utils/netutils.go
generated
vendored
Normal file
138
vendor/github.com/vulcand/oxy/utils/netutils.go
generated
vendored
Normal file
|
@ -0,0 +1,138 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"mime"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// ProxyWriter helps to capture response headers and status code
|
||||
// from the ServeHTTP. It can be safely passed to ServeHTTP handler,
|
||||
// wrapping the real response writer.
|
||||
type ProxyWriter struct {
|
||||
W http.ResponseWriter
|
||||
Code int
|
||||
}
|
||||
|
||||
func (p *ProxyWriter) StatusCode() int {
|
||||
if p.Code == 0 {
|
||||
// per contract standard lib will set this to http.StatusOK if not set
|
||||
// by user, here we avoid the confusion by mirroring this logic
|
||||
return http.StatusOK
|
||||
}
|
||||
return p.Code
|
||||
}
|
||||
|
||||
func (p *ProxyWriter) Header() http.Header {
|
||||
return p.W.Header()
|
||||
}
|
||||
|
||||
func (p *ProxyWriter) Write(buf []byte) (int, error) {
|
||||
return p.W.Write(buf)
|
||||
}
|
||||
|
||||
func (p *ProxyWriter) WriteHeader(code int) {
|
||||
p.Code = code
|
||||
p.W.WriteHeader(code)
|
||||
}
|
||||
|
||||
func (p *ProxyWriter) Flush() {
|
||||
if f, ok := p.W.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ProxyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
return p.W.(http.Hijacker).Hijack()
|
||||
}
|
||||
|
||||
func NewBufferWriter(w io.WriteCloser) *BufferWriter {
|
||||
return &BufferWriter{
|
||||
W: w,
|
||||
H: make(http.Header),
|
||||
}
|
||||
}
|
||||
|
||||
type BufferWriter struct {
|
||||
H http.Header
|
||||
Code int
|
||||
W io.WriteCloser
|
||||
}
|
||||
|
||||
func (b *BufferWriter) Close() error {
|
||||
return b.W.Close()
|
||||
}
|
||||
|
||||
func (b *BufferWriter) Header() http.Header {
|
||||
return b.H
|
||||
}
|
||||
|
||||
func (b *BufferWriter) Write(buf []byte) (int, error) {
|
||||
return b.W.Write(buf)
|
||||
}
|
||||
|
||||
// WriteHeader sets rw.Code.
|
||||
func (b *BufferWriter) WriteHeader(code int) {
|
||||
b.Code = code
|
||||
}
|
||||
|
||||
func (b *BufferWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
return b.W.(http.Hijacker).Hijack()
|
||||
}
|
||||
|
||||
type nopWriteCloser struct {
|
||||
io.Writer
|
||||
}
|
||||
|
||||
func (*nopWriteCloser) Close() error { return nil }
|
||||
|
||||
// NopCloser returns a WriteCloser with a no-op Close method wrapping
|
||||
// the provided Writer w.
|
||||
func NopWriteCloser(w io.Writer) io.WriteCloser {
|
||||
return &nopWriteCloser{w}
|
||||
}
|
||||
|
||||
// CopyURL provides update safe copy by avoiding shallow copying User field
|
||||
func CopyURL(i *url.URL) *url.URL {
|
||||
out := *i
|
||||
if i.User != nil {
|
||||
out.User = &(*i.User)
|
||||
}
|
||||
return &out
|
||||
}
|
||||
|
||||
// CopyHeaders copies http headers from source to destination, it
|
||||
// does not overide, but adds multiple headers
|
||||
func CopyHeaders(dst, src http.Header) {
|
||||
for k, vv := range src {
|
||||
for _, v := range vv {
|
||||
dst.Add(k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HasHeaders determines whether any of the header names is present in the http headers
|
||||
func HasHeaders(names []string, headers http.Header) bool {
|
||||
for _, h := range names {
|
||||
if headers.Get(h) != "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// RemoveHeaders removes the header with the given names from the headers map
|
||||
func RemoveHeaders(headers http.Header, names ...string) {
|
||||
for _, h := range names {
|
||||
headers.Del(h)
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the MIME media type value of a header.
|
||||
func GetHeaderMediaType(headers http.Header, name string) (string, error) {
|
||||
mediatype, _, err := mime.ParseMediaType(headers.Get(name))
|
||||
return mediatype, err
|
||||
}
|
57
vendor/github.com/vulcand/oxy/utils/source.go
generated
vendored
Normal file
57
vendor/github.com/vulcand/oxy/utils/source.go
generated
vendored
Normal file
|
@ -0,0 +1,57 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ExtractSource extracts the source from the request, e.g. that may be client ip, or particular header that
|
||||
// identifies the source. amount stands for amount of connections the source consumes, usually 1 for connection limiters
|
||||
// error should be returned when source can not be identified
|
||||
type SourceExtractor interface {
|
||||
Extract(req *http.Request) (token string, amount int64, err error)
|
||||
}
|
||||
|
||||
type ExtractorFunc func(req *http.Request) (token string, amount int64, err error)
|
||||
|
||||
func (f ExtractorFunc) Extract(req *http.Request) (string, int64, error) {
|
||||
return f(req)
|
||||
}
|
||||
|
||||
type ExtractSource func(req *http.Request)
|
||||
|
||||
func NewExtractor(variable string) (SourceExtractor, error) {
|
||||
if variable == "client.ip" {
|
||||
return ExtractorFunc(extractClientIP), nil
|
||||
}
|
||||
if variable == "request.host" {
|
||||
return ExtractorFunc(extractHost), nil
|
||||
}
|
||||
if strings.HasPrefix(variable, "request.header.") {
|
||||
header := strings.TrimPrefix(variable, "request.header.")
|
||||
if len(header) == 0 {
|
||||
return nil, fmt.Errorf("Wrong header: %s", header)
|
||||
}
|
||||
return makeHeaderExtractor(header), nil
|
||||
}
|
||||
return nil, fmt.Errorf("Unsupported limiting variable: '%s'", variable)
|
||||
}
|
||||
|
||||
func extractClientIP(req *http.Request) (string, int64, error) {
|
||||
vals := strings.SplitN(req.RemoteAddr, ":", 2)
|
||||
if len(vals[0]) == 0 {
|
||||
return "", 0, fmt.Errorf("Failed to parse client IP: %v", req.RemoteAddr)
|
||||
}
|
||||
return vals[0], 1, nil
|
||||
}
|
||||
|
||||
func extractHost(req *http.Request) (string, int64, error) {
|
||||
return req.Host, 1, nil
|
||||
}
|
||||
|
||||
func makeHeaderExtractor(header string) SourceExtractor {
|
||||
return ExtractorFunc(func(req *http.Request) (string, int64, error) {
|
||||
return req.Header.Get(header), 1, nil
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue