mirror of
https://gitlab.com/ric_harvey/MailHog.git
synced 2024-11-23 22:34:04 +00:00
add vendor directory
This commit is contained in:
parent
d10e6dbb44
commit
c68ed81a0c
186 changed files with 39370 additions and 0 deletions
27
vendor/github.com/gorilla/context/LICENSE
generated
vendored
Normal file
27
vendor/github.com/gorilla/context/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
Copyright (c) 2012 Rodrigo Moraes. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following disclaimer
|
||||||
|
in the documentation and/or other materials provided with the
|
||||||
|
distribution.
|
||||||
|
* Neither the name of Google Inc. nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
7
vendor/github.com/gorilla/context/README.md
generated
vendored
Normal file
7
vendor/github.com/gorilla/context/README.md
generated
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
context
|
||||||
|
=======
|
||||||
|
[![Build Status](https://travis-ci.org/gorilla/context.png?branch=master)](https://travis-ci.org/gorilla/context)
|
||||||
|
|
||||||
|
gorilla/context is a general purpose registry for global request variables.
|
||||||
|
|
||||||
|
Read the full documentation here: http://www.gorillatoolkit.org/pkg/context
|
143
vendor/github.com/gorilla/context/context.go
generated
vendored
Normal file
143
vendor/github.com/gorilla/context/context.go
generated
vendored
Normal file
|
@ -0,0 +1,143 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
mutex sync.RWMutex
|
||||||
|
data = make(map[*http.Request]map[interface{}]interface{})
|
||||||
|
datat = make(map[*http.Request]int64)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Set stores a value for a given key in a given request.
|
||||||
|
func Set(r *http.Request, key, val interface{}) {
|
||||||
|
mutex.Lock()
|
||||||
|
if data[r] == nil {
|
||||||
|
data[r] = make(map[interface{}]interface{})
|
||||||
|
datat[r] = time.Now().Unix()
|
||||||
|
}
|
||||||
|
data[r][key] = val
|
||||||
|
mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns a value stored for a given key in a given request.
|
||||||
|
func Get(r *http.Request, key interface{}) interface{} {
|
||||||
|
mutex.RLock()
|
||||||
|
if ctx := data[r]; ctx != nil {
|
||||||
|
value := ctx[key]
|
||||||
|
mutex.RUnlock()
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
mutex.RUnlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOk returns stored value and presence state like multi-value return of map access.
|
||||||
|
func GetOk(r *http.Request, key interface{}) (interface{}, bool) {
|
||||||
|
mutex.RLock()
|
||||||
|
if _, ok := data[r]; ok {
|
||||||
|
value, ok := data[r][key]
|
||||||
|
mutex.RUnlock()
|
||||||
|
return value, ok
|
||||||
|
}
|
||||||
|
mutex.RUnlock()
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAll returns all stored values for the request as a map. Nil is returned for invalid requests.
|
||||||
|
func GetAll(r *http.Request) map[interface{}]interface{} {
|
||||||
|
mutex.RLock()
|
||||||
|
if context, ok := data[r]; ok {
|
||||||
|
result := make(map[interface{}]interface{}, len(context))
|
||||||
|
for k, v := range context {
|
||||||
|
result[k] = v
|
||||||
|
}
|
||||||
|
mutex.RUnlock()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
mutex.RUnlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllOk returns all stored values for the request as a map and a boolean value that indicates if
|
||||||
|
// the request was registered.
|
||||||
|
func GetAllOk(r *http.Request) (map[interface{}]interface{}, bool) {
|
||||||
|
mutex.RLock()
|
||||||
|
context, ok := data[r]
|
||||||
|
result := make(map[interface{}]interface{}, len(context))
|
||||||
|
for k, v := range context {
|
||||||
|
result[k] = v
|
||||||
|
}
|
||||||
|
mutex.RUnlock()
|
||||||
|
return result, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete removes a value stored for a given key in a given request.
|
||||||
|
func Delete(r *http.Request, key interface{}) {
|
||||||
|
mutex.Lock()
|
||||||
|
if data[r] != nil {
|
||||||
|
delete(data[r], key)
|
||||||
|
}
|
||||||
|
mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear removes all values stored for a given request.
|
||||||
|
//
|
||||||
|
// This is usually called by a handler wrapper to clean up request
|
||||||
|
// variables at the end of a request lifetime. See ClearHandler().
|
||||||
|
func Clear(r *http.Request) {
|
||||||
|
mutex.Lock()
|
||||||
|
clear(r)
|
||||||
|
mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// clear is Clear without the lock.
|
||||||
|
func clear(r *http.Request) {
|
||||||
|
delete(data, r)
|
||||||
|
delete(datat, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Purge removes request data stored for longer than maxAge, in seconds.
|
||||||
|
// It returns the amount of requests removed.
|
||||||
|
//
|
||||||
|
// If maxAge <= 0, all request data is removed.
|
||||||
|
//
|
||||||
|
// This is only used for sanity check: in case context cleaning was not
|
||||||
|
// properly set some request data can be kept forever, consuming an increasing
|
||||||
|
// amount of memory. In case this is detected, Purge() must be called
|
||||||
|
// periodically until the problem is fixed.
|
||||||
|
func Purge(maxAge int) int {
|
||||||
|
mutex.Lock()
|
||||||
|
count := 0
|
||||||
|
if maxAge <= 0 {
|
||||||
|
count = len(data)
|
||||||
|
data = make(map[*http.Request]map[interface{}]interface{})
|
||||||
|
datat = make(map[*http.Request]int64)
|
||||||
|
} else {
|
||||||
|
min := time.Now().Unix() - int64(maxAge)
|
||||||
|
for r := range data {
|
||||||
|
if datat[r] < min {
|
||||||
|
clear(r)
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mutex.Unlock()
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClearHandler wraps an http.Handler and clears request values at the end
|
||||||
|
// of a request lifetime.
|
||||||
|
func ClearHandler(h http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer Clear(r)
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
82
vendor/github.com/gorilla/context/doc.go
generated
vendored
Normal file
82
vendor/github.com/gorilla/context/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package context stores values shared during a request lifetime.
|
||||||
|
|
||||||
|
For example, a router can set variables extracted from the URL and later
|
||||||
|
application handlers can access those values, or it can be used to store
|
||||||
|
sessions values to be saved at the end of a request. There are several
|
||||||
|
others common uses.
|
||||||
|
|
||||||
|
The idea was posted by Brad Fitzpatrick to the go-nuts mailing list:
|
||||||
|
|
||||||
|
http://groups.google.com/group/golang-nuts/msg/e2d679d303aa5d53
|
||||||
|
|
||||||
|
Here's the basic usage: first define the keys that you will need. The key
|
||||||
|
type is interface{} so a key can be of any type that supports equality.
|
||||||
|
Here we define a key using a custom int type to avoid name collisions:
|
||||||
|
|
||||||
|
package foo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gorilla/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type key int
|
||||||
|
|
||||||
|
const MyKey key = 0
|
||||||
|
|
||||||
|
Then set a variable. Variables are bound to an http.Request object, so you
|
||||||
|
need a request instance to set a value:
|
||||||
|
|
||||||
|
context.Set(r, MyKey, "bar")
|
||||||
|
|
||||||
|
The application can later access the variable using the same key you provided:
|
||||||
|
|
||||||
|
func MyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// val is "bar".
|
||||||
|
val := context.Get(r, foo.MyKey)
|
||||||
|
|
||||||
|
// returns ("bar", true)
|
||||||
|
val, ok := context.GetOk(r, foo.MyKey)
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
And that's all about the basic usage. We discuss some other ideas below.
|
||||||
|
|
||||||
|
Any type can be stored in the context. To enforce a given type, make the key
|
||||||
|
private and wrap Get() and Set() to accept and return values of a specific
|
||||||
|
type:
|
||||||
|
|
||||||
|
type key int
|
||||||
|
|
||||||
|
const mykey key = 0
|
||||||
|
|
||||||
|
// GetMyKey returns a value for this package from the request values.
|
||||||
|
func GetMyKey(r *http.Request) SomeType {
|
||||||
|
if rv := context.Get(r, mykey); rv != nil {
|
||||||
|
return rv.(SomeType)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMyKey sets a value for this package in the request values.
|
||||||
|
func SetMyKey(r *http.Request, val SomeType) {
|
||||||
|
context.Set(r, mykey, val)
|
||||||
|
}
|
||||||
|
|
||||||
|
Variables must be cleared at the end of a request, to remove all values
|
||||||
|
that were stored. This can be done in an http.Handler, after a request was
|
||||||
|
served. Just call Clear() passing the request:
|
||||||
|
|
||||||
|
context.Clear(r)
|
||||||
|
|
||||||
|
...or use ClearHandler(), which conveniently wraps an http.Handler to clear
|
||||||
|
variables at the end of a request lifetime.
|
||||||
|
|
||||||
|
The Routers from the packages gorilla/mux and gorilla/pat call Clear()
|
||||||
|
so if you are using either of them you don't need to clear the context manually.
|
||||||
|
*/
|
||||||
|
package context
|
27
vendor/github.com/gorilla/mux/LICENSE
generated
vendored
Normal file
27
vendor/github.com/gorilla/mux/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
Copyright (c) 2012 Rodrigo Moraes. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following disclaimer
|
||||||
|
in the documentation and/or other materials provided with the
|
||||||
|
distribution.
|
||||||
|
* Neither the name of Google Inc. nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
242
vendor/github.com/gorilla/mux/README.md
generated
vendored
Normal file
242
vendor/github.com/gorilla/mux/README.md
generated
vendored
Normal file
|
@ -0,0 +1,242 @@
|
||||||
|
mux
|
||||||
|
===
|
||||||
|
[![GoDoc](https://godoc.org/github.com/gorilla/mux?status.svg)](https://godoc.org/github.com/gorilla/mux)
|
||||||
|
[![Build Status](https://travis-ci.org/gorilla/mux.svg?branch=master)](https://travis-ci.org/gorilla/mux)
|
||||||
|
|
||||||
|
http://www.gorillatoolkit.org/pkg/mux
|
||||||
|
|
||||||
|
Package `gorilla/mux` implements a request router and dispatcher.
|
||||||
|
|
||||||
|
The name mux stands for "HTTP request multiplexer". Like the standard `http.ServeMux`, `mux.Router` matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions. The main features are:
|
||||||
|
|
||||||
|
* Requests can be matched based on URL host, path, path prefix, schemes, header and query values, HTTP methods or using custom matchers.
|
||||||
|
* URL hosts and paths can have variables with an optional regular expression.
|
||||||
|
* Registered URLs can be built, or "reversed", which helps maintaining references to resources.
|
||||||
|
* Routes can be used as subrouters: nested routes are only tested if the parent route matches. This is useful to define groups of routes that share common conditions like a host, a path prefix or other repeated attributes. As a bonus, this optimizes request matching.
|
||||||
|
* It implements the `http.Handler` interface so it is compatible with the standard `http.ServeMux`.
|
||||||
|
|
||||||
|
Let's start registering a couple of URL paths and handlers:
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/", HomeHandler)
|
||||||
|
r.HandleFunc("/products", ProductsHandler)
|
||||||
|
r.HandleFunc("/articles", ArticlesHandler)
|
||||||
|
http.Handle("/", r)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Here we register three routes mapping URL paths to handlers. This is equivalent to how `http.HandleFunc()` works: if an incoming request URL matches one of the paths, the corresponding handler is called passing (`http.ResponseWriter`, `*http.Request`) as parameters.
|
||||||
|
|
||||||
|
Paths can have variables. They are defined using the format `{name}` or `{name:pattern}`. If a regular expression pattern is not defined, the matched variable will be anything until the next slash. For example:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/products/{key}", ProductHandler)
|
||||||
|
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
|
||||||
|
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
|
||||||
|
```
|
||||||
|
|
||||||
|
The names are used to create a map of route variables which can be retrieved calling `mux.Vars()`:
|
||||||
|
|
||||||
|
```go
|
||||||
|
vars := mux.Vars(request)
|
||||||
|
category := vars["category"]
|
||||||
|
```
|
||||||
|
|
||||||
|
And this is all you need to know about the basic usage. More advanced options are explained below.
|
||||||
|
|
||||||
|
Routes can also be restricted to a domain or subdomain. Just define a host pattern to be matched. They can also have variables:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
// Only matches if domain is "www.example.com".
|
||||||
|
r.Host("www.example.com")
|
||||||
|
// Matches a dynamic subdomain.
|
||||||
|
r.Host("{subdomain:[a-z]+}.domain.com")
|
||||||
|
```
|
||||||
|
|
||||||
|
There are several other matchers that can be added. To match path prefixes:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.PathPrefix("/products/")
|
||||||
|
```
|
||||||
|
|
||||||
|
...or HTTP methods:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.Methods("GET", "POST")
|
||||||
|
```
|
||||||
|
|
||||||
|
...or URL schemes:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.Schemes("https")
|
||||||
|
```
|
||||||
|
|
||||||
|
...or header values:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.Headers("X-Requested-With", "XMLHttpRequest")
|
||||||
|
```
|
||||||
|
|
||||||
|
...or query values:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.Queries("key", "value")
|
||||||
|
```
|
||||||
|
|
||||||
|
...or to use a custom matcher function:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool {
|
||||||
|
return r.ProtoMajor == 0
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
...and finally, it is possible to combine several matchers in a single route:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.HandleFunc("/products", ProductsHandler).
|
||||||
|
Host("www.example.com").
|
||||||
|
Methods("GET").
|
||||||
|
Schemes("http")
|
||||||
|
```
|
||||||
|
|
||||||
|
Setting the same matching conditions again and again can be boring, so we have a way to group several routes that share the same requirements. We call it "subrouting".
|
||||||
|
|
||||||
|
For example, let's say we have several URLs that should only match when the host is `www.example.com`. Create a route for that host and get a "subrouter" from it:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
s := r.Host("www.example.com").Subrouter()
|
||||||
|
```
|
||||||
|
|
||||||
|
Then register routes in the subrouter:
|
||||||
|
|
||||||
|
```go
|
||||||
|
s.HandleFunc("/products/", ProductsHandler)
|
||||||
|
s.HandleFunc("/products/{key}", ProductHandler)
|
||||||
|
s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler)
|
||||||
|
```
|
||||||
|
|
||||||
|
The three URL paths we registered above will only be tested if the domain is `www.example.com`, because the subrouter is tested first. This is not only convenient, but also optimizes request matching. You can create subrouters combining any attribute matchers accepted by a route.
|
||||||
|
|
||||||
|
Subrouters can be used to create domain or path "namespaces": you define subrouters in a central place and then parts of the app can register its paths relatively to a given subrouter.
|
||||||
|
|
||||||
|
There's one more thing about subroutes. When a subrouter has a path prefix, the inner routes use it as base for their paths:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
s := r.PathPrefix("/products").Subrouter()
|
||||||
|
// "/products/"
|
||||||
|
s.HandleFunc("/", ProductsHandler)
|
||||||
|
// "/products/{key}/"
|
||||||
|
s.HandleFunc("/{key}/", ProductHandler)
|
||||||
|
// "/products/{key}/details"
|
||||||
|
s.HandleFunc("/{key}/details", ProductDetailsHandler)
|
||||||
|
```
|
||||||
|
|
||||||
|
Now let's see how to build registered URLs.
|
||||||
|
|
||||||
|
Routes can be named. All routes that define a name can have their URLs built, or "reversed". We define a name calling `Name()` on a route. For example:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
|
||||||
|
Name("article")
|
||||||
|
```
|
||||||
|
|
||||||
|
To build a URL, get the route and call the `URL()` method, passing a sequence of key/value pairs for the route variables. For the previous route, we would do:
|
||||||
|
|
||||||
|
```go
|
||||||
|
url, err := r.Get("article").URL("category", "technology", "id", "42")
|
||||||
|
```
|
||||||
|
|
||||||
|
...and the result will be a `url.URL` with the following path:
|
||||||
|
|
||||||
|
```
|
||||||
|
"/articles/technology/42"
|
||||||
|
```
|
||||||
|
|
||||||
|
This also works for host variables:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.Host("{subdomain}.domain.com").
|
||||||
|
Path("/articles/{category}/{id:[0-9]+}").
|
||||||
|
HandlerFunc(ArticleHandler).
|
||||||
|
Name("article")
|
||||||
|
|
||||||
|
// url.String() will be "http://news.domain.com/articles/technology/42"
|
||||||
|
url, err := r.Get("article").URL("subdomain", "news",
|
||||||
|
"category", "technology",
|
||||||
|
"id", "42")
|
||||||
|
```
|
||||||
|
|
||||||
|
All variables defined in the route are required, and their values must conform to the corresponding patterns. These requirements guarantee that a generated URL will always match a registered route -- the only exception is for explicitly defined "build-only" routes which never match.
|
||||||
|
|
||||||
|
Regex support also exists for matching Headers within a route. For example, we could do:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.HeadersRegexp("Content-Type", "application/(text|json)")
|
||||||
|
```
|
||||||
|
|
||||||
|
...and the route will match both requests with a Content-Type of `application/json` as well as `application/text`
|
||||||
|
|
||||||
|
There's also a way to build only the URL host or path for a route: use the methods `URLHost()` or `URLPath()` instead. For the previous route, we would do:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// "http://news.domain.com/"
|
||||||
|
host, err := r.Get("article").URLHost("subdomain", "news")
|
||||||
|
|
||||||
|
// "/articles/technology/42"
|
||||||
|
path, err := r.Get("article").URLPath("category", "technology", "id", "42")
|
||||||
|
```
|
||||||
|
|
||||||
|
And if you use subrouters, host and path defined separately can be built as well:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := mux.NewRouter()
|
||||||
|
s := r.Host("{subdomain}.domain.com").Subrouter()
|
||||||
|
s.Path("/articles/{category}/{id:[0-9]+}").
|
||||||
|
HandlerFunc(ArticleHandler).
|
||||||
|
Name("article")
|
||||||
|
|
||||||
|
// "http://news.domain.com/articles/technology/42"
|
||||||
|
url, err := r.Get("article").URL("subdomain", "news",
|
||||||
|
"category", "technology",
|
||||||
|
"id", "42")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Full Example
|
||||||
|
|
||||||
|
Here's a complete, runnable example of a small `mux` based server:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
func YourHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Write([]byte("Gorilla!\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := mux.NewRouter()
|
||||||
|
// Routes consist of a path and a handler function.
|
||||||
|
r.HandleFunc("/", YourHandler)
|
||||||
|
|
||||||
|
// Bind to a port and pass our router in
|
||||||
|
http.ListenAndServe(":8000", r)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
BSD licensed. See the LICENSE file for details.
|
206
vendor/github.com/gorilla/mux/doc.go
generated
vendored
Normal file
206
vendor/github.com/gorilla/mux/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,206 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package mux implements a request router and dispatcher.
|
||||||
|
|
||||||
|
The name mux stands for "HTTP request multiplexer". Like the standard
|
||||||
|
http.ServeMux, mux.Router matches incoming requests against a list of
|
||||||
|
registered routes and calls a handler for the route that matches the URL
|
||||||
|
or other conditions. The main features are:
|
||||||
|
|
||||||
|
* Requests can be matched based on URL host, path, path prefix, schemes,
|
||||||
|
header and query values, HTTP methods or using custom matchers.
|
||||||
|
* URL hosts and paths can have variables with an optional regular
|
||||||
|
expression.
|
||||||
|
* Registered URLs can be built, or "reversed", which helps maintaining
|
||||||
|
references to resources.
|
||||||
|
* Routes can be used as subrouters: nested routes are only tested if the
|
||||||
|
parent route matches. This is useful to define groups of routes that
|
||||||
|
share common conditions like a host, a path prefix or other repeated
|
||||||
|
attributes. As a bonus, this optimizes request matching.
|
||||||
|
* It implements the http.Handler interface so it is compatible with the
|
||||||
|
standard http.ServeMux.
|
||||||
|
|
||||||
|
Let's start registering a couple of URL paths and handlers:
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/", HomeHandler)
|
||||||
|
r.HandleFunc("/products", ProductsHandler)
|
||||||
|
r.HandleFunc("/articles", ArticlesHandler)
|
||||||
|
http.Handle("/", r)
|
||||||
|
}
|
||||||
|
|
||||||
|
Here we register three routes mapping URL paths to handlers. This is
|
||||||
|
equivalent to how http.HandleFunc() works: if an incoming request URL matches
|
||||||
|
one of the paths, the corresponding handler is called passing
|
||||||
|
(http.ResponseWriter, *http.Request) as parameters.
|
||||||
|
|
||||||
|
Paths can have variables. They are defined using the format {name} or
|
||||||
|
{name:pattern}. If a regular expression pattern is not defined, the matched
|
||||||
|
variable will be anything until the next slash. For example:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/products/{key}", ProductHandler)
|
||||||
|
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
|
||||||
|
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
|
||||||
|
|
||||||
|
The names are used to create a map of route variables which can be retrieved
|
||||||
|
calling mux.Vars():
|
||||||
|
|
||||||
|
vars := mux.Vars(request)
|
||||||
|
category := vars["category"]
|
||||||
|
|
||||||
|
And this is all you need to know about the basic usage. More advanced options
|
||||||
|
are explained below.
|
||||||
|
|
||||||
|
Routes can also be restricted to a domain or subdomain. Just define a host
|
||||||
|
pattern to be matched. They can also have variables:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
// Only matches if domain is "www.example.com".
|
||||||
|
r.Host("www.example.com")
|
||||||
|
// Matches a dynamic subdomain.
|
||||||
|
r.Host("{subdomain:[a-z]+}.domain.com")
|
||||||
|
|
||||||
|
There are several other matchers that can be added. To match path prefixes:
|
||||||
|
|
||||||
|
r.PathPrefix("/products/")
|
||||||
|
|
||||||
|
...or HTTP methods:
|
||||||
|
|
||||||
|
r.Methods("GET", "POST")
|
||||||
|
|
||||||
|
...or URL schemes:
|
||||||
|
|
||||||
|
r.Schemes("https")
|
||||||
|
|
||||||
|
...or header values:
|
||||||
|
|
||||||
|
r.Headers("X-Requested-With", "XMLHttpRequest")
|
||||||
|
|
||||||
|
...or query values:
|
||||||
|
|
||||||
|
r.Queries("key", "value")
|
||||||
|
|
||||||
|
...or to use a custom matcher function:
|
||||||
|
|
||||||
|
r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool {
|
||||||
|
return r.ProtoMajor == 0
|
||||||
|
})
|
||||||
|
|
||||||
|
...and finally, it is possible to combine several matchers in a single route:
|
||||||
|
|
||||||
|
r.HandleFunc("/products", ProductsHandler).
|
||||||
|
Host("www.example.com").
|
||||||
|
Methods("GET").
|
||||||
|
Schemes("http")
|
||||||
|
|
||||||
|
Setting the same matching conditions again and again can be boring, so we have
|
||||||
|
a way to group several routes that share the same requirements.
|
||||||
|
We call it "subrouting".
|
||||||
|
|
||||||
|
For example, let's say we have several URLs that should only match when the
|
||||||
|
host is "www.example.com". Create a route for that host and get a "subrouter"
|
||||||
|
from it:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
s := r.Host("www.example.com").Subrouter()
|
||||||
|
|
||||||
|
Then register routes in the subrouter:
|
||||||
|
|
||||||
|
s.HandleFunc("/products/", ProductsHandler)
|
||||||
|
s.HandleFunc("/products/{key}", ProductHandler)
|
||||||
|
s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler)
|
||||||
|
|
||||||
|
The three URL paths we registered above will only be tested if the domain is
|
||||||
|
"www.example.com", because the subrouter is tested first. This is not
|
||||||
|
only convenient, but also optimizes request matching. You can create
|
||||||
|
subrouters combining any attribute matchers accepted by a route.
|
||||||
|
|
||||||
|
Subrouters can be used to create domain or path "namespaces": you define
|
||||||
|
subrouters in a central place and then parts of the app can register its
|
||||||
|
paths relatively to a given subrouter.
|
||||||
|
|
||||||
|
There's one more thing about subroutes. When a subrouter has a path prefix,
|
||||||
|
the inner routes use it as base for their paths:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
s := r.PathPrefix("/products").Subrouter()
|
||||||
|
// "/products/"
|
||||||
|
s.HandleFunc("/", ProductsHandler)
|
||||||
|
// "/products/{key}/"
|
||||||
|
s.HandleFunc("/{key}/", ProductHandler)
|
||||||
|
// "/products/{key}/details"
|
||||||
|
s.HandleFunc("/{key}/details", ProductDetailsHandler)
|
||||||
|
|
||||||
|
Now let's see how to build registered URLs.
|
||||||
|
|
||||||
|
Routes can be named. All routes that define a name can have their URLs built,
|
||||||
|
or "reversed". We define a name calling Name() on a route. For example:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
|
||||||
|
Name("article")
|
||||||
|
|
||||||
|
To build a URL, get the route and call the URL() method, passing a sequence of
|
||||||
|
key/value pairs for the route variables. For the previous route, we would do:
|
||||||
|
|
||||||
|
url, err := r.Get("article").URL("category", "technology", "id", "42")
|
||||||
|
|
||||||
|
...and the result will be a url.URL with the following path:
|
||||||
|
|
||||||
|
"/articles/technology/42"
|
||||||
|
|
||||||
|
This also works for host variables:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.Host("{subdomain}.domain.com").
|
||||||
|
Path("/articles/{category}/{id:[0-9]+}").
|
||||||
|
HandlerFunc(ArticleHandler).
|
||||||
|
Name("article")
|
||||||
|
|
||||||
|
// url.String() will be "http://news.domain.com/articles/technology/42"
|
||||||
|
url, err := r.Get("article").URL("subdomain", "news",
|
||||||
|
"category", "technology",
|
||||||
|
"id", "42")
|
||||||
|
|
||||||
|
All variables defined in the route are required, and their values must
|
||||||
|
conform to the corresponding patterns. These requirements guarantee that a
|
||||||
|
generated URL will always match a registered route -- the only exception is
|
||||||
|
for explicitly defined "build-only" routes which never match.
|
||||||
|
|
||||||
|
Regex support also exists for matching Headers within a route. For example, we could do:
|
||||||
|
|
||||||
|
r.HeadersRegexp("Content-Type", "application/(text|json)")
|
||||||
|
|
||||||
|
...and the route will match both requests with a Content-Type of `application/json` as well as
|
||||||
|
`application/text`
|
||||||
|
|
||||||
|
There's also a way to build only the URL host or path for a route:
|
||||||
|
use the methods URLHost() or URLPath() instead. For the previous route,
|
||||||
|
we would do:
|
||||||
|
|
||||||
|
// "http://news.domain.com/"
|
||||||
|
host, err := r.Get("article").URLHost("subdomain", "news")
|
||||||
|
|
||||||
|
// "/articles/technology/42"
|
||||||
|
path, err := r.Get("article").URLPath("category", "technology", "id", "42")
|
||||||
|
|
||||||
|
And if you use subrouters, host and path defined separately can be built
|
||||||
|
as well:
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
s := r.Host("{subdomain}.domain.com").Subrouter()
|
||||||
|
s.Path("/articles/{category}/{id:[0-9]+}").
|
||||||
|
HandlerFunc(ArticleHandler).
|
||||||
|
Name("article")
|
||||||
|
|
||||||
|
// "http://news.domain.com/articles/technology/42"
|
||||||
|
url, err := r.Get("article").URL("subdomain", "news",
|
||||||
|
"category", "technology",
|
||||||
|
"id", "42")
|
||||||
|
*/
|
||||||
|
package mux
|
499
vendor/github.com/gorilla/mux/mux.go
generated
vendored
Normal file
499
vendor/github.com/gorilla/mux/mux.go
generated
vendored
Normal file
|
@ -0,0 +1,499 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package mux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/gorilla/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewRouter returns a new router instance.
|
||||||
|
func NewRouter() *Router {
|
||||||
|
return &Router{namedRoutes: make(map[string]*Route), KeepContext: false}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Router registers routes to be matched and dispatches a handler.
|
||||||
|
//
|
||||||
|
// It implements the http.Handler interface, so it can be registered to serve
|
||||||
|
// requests:
|
||||||
|
//
|
||||||
|
// var router = mux.NewRouter()
|
||||||
|
//
|
||||||
|
// func main() {
|
||||||
|
// http.Handle("/", router)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Or, for Google App Engine, register it in a init() function:
|
||||||
|
//
|
||||||
|
// func init() {
|
||||||
|
// http.Handle("/", router)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// This will send all incoming requests to the router.
|
||||||
|
type Router struct {
|
||||||
|
// Configurable Handler to be used when no route matches.
|
||||||
|
NotFoundHandler http.Handler
|
||||||
|
// Parent route, if this is a subrouter.
|
||||||
|
parent parentRoute
|
||||||
|
// Routes to be matched, in order.
|
||||||
|
routes []*Route
|
||||||
|
// Routes by name for URL building.
|
||||||
|
namedRoutes map[string]*Route
|
||||||
|
// See Router.StrictSlash(). This defines the flag for new routes.
|
||||||
|
strictSlash bool
|
||||||
|
// See Router.SkipClean(). This defines the flag for new routes.
|
||||||
|
skipClean bool
|
||||||
|
// If true, do not clear the request context after handling the request
|
||||||
|
KeepContext bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match matches registered routes against the request.
|
||||||
|
func (r *Router) Match(req *http.Request, match *RouteMatch) bool {
|
||||||
|
for _, route := range r.routes {
|
||||||
|
if route.Match(req, match) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Closest match for a router (includes sub-routers)
|
||||||
|
if r.NotFoundHandler != nil {
|
||||||
|
match.Handler = r.NotFoundHandler
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP dispatches the handler registered in the matched route.
|
||||||
|
//
|
||||||
|
// When there is a match, the route variables can be retrieved calling
|
||||||
|
// mux.Vars(request).
|
||||||
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
if !r.skipClean {
|
||||||
|
// Clean path to canonical form and redirect.
|
||||||
|
if p := cleanPath(req.URL.Path); p != req.URL.Path {
|
||||||
|
|
||||||
|
// Added 3 lines (Philip Schlump) - It was dropping the query string and #whatever from query.
|
||||||
|
// This matches with fix in go 1.2 r.c. 4 for same problem. Go Issue:
|
||||||
|
// http://code.google.com/p/go/issues/detail?id=5252
|
||||||
|
url := *req.URL
|
||||||
|
url.Path = p
|
||||||
|
p = url.String()
|
||||||
|
|
||||||
|
w.Header().Set("Location", p)
|
||||||
|
w.WriteHeader(http.StatusMovedPermanently)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var match RouteMatch
|
||||||
|
var handler http.Handler
|
||||||
|
if r.Match(req, &match) {
|
||||||
|
handler = match.Handler
|
||||||
|
setVars(req, match.Vars)
|
||||||
|
setCurrentRoute(req, match.Route)
|
||||||
|
}
|
||||||
|
if handler == nil {
|
||||||
|
handler = http.NotFoundHandler()
|
||||||
|
}
|
||||||
|
if !r.KeepContext {
|
||||||
|
defer context.Clear(req)
|
||||||
|
}
|
||||||
|
handler.ServeHTTP(w, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns a route registered with the given name.
|
||||||
|
func (r *Router) Get(name string) *Route {
|
||||||
|
return r.getNamedRoutes()[name]
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRoute returns a route registered with the given name. This method
|
||||||
|
// was renamed to Get() and remains here for backwards compatibility.
|
||||||
|
func (r *Router) GetRoute(name string) *Route {
|
||||||
|
return r.getNamedRoutes()[name]
|
||||||
|
}
|
||||||
|
|
||||||
|
// StrictSlash defines the trailing slash behavior for new routes. The initial
|
||||||
|
// value is false.
|
||||||
|
//
|
||||||
|
// When true, if the route path is "/path/", accessing "/path" will redirect
|
||||||
|
// to the former and vice versa. In other words, your application will always
|
||||||
|
// see the path as specified in the route.
|
||||||
|
//
|
||||||
|
// When false, if the route path is "/path", accessing "/path/" will not match
|
||||||
|
// this route and vice versa.
|
||||||
|
//
|
||||||
|
// Special case: when a route sets a path prefix using the PathPrefix() method,
|
||||||
|
// strict slash is ignored for that route because the redirect behavior can't
|
||||||
|
// be determined from a prefix alone. However, any subrouters created from that
|
||||||
|
// route inherit the original StrictSlash setting.
|
||||||
|
func (r *Router) StrictSlash(value bool) *Router {
|
||||||
|
r.strictSlash = value
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SkipClean defines the path cleaning behaviour for new routes. The initial
|
||||||
|
// value is false. Users should be careful about which routes are not cleaned
|
||||||
|
//
|
||||||
|
// When true, if the route path is "/path//to", it will remain with the double
|
||||||
|
// slash. This is helpful if you have a route like: /fetch/http://xkcd.com/534/
|
||||||
|
//
|
||||||
|
// When false, the path will be cleaned, so /fetch/http://xkcd.com/534/ will
|
||||||
|
// become /fetch/http/xkcd.com/534
|
||||||
|
func (r *Router) SkipClean(value bool) *Router {
|
||||||
|
r.skipClean = value
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// parentRoute
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// getNamedRoutes returns the map where named routes are registered.
|
||||||
|
func (r *Router) getNamedRoutes() map[string]*Route {
|
||||||
|
if r.namedRoutes == nil {
|
||||||
|
if r.parent != nil {
|
||||||
|
r.namedRoutes = r.parent.getNamedRoutes()
|
||||||
|
} else {
|
||||||
|
r.namedRoutes = make(map[string]*Route)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r.namedRoutes
|
||||||
|
}
|
||||||
|
|
||||||
|
// getRegexpGroup returns regexp definitions from the parent route, if any.
|
||||||
|
func (r *Router) getRegexpGroup() *routeRegexpGroup {
|
||||||
|
if r.parent != nil {
|
||||||
|
return r.parent.getRegexpGroup()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Router) buildVars(m map[string]string) map[string]string {
|
||||||
|
if r.parent != nil {
|
||||||
|
m = r.parent.buildVars(m)
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Route factories
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// NewRoute registers an empty route.
|
||||||
|
func (r *Router) NewRoute() *Route {
|
||||||
|
route := &Route{parent: r, strictSlash: r.strictSlash, skipClean: r.skipClean}
|
||||||
|
r.routes = append(r.routes, route)
|
||||||
|
return route
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle registers a new route with a matcher for the URL path.
|
||||||
|
// See Route.Path() and Route.Handler().
|
||||||
|
func (r *Router) Handle(path string, handler http.Handler) *Route {
|
||||||
|
return r.NewRoute().Path(path).Handler(handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleFunc registers a new route with a matcher for the URL path.
|
||||||
|
// See Route.Path() and Route.HandlerFunc().
|
||||||
|
func (r *Router) HandleFunc(path string, f func(http.ResponseWriter,
|
||||||
|
*http.Request)) *Route {
|
||||||
|
return r.NewRoute().Path(path).HandlerFunc(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Headers registers a new route with a matcher for request header values.
|
||||||
|
// See Route.Headers().
|
||||||
|
func (r *Router) Headers(pairs ...string) *Route {
|
||||||
|
return r.NewRoute().Headers(pairs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Host registers a new route with a matcher for the URL host.
|
||||||
|
// See Route.Host().
|
||||||
|
func (r *Router) Host(tpl string) *Route {
|
||||||
|
return r.NewRoute().Host(tpl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MatcherFunc registers a new route with a custom matcher function.
|
||||||
|
// See Route.MatcherFunc().
|
||||||
|
func (r *Router) MatcherFunc(f MatcherFunc) *Route {
|
||||||
|
return r.NewRoute().MatcherFunc(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Methods registers a new route with a matcher for HTTP methods.
|
||||||
|
// See Route.Methods().
|
||||||
|
func (r *Router) Methods(methods ...string) *Route {
|
||||||
|
return r.NewRoute().Methods(methods...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path registers a new route with a matcher for the URL path.
|
||||||
|
// See Route.Path().
|
||||||
|
func (r *Router) Path(tpl string) *Route {
|
||||||
|
return r.NewRoute().Path(tpl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PathPrefix registers a new route with a matcher for the URL path prefix.
|
||||||
|
// See Route.PathPrefix().
|
||||||
|
func (r *Router) PathPrefix(tpl string) *Route {
|
||||||
|
return r.NewRoute().PathPrefix(tpl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Queries registers a new route with a matcher for URL query values.
|
||||||
|
// See Route.Queries().
|
||||||
|
func (r *Router) Queries(pairs ...string) *Route {
|
||||||
|
return r.NewRoute().Queries(pairs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schemes registers a new route with a matcher for URL schemes.
|
||||||
|
// See Route.Schemes().
|
||||||
|
func (r *Router) Schemes(schemes ...string) *Route {
|
||||||
|
return r.NewRoute().Schemes(schemes...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildVarsFunc registers a new route with a custom function for modifying
|
||||||
|
// route variables before building a URL.
|
||||||
|
func (r *Router) BuildVarsFunc(f BuildVarsFunc) *Route {
|
||||||
|
return r.NewRoute().BuildVarsFunc(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Walk walks the router and all its sub-routers, calling walkFn for each route
|
||||||
|
// in the tree. The routes are walked in the order they were added. Sub-routers
|
||||||
|
// are explored depth-first.
|
||||||
|
func (r *Router) Walk(walkFn WalkFunc) error {
|
||||||
|
return r.walk(walkFn, []*Route{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SkipRouter is used as a return value from WalkFuncs to indicate that the
|
||||||
|
// router that walk is about to descend down to should be skipped.
|
||||||
|
var SkipRouter = errors.New("skip this router")
|
||||||
|
|
||||||
|
// WalkFunc is the type of the function called for each route visited by Walk.
|
||||||
|
// At every invocation, it is given the current route, and the current router,
|
||||||
|
// and a list of ancestor routes that lead to the current route.
|
||||||
|
type WalkFunc func(route *Route, router *Router, ancestors []*Route) error
|
||||||
|
|
||||||
|
func (r *Router) walk(walkFn WalkFunc, ancestors []*Route) error {
|
||||||
|
for _, t := range r.routes {
|
||||||
|
if t.regexp == nil || t.regexp.path == nil || t.regexp.path.template == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err := walkFn(t, r, ancestors)
|
||||||
|
if err == SkipRouter {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, sr := range t.matchers {
|
||||||
|
if h, ok := sr.(*Router); ok {
|
||||||
|
err := h.walk(walkFn, ancestors)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if h, ok := t.handler.(*Router); ok {
|
||||||
|
ancestors = append(ancestors, t)
|
||||||
|
err := h.walk(walkFn, ancestors)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ancestors = ancestors[:len(ancestors)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Context
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// RouteMatch stores information about a matched route.
|
||||||
|
type RouteMatch struct {
|
||||||
|
Route *Route
|
||||||
|
Handler http.Handler
|
||||||
|
Vars map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
type contextKey int
|
||||||
|
|
||||||
|
const (
|
||||||
|
varsKey contextKey = iota
|
||||||
|
routeKey
|
||||||
|
)
|
||||||
|
|
||||||
|
// Vars returns the route variables for the current request, if any.
|
||||||
|
func Vars(r *http.Request) map[string]string {
|
||||||
|
if rv := context.Get(r, varsKey); rv != nil {
|
||||||
|
return rv.(map[string]string)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurrentRoute returns the matched route for the current request, if any.
|
||||||
|
// This only works when called inside the handler of the matched route
|
||||||
|
// because the matched route is stored in the request context which is cleared
|
||||||
|
// after the handler returns, unless the KeepContext option is set on the
|
||||||
|
// Router.
|
||||||
|
func CurrentRoute(r *http.Request) *Route {
|
||||||
|
if rv := context.Get(r, routeKey); rv != nil {
|
||||||
|
return rv.(*Route)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setVars(r *http.Request, val interface{}) {
|
||||||
|
if val != nil {
|
||||||
|
context.Set(r, varsKey, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func setCurrentRoute(r *http.Request, val interface{}) {
|
||||||
|
if val != nil {
|
||||||
|
context.Set(r, routeKey, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Helpers
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// cleanPath returns the canonical path for p, eliminating . and .. elements.
|
||||||
|
// Borrowed from the net/http package.
|
||||||
|
func cleanPath(p string) string {
|
||||||
|
if p == "" {
|
||||||
|
return "/"
|
||||||
|
}
|
||||||
|
if p[0] != '/' {
|
||||||
|
p = "/" + p
|
||||||
|
}
|
||||||
|
np := path.Clean(p)
|
||||||
|
// path.Clean removes trailing slash except for root;
|
||||||
|
// put the trailing slash back if necessary.
|
||||||
|
if p[len(p)-1] == '/' && np != "/" {
|
||||||
|
np += "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
return np
|
||||||
|
}
|
||||||
|
|
||||||
|
// uniqueVars returns an error if two slices contain duplicated strings.
|
||||||
|
func uniqueVars(s1, s2 []string) error {
|
||||||
|
for _, v1 := range s1 {
|
||||||
|
for _, v2 := range s2 {
|
||||||
|
if v1 == v2 {
|
||||||
|
return fmt.Errorf("mux: duplicated route variable %q", v2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkPairs returns the count of strings passed in, and an error if
|
||||||
|
// the count is not an even number.
|
||||||
|
func checkPairs(pairs ...string) (int, error) {
|
||||||
|
length := len(pairs)
|
||||||
|
if length%2 != 0 {
|
||||||
|
return length, fmt.Errorf(
|
||||||
|
"mux: number of parameters must be multiple of 2, got %v", pairs)
|
||||||
|
}
|
||||||
|
return length, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// mapFromPairsToString converts variadic string parameters to a
|
||||||
|
// string to string map.
|
||||||
|
func mapFromPairsToString(pairs ...string) (map[string]string, error) {
|
||||||
|
length, err := checkPairs(pairs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
m := make(map[string]string, length/2)
|
||||||
|
for i := 0; i < length; i += 2 {
|
||||||
|
m[pairs[i]] = pairs[i+1]
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// mapFromPairsToRegex converts variadic string paramers to a
|
||||||
|
// string to regex map.
|
||||||
|
func mapFromPairsToRegex(pairs ...string) (map[string]*regexp.Regexp, error) {
|
||||||
|
length, err := checkPairs(pairs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
m := make(map[string]*regexp.Regexp, length/2)
|
||||||
|
for i := 0; i < length; i += 2 {
|
||||||
|
regex, err := regexp.Compile(pairs[i+1])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
m[pairs[i]] = regex
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// matchInArray returns true if the given string value is in the array.
|
||||||
|
func matchInArray(arr []string, value string) bool {
|
||||||
|
for _, v := range arr {
|
||||||
|
if v == value {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// matchMapWithString returns true if the given key/value pairs exist in a given map.
|
||||||
|
func matchMapWithString(toCheck map[string]string, toMatch map[string][]string, canonicalKey bool) bool {
|
||||||
|
for k, v := range toCheck {
|
||||||
|
// Check if key exists.
|
||||||
|
if canonicalKey {
|
||||||
|
k = http.CanonicalHeaderKey(k)
|
||||||
|
}
|
||||||
|
if values := toMatch[k]; values == nil {
|
||||||
|
return false
|
||||||
|
} else if v != "" {
|
||||||
|
// If value was defined as an empty string we only check that the
|
||||||
|
// key exists. Otherwise we also check for equality.
|
||||||
|
valueExists := false
|
||||||
|
for _, value := range values {
|
||||||
|
if v == value {
|
||||||
|
valueExists = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !valueExists {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// matchMapWithRegex returns true if the given key/value pairs exist in a given map compiled against
|
||||||
|
// the given regex
|
||||||
|
func matchMapWithRegex(toCheck map[string]*regexp.Regexp, toMatch map[string][]string, canonicalKey bool) bool {
|
||||||
|
for k, v := range toCheck {
|
||||||
|
// Check if key exists.
|
||||||
|
if canonicalKey {
|
||||||
|
k = http.CanonicalHeaderKey(k)
|
||||||
|
}
|
||||||
|
if values := toMatch[k]; values == nil {
|
||||||
|
return false
|
||||||
|
} else if v != nil {
|
||||||
|
// If value was defined as an empty string we only check that the
|
||||||
|
// key exists. Otherwise we also check for equality.
|
||||||
|
valueExists := false
|
||||||
|
for _, value := range values {
|
||||||
|
if v.MatchString(value) {
|
||||||
|
valueExists = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !valueExists {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
312
vendor/github.com/gorilla/mux/regexp.go
generated
vendored
Normal file
312
vendor/github.com/gorilla/mux/regexp.go
generated
vendored
Normal file
|
@ -0,0 +1,312 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package mux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// newRouteRegexp parses a route template and returns a routeRegexp,
|
||||||
|
// used to match a host, a path or a query string.
|
||||||
|
//
|
||||||
|
// It will extract named variables, assemble a regexp to be matched, create
|
||||||
|
// a "reverse" template to build URLs and compile regexps to validate variable
|
||||||
|
// values used in URL building.
|
||||||
|
//
|
||||||
|
// Previously we accepted only Python-like identifiers for variable
|
||||||
|
// names ([a-zA-Z_][a-zA-Z0-9_]*), but currently the only restriction is that
|
||||||
|
// name and pattern can't be empty, and names can't contain a colon.
|
||||||
|
func newRouteRegexp(tpl string, matchHost, matchPrefix, matchQuery, strictSlash bool) (*routeRegexp, error) {
|
||||||
|
// Check if it is well-formed.
|
||||||
|
idxs, errBraces := braceIndices(tpl)
|
||||||
|
if errBraces != nil {
|
||||||
|
return nil, errBraces
|
||||||
|
}
|
||||||
|
// Backup the original.
|
||||||
|
template := tpl
|
||||||
|
// Now let's parse it.
|
||||||
|
defaultPattern := "[^/]+"
|
||||||
|
if matchQuery {
|
||||||
|
defaultPattern = "[^?&]*"
|
||||||
|
} else if matchHost {
|
||||||
|
defaultPattern = "[^.]+"
|
||||||
|
matchPrefix = false
|
||||||
|
}
|
||||||
|
// Only match strict slash if not matching
|
||||||
|
if matchPrefix || matchHost || matchQuery {
|
||||||
|
strictSlash = false
|
||||||
|
}
|
||||||
|
// Set a flag for strictSlash.
|
||||||
|
endSlash := false
|
||||||
|
if strictSlash && strings.HasSuffix(tpl, "/") {
|
||||||
|
tpl = tpl[:len(tpl)-1]
|
||||||
|
endSlash = true
|
||||||
|
}
|
||||||
|
varsN := make([]string, len(idxs)/2)
|
||||||
|
varsR := make([]*regexp.Regexp, len(idxs)/2)
|
||||||
|
pattern := bytes.NewBufferString("")
|
||||||
|
pattern.WriteByte('^')
|
||||||
|
reverse := bytes.NewBufferString("")
|
||||||
|
var end int
|
||||||
|
var err error
|
||||||
|
for i := 0; i < len(idxs); i += 2 {
|
||||||
|
// Set all values we are interested in.
|
||||||
|
raw := tpl[end:idxs[i]]
|
||||||
|
end = idxs[i+1]
|
||||||
|
parts := strings.SplitN(tpl[idxs[i]+1:end-1], ":", 2)
|
||||||
|
name := parts[0]
|
||||||
|
patt := defaultPattern
|
||||||
|
if len(parts) == 2 {
|
||||||
|
patt = parts[1]
|
||||||
|
}
|
||||||
|
// Name or pattern can't be empty.
|
||||||
|
if name == "" || patt == "" {
|
||||||
|
return nil, fmt.Errorf("mux: missing name or pattern in %q",
|
||||||
|
tpl[idxs[i]:end])
|
||||||
|
}
|
||||||
|
// Build the regexp pattern.
|
||||||
|
fmt.Fprintf(pattern, "%s(?P<%s>%s)", regexp.QuoteMeta(raw), varGroupName(i/2), patt)
|
||||||
|
|
||||||
|
// Build the reverse template.
|
||||||
|
fmt.Fprintf(reverse, "%s%%s", raw)
|
||||||
|
|
||||||
|
// Append variable name and compiled pattern.
|
||||||
|
varsN[i/2] = name
|
||||||
|
varsR[i/2], err = regexp.Compile(fmt.Sprintf("^%s$", patt))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Add the remaining.
|
||||||
|
raw := tpl[end:]
|
||||||
|
pattern.WriteString(regexp.QuoteMeta(raw))
|
||||||
|
if strictSlash {
|
||||||
|
pattern.WriteString("[/]?")
|
||||||
|
}
|
||||||
|
if matchQuery {
|
||||||
|
// Add the default pattern if the query value is empty
|
||||||
|
if queryVal := strings.SplitN(template, "=", 2)[1]; queryVal == "" {
|
||||||
|
pattern.WriteString(defaultPattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !matchPrefix {
|
||||||
|
pattern.WriteByte('$')
|
||||||
|
}
|
||||||
|
reverse.WriteString(raw)
|
||||||
|
if endSlash {
|
||||||
|
reverse.WriteByte('/')
|
||||||
|
}
|
||||||
|
// Compile full regexp.
|
||||||
|
reg, errCompile := regexp.Compile(pattern.String())
|
||||||
|
if errCompile != nil {
|
||||||
|
return nil, errCompile
|
||||||
|
}
|
||||||
|
// Done!
|
||||||
|
return &routeRegexp{
|
||||||
|
template: template,
|
||||||
|
matchHost: matchHost,
|
||||||
|
matchQuery: matchQuery,
|
||||||
|
strictSlash: strictSlash,
|
||||||
|
regexp: reg,
|
||||||
|
reverse: reverse.String(),
|
||||||
|
varsN: varsN,
|
||||||
|
varsR: varsR,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// routeRegexp stores a regexp to match a host or path and information to
|
||||||
|
// collect and validate route variables.
|
||||||
|
type routeRegexp struct {
|
||||||
|
// The unmodified template.
|
||||||
|
template string
|
||||||
|
// True for host match, false for path or query string match.
|
||||||
|
matchHost bool
|
||||||
|
// True for query string match, false for path and host match.
|
||||||
|
matchQuery bool
|
||||||
|
// The strictSlash value defined on the route, but disabled if PathPrefix was used.
|
||||||
|
strictSlash bool
|
||||||
|
// Expanded regexp.
|
||||||
|
regexp *regexp.Regexp
|
||||||
|
// Reverse template.
|
||||||
|
reverse string
|
||||||
|
// Variable names.
|
||||||
|
varsN []string
|
||||||
|
// Variable regexps (validators).
|
||||||
|
varsR []*regexp.Regexp
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match matches the regexp against the URL host or path.
|
||||||
|
func (r *routeRegexp) Match(req *http.Request, match *RouteMatch) bool {
|
||||||
|
if !r.matchHost {
|
||||||
|
if r.matchQuery {
|
||||||
|
return r.matchQueryString(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.regexp.MatchString(req.URL.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.regexp.MatchString(getHost(req))
|
||||||
|
}
|
||||||
|
|
||||||
|
// url builds a URL part using the given values.
|
||||||
|
func (r *routeRegexp) url(values map[string]string) (string, error) {
|
||||||
|
urlValues := make([]interface{}, len(r.varsN))
|
||||||
|
for k, v := range r.varsN {
|
||||||
|
value, ok := values[v]
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("mux: missing route variable %q", v)
|
||||||
|
}
|
||||||
|
urlValues[k] = value
|
||||||
|
}
|
||||||
|
rv := fmt.Sprintf(r.reverse, urlValues...)
|
||||||
|
if !r.regexp.MatchString(rv) {
|
||||||
|
// The URL is checked against the full regexp, instead of checking
|
||||||
|
// individual variables. This is faster but to provide a good error
|
||||||
|
// message, we check individual regexps if the URL doesn't match.
|
||||||
|
for k, v := range r.varsN {
|
||||||
|
if !r.varsR[k].MatchString(values[v]) {
|
||||||
|
return "", fmt.Errorf(
|
||||||
|
"mux: variable %q doesn't match, expected %q", values[v],
|
||||||
|
r.varsR[k].String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rv, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getURLQuery returns a single query parameter from a request URL.
|
||||||
|
// For a URL with foo=bar&baz=ding, we return only the relevant key
|
||||||
|
// value pair for the routeRegexp.
|
||||||
|
func (r *routeRegexp) getURLQuery(req *http.Request) string {
|
||||||
|
if !r.matchQuery {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
templateKey := strings.SplitN(r.template, "=", 2)[0]
|
||||||
|
for key, vals := range req.URL.Query() {
|
||||||
|
if key == templateKey && len(vals) > 0 {
|
||||||
|
return key + "=" + vals[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *routeRegexp) matchQueryString(req *http.Request) bool {
|
||||||
|
return r.regexp.MatchString(r.getURLQuery(req))
|
||||||
|
}
|
||||||
|
|
||||||
|
// braceIndices returns the first level curly brace indices from a string.
|
||||||
|
// It returns an error in case of unbalanced braces.
|
||||||
|
func braceIndices(s string) ([]int, error) {
|
||||||
|
var level, idx int
|
||||||
|
var idxs []int
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
switch s[i] {
|
||||||
|
case '{':
|
||||||
|
if level++; level == 1 {
|
||||||
|
idx = i
|
||||||
|
}
|
||||||
|
case '}':
|
||||||
|
if level--; level == 0 {
|
||||||
|
idxs = append(idxs, idx, i+1)
|
||||||
|
} else if level < 0 {
|
||||||
|
return nil, fmt.Errorf("mux: unbalanced braces in %q", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if level != 0 {
|
||||||
|
return nil, fmt.Errorf("mux: unbalanced braces in %q", s)
|
||||||
|
}
|
||||||
|
return idxs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// varGroupName builds a capturing group name for the indexed variable.
|
||||||
|
func varGroupName(idx int) string {
|
||||||
|
return "v" + strconv.Itoa(idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// routeRegexpGroup
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// routeRegexpGroup groups the route matchers that carry variables.
|
||||||
|
type routeRegexpGroup struct {
|
||||||
|
host *routeRegexp
|
||||||
|
path *routeRegexp
|
||||||
|
queries []*routeRegexp
|
||||||
|
}
|
||||||
|
|
||||||
|
// setMatch extracts the variables from the URL once a route matches.
|
||||||
|
func (v *routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route) {
|
||||||
|
// Store host variables.
|
||||||
|
if v.host != nil {
|
||||||
|
host := getHost(req)
|
||||||
|
matches := v.host.regexp.FindStringSubmatchIndex(host)
|
||||||
|
if len(matches) > 0 {
|
||||||
|
extractVars(host, matches, v.host.varsN, m.Vars)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Store path variables.
|
||||||
|
if v.path != nil {
|
||||||
|
matches := v.path.regexp.FindStringSubmatchIndex(req.URL.Path)
|
||||||
|
if len(matches) > 0 {
|
||||||
|
extractVars(req.URL.Path, matches, v.path.varsN, m.Vars)
|
||||||
|
// Check if we should redirect.
|
||||||
|
if v.path.strictSlash {
|
||||||
|
p1 := strings.HasSuffix(req.URL.Path, "/")
|
||||||
|
p2 := strings.HasSuffix(v.path.template, "/")
|
||||||
|
if p1 != p2 {
|
||||||
|
u, _ := url.Parse(req.URL.String())
|
||||||
|
if p1 {
|
||||||
|
u.Path = u.Path[:len(u.Path)-1]
|
||||||
|
} else {
|
||||||
|
u.Path += "/"
|
||||||
|
}
|
||||||
|
m.Handler = http.RedirectHandler(u.String(), 301)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Store query string variables.
|
||||||
|
for _, q := range v.queries {
|
||||||
|
queryURL := q.getURLQuery(req)
|
||||||
|
matches := q.regexp.FindStringSubmatchIndex(queryURL)
|
||||||
|
if len(matches) > 0 {
|
||||||
|
extractVars(queryURL, matches, q.varsN, m.Vars)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getHost tries its best to return the request host.
|
||||||
|
func getHost(r *http.Request) string {
|
||||||
|
if r.URL.IsAbs() {
|
||||||
|
return r.URL.Host
|
||||||
|
}
|
||||||
|
host := r.Host
|
||||||
|
// Slice off any port information.
|
||||||
|
if i := strings.Index(host, ":"); i != -1 {
|
||||||
|
host = host[:i]
|
||||||
|
}
|
||||||
|
return host
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractVars(input string, matches []int, names []string, output map[string]string) {
|
||||||
|
matchesCount := 0
|
||||||
|
prevEnd := -1
|
||||||
|
for i := 2; i < len(matches) && matchesCount < len(names); i += 2 {
|
||||||
|
if prevEnd < matches[i+1] {
|
||||||
|
value := input[matches[i]:matches[i+1]]
|
||||||
|
output[names[matchesCount]] = value
|
||||||
|
prevEnd = matches[i+1]
|
||||||
|
matchesCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
634
vendor/github.com/gorilla/mux/route.go
generated
vendored
Normal file
634
vendor/github.com/gorilla/mux/route.go
generated
vendored
Normal file
|
@ -0,0 +1,634 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package mux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Route stores information to match a request and build URLs.
|
||||||
|
type Route struct {
|
||||||
|
// Parent where the route was registered (a Router).
|
||||||
|
parent parentRoute
|
||||||
|
// Request handler for the route.
|
||||||
|
handler http.Handler
|
||||||
|
// List of matchers.
|
||||||
|
matchers []matcher
|
||||||
|
// Manager for the variables from host and path.
|
||||||
|
regexp *routeRegexpGroup
|
||||||
|
// If true, when the path pattern is "/path/", accessing "/path" will
|
||||||
|
// redirect to the former and vice versa.
|
||||||
|
strictSlash bool
|
||||||
|
// If true, when the path pattern is "/path//to", accessing "/path//to"
|
||||||
|
// will not redirect
|
||||||
|
skipClean bool
|
||||||
|
// If true, this route never matches: it is only used to build URLs.
|
||||||
|
buildOnly bool
|
||||||
|
// The name used to build URLs.
|
||||||
|
name string
|
||||||
|
// Error resulted from building a route.
|
||||||
|
err error
|
||||||
|
|
||||||
|
buildVarsFunc BuildVarsFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Route) SkipClean() bool {
|
||||||
|
return r.skipClean
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match matches the route against the request.
|
||||||
|
func (r *Route) Match(req *http.Request, match *RouteMatch) bool {
|
||||||
|
if r.buildOnly || r.err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Match everything.
|
||||||
|
for _, m := range r.matchers {
|
||||||
|
if matched := m.Match(req, match); !matched {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Yay, we have a match. Let's collect some info about it.
|
||||||
|
if match.Route == nil {
|
||||||
|
match.Route = r
|
||||||
|
}
|
||||||
|
if match.Handler == nil {
|
||||||
|
match.Handler = r.handler
|
||||||
|
}
|
||||||
|
if match.Vars == nil {
|
||||||
|
match.Vars = make(map[string]string)
|
||||||
|
}
|
||||||
|
// Set variables.
|
||||||
|
if r.regexp != nil {
|
||||||
|
r.regexp.setMatch(req, match, r)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Route attributes
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// GetError returns an error resulted from building the route, if any.
|
||||||
|
func (r *Route) GetError() error {
|
||||||
|
return r.err
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildOnly sets the route to never match: it is only used to build URLs.
|
||||||
|
func (r *Route) BuildOnly() *Route {
|
||||||
|
r.buildOnly = true
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler --------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Handler sets a handler for the route.
|
||||||
|
func (r *Route) Handler(handler http.Handler) *Route {
|
||||||
|
if r.err == nil {
|
||||||
|
r.handler = handler
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandlerFunc sets a handler function for the route.
|
||||||
|
func (r *Route) HandlerFunc(f func(http.ResponseWriter, *http.Request)) *Route {
|
||||||
|
return r.Handler(http.HandlerFunc(f))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHandler returns the handler for the route, if any.
|
||||||
|
func (r *Route) GetHandler() http.Handler {
|
||||||
|
return r.handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Name sets the name for the route, used to build URLs.
|
||||||
|
// If the name was registered already it will be overwritten.
|
||||||
|
func (r *Route) Name(name string) *Route {
|
||||||
|
if r.name != "" {
|
||||||
|
r.err = fmt.Errorf("mux: route already has name %q, can't set %q",
|
||||||
|
r.name, name)
|
||||||
|
}
|
||||||
|
if r.err == nil {
|
||||||
|
r.name = name
|
||||||
|
r.getNamedRoutes()[name] = r
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetName returns the name for the route, if any.
|
||||||
|
func (r *Route) GetName() string {
|
||||||
|
return r.name
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Matchers
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// matcher types try to match a request.
|
||||||
|
type matcher interface {
|
||||||
|
Match(*http.Request, *RouteMatch) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// addMatcher adds a matcher to the route.
|
||||||
|
func (r *Route) addMatcher(m matcher) *Route {
|
||||||
|
if r.err == nil {
|
||||||
|
r.matchers = append(r.matchers, m)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// addRegexpMatcher adds a host or path matcher and builder to a route.
|
||||||
|
func (r *Route) addRegexpMatcher(tpl string, matchHost, matchPrefix, matchQuery bool) error {
|
||||||
|
if r.err != nil {
|
||||||
|
return r.err
|
||||||
|
}
|
||||||
|
r.regexp = r.getRegexpGroup()
|
||||||
|
if !matchHost && !matchQuery {
|
||||||
|
if len(tpl) == 0 || tpl[0] != '/' {
|
||||||
|
return fmt.Errorf("mux: path must start with a slash, got %q", tpl)
|
||||||
|
}
|
||||||
|
if r.regexp.path != nil {
|
||||||
|
tpl = strings.TrimRight(r.regexp.path.template, "/") + tpl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rr, err := newRouteRegexp(tpl, matchHost, matchPrefix, matchQuery, r.strictSlash)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, q := range r.regexp.queries {
|
||||||
|
if err = uniqueVars(rr.varsN, q.varsN); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if matchHost {
|
||||||
|
if r.regexp.path != nil {
|
||||||
|
if err = uniqueVars(rr.varsN, r.regexp.path.varsN); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r.regexp.host = rr
|
||||||
|
} else {
|
||||||
|
if r.regexp.host != nil {
|
||||||
|
if err = uniqueVars(rr.varsN, r.regexp.host.varsN); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if matchQuery {
|
||||||
|
r.regexp.queries = append(r.regexp.queries, rr)
|
||||||
|
} else {
|
||||||
|
r.regexp.path = rr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r.addMatcher(rr)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Headers --------------------------------------------------------------------
|
||||||
|
|
||||||
|
// headerMatcher matches the request against header values.
|
||||||
|
type headerMatcher map[string]string
|
||||||
|
|
||||||
|
func (m headerMatcher) Match(r *http.Request, match *RouteMatch) bool {
|
||||||
|
return matchMapWithString(m, r.Header, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Headers adds a matcher for request header values.
|
||||||
|
// It accepts a sequence of key/value pairs to be matched. For example:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter()
|
||||||
|
// r.Headers("Content-Type", "application/json",
|
||||||
|
// "X-Requested-With", "XMLHttpRequest")
|
||||||
|
//
|
||||||
|
// The above route will only match if both request header values match.
|
||||||
|
// If the value is an empty string, it will match any value if the key is set.
|
||||||
|
func (r *Route) Headers(pairs ...string) *Route {
|
||||||
|
if r.err == nil {
|
||||||
|
var headers map[string]string
|
||||||
|
headers, r.err = mapFromPairsToString(pairs...)
|
||||||
|
return r.addMatcher(headerMatcher(headers))
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// headerRegexMatcher matches the request against the route given a regex for the header
|
||||||
|
type headerRegexMatcher map[string]*regexp.Regexp
|
||||||
|
|
||||||
|
func (m headerRegexMatcher) Match(r *http.Request, match *RouteMatch) bool {
|
||||||
|
return matchMapWithRegex(m, r.Header, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HeadersRegexp accepts a sequence of key/value pairs, where the value has regex
|
||||||
|
// support. For example:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter()
|
||||||
|
// r.HeadersRegexp("Content-Type", "application/(text|json)",
|
||||||
|
// "X-Requested-With", "XMLHttpRequest")
|
||||||
|
//
|
||||||
|
// The above route will only match if both the request header matches both regular expressions.
|
||||||
|
// It the value is an empty string, it will match any value if the key is set.
|
||||||
|
func (r *Route) HeadersRegexp(pairs ...string) *Route {
|
||||||
|
if r.err == nil {
|
||||||
|
var headers map[string]*regexp.Regexp
|
||||||
|
headers, r.err = mapFromPairsToRegex(pairs...)
|
||||||
|
return r.addMatcher(headerRegexMatcher(headers))
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Host -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Host adds a matcher for the URL host.
|
||||||
|
// It accepts a template with zero or more URL variables enclosed by {}.
|
||||||
|
// Variables can define an optional regexp pattern to be matched:
|
||||||
|
//
|
||||||
|
// - {name} matches anything until the next dot.
|
||||||
|
//
|
||||||
|
// - {name:pattern} matches the given regexp pattern.
|
||||||
|
//
|
||||||
|
// For example:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter()
|
||||||
|
// r.Host("www.example.com")
|
||||||
|
// r.Host("{subdomain}.domain.com")
|
||||||
|
// r.Host("{subdomain:[a-z]+}.domain.com")
|
||||||
|
//
|
||||||
|
// Variable names must be unique in a given route. They can be retrieved
|
||||||
|
// calling mux.Vars(request).
|
||||||
|
func (r *Route) Host(tpl string) *Route {
|
||||||
|
r.err = r.addRegexpMatcher(tpl, true, false, false)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// MatcherFunc ----------------------------------------------------------------
|
||||||
|
|
||||||
|
// MatcherFunc is the function signature used by custom matchers.
|
||||||
|
type MatcherFunc func(*http.Request, *RouteMatch) bool
|
||||||
|
|
||||||
|
// Match returns the match for a given request.
|
||||||
|
func (m MatcherFunc) Match(r *http.Request, match *RouteMatch) bool {
|
||||||
|
return m(r, match)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MatcherFunc adds a custom function to be used as request matcher.
|
||||||
|
func (r *Route) MatcherFunc(f MatcherFunc) *Route {
|
||||||
|
return r.addMatcher(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Methods --------------------------------------------------------------------
|
||||||
|
|
||||||
|
// methodMatcher matches the request against HTTP methods.
|
||||||
|
type methodMatcher []string
|
||||||
|
|
||||||
|
func (m methodMatcher) Match(r *http.Request, match *RouteMatch) bool {
|
||||||
|
return matchInArray(m, r.Method)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Methods adds a matcher for HTTP methods.
|
||||||
|
// It accepts a sequence of one or more methods to be matched, e.g.:
|
||||||
|
// "GET", "POST", "PUT".
|
||||||
|
func (r *Route) Methods(methods ...string) *Route {
|
||||||
|
for k, v := range methods {
|
||||||
|
methods[k] = strings.ToUpper(v)
|
||||||
|
}
|
||||||
|
return r.addMatcher(methodMatcher(methods))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Path adds a matcher for the URL path.
|
||||||
|
// It accepts a template with zero or more URL variables enclosed by {}. The
|
||||||
|
// template must start with a "/".
|
||||||
|
// Variables can define an optional regexp pattern to be matched:
|
||||||
|
//
|
||||||
|
// - {name} matches anything until the next slash.
|
||||||
|
//
|
||||||
|
// - {name:pattern} matches the given regexp pattern.
|
||||||
|
//
|
||||||
|
// For example:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter()
|
||||||
|
// r.Path("/products/").Handler(ProductsHandler)
|
||||||
|
// r.Path("/products/{key}").Handler(ProductsHandler)
|
||||||
|
// r.Path("/articles/{category}/{id:[0-9]+}").
|
||||||
|
// Handler(ArticleHandler)
|
||||||
|
//
|
||||||
|
// Variable names must be unique in a given route. They can be retrieved
|
||||||
|
// calling mux.Vars(request).
|
||||||
|
func (r *Route) Path(tpl string) *Route {
|
||||||
|
r.err = r.addRegexpMatcher(tpl, false, false, false)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// PathPrefix -----------------------------------------------------------------
|
||||||
|
|
||||||
|
// PathPrefix adds a matcher for the URL path prefix. This matches if the given
|
||||||
|
// template is a prefix of the full URL path. See Route.Path() for details on
|
||||||
|
// the tpl argument.
|
||||||
|
//
|
||||||
|
// Note that it does not treat slashes specially ("/foobar/" will be matched by
|
||||||
|
// the prefix "/foo") so you may want to use a trailing slash here.
|
||||||
|
//
|
||||||
|
// Also note that the setting of Router.StrictSlash() has no effect on routes
|
||||||
|
// with a PathPrefix matcher.
|
||||||
|
func (r *Route) PathPrefix(tpl string) *Route {
|
||||||
|
r.err = r.addRegexpMatcher(tpl, false, true, false)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query ----------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Queries adds a matcher for URL query values.
|
||||||
|
// It accepts a sequence of key/value pairs. Values may define variables.
|
||||||
|
// For example:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter()
|
||||||
|
// r.Queries("foo", "bar", "id", "{id:[0-9]+}")
|
||||||
|
//
|
||||||
|
// The above route will only match if the URL contains the defined queries
|
||||||
|
// values, e.g.: ?foo=bar&id=42.
|
||||||
|
//
|
||||||
|
// It the value is an empty string, it will match any value if the key is set.
|
||||||
|
//
|
||||||
|
// Variables can define an optional regexp pattern to be matched:
|
||||||
|
//
|
||||||
|
// - {name} matches anything until the next slash.
|
||||||
|
//
|
||||||
|
// - {name:pattern} matches the given regexp pattern.
|
||||||
|
func (r *Route) Queries(pairs ...string) *Route {
|
||||||
|
length := len(pairs)
|
||||||
|
if length%2 != 0 {
|
||||||
|
r.err = fmt.Errorf(
|
||||||
|
"mux: number of parameters must be multiple of 2, got %v", pairs)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for i := 0; i < length; i += 2 {
|
||||||
|
if r.err = r.addRegexpMatcher(pairs[i]+"="+pairs[i+1], false, false, true); r.err != nil {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schemes --------------------------------------------------------------------
|
||||||
|
|
||||||
|
// schemeMatcher matches the request against URL schemes.
|
||||||
|
type schemeMatcher []string
|
||||||
|
|
||||||
|
func (m schemeMatcher) Match(r *http.Request, match *RouteMatch) bool {
|
||||||
|
return matchInArray(m, r.URL.Scheme)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schemes adds a matcher for URL schemes.
|
||||||
|
// It accepts a sequence of schemes to be matched, e.g.: "http", "https".
|
||||||
|
func (r *Route) Schemes(schemes ...string) *Route {
|
||||||
|
for k, v := range schemes {
|
||||||
|
schemes[k] = strings.ToLower(v)
|
||||||
|
}
|
||||||
|
return r.addMatcher(schemeMatcher(schemes))
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildVarsFunc --------------------------------------------------------------
|
||||||
|
|
||||||
|
// BuildVarsFunc is the function signature used by custom build variable
|
||||||
|
// functions (which can modify route variables before a route's URL is built).
|
||||||
|
type BuildVarsFunc func(map[string]string) map[string]string
|
||||||
|
|
||||||
|
// BuildVarsFunc adds a custom function to be used to modify build variables
|
||||||
|
// before a route's URL is built.
|
||||||
|
func (r *Route) BuildVarsFunc(f BuildVarsFunc) *Route {
|
||||||
|
r.buildVarsFunc = f
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subrouter ------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Subrouter creates a subrouter for the route.
|
||||||
|
//
|
||||||
|
// It will test the inner routes only if the parent route matched. For example:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter()
|
||||||
|
// s := r.Host("www.example.com").Subrouter()
|
||||||
|
// s.HandleFunc("/products/", ProductsHandler)
|
||||||
|
// s.HandleFunc("/products/{key}", ProductHandler)
|
||||||
|
// s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler)
|
||||||
|
//
|
||||||
|
// Here, the routes registered in the subrouter won't be tested if the host
|
||||||
|
// doesn't match.
|
||||||
|
func (r *Route) Subrouter() *Router {
|
||||||
|
router := &Router{parent: r, strictSlash: r.strictSlash}
|
||||||
|
r.addMatcher(router)
|
||||||
|
return router
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// URL building
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// URL builds a URL for the route.
|
||||||
|
//
|
||||||
|
// It accepts a sequence of key/value pairs for the route variables. For
|
||||||
|
// example, given this route:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter()
|
||||||
|
// r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
|
||||||
|
// Name("article")
|
||||||
|
//
|
||||||
|
// ...a URL for it can be built using:
|
||||||
|
//
|
||||||
|
// url, err := r.Get("article").URL("category", "technology", "id", "42")
|
||||||
|
//
|
||||||
|
// ...which will return an url.URL with the following path:
|
||||||
|
//
|
||||||
|
// "/articles/technology/42"
|
||||||
|
//
|
||||||
|
// This also works for host variables:
|
||||||
|
//
|
||||||
|
// r := mux.NewRouter()
|
||||||
|
// r.Host("{subdomain}.domain.com").
|
||||||
|
// HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
|
||||||
|
// Name("article")
|
||||||
|
//
|
||||||
|
// // url.String() will be "http://news.domain.com/articles/technology/42"
|
||||||
|
// url, err := r.Get("article").URL("subdomain", "news",
|
||||||
|
// "category", "technology",
|
||||||
|
// "id", "42")
|
||||||
|
//
|
||||||
|
// All variables defined in the route are required, and their values must
|
||||||
|
// conform to the corresponding patterns.
|
||||||
|
func (r *Route) URL(pairs ...string) (*url.URL, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
if r.regexp == nil {
|
||||||
|
return nil, errors.New("mux: route doesn't have a host or path")
|
||||||
|
}
|
||||||
|
values, err := r.prepareVars(pairs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var scheme, host, path string
|
||||||
|
if r.regexp.host != nil {
|
||||||
|
// Set a default scheme.
|
||||||
|
scheme = "http"
|
||||||
|
if host, err = r.regexp.host.url(values); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if r.regexp.path != nil {
|
||||||
|
if path, err = r.regexp.path.url(values); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &url.URL{
|
||||||
|
Scheme: scheme,
|
||||||
|
Host: host,
|
||||||
|
Path: path,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// URLHost builds the host part of the URL for a route. See Route.URL().
|
||||||
|
//
|
||||||
|
// The route must have a host defined.
|
||||||
|
func (r *Route) URLHost(pairs ...string) (*url.URL, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
if r.regexp == nil || r.regexp.host == nil {
|
||||||
|
return nil, errors.New("mux: route doesn't have a host")
|
||||||
|
}
|
||||||
|
values, err := r.prepareVars(pairs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
host, err := r.regexp.host.url(values)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &url.URL{
|
||||||
|
Scheme: "http",
|
||||||
|
Host: host,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// URLPath builds the path part of the URL for a route. See Route.URL().
|
||||||
|
//
|
||||||
|
// The route must have a path defined.
|
||||||
|
func (r *Route) URLPath(pairs ...string) (*url.URL, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
if r.regexp == nil || r.regexp.path == nil {
|
||||||
|
return nil, errors.New("mux: route doesn't have a path")
|
||||||
|
}
|
||||||
|
values, err := r.prepareVars(pairs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
path, err := r.regexp.path.url(values)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &url.URL{
|
||||||
|
Path: path,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPathTemplate returns the template used to build the
|
||||||
|
// route match.
|
||||||
|
// This is useful for building simple REST API documentation and for instrumentation
|
||||||
|
// against third-party services.
|
||||||
|
// An error will be returned if the route does not define a path.
|
||||||
|
func (r *Route) GetPathTemplate() (string, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return "", r.err
|
||||||
|
}
|
||||||
|
if r.regexp == nil || r.regexp.path == nil {
|
||||||
|
return "", errors.New("mux: route doesn't have a path")
|
||||||
|
}
|
||||||
|
return r.regexp.path.template, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHostTemplate returns the template used to build the
|
||||||
|
// route match.
|
||||||
|
// This is useful for building simple REST API documentation and for instrumentation
|
||||||
|
// against third-party services.
|
||||||
|
// An error will be returned if the route does not define a host.
|
||||||
|
func (r *Route) GetHostTemplate() (string, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return "", r.err
|
||||||
|
}
|
||||||
|
if r.regexp == nil || r.regexp.host == nil {
|
||||||
|
return "", errors.New("mux: route doesn't have a host")
|
||||||
|
}
|
||||||
|
return r.regexp.host.template, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepareVars converts the route variable pairs into a map. If the route has a
|
||||||
|
// BuildVarsFunc, it is invoked.
|
||||||
|
func (r *Route) prepareVars(pairs ...string) (map[string]string, error) {
|
||||||
|
m, err := mapFromPairsToString(pairs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.buildVars(m), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Route) buildVars(m map[string]string) map[string]string {
|
||||||
|
if r.parent != nil {
|
||||||
|
m = r.parent.buildVars(m)
|
||||||
|
}
|
||||||
|
if r.buildVarsFunc != nil {
|
||||||
|
m = r.buildVarsFunc(m)
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// parentRoute
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// parentRoute allows routes to know about parent host and path definitions.
|
||||||
|
type parentRoute interface {
|
||||||
|
getNamedRoutes() map[string]*Route
|
||||||
|
getRegexpGroup() *routeRegexpGroup
|
||||||
|
buildVars(map[string]string) map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
// getNamedRoutes returns the map where named routes are registered.
|
||||||
|
func (r *Route) getNamedRoutes() map[string]*Route {
|
||||||
|
if r.parent == nil {
|
||||||
|
// During tests router is not always set.
|
||||||
|
r.parent = NewRouter()
|
||||||
|
}
|
||||||
|
return r.parent.getNamedRoutes()
|
||||||
|
}
|
||||||
|
|
||||||
|
// getRegexpGroup returns regexp definitions from this route.
|
||||||
|
func (r *Route) getRegexpGroup() *routeRegexpGroup {
|
||||||
|
if r.regexp == nil {
|
||||||
|
if r.parent == nil {
|
||||||
|
// During tests router is not always set.
|
||||||
|
r.parent = NewRouter()
|
||||||
|
}
|
||||||
|
regexp := r.parent.getRegexpGroup()
|
||||||
|
if regexp == nil {
|
||||||
|
r.regexp = new(routeRegexpGroup)
|
||||||
|
} else {
|
||||||
|
// Copy.
|
||||||
|
r.regexp = &routeRegexpGroup{
|
||||||
|
host: regexp.host,
|
||||||
|
path: regexp.path,
|
||||||
|
queries: regexp.queries,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r.regexp
|
||||||
|
}
|
27
vendor/github.com/gorilla/pat/LICENSE
generated
vendored
Normal file
27
vendor/github.com/gorilla/pat/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
Copyright (c) 2012 Rodrigo Moraes. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following disclaimer
|
||||||
|
in the documentation and/or other materials provided with the
|
||||||
|
distribution.
|
||||||
|
* Neither the name of Google Inc. nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
70
vendor/github.com/gorilla/pat/README.md
generated
vendored
Normal file
70
vendor/github.com/gorilla/pat/README.md
generated
vendored
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
pat
|
||||||
|
===
|
||||||
|
[![GoDoc](https://godoc.org/github.com/gorilla/pat?status.svg)](https://godoc.org/github.com/gorilla/pat)
|
||||||
|
[![Build Status](https://travis-ci.org/gorilla/pat.svg?branch=master)](https://travis-ci.org/gorilla/pat)
|
||||||
|
|
||||||
|
### How to use?
|
||||||
|
|
||||||
|
pat is pretty simple. The documentation lives [here](http://www.gorillatoolkit.org/pkg/pat).
|
||||||
|
|
||||||
|
### Install
|
||||||
|
With a properly configured Go toolchain:
|
||||||
|
```sh
|
||||||
|
go get github.com/gorilla/pat
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example
|
||||||
|
|
||||||
|
Here's an example of a RESTful api:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/pat"
|
||||||
|
)
|
||||||
|
|
||||||
|
func homeHandler(wr http.ResponseWriter, req *http.Request) {
|
||||||
|
wr.WriteHeader(http.StatusOK)
|
||||||
|
wr.Write([]byte("Yay! We're home, Jim!"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAllTheThings(wr http.ResponseWriter, req *http.Request) {
|
||||||
|
wr.WriteHeader(http.StatusOK)
|
||||||
|
wr.Write([]byte("Look, Jim! Get all the things!"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func putOneThing(wr http.ResponseWriter, req *http.Request) {
|
||||||
|
wr.WriteHeader(http.StatusOK)
|
||||||
|
wr.Write([]byte("Look, Jim! Put one thing!"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteOneThing(wr http.ResponseWriter, req *http.Request) {
|
||||||
|
wr.WriteHeader(http.StatusOK)
|
||||||
|
wr.Write([]byte("Look, Jim! Delete one thing!"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
router := pat.New()
|
||||||
|
|
||||||
|
router.Get("/things", getAllTheThings)
|
||||||
|
|
||||||
|
router.Put("/things/{id}", putOneThing)
|
||||||
|
router.Delete("/things/{id}", deleteOneThing)
|
||||||
|
|
||||||
|
router.Get("/", homeHandler)
|
||||||
|
|
||||||
|
http.Handle("/", router)
|
||||||
|
|
||||||
|
log.Print("Listening on 127.0.0.1:8000...")
|
||||||
|
log.Fatal(http.ListenAndServe(":8000", nil))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Notice how the routes descend? That's because Pat will take the first route
|
||||||
|
that matches. For your own testing, take the line ```router.Get("/",
|
||||||
|
homeHandler)``` and put it above the other routes and run the example. When you
|
||||||
|
try to curl any of the routes, you'll only get what the homeHandler returns.
|
||||||
|
Design your routes carefully.
|
67
vendor/github.com/gorilla/pat/doc.go
generated
vendored
Normal file
67
vendor/github.com/gorilla/pat/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package gorilla/pat is a request router and dispatcher with a pat-like
|
||||||
|
interface. It is an alternative to gorilla/mux that showcases how it can
|
||||||
|
be used as a base for different API flavors. Package pat is documented at:
|
||||||
|
|
||||||
|
http://godoc.org/github.com/bmizerany/pat
|
||||||
|
|
||||||
|
Let's start registering a couple of URL paths and handlers:
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := pat.New()
|
||||||
|
r.Get("/products", ProductsHandler)
|
||||||
|
r.Get("/articles", ArticlesHandler)
|
||||||
|
r.Get("/", HomeHandler)
|
||||||
|
http.Handle("/", r)
|
||||||
|
}
|
||||||
|
|
||||||
|
Here we register three routes mapping URL paths to handlers. This is
|
||||||
|
equivalent to how http.HandleFunc() works: if an incoming GET request matches
|
||||||
|
one of the paths, the corresponding handler is called passing
|
||||||
|
(http.ResponseWriter, *http.Request) as parameters.
|
||||||
|
|
||||||
|
Note: gorilla/pat matches path prefixes, so you must register the most
|
||||||
|
specific paths first.
|
||||||
|
|
||||||
|
Note: differently from pat, these methods accept a handler function, and not an
|
||||||
|
http.Handler. We think this is shorter and more convenient. To set an
|
||||||
|
http.Handler, use the Add() method.
|
||||||
|
|
||||||
|
Paths can have variables. They are defined using the format {name} or
|
||||||
|
{name:pattern}. If a regular expression pattern is not defined, the matched
|
||||||
|
variable will be anything until the next slash. For example:
|
||||||
|
|
||||||
|
r := pat.New()
|
||||||
|
r.Get("/articles/{category}/{id:[0-9]+}", ArticleHandler)
|
||||||
|
r.Get("/articles/{category}/", ArticlesCategoryHandler)
|
||||||
|
r.Get("/products/{key}", ProductHandler)
|
||||||
|
|
||||||
|
The names are used to create a map of route variables which are stored in the
|
||||||
|
URL query, prefixed by a colon:
|
||||||
|
|
||||||
|
category := req.URL.Query().Get(":category")
|
||||||
|
|
||||||
|
As in the gorilla/mux package, other matchers can be added to the registered
|
||||||
|
routes and URLs can be reversed as well. To build a URL for a route, first
|
||||||
|
add a name to it:
|
||||||
|
|
||||||
|
r.Get("/products/{key}", ProductHandler).Name("product")
|
||||||
|
|
||||||
|
Then you can get it using the name and generate a URL:
|
||||||
|
|
||||||
|
url, err := r.GetRoute("product").URL("key", "transmogrifier")
|
||||||
|
|
||||||
|
...and the result will be a url.URL with the following path:
|
||||||
|
|
||||||
|
"/products/transmogrifier"
|
||||||
|
|
||||||
|
Check the mux documentation for more details about URL building and extra
|
||||||
|
matchers:
|
||||||
|
|
||||||
|
http://gorilla-web.appspot.com/pkg/mux/
|
||||||
|
*/
|
||||||
|
package pat
|
126
vendor/github.com/gorilla/pat/pat.go
generated
vendored
Normal file
126
vendor/github.com/gorilla/pat/pat.go
generated
vendored
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package pat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gorilla/context"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
// New returns a new router.
|
||||||
|
func New() *Router {
|
||||||
|
return &Router{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Router is a request router that implements a pat-like API.
|
||||||
|
//
|
||||||
|
// pat docs: http://godoc.org/github.com/bmizerany/pat
|
||||||
|
type Router struct {
|
||||||
|
mux.Router
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add registers a pattern with a handler for the given request method.
|
||||||
|
func (r *Router) Add(meth, pat string, h http.Handler) *mux.Route {
|
||||||
|
return r.NewRoute().PathPrefix(pat).Handler(h).Methods(meth)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options registers a pattern with a handler for OPTIONS requests.
|
||||||
|
func (r *Router) Options(pat string, h http.HandlerFunc) *mux.Route {
|
||||||
|
return r.Add("OPTIONS", pat, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete registers a pattern with a handler for DELETE requests.
|
||||||
|
func (r *Router) Delete(pat string, h http.HandlerFunc) *mux.Route {
|
||||||
|
return r.Add("DELETE", pat, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Head registers a pattern with a handler for HEAD requests.
|
||||||
|
func (r *Router) Head(pat string, h http.HandlerFunc) *mux.Route {
|
||||||
|
return r.Add("HEAD", pat, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get registers a pattern with a handler for GET requests.
|
||||||
|
func (r *Router) Get(pat string, h http.HandlerFunc) *mux.Route {
|
||||||
|
return r.Add("GET", pat, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Post registers a pattern with a handler for POST requests.
|
||||||
|
func (r *Router) Post(pat string, h http.HandlerFunc) *mux.Route {
|
||||||
|
return r.Add("POST", pat, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put registers a pattern with a handler for PUT requests.
|
||||||
|
func (r *Router) Put(pat string, h http.HandlerFunc) *mux.Route {
|
||||||
|
return r.Add("PUT", pat, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Patch registers a pattern with a handler for PATCH requests.
|
||||||
|
func (r *Router) Patch(pat string, h http.HandlerFunc) *mux.Route {
|
||||||
|
return r.Add("PATCH", pat, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP dispatches the handler registered in the matched route.
|
||||||
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
// Clean path to canonical form and redirect.
|
||||||
|
if p := cleanPath(req.URL.Path); p != req.URL.Path {
|
||||||
|
w.Header().Set("Location", p)
|
||||||
|
w.WriteHeader(http.StatusMovedPermanently)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var match mux.RouteMatch
|
||||||
|
var handler http.Handler
|
||||||
|
if matched := r.Match(req, &match); matched {
|
||||||
|
handler = match.Handler
|
||||||
|
registerVars(req, match.Vars)
|
||||||
|
}
|
||||||
|
if handler == nil {
|
||||||
|
if r.NotFoundHandler == nil {
|
||||||
|
r.NotFoundHandler = http.NotFoundHandler()
|
||||||
|
}
|
||||||
|
handler = r.NotFoundHandler
|
||||||
|
}
|
||||||
|
if !r.KeepContext {
|
||||||
|
defer context.Clear(req)
|
||||||
|
}
|
||||||
|
handler.ServeHTTP(w, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
// registerVars adds the matched route variables to the URL query.
|
||||||
|
func registerVars(r *http.Request, vars map[string]string) {
|
||||||
|
parts, i := make([]string, len(vars)), 0
|
||||||
|
for key, value := range vars {
|
||||||
|
parts[i] = url.QueryEscape(":"+key) + "=" + url.QueryEscape(value)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
q := strings.Join(parts, "&")
|
||||||
|
if r.URL.RawQuery == "" {
|
||||||
|
r.URL.RawQuery = q
|
||||||
|
} else {
|
||||||
|
r.URL.RawQuery += "&" + q
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cleanPath returns the canonical path for p, eliminating . and .. elements.
|
||||||
|
// Borrowed from the net/http package.
|
||||||
|
func cleanPath(p string) string {
|
||||||
|
if p == "" {
|
||||||
|
return "/"
|
||||||
|
}
|
||||||
|
if p[0] != '/' {
|
||||||
|
p = "/" + p
|
||||||
|
}
|
||||||
|
np := path.Clean(p)
|
||||||
|
// path.Clean removes trailing slash except for root;
|
||||||
|
// put the trailing slash back if necessary.
|
||||||
|
if p[len(p)-1] == '/' && np != "/" {
|
||||||
|
np += "/"
|
||||||
|
}
|
||||||
|
return np
|
||||||
|
}
|
8
vendor/github.com/gorilla/websocket/AUTHORS
generated
vendored
Normal file
8
vendor/github.com/gorilla/websocket/AUTHORS
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# This is the official list of Gorilla WebSocket authors for copyright
|
||||||
|
# purposes.
|
||||||
|
#
|
||||||
|
# Please keep the list sorted.
|
||||||
|
|
||||||
|
Gary Burd <gary@beagledreams.com>
|
||||||
|
Joachim Bauch <mail@joachim-bauch.de>
|
||||||
|
|
22
vendor/github.com/gorilla/websocket/LICENSE
generated
vendored
Normal file
22
vendor/github.com/gorilla/websocket/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
Copyright (c) 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
61
vendor/github.com/gorilla/websocket/README.md
generated
vendored
Normal file
61
vendor/github.com/gorilla/websocket/README.md
generated
vendored
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
# Gorilla WebSocket
|
||||||
|
|
||||||
|
Gorilla WebSocket is a [Go](http://golang.org/) implementation of the
|
||||||
|
[WebSocket](http://www.rfc-editor.org/rfc/rfc6455.txt) protocol.
|
||||||
|
|
||||||
|
### Documentation
|
||||||
|
|
||||||
|
* [API Reference](http://godoc.org/github.com/gorilla/websocket)
|
||||||
|
* [Chat example](https://github.com/gorilla/websocket/tree/master/examples/chat)
|
||||||
|
* [Command example](https://github.com/gorilla/websocket/tree/master/examples/command)
|
||||||
|
* [Client and server example](https://github.com/gorilla/websocket/tree/master/examples/echo)
|
||||||
|
* [File watch example](https://github.com/gorilla/websocket/tree/master/examples/filewatch)
|
||||||
|
|
||||||
|
### Status
|
||||||
|
|
||||||
|
The Gorilla WebSocket package provides a complete and tested implementation of
|
||||||
|
the [WebSocket](http://www.rfc-editor.org/rfc/rfc6455.txt) protocol. The
|
||||||
|
package API is stable.
|
||||||
|
|
||||||
|
### Installation
|
||||||
|
|
||||||
|
go get github.com/gorilla/websocket
|
||||||
|
|
||||||
|
### Protocol Compliance
|
||||||
|
|
||||||
|
The Gorilla WebSocket package passes the server tests in the [Autobahn Test
|
||||||
|
Suite](http://autobahn.ws/testsuite) using the application in the [examples/autobahn
|
||||||
|
subdirectory](https://github.com/gorilla/websocket/tree/master/examples/autobahn).
|
||||||
|
|
||||||
|
### Gorilla WebSocket compared with other packages
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th></th>
|
||||||
|
<th><a href="http://godoc.org/github.com/gorilla/websocket">github.com/gorilla</a></th>
|
||||||
|
<th><a href="http://godoc.org/golang.org/x/net/websocket">golang.org/x/net</a></th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<tr><td colspan="3"><a href="http://tools.ietf.org/html/rfc6455">RFC 6455</a> Features</td></tr>
|
||||||
|
<tr><td>Passes <a href="http://autobahn.ws/testsuite/">Autobahn Test Suite</a></td><td><a href="https://github.com/gorilla/websocket/tree/master/examples/autobahn">Yes</a></td><td>No</td></tr>
|
||||||
|
<tr><td>Receive <a href="https://tools.ietf.org/html/rfc6455#section-5.4">fragmented</a> message<td>Yes</td><td><a href="https://code.google.com/p/go/issues/detail?id=7632">No</a>, see note 1</td></tr>
|
||||||
|
<tr><td>Send <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1">close</a> message</td><td><a href="http://godoc.org/github.com/gorilla/websocket#hdr-Control_Messages">Yes</a></td><td><a href="https://code.google.com/p/go/issues/detail?id=4588">No</a></td></tr>
|
||||||
|
<tr><td>Send <a href="https://tools.ietf.org/html/rfc6455#section-5.5.2">pings</a> and receive <a href="https://tools.ietf.org/html/rfc6455#section-5.5.3">pongs</a></td><td><a href="http://godoc.org/github.com/gorilla/websocket#hdr-Control_Messages">Yes</a></td><td>No</td></tr>
|
||||||
|
<tr><td>Get the <a href="https://tools.ietf.org/html/rfc6455#section-5.6">type</a> of a received data message</td><td>Yes</td><td>Yes, see note 2</td></tr>
|
||||||
|
<tr><td colspan="3">Other Features</tr></td>
|
||||||
|
<tr><td>Limit size of received message</td><td><a href="http://godoc.org/github.com/gorilla/websocket#Conn.SetReadLimit">Yes</a></td><td><a href="https://code.google.com/p/go/issues/detail?id=5082">No</a></td></tr>
|
||||||
|
<tr><td>Read message using io.Reader</td><td><a href="http://godoc.org/github.com/gorilla/websocket#Conn.NextReader">Yes</a></td><td>No, see note 3</td></tr>
|
||||||
|
<tr><td>Write message using io.WriteCloser</td><td><a href="http://godoc.org/github.com/gorilla/websocket#Conn.NextWriter">Yes</a></td><td>No, see note 3</td></tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
1. Large messages are fragmented in [Chrome's new WebSocket implementation](http://www.ietf.org/mail-archive/web/hybi/current/msg10503.html).
|
||||||
|
2. The application can get the type of a received data message by implementing
|
||||||
|
a [Codec marshal](http://godoc.org/golang.org/x/net/websocket#Codec.Marshal)
|
||||||
|
function.
|
||||||
|
3. The go.net io.Reader and io.Writer operate across WebSocket frame boundaries.
|
||||||
|
Read returns when the input buffer is full or a frame boundary is
|
||||||
|
encountered. Each call to Write sends a single frame message. The Gorilla
|
||||||
|
io.Reader and io.WriteCloser operate on a single WebSocket message.
|
||||||
|
|
375
vendor/github.com/gorilla/websocket/client.go
generated
vendored
Normal file
375
vendor/github.com/gorilla/websocket/client.go
generated
vendored
Normal file
|
@ -0,0 +1,375 @@
|
||||||
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrBadHandshake is returned when the server response to opening handshake is
|
||||||
|
// invalid.
|
||||||
|
var ErrBadHandshake = errors.New("websocket: bad handshake")
|
||||||
|
|
||||||
|
// NewClient creates a new client connection using the given net connection.
|
||||||
|
// The URL u specifies the host and request URI. Use requestHeader to specify
|
||||||
|
// the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies
|
||||||
|
// (Cookie). Use the response.Header to get the selected subprotocol
|
||||||
|
// (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
|
||||||
|
//
|
||||||
|
// If the WebSocket handshake fails, ErrBadHandshake is returned along with a
|
||||||
|
// non-nil *http.Response so that callers can handle redirects, authentication,
|
||||||
|
// etc.
|
||||||
|
//
|
||||||
|
// Deprecated: Use Dialer instead.
|
||||||
|
func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufSize, writeBufSize int) (c *Conn, response *http.Response, err error) {
|
||||||
|
d := Dialer{
|
||||||
|
ReadBufferSize: readBufSize,
|
||||||
|
WriteBufferSize: writeBufSize,
|
||||||
|
NetDial: func(net, addr string) (net.Conn, error) {
|
||||||
|
return netConn, nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return d.Dial(u.String(), requestHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A Dialer contains options for connecting to WebSocket server.
|
||||||
|
type Dialer struct {
|
||||||
|
// NetDial specifies the dial function for creating TCP connections. If
|
||||||
|
// NetDial is nil, net.Dial is used.
|
||||||
|
NetDial func(network, addr string) (net.Conn, error)
|
||||||
|
|
||||||
|
// Proxy specifies a function to return a proxy for a given
|
||||||
|
// Request. If the function returns a non-nil error, the
|
||||||
|
// request is aborted with the provided error.
|
||||||
|
// If Proxy is nil or returns a nil *URL, no proxy is used.
|
||||||
|
Proxy func(*http.Request) (*url.URL, error)
|
||||||
|
|
||||||
|
// TLSClientConfig specifies the TLS configuration to use with tls.Client.
|
||||||
|
// If nil, the default configuration is used.
|
||||||
|
TLSClientConfig *tls.Config
|
||||||
|
|
||||||
|
// HandshakeTimeout specifies the duration for the handshake to complete.
|
||||||
|
HandshakeTimeout time.Duration
|
||||||
|
|
||||||
|
// Input and output buffer sizes. If the buffer size is zero, then a
|
||||||
|
// default value of 4096 is used.
|
||||||
|
ReadBufferSize, WriteBufferSize int
|
||||||
|
|
||||||
|
// Subprotocols specifies the client's requested subprotocols.
|
||||||
|
Subprotocols []string
|
||||||
|
}
|
||||||
|
|
||||||
|
var errMalformedURL = errors.New("malformed ws or wss URL")
|
||||||
|
|
||||||
|
// parseURL parses the URL.
|
||||||
|
//
|
||||||
|
// This function is a replacement for the standard library url.Parse function.
|
||||||
|
// In Go 1.4 and earlier, url.Parse loses information from the path.
|
||||||
|
func parseURL(s string) (*url.URL, error) {
|
||||||
|
// From the RFC:
|
||||||
|
//
|
||||||
|
// ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]
|
||||||
|
// wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
|
||||||
|
|
||||||
|
var u url.URL
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(s, "ws://"):
|
||||||
|
u.Scheme = "ws"
|
||||||
|
s = s[len("ws://"):]
|
||||||
|
case strings.HasPrefix(s, "wss://"):
|
||||||
|
u.Scheme = "wss"
|
||||||
|
s = s[len("wss://"):]
|
||||||
|
default:
|
||||||
|
return nil, errMalformedURL
|
||||||
|
}
|
||||||
|
|
||||||
|
if i := strings.Index(s, "?"); i >= 0 {
|
||||||
|
u.RawQuery = s[i+1:]
|
||||||
|
s = s[:i]
|
||||||
|
}
|
||||||
|
|
||||||
|
if i := strings.Index(s, "/"); i >= 0 {
|
||||||
|
u.Opaque = s[i:]
|
||||||
|
s = s[:i]
|
||||||
|
} else {
|
||||||
|
u.Opaque = "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
u.Host = s
|
||||||
|
|
||||||
|
if strings.Contains(u.Host, "@") {
|
||||||
|
// Don't bother parsing user information because user information is
|
||||||
|
// not allowed in websocket URIs.
|
||||||
|
return nil, errMalformedURL
|
||||||
|
}
|
||||||
|
|
||||||
|
return &u, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func hostPortNoPort(u *url.URL) (hostPort, hostNoPort string) {
|
||||||
|
hostPort = u.Host
|
||||||
|
hostNoPort = u.Host
|
||||||
|
if i := strings.LastIndex(u.Host, ":"); i > strings.LastIndex(u.Host, "]") {
|
||||||
|
hostNoPort = hostNoPort[:i]
|
||||||
|
} else {
|
||||||
|
switch u.Scheme {
|
||||||
|
case "wss":
|
||||||
|
hostPort += ":443"
|
||||||
|
case "https":
|
||||||
|
hostPort += ":443"
|
||||||
|
default:
|
||||||
|
hostPort += ":80"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hostPort, hostNoPort
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultDialer is a dialer with all fields set to the default zero values.
|
||||||
|
var DefaultDialer = &Dialer{
|
||||||
|
Proxy: http.ProxyFromEnvironment,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dial creates a new client connection. Use requestHeader to specify the
|
||||||
|
// origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie).
|
||||||
|
// Use the response.Header to get the selected subprotocol
|
||||||
|
// (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
|
||||||
|
//
|
||||||
|
// If the WebSocket handshake fails, ErrBadHandshake is returned along with a
|
||||||
|
// non-nil *http.Response so that callers can handle redirects, authentication,
|
||||||
|
// etcetera. The response body may not contain the entire response and does not
|
||||||
|
// need to be closed by the application.
|
||||||
|
func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
|
||||||
|
|
||||||
|
if d == nil {
|
||||||
|
d = &Dialer{
|
||||||
|
Proxy: http.ProxyFromEnvironment,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
challengeKey, err := generateChallengeKey()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
u, err := parseURL(urlStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch u.Scheme {
|
||||||
|
case "ws":
|
||||||
|
u.Scheme = "http"
|
||||||
|
case "wss":
|
||||||
|
u.Scheme = "https"
|
||||||
|
default:
|
||||||
|
return nil, nil, errMalformedURL
|
||||||
|
}
|
||||||
|
|
||||||
|
if u.User != nil {
|
||||||
|
// User name and password are not allowed in websocket URIs.
|
||||||
|
return nil, nil, errMalformedURL
|
||||||
|
}
|
||||||
|
|
||||||
|
req := &http.Request{
|
||||||
|
Method: "GET",
|
||||||
|
URL: u,
|
||||||
|
Proto: "HTTP/1.1",
|
||||||
|
ProtoMajor: 1,
|
||||||
|
ProtoMinor: 1,
|
||||||
|
Header: make(http.Header),
|
||||||
|
Host: u.Host,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the request headers using the capitalization for names and values in
|
||||||
|
// RFC examples. Although the capitalization shouldn't matter, there are
|
||||||
|
// servers that depend on it. The Header.Set method is not used because the
|
||||||
|
// method canonicalizes the header names.
|
||||||
|
req.Header["Upgrade"] = []string{"websocket"}
|
||||||
|
req.Header["Connection"] = []string{"Upgrade"}
|
||||||
|
req.Header["Sec-WebSocket-Key"] = []string{challengeKey}
|
||||||
|
req.Header["Sec-WebSocket-Version"] = []string{"13"}
|
||||||
|
if len(d.Subprotocols) > 0 {
|
||||||
|
req.Header["Sec-WebSocket-Protocol"] = []string{strings.Join(d.Subprotocols, ", ")}
|
||||||
|
}
|
||||||
|
for k, vs := range requestHeader {
|
||||||
|
switch {
|
||||||
|
case k == "Host":
|
||||||
|
if len(vs) > 0 {
|
||||||
|
req.Host = vs[0]
|
||||||
|
}
|
||||||
|
case k == "Upgrade" ||
|
||||||
|
k == "Connection" ||
|
||||||
|
k == "Sec-Websocket-Key" ||
|
||||||
|
k == "Sec-Websocket-Version" ||
|
||||||
|
(k == "Sec-Websocket-Protocol" && len(d.Subprotocols) > 0):
|
||||||
|
return nil, nil, errors.New("websocket: duplicate header not allowed: " + k)
|
||||||
|
default:
|
||||||
|
req.Header[k] = vs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hostPort, hostNoPort := hostPortNoPort(u)
|
||||||
|
|
||||||
|
var proxyURL *url.URL
|
||||||
|
// Check wether the proxy method has been configured
|
||||||
|
if d.Proxy != nil {
|
||||||
|
proxyURL, err = d.Proxy(req)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var targetHostPort string
|
||||||
|
if proxyURL != nil {
|
||||||
|
targetHostPort, _ = hostPortNoPort(proxyURL)
|
||||||
|
} else {
|
||||||
|
targetHostPort = hostPort
|
||||||
|
}
|
||||||
|
|
||||||
|
var deadline time.Time
|
||||||
|
if d.HandshakeTimeout != 0 {
|
||||||
|
deadline = time.Now().Add(d.HandshakeTimeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
netDial := d.NetDial
|
||||||
|
if netDial == nil {
|
||||||
|
netDialer := &net.Dialer{Deadline: deadline}
|
||||||
|
netDial = netDialer.Dial
|
||||||
|
}
|
||||||
|
|
||||||
|
netConn, err := netDial("tcp", targetHostPort)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if netConn != nil {
|
||||||
|
netConn.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := netConn.SetDeadline(deadline); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if proxyURL != nil {
|
||||||
|
connectHeader := make(http.Header)
|
||||||
|
if user := proxyURL.User; user != nil {
|
||||||
|
proxyUser := user.Username()
|
||||||
|
if proxyPassword, passwordSet := user.Password(); passwordSet {
|
||||||
|
credential := base64.StdEncoding.EncodeToString([]byte(proxyUser + ":" + proxyPassword))
|
||||||
|
connectHeader.Set("Proxy-Authorization", "Basic "+credential)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
connectReq := &http.Request{
|
||||||
|
Method: "CONNECT",
|
||||||
|
URL: &url.URL{Opaque: hostPort},
|
||||||
|
Host: hostPort,
|
||||||
|
Header: connectHeader,
|
||||||
|
}
|
||||||
|
|
||||||
|
connectReq.Write(netConn)
|
||||||
|
|
||||||
|
// Read response.
|
||||||
|
// Okay to use and discard buffered reader here, because
|
||||||
|
// TLS server will not speak until spoken to.
|
||||||
|
br := bufio.NewReader(netConn)
|
||||||
|
resp, err := http.ReadResponse(br, connectReq)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
f := strings.SplitN(resp.Status, " ", 2)
|
||||||
|
return nil, nil, errors.New(f[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if u.Scheme == "https" {
|
||||||
|
cfg := cloneTLSConfig(d.TLSClientConfig)
|
||||||
|
if cfg.ServerName == "" {
|
||||||
|
cfg.ServerName = hostNoPort
|
||||||
|
}
|
||||||
|
tlsConn := tls.Client(netConn, cfg)
|
||||||
|
netConn = tlsConn
|
||||||
|
if err := tlsConn.Handshake(); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if !cfg.InsecureSkipVerify {
|
||||||
|
if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize)
|
||||||
|
|
||||||
|
if err := req.Write(netConn); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.ReadResponse(conn.br, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if resp.StatusCode != 101 ||
|
||||||
|
!strings.EqualFold(resp.Header.Get("Upgrade"), "websocket") ||
|
||||||
|
!strings.EqualFold(resp.Header.Get("Connection"), "upgrade") ||
|
||||||
|
resp.Header.Get("Sec-Websocket-Accept") != computeAcceptKey(challengeKey) {
|
||||||
|
// Before closing the network connection on return from this
|
||||||
|
// function, slurp up some of the response to aid application
|
||||||
|
// debugging.
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
n, _ := io.ReadFull(resp.Body, buf)
|
||||||
|
resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n]))
|
||||||
|
return nil, resp, ErrBadHandshake
|
||||||
|
}
|
||||||
|
|
||||||
|
resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
|
||||||
|
conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")
|
||||||
|
|
||||||
|
netConn.SetDeadline(time.Time{})
|
||||||
|
netConn = nil // to avoid close in defer.
|
||||||
|
return conn, resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// cloneTLSConfig clones all public fields except the fields
|
||||||
|
// SessionTicketsDisabled and SessionTicketKey. This avoids copying the
|
||||||
|
// sync.Mutex in the sync.Once and makes it safe to call cloneTLSConfig on a
|
||||||
|
// config in active use.
|
||||||
|
func cloneTLSConfig(cfg *tls.Config) *tls.Config {
|
||||||
|
if cfg == nil {
|
||||||
|
return &tls.Config{}
|
||||||
|
}
|
||||||
|
return &tls.Config{
|
||||||
|
Rand: cfg.Rand,
|
||||||
|
Time: cfg.Time,
|
||||||
|
Certificates: cfg.Certificates,
|
||||||
|
NameToCertificate: cfg.NameToCertificate,
|
||||||
|
GetCertificate: cfg.GetCertificate,
|
||||||
|
RootCAs: cfg.RootCAs,
|
||||||
|
NextProtos: cfg.NextProtos,
|
||||||
|
ServerName: cfg.ServerName,
|
||||||
|
ClientAuth: cfg.ClientAuth,
|
||||||
|
ClientCAs: cfg.ClientCAs,
|
||||||
|
InsecureSkipVerify: cfg.InsecureSkipVerify,
|
||||||
|
CipherSuites: cfg.CipherSuites,
|
||||||
|
PreferServerCipherSuites: cfg.PreferServerCipherSuites,
|
||||||
|
ClientSessionCache: cfg.ClientSessionCache,
|
||||||
|
MinVersion: cfg.MinVersion,
|
||||||
|
MaxVersion: cfg.MaxVersion,
|
||||||
|
CurvePreferences: cfg.CurvePreferences,
|
||||||
|
}
|
||||||
|
}
|
85
vendor/github.com/gorilla/websocket/compression.go
generated
vendored
Normal file
85
vendor/github.com/gorilla/websocket/compression.go
generated
vendored
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"compress/flate"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func decompressNoContextTakeover(r io.Reader) io.Reader {
|
||||||
|
const tail =
|
||||||
|
// Add four bytes as specified in RFC
|
||||||
|
"\x00\x00\xff\xff" +
|
||||||
|
// Add final block to squelch unexpected EOF error from flate reader.
|
||||||
|
"\x01\x00\x00\xff\xff"
|
||||||
|
|
||||||
|
return flate.NewReader(io.MultiReader(r, strings.NewReader(tail)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func compressNoContextTakeover(w io.WriteCloser) (io.WriteCloser, error) {
|
||||||
|
tw := &truncWriter{w: w}
|
||||||
|
fw, err := flate.NewWriter(tw, 3)
|
||||||
|
return &flateWrapper{fw: fw, tw: tw}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// truncWriter is an io.Writer that writes all but the last four bytes of the
|
||||||
|
// stream to another io.Writer.
|
||||||
|
type truncWriter struct {
|
||||||
|
w io.WriteCloser
|
||||||
|
n int
|
||||||
|
p [4]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *truncWriter) Write(p []byte) (int, error) {
|
||||||
|
n := 0
|
||||||
|
|
||||||
|
// fill buffer first for simplicity.
|
||||||
|
if w.n < len(w.p) {
|
||||||
|
n = copy(w.p[w.n:], p)
|
||||||
|
p = p[n:]
|
||||||
|
w.n += n
|
||||||
|
if len(p) == 0 {
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m := len(p)
|
||||||
|
if m > len(w.p) {
|
||||||
|
m = len(w.p)
|
||||||
|
}
|
||||||
|
|
||||||
|
if nn, err := w.w.Write(w.p[:m]); err != nil {
|
||||||
|
return n + nn, err
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(w.p[:], w.p[m:])
|
||||||
|
copy(w.p[len(w.p)-m:], p[len(p)-m:])
|
||||||
|
nn, err := w.w.Write(p[:len(p)-m])
|
||||||
|
return n + nn, err
|
||||||
|
}
|
||||||
|
|
||||||
|
type flateWrapper struct {
|
||||||
|
fw *flate.Writer
|
||||||
|
tw *truncWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *flateWrapper) Write(p []byte) (int, error) {
|
||||||
|
return w.fw.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *flateWrapper) Close() error {
|
||||||
|
err1 := w.fw.Flush()
|
||||||
|
if w.tw.p != [4]byte{0, 0, 0xff, 0xff} {
|
||||||
|
return errors.New("websocket: internal error, unexpected bytes at end of flate stream")
|
||||||
|
}
|
||||||
|
err2 := w.tw.w.Close()
|
||||||
|
if err1 != nil {
|
||||||
|
return err1
|
||||||
|
}
|
||||||
|
return err2
|
||||||
|
}
|
994
vendor/github.com/gorilla/websocket/conn.go
generated
vendored
Normal file
994
vendor/github.com/gorilla/websocket/conn.go
generated
vendored
Normal file
|
@ -0,0 +1,994 @@
|
||||||
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Frame header byte 0 bits from Section 5.2 of RFC 6455
|
||||||
|
finalBit = 1 << 7
|
||||||
|
rsv1Bit = 1 << 6
|
||||||
|
rsv2Bit = 1 << 5
|
||||||
|
rsv3Bit = 1 << 4
|
||||||
|
|
||||||
|
// Frame header byte 1 bits from Section 5.2 of RFC 6455
|
||||||
|
maskBit = 1 << 7
|
||||||
|
|
||||||
|
maxFrameHeaderSize = 2 + 8 + 4 // Fixed header + length + mask
|
||||||
|
maxControlFramePayloadSize = 125
|
||||||
|
|
||||||
|
writeWait = time.Second
|
||||||
|
|
||||||
|
defaultReadBufferSize = 4096
|
||||||
|
defaultWriteBufferSize = 4096
|
||||||
|
|
||||||
|
continuationFrame = 0
|
||||||
|
noFrame = -1
|
||||||
|
)
|
||||||
|
|
||||||
|
// Close codes defined in RFC 6455, section 11.7.
|
||||||
|
const (
|
||||||
|
CloseNormalClosure = 1000
|
||||||
|
CloseGoingAway = 1001
|
||||||
|
CloseProtocolError = 1002
|
||||||
|
CloseUnsupportedData = 1003
|
||||||
|
CloseNoStatusReceived = 1005
|
||||||
|
CloseAbnormalClosure = 1006
|
||||||
|
CloseInvalidFramePayloadData = 1007
|
||||||
|
ClosePolicyViolation = 1008
|
||||||
|
CloseMessageTooBig = 1009
|
||||||
|
CloseMandatoryExtension = 1010
|
||||||
|
CloseInternalServerErr = 1011
|
||||||
|
CloseServiceRestart = 1012
|
||||||
|
CloseTryAgainLater = 1013
|
||||||
|
CloseTLSHandshake = 1015
|
||||||
|
)
|
||||||
|
|
||||||
|
// The message types are defined in RFC 6455, section 11.8.
|
||||||
|
const (
|
||||||
|
// TextMessage denotes a text data message. The text message payload is
|
||||||
|
// interpreted as UTF-8 encoded text data.
|
||||||
|
TextMessage = 1
|
||||||
|
|
||||||
|
// BinaryMessage denotes a binary data message.
|
||||||
|
BinaryMessage = 2
|
||||||
|
|
||||||
|
// CloseMessage denotes a close control message. The optional message
|
||||||
|
// payload contains a numeric code and text. Use the FormatCloseMessage
|
||||||
|
// function to format a close message payload.
|
||||||
|
CloseMessage = 8
|
||||||
|
|
||||||
|
// PingMessage denotes a ping control message. The optional message payload
|
||||||
|
// is UTF-8 encoded text.
|
||||||
|
PingMessage = 9
|
||||||
|
|
||||||
|
// PongMessage denotes a ping control message. The optional message payload
|
||||||
|
// is UTF-8 encoded text.
|
||||||
|
PongMessage = 10
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrCloseSent is returned when the application writes a message to the
|
||||||
|
// connection after sending a close message.
|
||||||
|
var ErrCloseSent = errors.New("websocket: close sent")
|
||||||
|
|
||||||
|
// ErrReadLimit is returned when reading a message that is larger than the
|
||||||
|
// read limit set for the connection.
|
||||||
|
var ErrReadLimit = errors.New("websocket: read limit exceeded")
|
||||||
|
|
||||||
|
// netError satisfies the net Error interface.
|
||||||
|
type netError struct {
|
||||||
|
msg string
|
||||||
|
temporary bool
|
||||||
|
timeout bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *netError) Error() string { return e.msg }
|
||||||
|
func (e *netError) Temporary() bool { return e.temporary }
|
||||||
|
func (e *netError) Timeout() bool { return e.timeout }
|
||||||
|
|
||||||
|
// CloseError represents close frame.
|
||||||
|
type CloseError struct {
|
||||||
|
|
||||||
|
// Code is defined in RFC 6455, section 11.7.
|
||||||
|
Code int
|
||||||
|
|
||||||
|
// Text is the optional text payload.
|
||||||
|
Text string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *CloseError) Error() string {
|
||||||
|
s := []byte("websocket: close ")
|
||||||
|
s = strconv.AppendInt(s, int64(e.Code), 10)
|
||||||
|
switch e.Code {
|
||||||
|
case CloseNormalClosure:
|
||||||
|
s = append(s, " (normal)"...)
|
||||||
|
case CloseGoingAway:
|
||||||
|
s = append(s, " (going away)"...)
|
||||||
|
case CloseProtocolError:
|
||||||
|
s = append(s, " (protocol error)"...)
|
||||||
|
case CloseUnsupportedData:
|
||||||
|
s = append(s, " (unsupported data)"...)
|
||||||
|
case CloseNoStatusReceived:
|
||||||
|
s = append(s, " (no status)"...)
|
||||||
|
case CloseAbnormalClosure:
|
||||||
|
s = append(s, " (abnormal closure)"...)
|
||||||
|
case CloseInvalidFramePayloadData:
|
||||||
|
s = append(s, " (invalid payload data)"...)
|
||||||
|
case ClosePolicyViolation:
|
||||||
|
s = append(s, " (policy violation)"...)
|
||||||
|
case CloseMessageTooBig:
|
||||||
|
s = append(s, " (message too big)"...)
|
||||||
|
case CloseMandatoryExtension:
|
||||||
|
s = append(s, " (mandatory extension missing)"...)
|
||||||
|
case CloseInternalServerErr:
|
||||||
|
s = append(s, " (internal server error)"...)
|
||||||
|
case CloseTLSHandshake:
|
||||||
|
s = append(s, " (TLS handshake error)"...)
|
||||||
|
}
|
||||||
|
if e.Text != "" {
|
||||||
|
s = append(s, ": "...)
|
||||||
|
s = append(s, e.Text...)
|
||||||
|
}
|
||||||
|
return string(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsCloseError returns boolean indicating whether the error is a *CloseError
|
||||||
|
// with one of the specified codes.
|
||||||
|
func IsCloseError(err error, codes ...int) bool {
|
||||||
|
if e, ok := err.(*CloseError); ok {
|
||||||
|
for _, code := range codes {
|
||||||
|
if e.Code == code {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsUnexpectedCloseError returns boolean indicating whether the error is a
|
||||||
|
// *CloseError with a code not in the list of expected codes.
|
||||||
|
func IsUnexpectedCloseError(err error, expectedCodes ...int) bool {
|
||||||
|
if e, ok := err.(*CloseError); ok {
|
||||||
|
for _, code := range expectedCodes {
|
||||||
|
if e.Code == code {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
errWriteTimeout = &netError{msg: "websocket: write timeout", timeout: true, temporary: true}
|
||||||
|
errUnexpectedEOF = &CloseError{Code: CloseAbnormalClosure, Text: io.ErrUnexpectedEOF.Error()}
|
||||||
|
errBadWriteOpCode = errors.New("websocket: bad write message type")
|
||||||
|
errWriteClosed = errors.New("websocket: write closed")
|
||||||
|
errInvalidControlFrame = errors.New("websocket: invalid control frame")
|
||||||
|
)
|
||||||
|
|
||||||
|
func hideTempErr(err error) error {
|
||||||
|
if e, ok := err.(net.Error); ok && e.Temporary() {
|
||||||
|
err = &netError{msg: e.Error(), timeout: e.Timeout()}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func isControl(frameType int) bool {
|
||||||
|
return frameType == CloseMessage || frameType == PingMessage || frameType == PongMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
func isData(frameType int) bool {
|
||||||
|
return frameType == TextMessage || frameType == BinaryMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
var validReceivedCloseCodes = map[int]bool{
|
||||||
|
// see http://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
|
||||||
|
|
||||||
|
CloseNormalClosure: true,
|
||||||
|
CloseGoingAway: true,
|
||||||
|
CloseProtocolError: true,
|
||||||
|
CloseUnsupportedData: true,
|
||||||
|
CloseNoStatusReceived: false,
|
||||||
|
CloseAbnormalClosure: false,
|
||||||
|
CloseInvalidFramePayloadData: true,
|
||||||
|
ClosePolicyViolation: true,
|
||||||
|
CloseMessageTooBig: true,
|
||||||
|
CloseMandatoryExtension: true,
|
||||||
|
CloseInternalServerErr: true,
|
||||||
|
CloseServiceRestart: true,
|
||||||
|
CloseTryAgainLater: true,
|
||||||
|
CloseTLSHandshake: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
func isValidReceivedCloseCode(code int) bool {
|
||||||
|
return validReceivedCloseCodes[code] || (code >= 3000 && code <= 4999)
|
||||||
|
}
|
||||||
|
|
||||||
|
func maskBytes(key [4]byte, pos int, b []byte) int {
|
||||||
|
for i := range b {
|
||||||
|
b[i] ^= key[pos&3]
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
return pos & 3
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMaskKey() [4]byte {
|
||||||
|
n := rand.Uint32()
|
||||||
|
return [4]byte{byte(n), byte(n >> 8), byte(n >> 16), byte(n >> 24)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conn represents a WebSocket connection.
|
||||||
|
type Conn struct {
|
||||||
|
conn net.Conn
|
||||||
|
isServer bool
|
||||||
|
subprotocol string
|
||||||
|
|
||||||
|
// Write fields
|
||||||
|
mu chan bool // used as mutex to protect write to conn and closeSent
|
||||||
|
closeSent bool // whether close message was sent
|
||||||
|
writeErr error
|
||||||
|
writeBuf []byte // frame is constructed in this buffer.
|
||||||
|
writePos int // end of data in writeBuf.
|
||||||
|
writeFrameType int // type of the current frame.
|
||||||
|
writeDeadline time.Time
|
||||||
|
messageWriter *messageWriter // the current low-level message writer
|
||||||
|
writer io.WriteCloser // the current writer returned to the application
|
||||||
|
isWriting bool // for best-effort concurrent write detection
|
||||||
|
|
||||||
|
enableWriteCompression bool
|
||||||
|
writeCompress bool // whether next call to flushFrame should set RSV1
|
||||||
|
newCompressionWriter func(io.WriteCloser) (io.WriteCloser, error)
|
||||||
|
|
||||||
|
// Read fields
|
||||||
|
readErr error
|
||||||
|
br *bufio.Reader
|
||||||
|
readRemaining int64 // bytes remaining in current frame.
|
||||||
|
readFinal bool // true the current message has more frames.
|
||||||
|
readLength int64 // Message size.
|
||||||
|
readLimit int64 // Maximum message size.
|
||||||
|
readMaskPos int
|
||||||
|
readMaskKey [4]byte
|
||||||
|
handlePong func(string) error
|
||||||
|
handlePing func(string) error
|
||||||
|
readErrCount int
|
||||||
|
messageReader *messageReader // the current low-level reader
|
||||||
|
|
||||||
|
readDecompress bool // whether last read frame had RSV1 set
|
||||||
|
newDecompressionReader func(io.Reader) io.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConn(conn net.Conn, isServer bool, readBufferSize, writeBufferSize int) *Conn {
|
||||||
|
mu := make(chan bool, 1)
|
||||||
|
mu <- true
|
||||||
|
|
||||||
|
if readBufferSize == 0 {
|
||||||
|
readBufferSize = defaultReadBufferSize
|
||||||
|
}
|
||||||
|
if readBufferSize < maxControlFramePayloadSize {
|
||||||
|
readBufferSize = maxControlFramePayloadSize
|
||||||
|
}
|
||||||
|
if writeBufferSize == 0 {
|
||||||
|
writeBufferSize = defaultWriteBufferSize
|
||||||
|
}
|
||||||
|
|
||||||
|
c := &Conn{
|
||||||
|
isServer: isServer,
|
||||||
|
br: bufio.NewReaderSize(conn, readBufferSize),
|
||||||
|
conn: conn,
|
||||||
|
mu: mu,
|
||||||
|
readFinal: true,
|
||||||
|
writeBuf: make([]byte, writeBufferSize+maxFrameHeaderSize),
|
||||||
|
writeFrameType: noFrame,
|
||||||
|
writePos: maxFrameHeaderSize,
|
||||||
|
enableWriteCompression: true,
|
||||||
|
}
|
||||||
|
c.SetPingHandler(nil)
|
||||||
|
c.SetPongHandler(nil)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subprotocol returns the negotiated protocol for the connection.
|
||||||
|
func (c *Conn) Subprotocol() string {
|
||||||
|
return c.subprotocol
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes the underlying network connection without sending or waiting for a close frame.
|
||||||
|
func (c *Conn) Close() error {
|
||||||
|
return c.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// LocalAddr returns the local network address.
|
||||||
|
func (c *Conn) LocalAddr() net.Addr {
|
||||||
|
return c.conn.LocalAddr()
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoteAddr returns the remote network address.
|
||||||
|
func (c *Conn) RemoteAddr() net.Addr {
|
||||||
|
return c.conn.RemoteAddr()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write methods
|
||||||
|
|
||||||
|
func (c *Conn) write(frameType int, deadline time.Time, bufs ...[]byte) error {
|
||||||
|
<-c.mu
|
||||||
|
defer func() { c.mu <- true }()
|
||||||
|
|
||||||
|
if c.closeSent {
|
||||||
|
return ErrCloseSent
|
||||||
|
} else if frameType == CloseMessage {
|
||||||
|
c.closeSent = true
|
||||||
|
}
|
||||||
|
|
||||||
|
c.conn.SetWriteDeadline(deadline)
|
||||||
|
for _, buf := range bufs {
|
||||||
|
if len(buf) > 0 {
|
||||||
|
n, err := c.conn.Write(buf)
|
||||||
|
if n != len(buf) {
|
||||||
|
// Close on partial write.
|
||||||
|
c.conn.Close()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteControl writes a control message with the given deadline. The allowed
|
||||||
|
// message types are CloseMessage, PingMessage and PongMessage.
|
||||||
|
func (c *Conn) WriteControl(messageType int, data []byte, deadline time.Time) error {
|
||||||
|
if !isControl(messageType) {
|
||||||
|
return errBadWriteOpCode
|
||||||
|
}
|
||||||
|
if len(data) > maxControlFramePayloadSize {
|
||||||
|
return errInvalidControlFrame
|
||||||
|
}
|
||||||
|
|
||||||
|
b0 := byte(messageType) | finalBit
|
||||||
|
b1 := byte(len(data))
|
||||||
|
if !c.isServer {
|
||||||
|
b1 |= maskBit
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, 0, maxFrameHeaderSize+maxControlFramePayloadSize)
|
||||||
|
buf = append(buf, b0, b1)
|
||||||
|
|
||||||
|
if c.isServer {
|
||||||
|
buf = append(buf, data...)
|
||||||
|
} else {
|
||||||
|
key := newMaskKey()
|
||||||
|
buf = append(buf, key[:]...)
|
||||||
|
buf = append(buf, data...)
|
||||||
|
maskBytes(key, 0, buf[6:])
|
||||||
|
}
|
||||||
|
|
||||||
|
d := time.Hour * 1000
|
||||||
|
if !deadline.IsZero() {
|
||||||
|
d = deadline.Sub(time.Now())
|
||||||
|
if d < 0 {
|
||||||
|
return errWriteTimeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
timer := time.NewTimer(d)
|
||||||
|
select {
|
||||||
|
case <-c.mu:
|
||||||
|
timer.Stop()
|
||||||
|
case <-timer.C:
|
||||||
|
return errWriteTimeout
|
||||||
|
}
|
||||||
|
defer func() { c.mu <- true }()
|
||||||
|
|
||||||
|
if c.closeSent {
|
||||||
|
return ErrCloseSent
|
||||||
|
} else if messageType == CloseMessage {
|
||||||
|
c.closeSent = true
|
||||||
|
}
|
||||||
|
|
||||||
|
c.conn.SetWriteDeadline(deadline)
|
||||||
|
n, err := c.conn.Write(buf)
|
||||||
|
if n != 0 && n != len(buf) {
|
||||||
|
c.conn.Close()
|
||||||
|
}
|
||||||
|
return hideTempErr(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NextWriter returns a writer for the next message to send. The writer's Close
|
||||||
|
// method flushes the complete message to the network.
|
||||||
|
//
|
||||||
|
// There can be at most one open writer on a connection. NextWriter closes the
|
||||||
|
// previous writer if the application has not already done so.
|
||||||
|
func (c *Conn) NextWriter(messageType int) (io.WriteCloser, error) {
|
||||||
|
if c.writeErr != nil {
|
||||||
|
return nil, c.writeErr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close previous writer if not already closed by the application. It's
|
||||||
|
// probably better to return an error in this situation, but we cannot
|
||||||
|
// change this without breaking existing applications.
|
||||||
|
if c.writer != nil {
|
||||||
|
err := c.writer.Close()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isControl(messageType) && !isData(messageType) {
|
||||||
|
return nil, errBadWriteOpCode
|
||||||
|
}
|
||||||
|
|
||||||
|
c.writeFrameType = messageType
|
||||||
|
c.messageWriter = &messageWriter{c}
|
||||||
|
|
||||||
|
var w io.WriteCloser = c.messageWriter
|
||||||
|
if c.newCompressionWriter != nil && c.enableWriteCompression && isData(messageType) {
|
||||||
|
c.writeCompress = true
|
||||||
|
var err error
|
||||||
|
w, err = c.newCompressionWriter(w)
|
||||||
|
if err != nil {
|
||||||
|
c.writer.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return w, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// flushFrame writes buffered data and extra as a frame to the network. The
|
||||||
|
// final argument indicates that this is the last frame in the message.
|
||||||
|
func (c *Conn) flushFrame(final bool, extra []byte) error {
|
||||||
|
length := c.writePos - maxFrameHeaderSize + len(extra)
|
||||||
|
|
||||||
|
// Check for invalid control frames.
|
||||||
|
if isControl(c.writeFrameType) &&
|
||||||
|
(!final || length > maxControlFramePayloadSize) {
|
||||||
|
c.messageWriter = nil
|
||||||
|
c.writer = nil
|
||||||
|
c.writeFrameType = noFrame
|
||||||
|
c.writePos = maxFrameHeaderSize
|
||||||
|
return errInvalidControlFrame
|
||||||
|
}
|
||||||
|
|
||||||
|
b0 := byte(c.writeFrameType)
|
||||||
|
if final {
|
||||||
|
b0 |= finalBit
|
||||||
|
}
|
||||||
|
if c.writeCompress {
|
||||||
|
b0 |= rsv1Bit
|
||||||
|
}
|
||||||
|
c.writeCompress = false
|
||||||
|
|
||||||
|
b1 := byte(0)
|
||||||
|
if !c.isServer {
|
||||||
|
b1 |= maskBit
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assume that the frame starts at beginning of c.writeBuf.
|
||||||
|
framePos := 0
|
||||||
|
if c.isServer {
|
||||||
|
// Adjust up if mask not included in the header.
|
||||||
|
framePos = 4
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case length >= 65536:
|
||||||
|
c.writeBuf[framePos] = b0
|
||||||
|
c.writeBuf[framePos+1] = b1 | 127
|
||||||
|
binary.BigEndian.PutUint64(c.writeBuf[framePos+2:], uint64(length))
|
||||||
|
case length > 125:
|
||||||
|
framePos += 6
|
||||||
|
c.writeBuf[framePos] = b0
|
||||||
|
c.writeBuf[framePos+1] = b1 | 126
|
||||||
|
binary.BigEndian.PutUint16(c.writeBuf[framePos+2:], uint16(length))
|
||||||
|
default:
|
||||||
|
framePos += 8
|
||||||
|
c.writeBuf[framePos] = b0
|
||||||
|
c.writeBuf[framePos+1] = b1 | byte(length)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.isServer {
|
||||||
|
key := newMaskKey()
|
||||||
|
copy(c.writeBuf[maxFrameHeaderSize-4:], key[:])
|
||||||
|
maskBytes(key, 0, c.writeBuf[maxFrameHeaderSize:c.writePos])
|
||||||
|
if len(extra) > 0 {
|
||||||
|
c.writeErr = errors.New("websocket: internal error, extra used in client mode")
|
||||||
|
return c.writeErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the buffers to the connection with best-effort detection of
|
||||||
|
// concurrent writes. See the concurrency section in the package
|
||||||
|
// documentation for more info.
|
||||||
|
|
||||||
|
if c.isWriting {
|
||||||
|
panic("concurrent write to websocket connection")
|
||||||
|
}
|
||||||
|
c.isWriting = true
|
||||||
|
|
||||||
|
c.writeErr = c.write(c.writeFrameType, c.writeDeadline, c.writeBuf[framePos:c.writePos], extra)
|
||||||
|
|
||||||
|
if !c.isWriting {
|
||||||
|
panic("concurrent write to websocket connection")
|
||||||
|
}
|
||||||
|
c.isWriting = false
|
||||||
|
|
||||||
|
// Setup for next frame.
|
||||||
|
c.writePos = maxFrameHeaderSize
|
||||||
|
c.writeFrameType = continuationFrame
|
||||||
|
if final {
|
||||||
|
c.messageWriter = nil
|
||||||
|
c.writer = nil
|
||||||
|
c.writeFrameType = noFrame
|
||||||
|
}
|
||||||
|
return c.writeErr
|
||||||
|
}
|
||||||
|
|
||||||
|
type messageWriter struct{ c *Conn }
|
||||||
|
|
||||||
|
func (w *messageWriter) err() error {
|
||||||
|
c := w.c
|
||||||
|
if c.messageWriter != w {
|
||||||
|
return errWriteClosed
|
||||||
|
}
|
||||||
|
if c.writeErr != nil {
|
||||||
|
return c.writeErr
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *messageWriter) ncopy(max int) (int, error) {
|
||||||
|
n := len(w.c.writeBuf) - w.c.writePos
|
||||||
|
if n <= 0 {
|
||||||
|
if err := w.c.flushFrame(false, nil); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
n = len(w.c.writeBuf) - w.c.writePos
|
||||||
|
}
|
||||||
|
if n > max {
|
||||||
|
n = max
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *messageWriter) Write(p []byte) (int, error) {
|
||||||
|
if err := w.err(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(p) > 2*len(w.c.writeBuf) && w.c.isServer {
|
||||||
|
// Don't buffer large messages.
|
||||||
|
err := w.c.flushFrame(false, p)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
nn := len(p)
|
||||||
|
for len(p) > 0 {
|
||||||
|
n, err := w.ncopy(len(p))
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
copy(w.c.writeBuf[w.c.writePos:], p[:n])
|
||||||
|
w.c.writePos += n
|
||||||
|
p = p[n:]
|
||||||
|
}
|
||||||
|
return nn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *messageWriter) WriteString(p string) (int, error) {
|
||||||
|
if err := w.err(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
nn := len(p)
|
||||||
|
for len(p) > 0 {
|
||||||
|
n, err := w.ncopy(len(p))
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
copy(w.c.writeBuf[w.c.writePos:], p[:n])
|
||||||
|
w.c.writePos += n
|
||||||
|
p = p[n:]
|
||||||
|
}
|
||||||
|
return nn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *messageWriter) ReadFrom(r io.Reader) (nn int64, err error) {
|
||||||
|
if err := w.err(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
if w.c.writePos == len(w.c.writeBuf) {
|
||||||
|
err = w.c.flushFrame(false, nil)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var n int
|
||||||
|
n, err = r.Read(w.c.writeBuf[w.c.writePos:])
|
||||||
|
w.c.writePos += n
|
||||||
|
nn += int64(n)
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nn, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *messageWriter) Close() error {
|
||||||
|
if err := w.err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return w.c.flushFrame(true, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteMessage is a helper method for getting a writer using NextWriter,
|
||||||
|
// writing the message and closing the writer.
|
||||||
|
func (c *Conn) WriteMessage(messageType int, data []byte) error {
|
||||||
|
w, err := c.NextWriter(messageType)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, ok := w.(*messageWriter); ok && c.isServer {
|
||||||
|
// Optimize write as a single frame.
|
||||||
|
n := copy(c.writeBuf[c.writePos:], data)
|
||||||
|
c.writePos += n
|
||||||
|
data = data[n:]
|
||||||
|
err = c.flushFrame(true, data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err = w.Write(data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return w.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetWriteDeadline sets the write deadline on the underlying network
|
||||||
|
// connection. After a write has timed out, the websocket state is corrupt and
|
||||||
|
// all future writes will return an error. A zero value for t means writes will
|
||||||
|
// not time out.
|
||||||
|
func (c *Conn) SetWriteDeadline(t time.Time) error {
|
||||||
|
c.writeDeadline = t
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read methods
|
||||||
|
|
||||||
|
func (c *Conn) advanceFrame() (int, error) {
|
||||||
|
|
||||||
|
// 1. Skip remainder of previous frame.
|
||||||
|
|
||||||
|
if c.readRemaining > 0 {
|
||||||
|
if _, err := io.CopyN(ioutil.Discard, c.br, c.readRemaining); err != nil {
|
||||||
|
return noFrame, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Read and parse first two bytes of frame header.
|
||||||
|
|
||||||
|
p, err := c.read(2)
|
||||||
|
if err != nil {
|
||||||
|
return noFrame, err
|
||||||
|
}
|
||||||
|
|
||||||
|
final := p[0]&finalBit != 0
|
||||||
|
frameType := int(p[0] & 0xf)
|
||||||
|
mask := p[1]&maskBit != 0
|
||||||
|
c.readRemaining = int64(p[1] & 0x7f)
|
||||||
|
|
||||||
|
c.readDecompress = false
|
||||||
|
if c.newDecompressionReader != nil && (p[0]&rsv1Bit) != 0 {
|
||||||
|
c.readDecompress = true
|
||||||
|
p[0] &^= rsv1Bit
|
||||||
|
}
|
||||||
|
|
||||||
|
if rsv := p[0] & (rsv1Bit | rsv2Bit | rsv3Bit); rsv != 0 {
|
||||||
|
return noFrame, c.handleProtocolError("unexpected reserved bits 0x" + strconv.FormatInt(int64(rsv), 16))
|
||||||
|
}
|
||||||
|
|
||||||
|
switch frameType {
|
||||||
|
case CloseMessage, PingMessage, PongMessage:
|
||||||
|
if c.readRemaining > maxControlFramePayloadSize {
|
||||||
|
return noFrame, c.handleProtocolError("control frame length > 125")
|
||||||
|
}
|
||||||
|
if !final {
|
||||||
|
return noFrame, c.handleProtocolError("control frame not final")
|
||||||
|
}
|
||||||
|
case TextMessage, BinaryMessage:
|
||||||
|
if !c.readFinal {
|
||||||
|
return noFrame, c.handleProtocolError("message start before final message frame")
|
||||||
|
}
|
||||||
|
c.readFinal = final
|
||||||
|
case continuationFrame:
|
||||||
|
if c.readFinal {
|
||||||
|
return noFrame, c.handleProtocolError("continuation after final message frame")
|
||||||
|
}
|
||||||
|
c.readFinal = final
|
||||||
|
default:
|
||||||
|
return noFrame, c.handleProtocolError("unknown opcode " + strconv.Itoa(frameType))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Read and parse frame length.
|
||||||
|
|
||||||
|
switch c.readRemaining {
|
||||||
|
case 126:
|
||||||
|
p, err := c.read(2)
|
||||||
|
if err != nil {
|
||||||
|
return noFrame, err
|
||||||
|
}
|
||||||
|
c.readRemaining = int64(binary.BigEndian.Uint16(p))
|
||||||
|
case 127:
|
||||||
|
p, err := c.read(8)
|
||||||
|
if err != nil {
|
||||||
|
return noFrame, err
|
||||||
|
}
|
||||||
|
c.readRemaining = int64(binary.BigEndian.Uint64(p))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Handle frame masking.
|
||||||
|
|
||||||
|
if mask != c.isServer {
|
||||||
|
return noFrame, c.handleProtocolError("incorrect mask flag")
|
||||||
|
}
|
||||||
|
|
||||||
|
if mask {
|
||||||
|
c.readMaskPos = 0
|
||||||
|
p, err := c.read(len(c.readMaskKey))
|
||||||
|
if err != nil {
|
||||||
|
return noFrame, err
|
||||||
|
}
|
||||||
|
copy(c.readMaskKey[:], p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. For text and binary messages, enforce read limit and return.
|
||||||
|
|
||||||
|
if frameType == continuationFrame || frameType == TextMessage || frameType == BinaryMessage {
|
||||||
|
|
||||||
|
c.readLength += c.readRemaining
|
||||||
|
if c.readLimit > 0 && c.readLength > c.readLimit {
|
||||||
|
c.WriteControl(CloseMessage, FormatCloseMessage(CloseMessageTooBig, ""), time.Now().Add(writeWait))
|
||||||
|
return noFrame, ErrReadLimit
|
||||||
|
}
|
||||||
|
|
||||||
|
return frameType, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. Read control frame payload.
|
||||||
|
|
||||||
|
var payload []byte
|
||||||
|
if c.readRemaining > 0 {
|
||||||
|
payload, err = c.read(int(c.readRemaining))
|
||||||
|
c.readRemaining = 0
|
||||||
|
if err != nil {
|
||||||
|
return noFrame, err
|
||||||
|
}
|
||||||
|
if c.isServer {
|
||||||
|
maskBytes(c.readMaskKey, 0, payload)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7. Process control frame payload.
|
||||||
|
|
||||||
|
switch frameType {
|
||||||
|
case PongMessage:
|
||||||
|
if err := c.handlePong(string(payload)); err != nil {
|
||||||
|
return noFrame, err
|
||||||
|
}
|
||||||
|
case PingMessage:
|
||||||
|
if err := c.handlePing(string(payload)); err != nil {
|
||||||
|
return noFrame, err
|
||||||
|
}
|
||||||
|
case CloseMessage:
|
||||||
|
echoMessage := []byte{}
|
||||||
|
closeCode := CloseNoStatusReceived
|
||||||
|
closeText := ""
|
||||||
|
if len(payload) >= 2 {
|
||||||
|
echoMessage = payload[:2]
|
||||||
|
closeCode = int(binary.BigEndian.Uint16(payload))
|
||||||
|
if !isValidReceivedCloseCode(closeCode) {
|
||||||
|
return noFrame, c.handleProtocolError("invalid close code")
|
||||||
|
}
|
||||||
|
closeText = string(payload[2:])
|
||||||
|
if !utf8.ValidString(closeText) {
|
||||||
|
return noFrame, c.handleProtocolError("invalid utf8 payload in close frame")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.WriteControl(CloseMessage, echoMessage, time.Now().Add(writeWait))
|
||||||
|
return noFrame, &CloseError{Code: closeCode, Text: closeText}
|
||||||
|
}
|
||||||
|
|
||||||
|
return frameType, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Conn) handleProtocolError(message string) error {
|
||||||
|
c.WriteControl(CloseMessage, FormatCloseMessage(CloseProtocolError, message), time.Now().Add(writeWait))
|
||||||
|
return errors.New("websocket: " + message)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NextReader returns the next data message received from the peer. The
|
||||||
|
// returned messageType is either TextMessage or BinaryMessage.
|
||||||
|
//
|
||||||
|
// There can be at most one open reader on a connection. NextReader discards
|
||||||
|
// the previous message if the application has not already consumed it.
|
||||||
|
//
|
||||||
|
// Applications must break out of the application's read loop when this method
|
||||||
|
// returns a non-nil error value. Errors returned from this method are
|
||||||
|
// permanent. Once this method returns a non-nil error, all subsequent calls to
|
||||||
|
// this method return the same error.
|
||||||
|
func (c *Conn) NextReader() (messageType int, r io.Reader, err error) {
|
||||||
|
|
||||||
|
c.messageReader = nil
|
||||||
|
c.readLength = 0
|
||||||
|
|
||||||
|
for c.readErr == nil {
|
||||||
|
frameType, err := c.advanceFrame()
|
||||||
|
if err != nil {
|
||||||
|
c.readErr = hideTempErr(err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if frameType == TextMessage || frameType == BinaryMessage {
|
||||||
|
c.messageReader = &messageReader{c}
|
||||||
|
var r io.Reader = c.messageReader
|
||||||
|
if c.readDecompress {
|
||||||
|
r = c.newDecompressionReader(r)
|
||||||
|
}
|
||||||
|
return frameType, r, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Applications that do handle the error returned from this method spin in
|
||||||
|
// tight loop on connection failure. To help application developers detect
|
||||||
|
// this error, panic on repeated reads to the failed connection.
|
||||||
|
c.readErrCount++
|
||||||
|
if c.readErrCount >= 1000 {
|
||||||
|
panic("repeated read on failed websocket connection")
|
||||||
|
}
|
||||||
|
|
||||||
|
return noFrame, nil, c.readErr
|
||||||
|
}
|
||||||
|
|
||||||
|
type messageReader struct{ c *Conn }
|
||||||
|
|
||||||
|
func (r *messageReader) Read(b []byte) (int, error) {
|
||||||
|
c := r.c
|
||||||
|
if c.messageReader != r {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
for c.readErr == nil {
|
||||||
|
|
||||||
|
if c.readRemaining > 0 {
|
||||||
|
if int64(len(b)) > c.readRemaining {
|
||||||
|
b = b[:c.readRemaining]
|
||||||
|
}
|
||||||
|
n, err := c.br.Read(b)
|
||||||
|
c.readErr = hideTempErr(err)
|
||||||
|
if c.isServer {
|
||||||
|
c.readMaskPos = maskBytes(c.readMaskKey, c.readMaskPos, b[:n])
|
||||||
|
}
|
||||||
|
c.readRemaining -= int64(n)
|
||||||
|
if c.readRemaining > 0 && c.readErr == io.EOF {
|
||||||
|
c.readErr = errUnexpectedEOF
|
||||||
|
}
|
||||||
|
return n, c.readErr
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.readFinal {
|
||||||
|
c.messageReader = nil
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
frameType, err := c.advanceFrame()
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
c.readErr = hideTempErr(err)
|
||||||
|
case frameType == TextMessage || frameType == BinaryMessage:
|
||||||
|
c.readErr = errors.New("websocket: internal error, unexpected text or binary in Reader")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err := c.readErr
|
||||||
|
if err == io.EOF && c.messageReader == r {
|
||||||
|
err = errUnexpectedEOF
|
||||||
|
}
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadMessage is a helper method for getting a reader using NextReader and
|
||||||
|
// reading from that reader to a buffer.
|
||||||
|
func (c *Conn) ReadMessage() (messageType int, p []byte, err error) {
|
||||||
|
var r io.Reader
|
||||||
|
messageType, r, err = c.NextReader()
|
||||||
|
if err != nil {
|
||||||
|
return messageType, nil, err
|
||||||
|
}
|
||||||
|
p, err = ioutil.ReadAll(r)
|
||||||
|
return messageType, p, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetReadDeadline sets the read deadline on the underlying network connection.
|
||||||
|
// After a read has timed out, the websocket connection state is corrupt and
|
||||||
|
// all future reads will return an error. A zero value for t means reads will
|
||||||
|
// not time out.
|
||||||
|
func (c *Conn) SetReadDeadline(t time.Time) error {
|
||||||
|
return c.conn.SetReadDeadline(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetReadLimit sets the maximum size for a message read from the peer. If a
|
||||||
|
// message exceeds the limit, the connection sends a close frame to the peer
|
||||||
|
// and returns ErrReadLimit to the application.
|
||||||
|
func (c *Conn) SetReadLimit(limit int64) {
|
||||||
|
c.readLimit = limit
|
||||||
|
}
|
||||||
|
|
||||||
|
// PingHandler returns the current ping handler
|
||||||
|
func (c *Conn) PingHandler() func(appData string) error {
|
||||||
|
return c.handlePing
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPingHandler sets the handler for ping messages received from the peer.
|
||||||
|
// The appData argument to h is the PING frame application data. The default
|
||||||
|
// ping handler sends a pong to the peer.
|
||||||
|
func (c *Conn) SetPingHandler(h func(appData string) error) {
|
||||||
|
if h == nil {
|
||||||
|
h = func(message string) error {
|
||||||
|
err := c.WriteControl(PongMessage, []byte(message), time.Now().Add(writeWait))
|
||||||
|
if err == ErrCloseSent {
|
||||||
|
return nil
|
||||||
|
} else if e, ok := err.(net.Error); ok && e.Temporary() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.handlePing = h
|
||||||
|
}
|
||||||
|
|
||||||
|
// PongHandler returns the current pong handler
|
||||||
|
func (c *Conn) PongHandler() func(appData string) error {
|
||||||
|
return c.handlePong
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPongHandler sets the handler for pong messages received from the peer.
|
||||||
|
// The appData argument to h is the PONG frame application data. The default
|
||||||
|
// pong handler does nothing.
|
||||||
|
func (c *Conn) SetPongHandler(h func(appData string) error) {
|
||||||
|
if h == nil {
|
||||||
|
h = func(string) error { return nil }
|
||||||
|
}
|
||||||
|
c.handlePong = h
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnderlyingConn returns the internal net.Conn. This can be used to further
|
||||||
|
// modifications to connection specific flags.
|
||||||
|
func (c *Conn) UnderlyingConn() net.Conn {
|
||||||
|
return c.conn
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormatCloseMessage formats closeCode and text as a WebSocket close message.
|
||||||
|
func FormatCloseMessage(closeCode int, text string) []byte {
|
||||||
|
buf := make([]byte, 2+len(text))
|
||||||
|
binary.BigEndian.PutUint16(buf, uint16(closeCode))
|
||||||
|
copy(buf[2:], text)
|
||||||
|
return buf
|
||||||
|
}
|
18
vendor/github.com/gorilla/websocket/conn_read.go
generated
vendored
Normal file
18
vendor/github.com/gorilla/websocket/conn_read.go
generated
vendored
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// +build go1.5
|
||||||
|
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
func (c *Conn) read(n int) ([]byte, error) {
|
||||||
|
p, err := c.br.Peek(n)
|
||||||
|
if err == io.EOF {
|
||||||
|
err = errUnexpectedEOF
|
||||||
|
}
|
||||||
|
c.br.Discard(len(p))
|
||||||
|
return p, err
|
||||||
|
}
|
21
vendor/github.com/gorilla/websocket/conn_read_legacy.go
generated
vendored
Normal file
21
vendor/github.com/gorilla/websocket/conn_read_legacy.go
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// +build !go1.5
|
||||||
|
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
func (c *Conn) read(n int) ([]byte, error) {
|
||||||
|
p, err := c.br.Peek(n)
|
||||||
|
if err == io.EOF {
|
||||||
|
err = errUnexpectedEOF
|
||||||
|
}
|
||||||
|
if len(p) > 0 {
|
||||||
|
// advance over the bytes just read
|
||||||
|
io.ReadFull(c.br, p)
|
||||||
|
}
|
||||||
|
return p, err
|
||||||
|
}
|
152
vendor/github.com/gorilla/websocket/doc.go
generated
vendored
Normal file
152
vendor/github.com/gorilla/websocket/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,152 @@
|
||||||
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package websocket implements the WebSocket protocol defined in RFC 6455.
|
||||||
|
//
|
||||||
|
// Overview
|
||||||
|
//
|
||||||
|
// The Conn type represents a WebSocket connection. A server application uses
|
||||||
|
// the Upgrade function from an Upgrader object with a HTTP request handler
|
||||||
|
// to get a pointer to a Conn:
|
||||||
|
//
|
||||||
|
// var upgrader = websocket.Upgrader{
|
||||||
|
// ReadBufferSize: 1024,
|
||||||
|
// WriteBufferSize: 1024,
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Println(err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// ... Use conn to send and receive messages.
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Call the connection's WriteMessage and ReadMessage methods to send and
|
||||||
|
// receive messages as a slice of bytes. This snippet of code shows how to echo
|
||||||
|
// messages using these methods:
|
||||||
|
//
|
||||||
|
// for {
|
||||||
|
// messageType, p, err := conn.ReadMessage()
|
||||||
|
// if err != nil {
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// if err = conn.WriteMessage(messageType, p); err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// In above snippet of code, p is a []byte and messageType is an int with value
|
||||||
|
// websocket.BinaryMessage or websocket.TextMessage.
|
||||||
|
//
|
||||||
|
// An application can also send and receive messages using the io.WriteCloser
|
||||||
|
// and io.Reader interfaces. To send a message, call the connection NextWriter
|
||||||
|
// method to get an io.WriteCloser, write the message to the writer and close
|
||||||
|
// the writer when done. To receive a message, call the connection NextReader
|
||||||
|
// method to get an io.Reader and read until io.EOF is returned. This snippet
|
||||||
|
// shows how to echo messages using the NextWriter and NextReader methods:
|
||||||
|
//
|
||||||
|
// for {
|
||||||
|
// messageType, r, err := conn.NextReader()
|
||||||
|
// if err != nil {
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// w, err := conn.NextWriter(messageType)
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// if _, err := io.Copy(w, r); err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// if err := w.Close(); err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Data Messages
|
||||||
|
//
|
||||||
|
// The WebSocket protocol distinguishes between text and binary data messages.
|
||||||
|
// Text messages are interpreted as UTF-8 encoded text. The interpretation of
|
||||||
|
// binary messages is left to the application.
|
||||||
|
//
|
||||||
|
// This package uses the TextMessage and BinaryMessage integer constants to
|
||||||
|
// identify the two data message types. The ReadMessage and NextReader methods
|
||||||
|
// return the type of the received message. The messageType argument to the
|
||||||
|
// WriteMessage and NextWriter methods specifies the type of a sent message.
|
||||||
|
//
|
||||||
|
// It is the application's responsibility to ensure that text messages are
|
||||||
|
// valid UTF-8 encoded text.
|
||||||
|
//
|
||||||
|
// Control Messages
|
||||||
|
//
|
||||||
|
// The WebSocket protocol defines three types of control messages: close, ping
|
||||||
|
// and pong. Call the connection WriteControl, WriteMessage or NextWriter
|
||||||
|
// methods to send a control message to the peer.
|
||||||
|
//
|
||||||
|
// Connections handle received close messages by sending a close message to the
|
||||||
|
// peer and returning a *CloseError from the the NextReader, ReadMessage or the
|
||||||
|
// message Read method.
|
||||||
|
//
|
||||||
|
// Connections handle received ping and pong messages by invoking callback
|
||||||
|
// functions set with SetPingHandler and SetPongHandler methods. The callback
|
||||||
|
// functions are called from the NextReader, ReadMessage and the message Read
|
||||||
|
// methods.
|
||||||
|
//
|
||||||
|
// The default ping handler sends a pong to the peer. The application's reading
|
||||||
|
// goroutine can block for a short time while the handler writes the pong data
|
||||||
|
// to the connection.
|
||||||
|
//
|
||||||
|
// The application must read the connection to process ping, pong and close
|
||||||
|
// messages sent from the peer. If the application is not otherwise interested
|
||||||
|
// in messages from the peer, then the application should start a goroutine to
|
||||||
|
// read and discard messages from the peer. A simple example is:
|
||||||
|
//
|
||||||
|
// func readLoop(c *websocket.Conn) {
|
||||||
|
// for {
|
||||||
|
// if _, _, err := c.NextReader(); err != nil {
|
||||||
|
// c.Close()
|
||||||
|
// break
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Concurrency
|
||||||
|
//
|
||||||
|
// Connections support one concurrent reader and one concurrent writer.
|
||||||
|
//
|
||||||
|
// Applications are responsible for ensuring that no more than one goroutine
|
||||||
|
// calls the write methods (NextWriter, SetWriteDeadline, WriteMessage,
|
||||||
|
// WriteJSON) concurrently and that no more than one goroutine calls the read
|
||||||
|
// methods (NextReader, SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler,
|
||||||
|
// SetPingHandler) concurrently.
|
||||||
|
//
|
||||||
|
// The Close and WriteControl methods can be called concurrently with all other
|
||||||
|
// methods.
|
||||||
|
//
|
||||||
|
// Origin Considerations
|
||||||
|
//
|
||||||
|
// Web browsers allow Javascript applications to open a WebSocket connection to
|
||||||
|
// any host. It's up to the server to enforce an origin policy using the Origin
|
||||||
|
// request header sent by the browser.
|
||||||
|
//
|
||||||
|
// The Upgrader calls the function specified in the CheckOrigin field to check
|
||||||
|
// the origin. If the CheckOrigin function returns false, then the Upgrade
|
||||||
|
// method fails the WebSocket handshake with HTTP status 403.
|
||||||
|
//
|
||||||
|
// If the CheckOrigin field is nil, then the Upgrader uses a safe default: fail
|
||||||
|
// the handshake if the Origin request header is present and not equal to the
|
||||||
|
// Host request header.
|
||||||
|
//
|
||||||
|
// An application can allow connections from any origin by specifying a
|
||||||
|
// function that always returns true:
|
||||||
|
//
|
||||||
|
// var upgrader = websocket.Upgrader{
|
||||||
|
// CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// The deprecated Upgrade function does not enforce an origin policy. It's the
|
||||||
|
// application's responsibility to check the Origin header before calling
|
||||||
|
// Upgrade.
|
||||||
|
package websocket
|
55
vendor/github.com/gorilla/websocket/json.go
generated
vendored
Normal file
55
vendor/github.com/gorilla/websocket/json.go
generated
vendored
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WriteJSON is deprecated, use c.WriteJSON instead.
|
||||||
|
func WriteJSON(c *Conn, v interface{}) error {
|
||||||
|
return c.WriteJSON(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteJSON writes the JSON encoding of v to the connection.
|
||||||
|
//
|
||||||
|
// See the documentation for encoding/json Marshal for details about the
|
||||||
|
// conversion of Go values to JSON.
|
||||||
|
func (c *Conn) WriteJSON(v interface{}) error {
|
||||||
|
w, err := c.NextWriter(TextMessage)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err1 := json.NewEncoder(w).Encode(v)
|
||||||
|
err2 := w.Close()
|
||||||
|
if err1 != nil {
|
||||||
|
return err1
|
||||||
|
}
|
||||||
|
return err2
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadJSON is deprecated, use c.ReadJSON instead.
|
||||||
|
func ReadJSON(c *Conn, v interface{}) error {
|
||||||
|
return c.ReadJSON(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadJSON reads the next JSON-encoded message from the connection and stores
|
||||||
|
// it in the value pointed to by v.
|
||||||
|
//
|
||||||
|
// See the documentation for the encoding/json Unmarshal function for details
|
||||||
|
// about the conversion of JSON to a Go value.
|
||||||
|
func (c *Conn) ReadJSON(v interface{}) error {
|
||||||
|
_, r, err := c.NextReader()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = json.NewDecoder(r).Decode(v)
|
||||||
|
if err == io.EOF {
|
||||||
|
// One value is expected in the message.
|
||||||
|
err = io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
261
vendor/github.com/gorilla/websocket/server.go
generated
vendored
Normal file
261
vendor/github.com/gorilla/websocket/server.go
generated
vendored
Normal file
|
@ -0,0 +1,261 @@
|
||||||
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandshakeError describes an error with the handshake from the peer.
|
||||||
|
type HandshakeError struct {
|
||||||
|
message string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e HandshakeError) Error() string { return e.message }
|
||||||
|
|
||||||
|
// Upgrader specifies parameters for upgrading an HTTP connection to a
|
||||||
|
// WebSocket connection.
|
||||||
|
type Upgrader struct {
|
||||||
|
// HandshakeTimeout specifies the duration for the handshake to complete.
|
||||||
|
HandshakeTimeout time.Duration
|
||||||
|
|
||||||
|
// ReadBufferSize and WriteBufferSize specify I/O buffer sizes. If a buffer
|
||||||
|
// size is zero, then a default value of 4096 is used. The I/O buffer sizes
|
||||||
|
// do not limit the size of the messages that can be sent or received.
|
||||||
|
ReadBufferSize, WriteBufferSize int
|
||||||
|
|
||||||
|
// Subprotocols specifies the server's supported protocols in order of
|
||||||
|
// preference. If this field is set, then the Upgrade method negotiates a
|
||||||
|
// subprotocol by selecting the first match in this list with a protocol
|
||||||
|
// requested by the client.
|
||||||
|
Subprotocols []string
|
||||||
|
|
||||||
|
// Error specifies the function for generating HTTP error responses. If Error
|
||||||
|
// is nil, then http.Error is used to generate the HTTP response.
|
||||||
|
Error func(w http.ResponseWriter, r *http.Request, status int, reason error)
|
||||||
|
|
||||||
|
// CheckOrigin returns true if the request Origin header is acceptable. If
|
||||||
|
// CheckOrigin is nil, the host in the Origin header must not be set or
|
||||||
|
// must match the host of the request.
|
||||||
|
CheckOrigin func(r *http.Request) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *Upgrader) returnError(w http.ResponseWriter, r *http.Request, status int, reason string) (*Conn, error) {
|
||||||
|
err := HandshakeError{reason}
|
||||||
|
if u.Error != nil {
|
||||||
|
u.Error(w, r, status, err)
|
||||||
|
} else {
|
||||||
|
w.Header().Set("Sec-Websocket-Version", "13")
|
||||||
|
http.Error(w, http.StatusText(status), status)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkSameOrigin returns true if the origin is not set or is equal to the request host.
|
||||||
|
func checkSameOrigin(r *http.Request) bool {
|
||||||
|
origin := r.Header["Origin"]
|
||||||
|
if len(origin) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
u, err := url.Parse(origin[0])
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return u.Host == r.Host
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *Upgrader) selectSubprotocol(r *http.Request, responseHeader http.Header) string {
|
||||||
|
if u.Subprotocols != nil {
|
||||||
|
clientProtocols := Subprotocols(r)
|
||||||
|
for _, serverProtocol := range u.Subprotocols {
|
||||||
|
for _, clientProtocol := range clientProtocols {
|
||||||
|
if clientProtocol == serverProtocol {
|
||||||
|
return clientProtocol
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if responseHeader != nil {
|
||||||
|
return responseHeader.Get("Sec-Websocket-Protocol")
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upgrade upgrades the HTTP server connection to the WebSocket protocol.
|
||||||
|
//
|
||||||
|
// The responseHeader is included in the response to the client's upgrade
|
||||||
|
// request. Use the responseHeader to specify cookies (Set-Cookie) and the
|
||||||
|
// application negotiated subprotocol (Sec-Websocket-Protocol).
|
||||||
|
//
|
||||||
|
// If the upgrade fails, then Upgrade replies to the client with an HTTP error
|
||||||
|
// response.
|
||||||
|
func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error) {
|
||||||
|
if r.Method != "GET" {
|
||||||
|
return u.returnError(w, r, http.StatusMethodNotAllowed, "websocket: method not GET")
|
||||||
|
}
|
||||||
|
if !tokenListContainsValue(r.Header, "Sec-Websocket-Version", "13") {
|
||||||
|
return u.returnError(w, r, http.StatusBadRequest, "websocket: version != 13")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !tokenListContainsValue(r.Header, "Connection", "upgrade") {
|
||||||
|
return u.returnError(w, r, http.StatusBadRequest, "websocket: could not find connection header with token 'upgrade'")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !tokenListContainsValue(r.Header, "Upgrade", "websocket") {
|
||||||
|
return u.returnError(w, r, http.StatusBadRequest, "websocket: could not find upgrade header with token 'websocket'")
|
||||||
|
}
|
||||||
|
|
||||||
|
checkOrigin := u.CheckOrigin
|
||||||
|
if checkOrigin == nil {
|
||||||
|
checkOrigin = checkSameOrigin
|
||||||
|
}
|
||||||
|
if !checkOrigin(r) {
|
||||||
|
return u.returnError(w, r, http.StatusForbidden, "websocket: origin not allowed")
|
||||||
|
}
|
||||||
|
|
||||||
|
challengeKey := r.Header.Get("Sec-Websocket-Key")
|
||||||
|
if challengeKey == "" {
|
||||||
|
return u.returnError(w, r, http.StatusBadRequest, "websocket: key missing or blank")
|
||||||
|
}
|
||||||
|
|
||||||
|
subprotocol := u.selectSubprotocol(r, responseHeader)
|
||||||
|
|
||||||
|
var (
|
||||||
|
netConn net.Conn
|
||||||
|
br *bufio.Reader
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
h, ok := w.(http.Hijacker)
|
||||||
|
if !ok {
|
||||||
|
return u.returnError(w, r, http.StatusInternalServerError, "websocket: response does not implement http.Hijacker")
|
||||||
|
}
|
||||||
|
var rw *bufio.ReadWriter
|
||||||
|
netConn, rw, err = h.Hijack()
|
||||||
|
if err != nil {
|
||||||
|
return u.returnError(w, r, http.StatusInternalServerError, err.Error())
|
||||||
|
}
|
||||||
|
br = rw.Reader
|
||||||
|
|
||||||
|
if br.Buffered() > 0 {
|
||||||
|
netConn.Close()
|
||||||
|
return nil, errors.New("websocket: client sent data before handshake is complete")
|
||||||
|
}
|
||||||
|
|
||||||
|
c := newConn(netConn, true, u.ReadBufferSize, u.WriteBufferSize)
|
||||||
|
c.subprotocol = subprotocol
|
||||||
|
|
||||||
|
p := c.writeBuf[:0]
|
||||||
|
p = append(p, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: "...)
|
||||||
|
p = append(p, computeAcceptKey(challengeKey)...)
|
||||||
|
p = append(p, "\r\n"...)
|
||||||
|
if c.subprotocol != "" {
|
||||||
|
p = append(p, "Sec-Websocket-Protocol: "...)
|
||||||
|
p = append(p, c.subprotocol...)
|
||||||
|
p = append(p, "\r\n"...)
|
||||||
|
}
|
||||||
|
for k, vs := range responseHeader {
|
||||||
|
if k == "Sec-Websocket-Protocol" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, v := range vs {
|
||||||
|
p = append(p, k...)
|
||||||
|
p = append(p, ": "...)
|
||||||
|
for i := 0; i < len(v); i++ {
|
||||||
|
b := v[i]
|
||||||
|
if b <= 31 {
|
||||||
|
// prevent response splitting.
|
||||||
|
b = ' '
|
||||||
|
}
|
||||||
|
p = append(p, b)
|
||||||
|
}
|
||||||
|
p = append(p, "\r\n"...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p = append(p, "\r\n"...)
|
||||||
|
|
||||||
|
// Clear deadlines set by HTTP server.
|
||||||
|
netConn.SetDeadline(time.Time{})
|
||||||
|
|
||||||
|
if u.HandshakeTimeout > 0 {
|
||||||
|
netConn.SetWriteDeadline(time.Now().Add(u.HandshakeTimeout))
|
||||||
|
}
|
||||||
|
if _, err = netConn.Write(p); err != nil {
|
||||||
|
netConn.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if u.HandshakeTimeout > 0 {
|
||||||
|
netConn.SetWriteDeadline(time.Time{})
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upgrade upgrades the HTTP server connection to the WebSocket protocol.
|
||||||
|
//
|
||||||
|
// This function is deprecated, use websocket.Upgrader instead.
|
||||||
|
//
|
||||||
|
// The application is responsible for checking the request origin before
|
||||||
|
// calling Upgrade. An example implementation of the same origin policy is:
|
||||||
|
//
|
||||||
|
// if req.Header.Get("Origin") != "http://"+req.Host {
|
||||||
|
// http.Error(w, "Origin not allowed", 403)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// If the endpoint supports subprotocols, then the application is responsible
|
||||||
|
// for negotiating the protocol used on the connection. Use the Subprotocols()
|
||||||
|
// function to get the subprotocols requested by the client. Use the
|
||||||
|
// Sec-Websocket-Protocol response header to specify the subprotocol selected
|
||||||
|
// by the application.
|
||||||
|
//
|
||||||
|
// The responseHeader is included in the response to the client's upgrade
|
||||||
|
// request. Use the responseHeader to specify cookies (Set-Cookie) and the
|
||||||
|
// negotiated subprotocol (Sec-Websocket-Protocol).
|
||||||
|
//
|
||||||
|
// The connection buffers IO to the underlying network connection. The
|
||||||
|
// readBufSize and writeBufSize parameters specify the size of the buffers to
|
||||||
|
// use. Messages can be larger than the buffers.
|
||||||
|
//
|
||||||
|
// If the request is not a valid WebSocket handshake, then Upgrade returns an
|
||||||
|
// error of type HandshakeError. Applications should handle this error by
|
||||||
|
// replying to the client with an HTTP error response.
|
||||||
|
func Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header, readBufSize, writeBufSize int) (*Conn, error) {
|
||||||
|
u := Upgrader{ReadBufferSize: readBufSize, WriteBufferSize: writeBufSize}
|
||||||
|
u.Error = func(w http.ResponseWriter, r *http.Request, status int, reason error) {
|
||||||
|
// don't return errors to maintain backwards compatibility
|
||||||
|
}
|
||||||
|
u.CheckOrigin = func(r *http.Request) bool {
|
||||||
|
// allow all connections by default
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return u.Upgrade(w, r, responseHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subprotocols returns the subprotocols requested by the client in the
|
||||||
|
// Sec-Websocket-Protocol header.
|
||||||
|
func Subprotocols(r *http.Request) []string {
|
||||||
|
h := strings.TrimSpace(r.Header.Get("Sec-Websocket-Protocol"))
|
||||||
|
if h == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
protocols := strings.Split(h, ",")
|
||||||
|
for i := range protocols {
|
||||||
|
protocols[i] = strings.TrimSpace(protocols[i])
|
||||||
|
}
|
||||||
|
return protocols
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsWebSocketUpgrade returns true if the client requested upgrade to the
|
||||||
|
// WebSocket protocol.
|
||||||
|
func IsWebSocketUpgrade(r *http.Request) bool {
|
||||||
|
return tokenListContainsValue(r.Header, "Connection", "upgrade") &&
|
||||||
|
tokenListContainsValue(r.Header, "Upgrade", "websocket")
|
||||||
|
}
|
214
vendor/github.com/gorilla/websocket/util.go
generated
vendored
Normal file
214
vendor/github.com/gorilla/websocket/util.go
generated
vendored
Normal file
|
@ -0,0 +1,214 @@
|
||||||
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/base64"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var keyGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
|
||||||
|
|
||||||
|
func computeAcceptKey(challengeKey string) string {
|
||||||
|
h := sha1.New()
|
||||||
|
h.Write([]byte(challengeKey))
|
||||||
|
h.Write(keyGUID)
|
||||||
|
return base64.StdEncoding.EncodeToString(h.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateChallengeKey() (string, error) {
|
||||||
|
p := make([]byte, 16)
|
||||||
|
if _, err := io.ReadFull(rand.Reader, p); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return base64.StdEncoding.EncodeToString(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Octet types from RFC 2616.
|
||||||
|
var octetTypes [256]byte
|
||||||
|
|
||||||
|
const (
|
||||||
|
isTokenOctet = 1 << iota
|
||||||
|
isSpaceOctet
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// From RFC 2616
|
||||||
|
//
|
||||||
|
// OCTET = <any 8-bit sequence of data>
|
||||||
|
// CHAR = <any US-ASCII character (octets 0 - 127)>
|
||||||
|
// CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
|
||||||
|
// CR = <US-ASCII CR, carriage return (13)>
|
||||||
|
// LF = <US-ASCII LF, linefeed (10)>
|
||||||
|
// SP = <US-ASCII SP, space (32)>
|
||||||
|
// HT = <US-ASCII HT, horizontal-tab (9)>
|
||||||
|
// <"> = <US-ASCII double-quote mark (34)>
|
||||||
|
// CRLF = CR LF
|
||||||
|
// LWS = [CRLF] 1*( SP | HT )
|
||||||
|
// TEXT = <any OCTET except CTLs, but including LWS>
|
||||||
|
// separators = "(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\" | <">
|
||||||
|
// | "/" | "[" | "]" | "?" | "=" | "{" | "}" | SP | HT
|
||||||
|
// token = 1*<any CHAR except CTLs or separators>
|
||||||
|
// qdtext = <any TEXT except <">>
|
||||||
|
|
||||||
|
for c := 0; c < 256; c++ {
|
||||||
|
var t byte
|
||||||
|
isCtl := c <= 31 || c == 127
|
||||||
|
isChar := 0 <= c && c <= 127
|
||||||
|
isSeparator := strings.IndexRune(" \t\"(),/:;<=>?@[]\\{}", rune(c)) >= 0
|
||||||
|
if strings.IndexRune(" \t\r\n", rune(c)) >= 0 {
|
||||||
|
t |= isSpaceOctet
|
||||||
|
}
|
||||||
|
if isChar && !isCtl && !isSeparator {
|
||||||
|
t |= isTokenOctet
|
||||||
|
}
|
||||||
|
octetTypes[c] = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func skipSpace(s string) (rest string) {
|
||||||
|
i := 0
|
||||||
|
for ; i < len(s); i++ {
|
||||||
|
if octetTypes[s[i]]&isSpaceOctet == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s[i:]
|
||||||
|
}
|
||||||
|
|
||||||
|
func nextToken(s string) (token, rest string) {
|
||||||
|
i := 0
|
||||||
|
for ; i < len(s); i++ {
|
||||||
|
if octetTypes[s[i]]&isTokenOctet == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s[:i], s[i:]
|
||||||
|
}
|
||||||
|
|
||||||
|
func nextTokenOrQuoted(s string) (value string, rest string) {
|
||||||
|
if !strings.HasPrefix(s, "\"") {
|
||||||
|
return nextToken(s)
|
||||||
|
}
|
||||||
|
s = s[1:]
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
switch s[i] {
|
||||||
|
case '"':
|
||||||
|
return s[:i], s[i+1:]
|
||||||
|
case '\\':
|
||||||
|
p := make([]byte, len(s)-1)
|
||||||
|
j := copy(p, s[:i])
|
||||||
|
escape := true
|
||||||
|
for i = i + 1; i < len(s); i++ {
|
||||||
|
b := s[i]
|
||||||
|
switch {
|
||||||
|
case escape:
|
||||||
|
escape = false
|
||||||
|
p[j] = b
|
||||||
|
j += 1
|
||||||
|
case b == '\\':
|
||||||
|
escape = true
|
||||||
|
case b == '"':
|
||||||
|
return string(p[:j]), s[i+1:]
|
||||||
|
default:
|
||||||
|
p[j] = b
|
||||||
|
j += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// tokenListContainsValue returns true if the 1#token header with the given
|
||||||
|
// name contains token.
|
||||||
|
func tokenListContainsValue(header http.Header, name string, value string) bool {
|
||||||
|
headers:
|
||||||
|
for _, s := range header[name] {
|
||||||
|
for {
|
||||||
|
var t string
|
||||||
|
t, s = nextToken(skipSpace(s))
|
||||||
|
if t == "" {
|
||||||
|
continue headers
|
||||||
|
}
|
||||||
|
s = skipSpace(s)
|
||||||
|
if s != "" && s[0] != ',' {
|
||||||
|
continue headers
|
||||||
|
}
|
||||||
|
if strings.EqualFold(t, value) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if s == "" {
|
||||||
|
continue headers
|
||||||
|
}
|
||||||
|
s = s[1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseExtensiosn parses WebSocket extensions from a header.
|
||||||
|
func parseExtensions(header http.Header) []map[string]string {
|
||||||
|
|
||||||
|
// From RFC 6455:
|
||||||
|
//
|
||||||
|
// Sec-WebSocket-Extensions = extension-list
|
||||||
|
// extension-list = 1#extension
|
||||||
|
// extension = extension-token *( ";" extension-param )
|
||||||
|
// extension-token = registered-token
|
||||||
|
// registered-token = token
|
||||||
|
// extension-param = token [ "=" (token | quoted-string) ]
|
||||||
|
// ;When using the quoted-string syntax variant, the value
|
||||||
|
// ;after quoted-string unescaping MUST conform to the
|
||||||
|
// ;'token' ABNF.
|
||||||
|
|
||||||
|
var result []map[string]string
|
||||||
|
headers:
|
||||||
|
for _, s := range header["Sec-Websocket-Extensions"] {
|
||||||
|
for {
|
||||||
|
var t string
|
||||||
|
t, s = nextToken(skipSpace(s))
|
||||||
|
if t == "" {
|
||||||
|
continue headers
|
||||||
|
}
|
||||||
|
ext := map[string]string{"": t}
|
||||||
|
for {
|
||||||
|
s = skipSpace(s)
|
||||||
|
if !strings.HasPrefix(s, ";") {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
var k string
|
||||||
|
k, s = nextToken(skipSpace(s[1:]))
|
||||||
|
if k == "" {
|
||||||
|
continue headers
|
||||||
|
}
|
||||||
|
s = skipSpace(s)
|
||||||
|
var v string
|
||||||
|
if strings.HasPrefix(s, "=") {
|
||||||
|
v, s = nextTokenOrQuoted(skipSpace(s[1:]))
|
||||||
|
s = skipSpace(s)
|
||||||
|
}
|
||||||
|
if s != "" && s[0] != ',' && s[0] != ';' {
|
||||||
|
continue headers
|
||||||
|
}
|
||||||
|
ext[k] = v
|
||||||
|
}
|
||||||
|
if s != "" && s[0] != ',' {
|
||||||
|
continue headers
|
||||||
|
}
|
||||||
|
result = append(result, ext)
|
||||||
|
if s == "" {
|
||||||
|
continue headers
|
||||||
|
}
|
||||||
|
s = s[1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
21
vendor/github.com/ian-kent/envconf/LICENSE.md
generated
vendored
Normal file
21
vendor/github.com/ian-kent/envconf/LICENSE.md
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Ian Kent
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
30
vendor/github.com/ian-kent/envconf/README.md
generated
vendored
Normal file
30
vendor/github.com/ian-kent/envconf/README.md
generated
vendored
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
envconf
|
||||||
|
=======
|
||||||
|
|
||||||
|
Configure your Go application from the environment.
|
||||||
|
|
||||||
|
Supports most basic Go types and works nicely with the built in `flag` package.
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import(
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
. "github.com/ian-kent/envconf"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
count := flag.Int("count", FromEnvP("COUNT", 15).(int), "Count target")
|
||||||
|
flag.Parse()
|
||||||
|
for i := 1; i <= *count; i++ {
|
||||||
|
fmt.Printf("%d\n", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Licence
|
||||||
|
|
||||||
|
Copyright © 2014, Ian Kent (http://iankent.uk).
|
||||||
|
|
||||||
|
Released under MIT license, see [LICENSE](LICENSE.md) for details.
|
88
vendor/github.com/ian-kent/envconf/envconf.go
generated
vendored
Normal file
88
vendor/github.com/ian-kent/envconf/envconf.go
generated
vendored
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
package envconf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrUnsupportedType is returned if the type passed in is unsupported
|
||||||
|
ErrUnsupportedType = errors.New("Unsupported type")
|
||||||
|
)
|
||||||
|
|
||||||
|
// FromEnvP is the same as FromEnv, but panics on error
|
||||||
|
func FromEnvP(env string, value interface{}) interface{} {
|
||||||
|
ev, err := FromEnv(env, value)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return ev
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromEnv returns the environment variable specified by env
|
||||||
|
// using the type of value
|
||||||
|
func FromEnv(env string, value interface{}) (interface{}, error) {
|
||||||
|
envs := os.Environ()
|
||||||
|
found := false
|
||||||
|
for _, e := range envs {
|
||||||
|
if strings.HasPrefix(e, env+"=") {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
return value, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ev := os.Getenv(env)
|
||||||
|
|
||||||
|
switch value.(type) {
|
||||||
|
case string:
|
||||||
|
vt := interface{}(ev)
|
||||||
|
return vt, nil
|
||||||
|
case int:
|
||||||
|
i, e := strconv.ParseInt(ev, 10, 64)
|
||||||
|
return int(i), e
|
||||||
|
case int8:
|
||||||
|
i, e := strconv.ParseInt(ev, 10, 8)
|
||||||
|
return int8(i), e
|
||||||
|
case int16:
|
||||||
|
i, e := strconv.ParseInt(ev, 10, 16)
|
||||||
|
return int16(i), e
|
||||||
|
case int32:
|
||||||
|
i, e := strconv.ParseInt(ev, 10, 32)
|
||||||
|
return int32(i), e
|
||||||
|
case int64:
|
||||||
|
i, e := strconv.ParseInt(ev, 10, 64)
|
||||||
|
return i, e
|
||||||
|
case uint:
|
||||||
|
i, e := strconv.ParseUint(ev, 10, 64)
|
||||||
|
return uint(i), e
|
||||||
|
case uint8:
|
||||||
|
i, e := strconv.ParseUint(ev, 10, 8)
|
||||||
|
return uint8(i), e
|
||||||
|
case uint16:
|
||||||
|
i, e := strconv.ParseUint(ev, 10, 16)
|
||||||
|
return uint16(i), e
|
||||||
|
case uint32:
|
||||||
|
i, e := strconv.ParseUint(ev, 10, 32)
|
||||||
|
return uint32(i), e
|
||||||
|
case uint64:
|
||||||
|
i, e := strconv.ParseUint(ev, 10, 64)
|
||||||
|
return i, e
|
||||||
|
case float32:
|
||||||
|
i, e := strconv.ParseFloat(ev, 32)
|
||||||
|
return float32(i), e
|
||||||
|
case float64:
|
||||||
|
i, e := strconv.ParseFloat(ev, 64)
|
||||||
|
return float64(i), e
|
||||||
|
case bool:
|
||||||
|
i, e := strconv.ParseBool(ev)
|
||||||
|
return i, e
|
||||||
|
default:
|
||||||
|
return value, ErrUnsupportedType
|
||||||
|
}
|
||||||
|
}
|
21
vendor/github.com/ian-kent/go-log/LICENSE.md
generated
vendored
Normal file
21
vendor/github.com/ian-kent/go-log/LICENSE.md
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Ian Kent
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
22
vendor/github.com/ian-kent/go-log/appenders/appender.go
generated
vendored
Normal file
22
vendor/github.com/ian-kent/go-log/appenders/appender.go
generated
vendored
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package appenders
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
Appenders control the flow of data from a logger to an output.
|
||||||
|
|
||||||
|
For example, a Console appender outputs log data to stdout.
|
||||||
|
|
||||||
|
Satisfy the Appender interface to implement your own log appender.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/ian-kent/go-log/layout"
|
||||||
|
"github.com/ian-kent/go-log/levels"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Appender interface {
|
||||||
|
Write(level levels.LogLevel, message string, args ...interface{})
|
||||||
|
Layout() layout.Layout
|
||||||
|
SetLayout(layout.Layout)
|
||||||
|
}
|
31
vendor/github.com/ian-kent/go-log/appenders/console.go
generated
vendored
Normal file
31
vendor/github.com/ian-kent/go-log/appenders/console.go
generated
vendored
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package appenders
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/ian-kent/go-log/layout"
|
||||||
|
"github.com/ian-kent/go-log/levels"
|
||||||
|
)
|
||||||
|
|
||||||
|
type consoleAppender struct {
|
||||||
|
Appender
|
||||||
|
layout layout.Layout
|
||||||
|
}
|
||||||
|
|
||||||
|
func Console() *consoleAppender {
|
||||||
|
a := &consoleAppender{
|
||||||
|
layout: layout.Default(),
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *consoleAppender) Write(level levels.LogLevel, message string, args ...interface{}) {
|
||||||
|
fmt.Println(a.Layout().Format(level, message, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *consoleAppender) Layout() layout.Layout {
|
||||||
|
return a.layout
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *consoleAppender) SetLayout(layout layout.Layout) {
|
||||||
|
a.layout = layout
|
||||||
|
}
|
64
vendor/github.com/ian-kent/go-log/appenders/fluentd.go
generated
vendored
Normal file
64
vendor/github.com/ian-kent/go-log/appenders/fluentd.go
generated
vendored
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
package appenders
|
||||||
|
|
||||||
|
// TODO add tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/ian-kent/go-log/layout"
|
||||||
|
"github.com/ian-kent/go-log/levels"
|
||||||
|
"github.com/t-k/fluent-logger-golang/fluent"
|
||||||
|
)
|
||||||
|
|
||||||
|
type fluentdAppender struct {
|
||||||
|
Appender
|
||||||
|
layout layout.Layout
|
||||||
|
fluent *fluent.Fluent
|
||||||
|
fluentConfig fluent.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func SafeFluentd(config fluent.Config) (*fluentdAppender, error) {
|
||||||
|
a := &fluentdAppender{
|
||||||
|
layout: layout.Default(),
|
||||||
|
fluentConfig: config,
|
||||||
|
}
|
||||||
|
if err := a.Open(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return a, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fluentd(config fluent.Config) *fluentdAppender {
|
||||||
|
a, _ := SafeFluentd(config)
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *fluentdAppender) Close() {
|
||||||
|
a.fluent.Close()
|
||||||
|
a.fluent = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *fluentdAppender) Open() error {
|
||||||
|
f, err := fluent.New(a.fluentConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
a.fluent = f
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *fluentdAppender) Write(level levels.LogLevel, message string, args ...interface{}) {
|
||||||
|
// FIXME
|
||||||
|
// - use tag instead of "go-log"
|
||||||
|
// - get layout to return the map
|
||||||
|
var data = map[string]string{
|
||||||
|
"message": a.Layout().Format(level, message, args...),
|
||||||
|
}
|
||||||
|
a.fluent.Post("go-log", data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *fluentdAppender) Layout() layout.Layout {
|
||||||
|
return a.layout
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *fluentdAppender) SetLayout(layout layout.Layout) {
|
||||||
|
a.layout = layout
|
||||||
|
}
|
32
vendor/github.com/ian-kent/go-log/appenders/multiple_appender.go
generated
vendored
Normal file
32
vendor/github.com/ian-kent/go-log/appenders/multiple_appender.go
generated
vendored
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package appenders
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/ian-kent/go-log/layout"
|
||||||
|
"github.com/ian-kent/go-log/levels"
|
||||||
|
)
|
||||||
|
|
||||||
|
type multipleAppender struct {
|
||||||
|
currentLayout layout.Layout
|
||||||
|
listOfAppenders []Appender
|
||||||
|
}
|
||||||
|
|
||||||
|
func Multiple(layout layout.Layout, appenders ...Appender) Appender {
|
||||||
|
return &multipleAppender{
|
||||||
|
listOfAppenders: appenders,
|
||||||
|
currentLayout: layout,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *multipleAppender) Layout() layout.Layout {
|
||||||
|
return this.currentLayout
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *multipleAppender) SetLayout(l layout.Layout) {
|
||||||
|
this.currentLayout = l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *multipleAppender) Write(level levels.LogLevel, message string, args ...interface{}) {
|
||||||
|
for _, appender := range this.listOfAppenders {
|
||||||
|
appender.Write(level, message, args...)
|
||||||
|
}
|
||||||
|
}
|
122
vendor/github.com/ian-kent/go-log/appenders/rollingfile.go
generated
vendored
Normal file
122
vendor/github.com/ian-kent/go-log/appenders/rollingfile.go
generated
vendored
Normal file
|
@ -0,0 +1,122 @@
|
||||||
|
package appenders
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/ian-kent/go-log/layout"
|
||||||
|
"github.com/ian-kent/go-log/levels"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type rollingFileAppender struct {
|
||||||
|
Appender
|
||||||
|
layout layout.Layout
|
||||||
|
MaxFileSize int64
|
||||||
|
MaxBackupIndex int
|
||||||
|
|
||||||
|
filename string
|
||||||
|
file *os.File
|
||||||
|
append bool
|
||||||
|
writeMutex sync.Mutex
|
||||||
|
|
||||||
|
bytesWritten int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func RollingFile(filename string, append bool) *rollingFileAppender {
|
||||||
|
a := &rollingFileAppender{
|
||||||
|
layout: layout.Default(),
|
||||||
|
MaxFileSize: 104857600,
|
||||||
|
MaxBackupIndex: 1,
|
||||||
|
append: append,
|
||||||
|
bytesWritten: 0,
|
||||||
|
}
|
||||||
|
err := a.SetFilename(filename)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error opening file: %s\n", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *rollingFileAppender) Close() {
|
||||||
|
if a.file != nil {
|
||||||
|
a.file.Close()
|
||||||
|
a.file = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *rollingFileAppender) Write(level levels.LogLevel, message string, args ...interface{}) {
|
||||||
|
m := a.Layout().Format(level, message, args...)
|
||||||
|
if !strings.HasSuffix(m, "\n") {
|
||||||
|
m += "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
a.writeMutex.Lock()
|
||||||
|
a.file.Write([]byte(m))
|
||||||
|
|
||||||
|
a.bytesWritten += int64(len(m))
|
||||||
|
if a.bytesWritten >= a.MaxFileSize {
|
||||||
|
a.bytesWritten = 0
|
||||||
|
a.rotateFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
a.writeMutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *rollingFileAppender) Layout() layout.Layout {
|
||||||
|
return a.layout
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *rollingFileAppender) SetLayout(layout layout.Layout) {
|
||||||
|
a.layout = layout
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *rollingFileAppender) Filename() string {
|
||||||
|
return a.filename
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *rollingFileAppender) SetFilename(filename string) error {
|
||||||
|
if a.filename != filename || a.file == nil {
|
||||||
|
a.closeFile()
|
||||||
|
a.filename = filename
|
||||||
|
err := a.openFile()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *rollingFileAppender) rotateFile() {
|
||||||
|
a.closeFile()
|
||||||
|
|
||||||
|
lastFile := a.filename + "." + strconv.Itoa(a.MaxBackupIndex)
|
||||||
|
if _, err := os.Stat(lastFile); err == nil {
|
||||||
|
os.Remove(lastFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
for n := a.MaxBackupIndex; n > 0; n-- {
|
||||||
|
f1 := a.filename + "." + strconv.Itoa(n)
|
||||||
|
f2 := a.filename + "." + strconv.Itoa(n+1)
|
||||||
|
os.Rename(f1, f2)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Rename(a.filename, a.filename+".1")
|
||||||
|
|
||||||
|
a.openFile()
|
||||||
|
}
|
||||||
|
func (a *rollingFileAppender) closeFile() {
|
||||||
|
if a.file != nil {
|
||||||
|
a.file.Close()
|
||||||
|
a.file = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (a *rollingFileAppender) openFile() error {
|
||||||
|
mode := os.O_WRONLY | os.O_APPEND | os.O_CREATE
|
||||||
|
if !a.append {
|
||||||
|
mode = os.O_WRONLY | os.O_CREATE
|
||||||
|
}
|
||||||
|
f, err := os.OpenFile(a.filename, mode, 0666)
|
||||||
|
a.file = f
|
||||||
|
return err
|
||||||
|
}
|
18
vendor/github.com/ian-kent/go-log/layout/basic.go
generated
vendored
Normal file
18
vendor/github.com/ian-kent/go-log/layout/basic.go
generated
vendored
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
package layout
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/ian-kent/go-log/levels"
|
||||||
|
)
|
||||||
|
|
||||||
|
type basicLayout struct {
|
||||||
|
Layout
|
||||||
|
}
|
||||||
|
|
||||||
|
func Basic() *basicLayout {
|
||||||
|
return &basicLayout{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *basicLayout) Format(level levels.LogLevel, message string, args ...interface{}) string {
|
||||||
|
return fmt.Sprintf(message, args...)
|
||||||
|
}
|
24
vendor/github.com/ian-kent/go-log/layout/layout.go
generated
vendored
Normal file
24
vendor/github.com/ian-kent/go-log/layout/layout.go
generated
vendored
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package layout
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
Layouts control the formatting of data into a printable log string.
|
||||||
|
|
||||||
|
For example, the Basic layout passes the log message and arguments
|
||||||
|
through fmt.Sprintf.
|
||||||
|
|
||||||
|
Satisfy the Layout interface to implement your own log layout.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/ian-kent/go-log/levels"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Layout interface {
|
||||||
|
Format(level levels.LogLevel, message string, args ...interface{}) string
|
||||||
|
}
|
||||||
|
|
||||||
|
func Default() Layout {
|
||||||
|
return Basic()
|
||||||
|
}
|
112
vendor/github.com/ian-kent/go-log/layout/pattern.go
generated
vendored
Normal file
112
vendor/github.com/ian-kent/go-log/layout/pattern.go
generated
vendored
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
package layout
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ian-kent/go-log/levels"
|
||||||
|
)
|
||||||
|
|
||||||
|
// http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
|
||||||
|
|
||||||
|
// DefaultTimeLayout is the default layout used by %d
|
||||||
|
var DefaultTimeLayout = "2006-01-02 15:04:05.000000000 -0700 MST"
|
||||||
|
|
||||||
|
// LegacyDefaultTimeLayout is the legacy (non-zero padded) time layout.
|
||||||
|
// Set layout.DefaultTimeLayout = layout.LegacyDefaultTimeLayout to revert behaviour.
|
||||||
|
var LegacyDefaultTimeLayout = "2006-01-02 15:04:05.999999999 -0700 MST"
|
||||||
|
|
||||||
|
type patternLayout struct {
|
||||||
|
Layout
|
||||||
|
Pattern string
|
||||||
|
created int64
|
||||||
|
re *regexp.Regexp
|
||||||
|
}
|
||||||
|
|
||||||
|
type caller struct {
|
||||||
|
pc uintptr
|
||||||
|
file string
|
||||||
|
line int
|
||||||
|
ok bool
|
||||||
|
pkg string
|
||||||
|
fullpkg string
|
||||||
|
filename string
|
||||||
|
}
|
||||||
|
|
||||||
|
func Pattern(pattern string) *patternLayout {
|
||||||
|
return &patternLayout{
|
||||||
|
Pattern: pattern,
|
||||||
|
re: regexp.MustCompile("%(\\w|%)(?:{([^}]+)})?"),
|
||||||
|
created: time.Now().UnixNano(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCaller() *caller {
|
||||||
|
pc, file, line, ok := runtime.Caller(2)
|
||||||
|
|
||||||
|
// TODO feels nasty?
|
||||||
|
dir, fn := filepath.Split(file)
|
||||||
|
bits := strings.Split(dir, "/")
|
||||||
|
pkg := bits[len(bits)-2]
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
return &caller{pc, file, line, ok, pkg, pkg, fn}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *patternLayout) Format(level levels.LogLevel, message string, args ...interface{}) string {
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// padding, e.g. %20c, %-20c, %.30c, %20.30c, %-20.30c
|
||||||
|
// %t - thread name
|
||||||
|
// %M - function name
|
||||||
|
|
||||||
|
caller := getCaller()
|
||||||
|
r := time.Now().UnixNano()
|
||||||
|
|
||||||
|
msg := a.re.ReplaceAllStringFunc(a.Pattern, func(m string) string {
|
||||||
|
parts := a.re.FindStringSubmatch(m)
|
||||||
|
switch parts[1] {
|
||||||
|
// FIXME
|
||||||
|
// %c and %C should probably return the logger name, not the package
|
||||||
|
// name, since that's how the logger is created in the first place!
|
||||||
|
case "c":
|
||||||
|
return caller.pkg
|
||||||
|
case "C":
|
||||||
|
return caller.pkg
|
||||||
|
case "d":
|
||||||
|
// FIXME specifier, e.g. %d{HH:mm:ss,SSS}
|
||||||
|
return time.Now().Format(DefaultTimeLayout)
|
||||||
|
case "F":
|
||||||
|
return caller.file
|
||||||
|
case "l":
|
||||||
|
return fmt.Sprintf("%s/%s:%d", caller.pkg, caller.filename, caller.line)
|
||||||
|
case "L":
|
||||||
|
return strconv.Itoa(caller.line)
|
||||||
|
case "m":
|
||||||
|
return fmt.Sprintf(message, args...)
|
||||||
|
case "n":
|
||||||
|
// FIXME platform-specific?
|
||||||
|
return "\n"
|
||||||
|
case "p":
|
||||||
|
return levels.LogLevelsToString[level]
|
||||||
|
case "r":
|
||||||
|
return strconv.FormatInt((r-a.created)/100000, 10)
|
||||||
|
case "x":
|
||||||
|
return "" // NDC
|
||||||
|
case "X":
|
||||||
|
return "" // MDC (must specify key)
|
||||||
|
case "%":
|
||||||
|
return "%"
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
})
|
||||||
|
|
||||||
|
return msg
|
||||||
|
}
|
33
vendor/github.com/ian-kent/go-log/levels/levels.go
generated
vendored
Normal file
33
vendor/github.com/ian-kent/go-log/levels/levels.go
generated
vendored
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package levels
|
||||||
|
|
||||||
|
type LogLevel int
|
||||||
|
|
||||||
|
const (
|
||||||
|
FATAL LogLevel = iota
|
||||||
|
ERROR
|
||||||
|
INFO
|
||||||
|
WARN
|
||||||
|
DEBUG
|
||||||
|
TRACE
|
||||||
|
INHERIT
|
||||||
|
)
|
||||||
|
|
||||||
|
var StringToLogLevels = map[string]LogLevel{
|
||||||
|
"TRACE": TRACE,
|
||||||
|
"DEBUG": DEBUG,
|
||||||
|
"WARN": WARN,
|
||||||
|
"INFO": INFO,
|
||||||
|
"ERROR": ERROR,
|
||||||
|
"FATAL": FATAL,
|
||||||
|
"INHERIT": INHERIT,
|
||||||
|
}
|
||||||
|
|
||||||
|
var LogLevelsToString = map[LogLevel]string{
|
||||||
|
TRACE: "TRACE",
|
||||||
|
DEBUG: "DEBUG",
|
||||||
|
WARN: "WARN",
|
||||||
|
INFO: "INFO",
|
||||||
|
ERROR: "ERROR",
|
||||||
|
FATAL: "FATAL",
|
||||||
|
INHERIT: "INHERIT",
|
||||||
|
}
|
54
vendor/github.com/ian-kent/go-log/log/log.go
generated
vendored
Normal file
54
vendor/github.com/ian-kent/go-log/log/log.go
generated
vendored
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/ian-kent/go-log/levels"
|
||||||
|
"github.com/ian-kent/go-log/logger"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var global logger.Logger
|
||||||
|
|
||||||
|
// Converts a string level (e.g. DEBUG) to a LogLevel
|
||||||
|
func Stol(level string) levels.LogLevel {
|
||||||
|
return levels.StringToLogLevels[strings.ToUpper(level)]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a Logger instance
|
||||||
|
//
|
||||||
|
// If no arguments are given, the global/root logger
|
||||||
|
// instance will be returned.
|
||||||
|
//
|
||||||
|
// If at least one argument is given, the logger instance
|
||||||
|
// for that namespace will be returned.
|
||||||
|
func Logger(args ...string) logger.Logger {
|
||||||
|
var name string
|
||||||
|
if len(args) > 0 {
|
||||||
|
name = args[0]
|
||||||
|
} else {
|
||||||
|
name = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if global == nil {
|
||||||
|
global = logger.New("")
|
||||||
|
global.SetLevel(levels.DEBUG)
|
||||||
|
}
|
||||||
|
|
||||||
|
l := global.GetLogger(name)
|
||||||
|
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func Log(level levels.LogLevel, params ...interface{}) {
|
||||||
|
Logger().Log(level, params...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Level(level levels.LogLevel) { Logger().Level() }
|
||||||
|
func Debug(params ...interface{}) { Log(levels.DEBUG, params...) }
|
||||||
|
func Info(params ...interface{}) { Log(levels.INFO, params...) }
|
||||||
|
func Warn(params ...interface{}) { Log(levels.WARN, params...) }
|
||||||
|
func Error(params ...interface{}) { Log(levels.ERROR, params...) }
|
||||||
|
func Trace(params ...interface{}) { Log(levels.TRACE, params...) }
|
||||||
|
func Fatal(params ...interface{}) { Log(levels.FATAL, params...) }
|
||||||
|
func Printf(params ...interface{}) { Log(levels.INFO, params...) }
|
||||||
|
func Println(params ...interface{}) { Log(levels.INFO, params...) }
|
||||||
|
func Fatalf(params ...interface{}) { Log(levels.FATAL, params...) }
|
221
vendor/github.com/ian-kent/go-log/logger/logger.go
generated
vendored
Normal file
221
vendor/github.com/ian-kent/go-log/logger/logger.go
generated
vendored
Normal file
|
@ -0,0 +1,221 @@
|
||||||
|
package logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/ian-kent/go-log/appenders"
|
||||||
|
"github.com/ian-kent/go-log/layout"
|
||||||
|
"github.com/ian-kent/go-log/levels"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Logger represents a logger
|
||||||
|
type Logger interface {
|
||||||
|
Level() levels.LogLevel
|
||||||
|
Name() string
|
||||||
|
FullName() string
|
||||||
|
Enabled() map[levels.LogLevel]bool
|
||||||
|
Appender() Appender
|
||||||
|
Children() []Logger
|
||||||
|
Parent() Logger
|
||||||
|
GetLogger(string) Logger
|
||||||
|
SetLevel(levels.LogLevel)
|
||||||
|
Log(levels.LogLevel, ...interface{})
|
||||||
|
SetAppender(appender Appender)
|
||||||
|
|
||||||
|
Debug(params ...interface{})
|
||||||
|
Info(params ...interface{})
|
||||||
|
Warn(params ...interface{})
|
||||||
|
Error(params ...interface{})
|
||||||
|
Trace(params ...interface{})
|
||||||
|
Printf(params ...interface{})
|
||||||
|
Println(params ...interface{})
|
||||||
|
Fatal(params ...interface{})
|
||||||
|
Fatalf(params ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type logger struct {
|
||||||
|
level levels.LogLevel
|
||||||
|
name string
|
||||||
|
enabled map[levels.LogLevel]bool
|
||||||
|
appender Appender
|
||||||
|
children []Logger
|
||||||
|
parent Logger
|
||||||
|
ExitOnFatal bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Appender represents a log appender
|
||||||
|
type Appender interface {
|
||||||
|
Write(level levels.LogLevel, message string, args ...interface{})
|
||||||
|
SetLayout(layout layout.Layout)
|
||||||
|
Layout() layout.Layout
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a new Logger
|
||||||
|
func New(name string) Logger {
|
||||||
|
l := Logger(&logger{
|
||||||
|
level: levels.DEBUG,
|
||||||
|
name: name,
|
||||||
|
enabled: make(map[levels.LogLevel]bool),
|
||||||
|
appender: appenders.Console(),
|
||||||
|
children: make([]Logger, 0),
|
||||||
|
parent: nil,
|
||||||
|
ExitOnFatal: true,
|
||||||
|
})
|
||||||
|
l.SetLevel(levels.DEBUG)
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func unwrap(args ...interface{}) []interface{} {
|
||||||
|
head := args[0]
|
||||||
|
switch head.(type) {
|
||||||
|
case func() (string, []interface{}):
|
||||||
|
msg, args := head.(func() (string, []interface{}))()
|
||||||
|
args = unwrap(args...)
|
||||||
|
return append([]interface{}{msg}, args...)
|
||||||
|
case func() []interface{}:
|
||||||
|
args = unwrap(head.(func() []interface{})()...)
|
||||||
|
case func(...interface{}) []interface{}:
|
||||||
|
args = unwrap(head.(func(...interface{}) []interface{})(args[1:]...)...)
|
||||||
|
}
|
||||||
|
return args
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) New(name string) Logger {
|
||||||
|
lg := Logger(&logger{
|
||||||
|
level: levels.INHERIT,
|
||||||
|
name: name,
|
||||||
|
enabled: make(map[levels.LogLevel]bool),
|
||||||
|
appender: nil,
|
||||||
|
children: make([]Logger, 0),
|
||||||
|
parent: l,
|
||||||
|
})
|
||||||
|
l.children = append(l.children, lg)
|
||||||
|
return lg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) GetLogger(name string) Logger {
|
||||||
|
bits := strings.Split(name, ".")
|
||||||
|
|
||||||
|
if l.name == bits[0] {
|
||||||
|
if len(bits) == 1 {
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
child := bits[1]
|
||||||
|
n := strings.Join(bits[1:], ".")
|
||||||
|
for _, c := range l.children {
|
||||||
|
if c.Name() == child {
|
||||||
|
return c.GetLogger(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lg := l.New(child)
|
||||||
|
return lg.GetLogger(n)
|
||||||
|
}
|
||||||
|
lg := l.New(bits[0])
|
||||||
|
return lg.GetLogger(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
type stringer interface {
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) write(level levels.LogLevel, params ...interface{}) {
|
||||||
|
a := l.Appender()
|
||||||
|
if a != nil {
|
||||||
|
if s, ok := params[0].(string); ok {
|
||||||
|
a.Write(level, s, params[1:]...)
|
||||||
|
} else if s, ok := params[0].(stringer); ok {
|
||||||
|
a.Write(level, s.String(), params[1:]...)
|
||||||
|
} else {
|
||||||
|
a.Write(level, fmt.Sprintf("%s", params[0]), params[1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Appender() Appender {
|
||||||
|
if a := l.appender; a != nil {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
if l.parent != nil {
|
||||||
|
if a := l.parent.Appender(); a != nil {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Log(level levels.LogLevel, params ...interface{}) {
|
||||||
|
if !l.Enabled()[level] {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.write(level, unwrap(params...)...)
|
||||||
|
|
||||||
|
if l.ExitOnFatal && level == levels.FATAL {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Level() levels.LogLevel {
|
||||||
|
if l.level == levels.INHERIT {
|
||||||
|
return l.parent.Level()
|
||||||
|
}
|
||||||
|
return l.level
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Enabled() map[levels.LogLevel]bool {
|
||||||
|
if l.level == levels.INHERIT {
|
||||||
|
return l.parent.Enabled()
|
||||||
|
}
|
||||||
|
return l.enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Name() string {
|
||||||
|
return l.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) FullName() string {
|
||||||
|
n := l.name
|
||||||
|
if l.parent != nil {
|
||||||
|
p := l.parent.FullName()
|
||||||
|
if len(p) > 0 {
|
||||||
|
n = l.parent.FullName() + "." + n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Children() []Logger {
|
||||||
|
return l.children
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Parent() Logger {
|
||||||
|
return l.parent
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) SetLevel(level levels.LogLevel) {
|
||||||
|
l.level = level
|
||||||
|
for k := range levels.LogLevelsToString {
|
||||||
|
if k <= level {
|
||||||
|
l.enabled[k] = true
|
||||||
|
} else {
|
||||||
|
l.enabled[k] = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) SetAppender(appender Appender) {
|
||||||
|
l.appender = appender
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Debug(params ...interface{}) { l.Log(levels.DEBUG, params...) }
|
||||||
|
func (l *logger) Info(params ...interface{}) { l.Log(levels.INFO, params...) }
|
||||||
|
func (l *logger) Warn(params ...interface{}) { l.Log(levels.WARN, params...) }
|
||||||
|
func (l *logger) Error(params ...interface{}) { l.Log(levels.ERROR, params...) }
|
||||||
|
func (l *logger) Trace(params ...interface{}) { l.Log(levels.TRACE, params...) }
|
||||||
|
func (l *logger) Printf(params ...interface{}) { l.Log(levels.INFO, params...) }
|
||||||
|
func (l *logger) Println(params ...interface{}) { l.Log(levels.INFO, params...) }
|
||||||
|
func (l *logger) Fatal(params ...interface{}) { l.Log(levels.FATAL, params...) }
|
||||||
|
func (l *logger) Fatalf(params ...interface{}) { l.Log(levels.FATAL, params...) }
|
21
vendor/github.com/ian-kent/goose/LICENSE.md
generated
vendored
Normal file
21
vendor/github.com/ian-kent/goose/LICENSE.md
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Ian Kent
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
12
vendor/github.com/ian-kent/goose/README.md
generated
vendored
Normal file
12
vendor/github.com/ian-kent/goose/README.md
generated
vendored
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
Goose - Go Server-Sent Events [![GoDoc](https://godoc.org/github.com/ian-kent/goose?status.svg)](https://godoc.org/github.com/ian-kent/goose)
|
||||||
|
=============================
|
||||||
|
|
||||||
|
Goose implements Server-Sent Events in Go.
|
||||||
|
|
||||||
|
See [this example](example/main.go).
|
||||||
|
|
||||||
|
### Licence
|
||||||
|
|
||||||
|
Copyright © 2014, Ian Kent (http://www.iankent.eu).
|
||||||
|
|
||||||
|
Released under MIT license, see [LICENSE](LICENSE.md) for details.
|
122
vendor/github.com/ian-kent/goose/goose.go
generated
vendored
Normal file
122
vendor/github.com/ian-kent/goose/goose.go
generated
vendored
Normal file
|
@ -0,0 +1,122 @@
|
||||||
|
package goose
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrUnableToHijackRequest is returned by AddReceiver if the type
|
||||||
|
// conversion to http.Hijacker is unsuccessful
|
||||||
|
ErrUnableToHijackRequest = errors.New("Unable to hijack request")
|
||||||
|
)
|
||||||
|
|
||||||
|
// EventStream represents a collection of receivers
|
||||||
|
type EventStream struct {
|
||||||
|
mutex *sync.Mutex
|
||||||
|
receivers map[net.Conn]*EventReceiver
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewEventStream creates a new event stream
|
||||||
|
func NewEventStream() *EventStream {
|
||||||
|
return &EventStream{
|
||||||
|
mutex: new(sync.Mutex),
|
||||||
|
receivers: make(map[net.Conn]*EventReceiver),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventReceiver represents a hijacked HTTP connection
|
||||||
|
type EventReceiver struct {
|
||||||
|
stream *EventStream
|
||||||
|
conn net.Conn
|
||||||
|
bufrw *bufio.ReadWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify sends the event to all event stream receivers
|
||||||
|
func (es *EventStream) Notify(event string, bytes []byte) {
|
||||||
|
// TODO reader?
|
||||||
|
|
||||||
|
lines := strings.Split(string(bytes), "\n")
|
||||||
|
|
||||||
|
data := ""
|
||||||
|
for _, l := range lines {
|
||||||
|
data += event + ": " + l + "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
sz := len(data) + 1
|
||||||
|
size := fmt.Sprintf("%X", sz)
|
||||||
|
|
||||||
|
for _, er := range es.receivers {
|
||||||
|
go er.send(size, data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (er *EventReceiver) send(size, data string) {
|
||||||
|
_, err := er.write([]byte(size + "\r\n"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := strings.Split(data, "\n")
|
||||||
|
for _, ln := range lines {
|
||||||
|
_, err = er.write([]byte(ln + "\n"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
er.write([]byte("\r\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (er *EventReceiver) write(bytes []byte) (int, error) {
|
||||||
|
n, err := er.bufrw.Write(bytes)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
er.stream.mutex.Lock()
|
||||||
|
delete(er.stream.receivers, er.conn)
|
||||||
|
er.stream.mutex.Unlock()
|
||||||
|
er.conn.Close()
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = er.bufrw.Flush()
|
||||||
|
if err != nil {
|
||||||
|
er.stream.mutex.Lock()
|
||||||
|
delete(er.stream.receivers, er.conn)
|
||||||
|
er.stream.mutex.Unlock()
|
||||||
|
er.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddReceiver hijacks a http.ResponseWriter and attaches it to the event stream
|
||||||
|
func (es *EventStream) AddReceiver(w http.ResponseWriter) (*EventReceiver, error) {
|
||||||
|
w.Header().Set("Content-Type", "text/event-stream")
|
||||||
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
|
w.Header().Set("Connection", "keep-alive")
|
||||||
|
|
||||||
|
w.WriteHeader(200)
|
||||||
|
|
||||||
|
hj, ok := w.(http.Hijacker)
|
||||||
|
if !ok {
|
||||||
|
return nil, ErrUnableToHijackRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
hjConn, hjBufrw, err := hj.Hijack()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rec := &EventReceiver{es, hjConn, hjBufrw}
|
||||||
|
|
||||||
|
es.mutex.Lock()
|
||||||
|
es.receivers[hjConn] = rec
|
||||||
|
es.mutex.Unlock()
|
||||||
|
|
||||||
|
return rec, nil
|
||||||
|
}
|
41
vendor/github.com/ian-kent/linkio/README.md
generated
vendored
Normal file
41
vendor/github.com/ian-kent/linkio/README.md
generated
vendored
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
linkio [![GoDoc](https://godoc.org/github.com/ian-kent/linkio?status.svg)](https://godoc.org/github.com/ian-kent/linkio) [![Build Status](https://travis-ci.org/ian-kent/linkio.svg?branch=master)](https://travis-ci.org/ian-kent/linkio)
|
||||||
|
======
|
||||||
|
|
||||||
|
linkio provides an io.Reader and io.Writer that simulate a network connection of a certain speed, e.g. to simulate a mobile connection.
|
||||||
|
|
||||||
|
### Quick start
|
||||||
|
|
||||||
|
You can use `linkio` to wrap existing io.Reader and io.Writer interfaces:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Create a new link at 512kbps
|
||||||
|
link = linkio.NewLink(512 * linkio.KilobitPerSecond)
|
||||||
|
|
||||||
|
// Open a connection
|
||||||
|
conn, err := net.Dial("tcp", "google.com:80")
|
||||||
|
if err != nil {
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a link reader/writer
|
||||||
|
linkReader := link.NewLinkReader(io.Reader(conn))
|
||||||
|
linkWriter := link.NewLinkWriter(io.Writer(conn))
|
||||||
|
|
||||||
|
// Use them as you would normally...
|
||||||
|
fmt.Fprintf(linkWriter, "GET / HTTP/1.0\r\n\r\n")
|
||||||
|
status, err := bufio.NewReader(linkReader).ReadString('\n')
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### LICENSE
|
||||||
|
|
||||||
|
This code is originally a fork of [code.google.com/p/jra-go/linkio](https://code.google.com/p/jra-go/source/browse/#hg%2Flinkio).
|
||||||
|
|
||||||
|
The source contained this license text:
|
||||||
|
|
||||||
|
Use of this source code is governed by a BSD-style
|
||||||
|
license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
There is no LICENSE file, but it [may be referring to this](http://opensource.org/licenses/BSD-3-Clause).
|
||||||
|
|
||||||
|
Any modifications since the initial commit are Copyright © 2014, Ian Kent (http://iankent.uk), and are released under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
169
vendor/github.com/ian-kent/linkio/linkio.go
generated
vendored
Normal file
169
vendor/github.com/ian-kent/linkio/linkio.go
generated
vendored
Normal file
|
@ -0,0 +1,169 @@
|
||||||
|
// Copyright 2011 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package linkio provides an io.Reader and io.Writer that
|
||||||
|
// simulate a network connection of a certain speed.
|
||||||
|
package linkio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Throughput represents the link speed as an int64 bits per second
|
||||||
|
// count. The representation limits the largest representable throughput
|
||||||
|
// to approximately 9223 petabits per second.
|
||||||
|
type Throughput int64
|
||||||
|
|
||||||
|
// Common throughputs.
|
||||||
|
//
|
||||||
|
// To count the number of units in a Duration, divide:
|
||||||
|
// kilobit := linkio.KilobitPerSecond
|
||||||
|
// fmt.Print(int64(kilobit/linkio.BitPerSecond)) // prints 1024
|
||||||
|
//
|
||||||
|
// To convert an integer number of units to a Throughput, multiply:
|
||||||
|
// megabits := 10
|
||||||
|
// fmt.Print(linkio.Throughput(megabits)*time.BitPerSecond) // prints 10s
|
||||||
|
//
|
||||||
|
const (
|
||||||
|
BitPerSecond Throughput = 1
|
||||||
|
BytePerSecond = 8 * BitPerSecond
|
||||||
|
KilobitPerSecond = 1024 * BitPerSecond
|
||||||
|
KilobytePerSecond = 1024 * BytePerSecond
|
||||||
|
MegabitPerSecond = 1024 * KilobitPerSecond
|
||||||
|
MegabytePerSecond = 1024 * KilobytePerSecond
|
||||||
|
GigabitPerSecond = 1024 * MegabitPerSecond
|
||||||
|
GigabytePerSecond = 1024 * MegabytePerSecond
|
||||||
|
)
|
||||||
|
|
||||||
|
// A LinkReader wraps an io.Reader, simulating reading from a
|
||||||
|
// shared access link with a fixed maximum speed.
|
||||||
|
type LinkReader struct {
|
||||||
|
r io.Reader
|
||||||
|
link *Link
|
||||||
|
}
|
||||||
|
|
||||||
|
// A LinkWriter wraps an io.Writer, simulating writer to a
|
||||||
|
// shared access link with a fixed maximum speed.
|
||||||
|
type LinkWriter struct {
|
||||||
|
w io.Writer
|
||||||
|
link *Link
|
||||||
|
}
|
||||||
|
|
||||||
|
// A Link serializes requests to sleep, simulating the way data travels
|
||||||
|
// across a link which is running at a certain kbps (kilo = 1024).
|
||||||
|
// Multiple LinkReaders can share a link (simulating multiple apps
|
||||||
|
// sharing a link). The sharing behavior is approximately fair, as implemented
|
||||||
|
// by Go when scheduling reads from a contested blocking channel.
|
||||||
|
type Link struct {
|
||||||
|
in chan linkRequest
|
||||||
|
out chan linkRequest
|
||||||
|
speed int64 // nanosec per bit
|
||||||
|
}
|
||||||
|
|
||||||
|
// A linkRequest asks the link to simulate sending that much data
|
||||||
|
// and return a true on the channel when it has accomplished the request.
|
||||||
|
type linkRequest struct {
|
||||||
|
bytes int
|
||||||
|
done chan bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLinkReader returns a LinkReader that returns bytes from r,
|
||||||
|
// simulating that they arrived from a shared link.
|
||||||
|
func (link *Link) NewLinkReader(r io.Reader) (s *LinkReader) {
|
||||||
|
s = &LinkReader{r: r, link: link}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLinkWriter returns a LinkWriter that writes bytes to r,
|
||||||
|
// simulating that they arrived from a shared link.
|
||||||
|
func (link *Link) NewLinkWriter(w io.Writer) (s *LinkWriter) {
|
||||||
|
s = &LinkWriter{w: w, link: link}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLink returns a new Link running at kbps.
|
||||||
|
func NewLink(throughput Throughput) (l *Link) {
|
||||||
|
// allow up to 100 outstanding requests
|
||||||
|
l = &Link{in: make(chan linkRequest, 100), out: make(chan linkRequest, 100)}
|
||||||
|
l.SetThroughput(throughput)
|
||||||
|
|
||||||
|
// This goroutine serializes the requests. He could calculate
|
||||||
|
// link utilization by comparing the time he sleeps waiting for
|
||||||
|
// linkRequests to arrive and the time he spends sleeping to simulate
|
||||||
|
// traffic flowing.
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for lr := range l.in {
|
||||||
|
// bits * nanosec/bit = nano to wait
|
||||||
|
delay := time.Duration(int64(lr.bytes*8) * l.speed)
|
||||||
|
time.Sleep(delay)
|
||||||
|
lr.done <- true
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
for lr := range l.out {
|
||||||
|
// bits * nanosec/bit = nano to wait
|
||||||
|
delay := time.Duration(int64(lr.bytes*8) * l.speed)
|
||||||
|
time.Sleep(delay)
|
||||||
|
lr.done <- true
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetThroughput sets the current link throughput
|
||||||
|
func (link *Link) SetThroughput(throughput Throughput) {
|
||||||
|
// link.speed is stored in ns/bit
|
||||||
|
link.speed = 1e9 / int64(throughput)
|
||||||
|
}
|
||||||
|
|
||||||
|
// why isn't this in package math? hmm.
|
||||||
|
func min(a, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Satisfies interface io.Reader.
|
||||||
|
func (l *LinkReader) Read(buf []byte) (n int, err error) {
|
||||||
|
// Read small chunks at a time, even if they ask for more,
|
||||||
|
// preventing one LinkReader from saturating the simulated link.
|
||||||
|
// 1500 is the MTU for Ethernet, i.e. a likely maximum packet
|
||||||
|
// size.
|
||||||
|
toRead := min(len(buf), 1500)
|
||||||
|
n, err = l.r.Read(buf[0:toRead])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// send in the request to sleep to the Link and sleep
|
||||||
|
lr := linkRequest{bytes: n, done: make(chan bool)}
|
||||||
|
l.link.in <- lr
|
||||||
|
_ = <-lr.done
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Satisfies interface io.Writer.
|
||||||
|
func (l *LinkWriter) Write(buf []byte) (n int, err error) {
|
||||||
|
// Write small chunks at a time, even if they attempt more,
|
||||||
|
// preventing one LinkReader from saturating the simulated link.
|
||||||
|
// 1500 is the MTU for Ethernet, i.e. a likely maximum packet
|
||||||
|
// size.
|
||||||
|
toWrite := min(len(buf), 1500)
|
||||||
|
n, err = l.w.Write(buf[0:toWrite])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// send in the request to sleep to the Link and sleep
|
||||||
|
lr := linkRequest{bytes: n, done: make(chan bool)}
|
||||||
|
l.link.in <- lr
|
||||||
|
_ = <-lr.done
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
21
vendor/github.com/mailhog/MailHog-Server/LICENSE.md
generated
vendored
Normal file
21
vendor/github.com/mailhog/MailHog-Server/LICENSE.md
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 - 2016 Ian Kent
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
23
vendor/github.com/mailhog/MailHog-Server/api/api.go
generated
vendored
Normal file
23
vendor/github.com/mailhog/MailHog-Server/api/api.go
generated
vendored
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
gohttp "net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/pat"
|
||||||
|
"github.com/mailhog/MailHog-Server/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateAPI(conf *config.Config, r gohttp.Handler) {
|
||||||
|
apiv1 := createAPIv1(conf, r.(*pat.Router))
|
||||||
|
apiv2 := createAPIv2(conf, r.(*pat.Router))
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case msg := <-conf.MessageChan:
|
||||||
|
apiv1.messageChan <- msg
|
||||||
|
apiv2.messageChan <- msg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
359
vendor/github.com/mailhog/MailHog-Server/api/v1.go
generated
vendored
Normal file
359
vendor/github.com/mailhog/MailHog-Server/api/v1.go
generated
vendored
Normal file
|
@ -0,0 +1,359 @@
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/smtp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/pat"
|
||||||
|
"github.com/ian-kent/go-log/log"
|
||||||
|
"github.com/mailhog/MailHog-Server/config"
|
||||||
|
"github.com/mailhog/data"
|
||||||
|
"github.com/mailhog/storage"
|
||||||
|
|
||||||
|
"github.com/ian-kent/goose"
|
||||||
|
)
|
||||||
|
|
||||||
|
// APIv1 implements version 1 of the MailHog API
|
||||||
|
//
|
||||||
|
// The specification has been frozen and will eventually be deprecated.
|
||||||
|
// Only bug fixes and non-breaking changes will be applied here.
|
||||||
|
//
|
||||||
|
// Any changes/additions should be added in APIv2.
|
||||||
|
type APIv1 struct {
|
||||||
|
config *config.Config
|
||||||
|
messageChan chan *data.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME should probably move this into APIv1 struct
|
||||||
|
var stream *goose.EventStream
|
||||||
|
|
||||||
|
// ReleaseConfig is an alias to preserve go package API
|
||||||
|
type ReleaseConfig config.OutgoingSMTP
|
||||||
|
|
||||||
|
func createAPIv1(conf *config.Config, r *pat.Router) *APIv1 {
|
||||||
|
log.Println("Creating API v1 with WebPath: " + conf.WebPath)
|
||||||
|
apiv1 := &APIv1{
|
||||||
|
config: conf,
|
||||||
|
messageChan: make(chan *data.Message),
|
||||||
|
}
|
||||||
|
|
||||||
|
stream = goose.NewEventStream()
|
||||||
|
|
||||||
|
r.Path(conf.WebPath + "/api/v1/messages").Methods("GET").HandlerFunc(apiv1.messages)
|
||||||
|
r.Path(conf.WebPath + "/api/v1/messages").Methods("DELETE").HandlerFunc(apiv1.delete_all)
|
||||||
|
r.Path(conf.WebPath + "/api/v1/messages").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)
|
||||||
|
|
||||||
|
r.Path(conf.WebPath + "/api/v1/messages/{id}").Methods("GET").HandlerFunc(apiv1.message)
|
||||||
|
r.Path(conf.WebPath + "/api/v1/messages/{id}").Methods("DELETE").HandlerFunc(apiv1.delete_one)
|
||||||
|
r.Path(conf.WebPath + "/api/v1/messages/{id}").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)
|
||||||
|
|
||||||
|
r.Path(conf.WebPath + "/api/v1/messages/{id}/download").Methods("GET").HandlerFunc(apiv1.download)
|
||||||
|
r.Path(conf.WebPath + "/api/v1/messages/{id}/download").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)
|
||||||
|
|
||||||
|
r.Path(conf.WebPath + "/api/v1/messages/{id}/mime/part/{part}/download").Methods("GET").HandlerFunc(apiv1.download_part)
|
||||||
|
r.Path(conf.WebPath + "/api/v1/messages/{id}/mime/part/{part}/download").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)
|
||||||
|
|
||||||
|
r.Path(conf.WebPath + "/api/v1/messages/{id}/release").Methods("POST").HandlerFunc(apiv1.release_one)
|
||||||
|
r.Path(conf.WebPath + "/api/v1/messages/{id}/release").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)
|
||||||
|
|
||||||
|
r.Path(conf.WebPath + "/api/v1/events").Methods("GET").HandlerFunc(apiv1.eventstream)
|
||||||
|
r.Path(conf.WebPath + "/api/v1/events").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
keepaliveTicker := time.Tick(time.Minute)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case msg := <-apiv1.messageChan:
|
||||||
|
log.Println("Got message in APIv1 event stream")
|
||||||
|
bytes, _ := json.MarshalIndent(msg, "", " ")
|
||||||
|
json := string(bytes)
|
||||||
|
log.Printf("Sending content: %s\n", json)
|
||||||
|
apiv1.broadcast(json)
|
||||||
|
case <-keepaliveTicker:
|
||||||
|
apiv1.keepalive()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return apiv1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv1 *APIv1) defaultOptions(w http.ResponseWriter, req *http.Request) {
|
||||||
|
if len(apiv1.config.CORSOrigin) > 0 {
|
||||||
|
w.Header().Add("Access-Control-Allow-Origin", apiv1.config.CORSOrigin)
|
||||||
|
w.Header().Add("Access-Control-Allow-Methods", "OPTIONS,GET,POST,DELETE")
|
||||||
|
w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv1 *APIv1) broadcast(json string) {
|
||||||
|
log.Println("[APIv1] BROADCAST /api/v1/events")
|
||||||
|
b := []byte(json)
|
||||||
|
stream.Notify("data", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// keepalive sends an empty keep alive message.
|
||||||
|
//
|
||||||
|
// This not only can keep connections alive, but also will detect broken
|
||||||
|
// connections. Without this it is possible for the server to become
|
||||||
|
// unresponsive due to too many open files.
|
||||||
|
func (apiv1 *APIv1) keepalive() {
|
||||||
|
log.Println("[APIv1] KEEPALIVE /api/v1/events")
|
||||||
|
stream.Notify("keepalive", []byte{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv1 *APIv1) eventstream(w http.ResponseWriter, req *http.Request) {
|
||||||
|
log.Println("[APIv1] GET /api/v1/events")
|
||||||
|
|
||||||
|
//apiv1.defaultOptions(session)
|
||||||
|
if len(apiv1.config.CORSOrigin) > 0 {
|
||||||
|
w.Header().Add("Access-Control-Allow-Origin", apiv1.config.CORSOrigin)
|
||||||
|
w.Header().Add("Access-Control-Allow-Methods", "OPTIONS,GET,POST,DELETE")
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.AddReceiver(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv1 *APIv1) messages(w http.ResponseWriter, req *http.Request) {
|
||||||
|
log.Println("[APIv1] GET /api/v1/messages")
|
||||||
|
|
||||||
|
apiv1.defaultOptions(w, req)
|
||||||
|
|
||||||
|
// TODO start, limit
|
||||||
|
switch apiv1.config.Storage.(type) {
|
||||||
|
case *storage.MongoDB:
|
||||||
|
messages, _ := apiv1.config.Storage.(*storage.MongoDB).List(0, 1000)
|
||||||
|
bytes, _ := json.Marshal(messages)
|
||||||
|
w.Header().Add("Content-Type", "text/json")
|
||||||
|
w.Write(bytes)
|
||||||
|
case *storage.InMemory:
|
||||||
|
messages, _ := apiv1.config.Storage.(*storage.InMemory).List(0, 1000)
|
||||||
|
bytes, _ := json.Marshal(messages)
|
||||||
|
w.Header().Add("Content-Type", "text/json")
|
||||||
|
w.Write(bytes)
|
||||||
|
default:
|
||||||
|
w.WriteHeader(500)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv1 *APIv1) message(w http.ResponseWriter, req *http.Request) {
|
||||||
|
id := req.URL.Query().Get(":id")
|
||||||
|
log.Printf("[APIv1] GET /api/v1/messages/%s\n", id)
|
||||||
|
|
||||||
|
apiv1.defaultOptions(w, req)
|
||||||
|
|
||||||
|
message, err := apiv1.config.Storage.Load(id)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("- Error: %s", err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := json.Marshal(message)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("- Error: %s", err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/json")
|
||||||
|
w.Write(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv1 *APIv1) download(w http.ResponseWriter, req *http.Request) {
|
||||||
|
id := req.URL.Query().Get(":id")
|
||||||
|
log.Printf("[APIv1] GET /api/v1/messages/%s\n", id)
|
||||||
|
|
||||||
|
apiv1.defaultOptions(w, req)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "message/rfc822")
|
||||||
|
w.Header().Set("Content-Disposition", "attachment; filename=\""+id+".eml\"")
|
||||||
|
|
||||||
|
switch apiv1.config.Storage.(type) {
|
||||||
|
case *storage.MongoDB:
|
||||||
|
message, _ := apiv1.config.Storage.(*storage.MongoDB).Load(id)
|
||||||
|
for h, l := range message.Content.Headers {
|
||||||
|
for _, v := range l {
|
||||||
|
w.Write([]byte(h + ": " + v + "\r\n"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.Write([]byte("\r\n" + message.Content.Body))
|
||||||
|
case *storage.InMemory:
|
||||||
|
message, _ := apiv1.config.Storage.(*storage.InMemory).Load(id)
|
||||||
|
for h, l := range message.Content.Headers {
|
||||||
|
for _, v := range l {
|
||||||
|
w.Write([]byte(h + ": " + v + "\r\n"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.Write([]byte("\r\n" + message.Content.Body))
|
||||||
|
default:
|
||||||
|
w.WriteHeader(500)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv1 *APIv1) download_part(w http.ResponseWriter, req *http.Request) {
|
||||||
|
id := req.URL.Query().Get(":id")
|
||||||
|
part := req.URL.Query().Get(":part")
|
||||||
|
log.Printf("[APIv1] GET /api/v1/messages/%s/mime/part/%s/download\n", id, part)
|
||||||
|
|
||||||
|
// TODO extension from content-type?
|
||||||
|
apiv1.defaultOptions(w, req)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Disposition", "attachment; filename=\""+id+"-part-"+part+"\"")
|
||||||
|
|
||||||
|
message, _ := apiv1.config.Storage.Load(id)
|
||||||
|
contentTransferEncoding := ""
|
||||||
|
pid, _ := strconv.Atoi(part)
|
||||||
|
for h, l := range message.MIME.Parts[pid].Headers {
|
||||||
|
for _, v := range l {
|
||||||
|
switch strings.ToLower(h) {
|
||||||
|
case "content-disposition":
|
||||||
|
// Prevent duplicate "content-disposition"
|
||||||
|
w.Header().Set(h, v)
|
||||||
|
case "content-transfer-encoding":
|
||||||
|
if contentTransferEncoding == "" {
|
||||||
|
contentTransferEncoding = v
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
w.Header().Add(h, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
body := []byte(message.MIME.Parts[pid].Body)
|
||||||
|
if strings.ToLower(contentTransferEncoding) == "base64" {
|
||||||
|
var e error
|
||||||
|
body, e = base64.StdEncoding.DecodeString(message.MIME.Parts[pid].Body)
|
||||||
|
if e != nil {
|
||||||
|
log.Printf("[APIv1] Decoding base64 encoded body failed: %s", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.Write(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv1 *APIv1) delete_all(w http.ResponseWriter, req *http.Request) {
|
||||||
|
log.Println("[APIv1] POST /api/v1/messages")
|
||||||
|
|
||||||
|
apiv1.defaultOptions(w, req)
|
||||||
|
|
||||||
|
w.Header().Add("Content-Type", "text/json")
|
||||||
|
|
||||||
|
err := apiv1.config.Storage.DeleteAll()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(200)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv1 *APIv1) release_one(w http.ResponseWriter, req *http.Request) {
|
||||||
|
id := req.URL.Query().Get(":id")
|
||||||
|
log.Printf("[APIv1] POST /api/v1/messages/%s/release\n", id)
|
||||||
|
|
||||||
|
apiv1.defaultOptions(w, req)
|
||||||
|
|
||||||
|
w.Header().Add("Content-Type", "text/json")
|
||||||
|
msg, _ := apiv1.config.Storage.Load(id)
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(req.Body)
|
||||||
|
var cfg ReleaseConfig
|
||||||
|
err := decoder.Decode(&cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error decoding request body: %s", err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte("Error decoding request body"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%+v", cfg)
|
||||||
|
|
||||||
|
log.Printf("Got message: %s", msg.ID)
|
||||||
|
|
||||||
|
if cfg.Save {
|
||||||
|
if _, ok := apiv1.config.OutgoingSMTP[cfg.Name]; ok {
|
||||||
|
log.Printf("Server already exists named %s", cfg.Name)
|
||||||
|
w.WriteHeader(400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cf := config.OutgoingSMTP(cfg)
|
||||||
|
apiv1.config.OutgoingSMTP[cfg.Name] = &cf
|
||||||
|
log.Printf("Saved server with name %s", cfg.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cfg.Name) > 0 {
|
||||||
|
if c, ok := apiv1.config.OutgoingSMTP[cfg.Name]; ok {
|
||||||
|
log.Printf("Using server with name: %s", cfg.Name)
|
||||||
|
cfg.Name = c.Name
|
||||||
|
if len(cfg.Email) == 0 {
|
||||||
|
cfg.Email = c.Email
|
||||||
|
}
|
||||||
|
cfg.Host = c.Host
|
||||||
|
cfg.Port = c.Port
|
||||||
|
cfg.Username = c.Username
|
||||||
|
cfg.Password = c.Password
|
||||||
|
cfg.Mechanism = c.Mechanism
|
||||||
|
} else {
|
||||||
|
log.Printf("Server not found: %s", cfg.Name)
|
||||||
|
w.WriteHeader(400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Releasing to %s (via %s:%s)", cfg.Email, cfg.Host, cfg.Port)
|
||||||
|
|
||||||
|
bytes := make([]byte, 0)
|
||||||
|
for h, l := range msg.Content.Headers {
|
||||||
|
for _, v := range l {
|
||||||
|
bytes = append(bytes, []byte(h+": "+v+"\r\n")...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bytes = append(bytes, []byte("\r\n"+msg.Content.Body)...)
|
||||||
|
|
||||||
|
var auth smtp.Auth
|
||||||
|
|
||||||
|
if len(cfg.Username) > 0 || len(cfg.Password) > 0 {
|
||||||
|
log.Printf("Found username/password, using auth mechanism: [%s]", cfg.Mechanism)
|
||||||
|
switch cfg.Mechanism {
|
||||||
|
case "CRAMMD5":
|
||||||
|
auth = smtp.CRAMMD5Auth(cfg.Username, cfg.Password)
|
||||||
|
case "PLAIN":
|
||||||
|
auth = smtp.PlainAuth("", cfg.Username, cfg.Password, cfg.Host)
|
||||||
|
default:
|
||||||
|
log.Printf("Error - invalid authentication mechanism")
|
||||||
|
w.WriteHeader(400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = smtp.SendMail(cfg.Host+":"+cfg.Port, auth, "nobody@"+apiv1.config.Hostname, []string{cfg.Email}, bytes)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to release message: %s", err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("Message released successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv1 *APIv1) delete_one(w http.ResponseWriter, req *http.Request) {
|
||||||
|
id := req.URL.Query().Get(":id")
|
||||||
|
|
||||||
|
log.Printf("[APIv1] POST /api/v1/messages/%s/delete\n", id)
|
||||||
|
|
||||||
|
apiv1.defaultOptions(w, req)
|
||||||
|
|
||||||
|
w.Header().Add("Content-Type", "text/json")
|
||||||
|
err := apiv1.config.Storage.DeleteOne(id)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(200)
|
||||||
|
}
|
258
vendor/github.com/mailhog/MailHog-Server/api/v2.go
generated
vendored
Normal file
258
vendor/github.com/mailhog/MailHog-Server/api/v2.go
generated
vendored
Normal file
|
@ -0,0 +1,258 @@
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/gorilla/pat"
|
||||||
|
"github.com/ian-kent/go-log/log"
|
||||||
|
"github.com/mailhog/MailHog-Server/config"
|
||||||
|
"github.com/mailhog/MailHog-Server/monkey"
|
||||||
|
"github.com/mailhog/MailHog-Server/websockets"
|
||||||
|
"github.com/mailhog/data"
|
||||||
|
)
|
||||||
|
|
||||||
|
// APIv2 implements version 2 of the MailHog API
|
||||||
|
//
|
||||||
|
// It is currently experimental and may change in future releases.
|
||||||
|
// Use APIv1 for guaranteed compatibility.
|
||||||
|
type APIv2 struct {
|
||||||
|
config *config.Config
|
||||||
|
messageChan chan *data.Message
|
||||||
|
wsHub *websockets.Hub
|
||||||
|
}
|
||||||
|
|
||||||
|
func createAPIv2(conf *config.Config, r *pat.Router) *APIv2 {
|
||||||
|
log.Println("Creating API v2 with WebPath: " + conf.WebPath)
|
||||||
|
apiv2 := &APIv2{
|
||||||
|
config: conf,
|
||||||
|
messageChan: make(chan *data.Message),
|
||||||
|
wsHub: websockets.NewHub(),
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Path(conf.WebPath + "/api/v2/messages").Methods("GET").HandlerFunc(apiv2.messages)
|
||||||
|
r.Path(conf.WebPath + "/api/v2/messages").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)
|
||||||
|
|
||||||
|
r.Path(conf.WebPath + "/api/v2/search").Methods("GET").HandlerFunc(apiv2.search)
|
||||||
|
r.Path(conf.WebPath + "/api/v2/search").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)
|
||||||
|
|
||||||
|
r.Path(conf.WebPath + "/api/v2/jim").Methods("GET").HandlerFunc(apiv2.jim)
|
||||||
|
r.Path(conf.WebPath + "/api/v2/jim").Methods("POST").HandlerFunc(apiv2.createJim)
|
||||||
|
r.Path(conf.WebPath + "/api/v2/jim").Methods("PUT").HandlerFunc(apiv2.updateJim)
|
||||||
|
r.Path(conf.WebPath + "/api/v2/jim").Methods("DELETE").HandlerFunc(apiv2.deleteJim)
|
||||||
|
r.Path(conf.WebPath + "/api/v2/jim").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)
|
||||||
|
|
||||||
|
r.Path(conf.WebPath + "/api/v2/outgoing-smtp").Methods("GET").HandlerFunc(apiv2.listOutgoingSMTP)
|
||||||
|
r.Path(conf.WebPath + "/api/v2/outgoing-smtp").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)
|
||||||
|
|
||||||
|
r.Path(conf.WebPath + "/api/v2/websocket").Methods("GET").HandlerFunc(apiv2.websocket)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case msg := <-apiv2.messageChan:
|
||||||
|
log.Println("Got message in APIv2 websocket channel")
|
||||||
|
apiv2.broadcast(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return apiv2
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv2 *APIv2) defaultOptions(w http.ResponseWriter, req *http.Request) {
|
||||||
|
if len(apiv2.config.CORSOrigin) > 0 {
|
||||||
|
w.Header().Add("Access-Control-Allow-Origin", apiv2.config.CORSOrigin)
|
||||||
|
w.Header().Add("Access-Control-Allow-Methods", "OPTIONS,GET,PUT,POST,DELETE")
|
||||||
|
w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type messagesResult struct {
|
||||||
|
Total int `json:"total"`
|
||||||
|
Count int `json:"count"`
|
||||||
|
Start int `json:"start"`
|
||||||
|
Items []data.Message `json:"items"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv2 *APIv2) getStartLimit(w http.ResponseWriter, req *http.Request) (start, limit int) {
|
||||||
|
start = 0
|
||||||
|
limit = 50
|
||||||
|
|
||||||
|
s := req.URL.Query().Get("start")
|
||||||
|
if n, e := strconv.ParseInt(s, 10, 64); e == nil && n > 0 {
|
||||||
|
start = int(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
l := req.URL.Query().Get("limit")
|
||||||
|
if n, e := strconv.ParseInt(l, 10, 64); e == nil && n > 0 {
|
||||||
|
if n > 250 {
|
||||||
|
n = 250
|
||||||
|
}
|
||||||
|
limit = int(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv2 *APIv2) messages(w http.ResponseWriter, req *http.Request) {
|
||||||
|
log.Println("[APIv2] GET /api/v2/messages")
|
||||||
|
|
||||||
|
apiv2.defaultOptions(w, req)
|
||||||
|
|
||||||
|
start, limit := apiv2.getStartLimit(w, req)
|
||||||
|
|
||||||
|
var res messagesResult
|
||||||
|
|
||||||
|
messages, err := apiv2.config.Storage.List(start, limit)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res.Count = len([]data.Message(*messages))
|
||||||
|
res.Start = start
|
||||||
|
res.Items = []data.Message(*messages)
|
||||||
|
res.Total = apiv2.config.Storage.Count()
|
||||||
|
|
||||||
|
bytes, _ := json.Marshal(res)
|
||||||
|
w.Header().Add("Content-Type", "text/json")
|
||||||
|
w.Write(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv2 *APIv2) search(w http.ResponseWriter, req *http.Request) {
|
||||||
|
log.Println("[APIv2] GET /api/v2/search")
|
||||||
|
|
||||||
|
apiv2.defaultOptions(w, req)
|
||||||
|
|
||||||
|
start, limit := apiv2.getStartLimit(w, req)
|
||||||
|
|
||||||
|
kind := req.URL.Query().Get("kind")
|
||||||
|
if kind != "from" && kind != "to" && kind != "containing" {
|
||||||
|
w.WriteHeader(400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
query := req.URL.Query().Get("query")
|
||||||
|
if len(query) == 0 {
|
||||||
|
w.WriteHeader(400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var res messagesResult
|
||||||
|
|
||||||
|
messages, total, _ := apiv2.config.Storage.Search(kind, query, start, limit)
|
||||||
|
|
||||||
|
res.Count = len([]data.Message(*messages))
|
||||||
|
res.Start = start
|
||||||
|
res.Items = []data.Message(*messages)
|
||||||
|
res.Total = total
|
||||||
|
|
||||||
|
b, _ := json.Marshal(res)
|
||||||
|
w.Header().Add("Content-Type", "application/json")
|
||||||
|
w.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv2 *APIv2) jim(w http.ResponseWriter, req *http.Request) {
|
||||||
|
log.Println("[APIv2] GET /api/v2/jim")
|
||||||
|
|
||||||
|
apiv2.defaultOptions(w, req)
|
||||||
|
|
||||||
|
if apiv2.config.Monkey == nil {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
b, _ := json.Marshal(apiv2.config.Monkey)
|
||||||
|
w.Header().Add("Content-Type", "application/json")
|
||||||
|
w.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv2 *APIv2) deleteJim(w http.ResponseWriter, req *http.Request) {
|
||||||
|
log.Println("[APIv2] DELETE /api/v2/jim")
|
||||||
|
|
||||||
|
apiv2.defaultOptions(w, req)
|
||||||
|
|
||||||
|
if apiv2.config.Monkey == nil {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
apiv2.config.Monkey = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv2 *APIv2) createJim(w http.ResponseWriter, req *http.Request) {
|
||||||
|
log.Println("[APIv2] POST /api/v2/jim")
|
||||||
|
|
||||||
|
apiv2.defaultOptions(w, req)
|
||||||
|
|
||||||
|
if apiv2.config.Monkey != nil {
|
||||||
|
w.WriteHeader(400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
apiv2.config.Monkey = config.Jim
|
||||||
|
|
||||||
|
// Try, but ignore errors
|
||||||
|
// Could be better (e.g., ok if no json, error if badly formed json)
|
||||||
|
// but this works for now
|
||||||
|
apiv2.newJimFromBody(w, req)
|
||||||
|
|
||||||
|
w.WriteHeader(201)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv2 *APIv2) newJimFromBody(w http.ResponseWriter, req *http.Request) error {
|
||||||
|
var jim monkey.Jim
|
||||||
|
|
||||||
|
dec := json.NewDecoder(req.Body)
|
||||||
|
err := dec.Decode(&jim)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
jim.ConfigureFrom(config.Jim)
|
||||||
|
|
||||||
|
config.Jim = &jim
|
||||||
|
apiv2.config.Monkey = &jim
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv2 *APIv2) updateJim(w http.ResponseWriter, req *http.Request) {
|
||||||
|
log.Println("[APIv2] PUT /api/v2/jim")
|
||||||
|
|
||||||
|
apiv2.defaultOptions(w, req)
|
||||||
|
|
||||||
|
if apiv2.config.Monkey == nil {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := apiv2.newJimFromBody(w, req)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(400)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv2 *APIv2) listOutgoingSMTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
log.Println("[APIv2] GET /api/v2/outgoing-smtp")
|
||||||
|
|
||||||
|
apiv2.defaultOptions(w, req)
|
||||||
|
|
||||||
|
b, _ := json.Marshal(apiv2.config.OutgoingSMTP)
|
||||||
|
w.Header().Add("Content-Type", "application/json")
|
||||||
|
w.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv2 *APIv2) websocket(w http.ResponseWriter, req *http.Request) {
|
||||||
|
log.Println("[APIv2] GET /api/v2/websocket")
|
||||||
|
|
||||||
|
apiv2.wsHub.Serve(w, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apiv2 *APIv2) broadcast(msg *data.Message) {
|
||||||
|
log.Println("[APIv2] BROADCAST /api/v2/websocket")
|
||||||
|
|
||||||
|
apiv2.wsHub.Broadcast(msg)
|
||||||
|
}
|
132
vendor/github.com/mailhog/MailHog-Server/config/config.go
generated
vendored
Normal file
132
vendor/github.com/mailhog/MailHog-Server/config/config.go
generated
vendored
Normal file
|
@ -0,0 +1,132 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/ian-kent/envconf"
|
||||||
|
"github.com/mailhog/MailHog-Server/monkey"
|
||||||
|
"github.com/mailhog/data"
|
||||||
|
"github.com/mailhog/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultConfig is the default config
|
||||||
|
func DefaultConfig() *Config {
|
||||||
|
return &Config{
|
||||||
|
SMTPBindAddr: "0.0.0.0:1025",
|
||||||
|
APIBindAddr: "0.0.0.0:8025",
|
||||||
|
Hostname: "mailhog.example",
|
||||||
|
MongoURI: "127.0.0.1:27017",
|
||||||
|
MongoDb: "mailhog",
|
||||||
|
MongoColl: "messages",
|
||||||
|
MaildirPath: "",
|
||||||
|
StorageType: "memory",
|
||||||
|
CORSOrigin: "",
|
||||||
|
WebPath: "",
|
||||||
|
MessageChan: make(chan *data.Message),
|
||||||
|
OutgoingSMTP: make(map[string]*OutgoingSMTP),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Config is the config, kind of
|
||||||
|
type Config struct {
|
||||||
|
SMTPBindAddr string
|
||||||
|
APIBindAddr string
|
||||||
|
Hostname string
|
||||||
|
MongoURI string
|
||||||
|
MongoDb string
|
||||||
|
MongoColl string
|
||||||
|
StorageType string
|
||||||
|
CORSOrigin string
|
||||||
|
MaildirPath string
|
||||||
|
InviteJim bool
|
||||||
|
Storage storage.Storage
|
||||||
|
MessageChan chan *data.Message
|
||||||
|
Assets func(asset string) ([]byte, error)
|
||||||
|
Monkey monkey.ChaosMonkey
|
||||||
|
OutgoingSMTPFile string
|
||||||
|
OutgoingSMTP map[string]*OutgoingSMTP
|
||||||
|
WebPath string
|
||||||
|
}
|
||||||
|
|
||||||
|
// OutgoingSMTP is an outgoing SMTP server config
|
||||||
|
type OutgoingSMTP struct {
|
||||||
|
Name string
|
||||||
|
Save bool
|
||||||
|
Email string
|
||||||
|
Host string
|
||||||
|
Port string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
Mechanism string
|
||||||
|
}
|
||||||
|
|
||||||
|
var cfg = DefaultConfig()
|
||||||
|
|
||||||
|
// Jim is a monkey
|
||||||
|
var Jim = &monkey.Jim{}
|
||||||
|
|
||||||
|
// Configure configures stuff
|
||||||
|
func Configure() *Config {
|
||||||
|
switch cfg.StorageType {
|
||||||
|
case "memory":
|
||||||
|
log.Println("Using in-memory storage")
|
||||||
|
cfg.Storage = storage.CreateInMemory()
|
||||||
|
case "mongodb":
|
||||||
|
log.Println("Using MongoDB message storage")
|
||||||
|
s := storage.CreateMongoDB(cfg.MongoURI, cfg.MongoDb, cfg.MongoColl)
|
||||||
|
if s == nil {
|
||||||
|
log.Println("MongoDB storage unavailable, reverting to in-memory storage")
|
||||||
|
cfg.Storage = storage.CreateInMemory()
|
||||||
|
} else {
|
||||||
|
log.Println("Connected to MongoDB")
|
||||||
|
cfg.Storage = s
|
||||||
|
}
|
||||||
|
case "maildir":
|
||||||
|
log.Println("Using maildir message storage")
|
||||||
|
s := storage.CreateMaildir(cfg.MaildirPath)
|
||||||
|
cfg.Storage = s
|
||||||
|
default:
|
||||||
|
log.Fatalf("Invalid storage type %s", cfg.StorageType)
|
||||||
|
}
|
||||||
|
|
||||||
|
Jim.Configure(func(message string, args ...interface{}) {
|
||||||
|
log.Printf(message, args...)
|
||||||
|
})
|
||||||
|
if cfg.InviteJim {
|
||||||
|
cfg.Monkey = Jim
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cfg.OutgoingSMTPFile) > 0 {
|
||||||
|
b, err := ioutil.ReadFile(cfg.OutgoingSMTPFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
var o map[string]*OutgoingSMTP
|
||||||
|
err = json.Unmarshal(b, &o)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
cfg.OutgoingSMTP = o
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterFlags registers flags
|
||||||
|
func RegisterFlags() {
|
||||||
|
flag.StringVar(&cfg.SMTPBindAddr, "smtp-bind-addr", envconf.FromEnvP("MH_SMTP_BIND_ADDR", "0.0.0.0:1025").(string), "SMTP bind interface and port, e.g. 0.0.0.0:1025 or just :1025")
|
||||||
|
flag.StringVar(&cfg.APIBindAddr, "api-bind-addr", envconf.FromEnvP("MH_API_BIND_ADDR", "0.0.0.0:8025").(string), "HTTP bind interface and port for API, e.g. 0.0.0.0:8025 or just :8025")
|
||||||
|
flag.StringVar(&cfg.Hostname, "hostname", envconf.FromEnvP("MH_HOSTNAME", "mailhog.example").(string), "Hostname for EHLO/HELO response, e.g. mailhog.example")
|
||||||
|
flag.StringVar(&cfg.StorageType, "storage", envconf.FromEnvP("MH_STORAGE", "memory").(string), "Message storage: 'memory' (default), 'mongodb' or 'maildir'")
|
||||||
|
flag.StringVar(&cfg.MongoURI, "mongo-uri", envconf.FromEnvP("MH_MONGO_URI", "127.0.0.1:27017").(string), "MongoDB URI, e.g. 127.0.0.1:27017")
|
||||||
|
flag.StringVar(&cfg.MongoDb, "mongo-db", envconf.FromEnvP("MH_MONGO_DB", "mailhog").(string), "MongoDB database, e.g. mailhog")
|
||||||
|
flag.StringVar(&cfg.MongoColl, "mongo-coll", envconf.FromEnvP("MH_MONGO_COLLECTION", "messages").(string), "MongoDB collection, e.g. messages")
|
||||||
|
flag.StringVar(&cfg.CORSOrigin, "cors-origin", envconf.FromEnvP("MH_CORS_ORIGIN", "").(string), "CORS Access-Control-Allow-Origin header for API endpoints")
|
||||||
|
flag.StringVar(&cfg.MaildirPath, "maildir-path", envconf.FromEnvP("MH_MAILDIR_PATH", "").(string), "Maildir path (if storage type is 'maildir')")
|
||||||
|
flag.BoolVar(&cfg.InviteJim, "invite-jim", envconf.FromEnvP("MH_INVITE_JIM", false).(bool), "Decide whether to invite Jim (beware, he causes trouble)")
|
||||||
|
flag.StringVar(&cfg.OutgoingSMTPFile, "outgoing-smtp", envconf.FromEnvP("MH_OUTGOING_SMTP", "").(string), "JSON file containing outgoing SMTP servers")
|
||||||
|
Jim.RegisterFlags()
|
||||||
|
}
|
111
vendor/github.com/mailhog/MailHog-Server/monkey/jim.go
generated
vendored
Normal file
111
vendor/github.com/mailhog/MailHog-Server/monkey/jim.go
generated
vendored
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
package monkey
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ian-kent/linkio"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Jim is a chaos monkey
|
||||||
|
type Jim struct {
|
||||||
|
DisconnectChance float64
|
||||||
|
AcceptChance float64
|
||||||
|
LinkSpeedAffect float64
|
||||||
|
LinkSpeedMin float64
|
||||||
|
LinkSpeedMax float64
|
||||||
|
RejectSenderChance float64
|
||||||
|
RejectRecipientChance float64
|
||||||
|
RejectAuthChance float64
|
||||||
|
logf func(message string, args ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterFlags implements ChaosMonkey.RegisterFlags
|
||||||
|
func (j *Jim) RegisterFlags() {
|
||||||
|
flag.Float64Var(&j.DisconnectChance, "jim-disconnect", 0.005, "Chance of disconnect")
|
||||||
|
flag.Float64Var(&j.AcceptChance, "jim-accept", 0.99, "Chance of accept")
|
||||||
|
flag.Float64Var(&j.LinkSpeedAffect, "jim-linkspeed-affect", 0.1, "Chance of affecting link speed")
|
||||||
|
flag.Float64Var(&j.LinkSpeedMin, "jim-linkspeed-min", 1024, "Minimum link speed (in bytes per second)")
|
||||||
|
flag.Float64Var(&j.LinkSpeedMax, "jim-linkspeed-max", 10240, "Maximum link speed (in bytes per second)")
|
||||||
|
flag.Float64Var(&j.RejectSenderChance, "jim-reject-sender", 0.05, "Chance of rejecting a sender (MAIL FROM)")
|
||||||
|
flag.Float64Var(&j.RejectRecipientChance, "jim-reject-recipient", 0.05, "Chance of rejecting a recipient (RCPT TO)")
|
||||||
|
flag.Float64Var(&j.RejectAuthChance, "jim-reject-auth", 0.05, "Chance of rejecting authentication (AUTH)")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure implements ChaosMonkey.Configure
|
||||||
|
func (j *Jim) Configure(logf func(string, ...interface{})) {
|
||||||
|
j.logf = logf
|
||||||
|
rand.Seed(time.Now().Unix())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConfigureFrom lets us configure a new Jim from an old one without
|
||||||
|
// having to expose logf (and any other future private vars)
|
||||||
|
func (j *Jim) ConfigureFrom(j2 *Jim) {
|
||||||
|
j.Configure(j2.logf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accept implements ChaosMonkey.Accept
|
||||||
|
func (j *Jim) Accept(conn net.Conn) bool {
|
||||||
|
if rand.Float64() > j.AcceptChance {
|
||||||
|
j.logf("Jim: Rejecting connection\n")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
j.logf("Jim: Allowing connection\n")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// LinkSpeed implements ChaosMonkey.LinkSpeed
|
||||||
|
func (j *Jim) LinkSpeed() *linkio.Throughput {
|
||||||
|
rand.Seed(time.Now().Unix())
|
||||||
|
if rand.Float64() < j.LinkSpeedAffect {
|
||||||
|
lsDiff := j.LinkSpeedMax - j.LinkSpeedMin
|
||||||
|
lsAffect := j.LinkSpeedMin + (lsDiff * rand.Float64())
|
||||||
|
f := linkio.Throughput(lsAffect) * linkio.BytePerSecond
|
||||||
|
j.logf("Jim: Restricting throughput to %s\n", f)
|
||||||
|
return &f
|
||||||
|
}
|
||||||
|
j.logf("Jim: Allowing unrestricted throughput")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidRCPT implements ChaosMonkey.ValidRCPT
|
||||||
|
func (j *Jim) ValidRCPT(rcpt string) bool {
|
||||||
|
if rand.Float64() < j.RejectRecipientChance {
|
||||||
|
j.logf("Jim: Rejecting recipient %s\n", rcpt)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
j.logf("Jim: Allowing recipient%s\n", rcpt)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidMAIL implements ChaosMonkey.ValidMAIL
|
||||||
|
func (j *Jim) ValidMAIL(mail string) bool {
|
||||||
|
if rand.Float64() < j.RejectSenderChance {
|
||||||
|
j.logf("Jim: Rejecting sender %s\n", mail)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
j.logf("Jim: Allowing sender %s\n", mail)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidAUTH implements ChaosMonkey.ValidAUTH
|
||||||
|
func (j *Jim) ValidAUTH(mechanism string, args ...string) bool {
|
||||||
|
if rand.Float64() < j.RejectAuthChance {
|
||||||
|
j.logf("Jim: Rejecting authentication %s: %s\n", mechanism, args)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
j.logf("Jim: Allowing authentication %s: %s\n", mechanism, args)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disconnect implements ChaosMonkey.Disconnect
|
||||||
|
func (j *Jim) Disconnect() bool {
|
||||||
|
if rand.Float64() < j.DisconnectChance {
|
||||||
|
j.logf("Jim: Being nasty, kicking them off\n")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
j.logf("Jim: Being nice, letting them stay\n")
|
||||||
|
return false
|
||||||
|
}
|
28
vendor/github.com/mailhog/MailHog-Server/monkey/monkey.go
generated
vendored
Normal file
28
vendor/github.com/mailhog/MailHog-Server/monkey/monkey.go
generated
vendored
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
package monkey
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/ian-kent/linkio"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ChaosMonkey should be implemented by chaos monkeys!
|
||||||
|
type ChaosMonkey interface {
|
||||||
|
RegisterFlags()
|
||||||
|
Configure(func(string, ...interface{}))
|
||||||
|
|
||||||
|
// Accept is called for each incoming connection. Returning false closes the connection.
|
||||||
|
Accept(conn net.Conn) bool
|
||||||
|
// LinkSpeed sets the maximum connection throughput (in one direction)
|
||||||
|
LinkSpeed() *linkio.Throughput
|
||||||
|
|
||||||
|
// ValidRCPT is called for the RCPT command. Returning false signals an invalid recipient.
|
||||||
|
ValidRCPT(rcpt string) bool
|
||||||
|
// ValidMAIL is called for the MAIL command. Returning false signals an invalid sender.
|
||||||
|
ValidMAIL(mail string) bool
|
||||||
|
// ValidAUTH is called after authentication. Returning false signals invalid authentication.
|
||||||
|
ValidAUTH(mechanism string, args ...string) bool
|
||||||
|
|
||||||
|
// Disconnect is called after every read. Returning true will close the connection.
|
||||||
|
Disconnect() bool
|
||||||
|
}
|
163
vendor/github.com/mailhog/MailHog-Server/smtp/session.go
generated
vendored
Normal file
163
vendor/github.com/mailhog/MailHog-Server/smtp/session.go
generated
vendored
Normal file
|
@ -0,0 +1,163 @@
|
||||||
|
package smtp
|
||||||
|
|
||||||
|
// http://www.rfc-editor.org/rfc/rfc5321.txt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/ian-kent/linkio"
|
||||||
|
"github.com/mailhog/MailHog-Server/monkey"
|
||||||
|
"github.com/mailhog/data"
|
||||||
|
"github.com/mailhog/smtp"
|
||||||
|
"github.com/mailhog/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Session represents a SMTP session using net.TCPConn
|
||||||
|
type Session struct {
|
||||||
|
conn io.ReadWriteCloser
|
||||||
|
proto *smtp.Protocol
|
||||||
|
storage storage.Storage
|
||||||
|
messageChan chan *data.Message
|
||||||
|
remoteAddress string
|
||||||
|
isTLS bool
|
||||||
|
line string
|
||||||
|
link *linkio.Link
|
||||||
|
|
||||||
|
reader io.Reader
|
||||||
|
writer io.Writer
|
||||||
|
monkey monkey.ChaosMonkey
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accept starts a new SMTP session using io.ReadWriteCloser
|
||||||
|
func Accept(remoteAddress string, conn io.ReadWriteCloser, storage storage.Storage, messageChan chan *data.Message, hostname string, monkey monkey.ChaosMonkey) {
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
proto := smtp.NewProtocol()
|
||||||
|
proto.Hostname = hostname
|
||||||
|
var link *linkio.Link
|
||||||
|
reader := io.Reader(conn)
|
||||||
|
writer := io.Writer(conn)
|
||||||
|
if monkey != nil {
|
||||||
|
linkSpeed := monkey.LinkSpeed()
|
||||||
|
if linkSpeed != nil {
|
||||||
|
link = linkio.NewLink(*linkSpeed * linkio.BytePerSecond)
|
||||||
|
reader = link.NewLinkReader(io.Reader(conn))
|
||||||
|
writer = link.NewLinkWriter(io.Writer(conn))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
session := &Session{conn, proto, storage, messageChan, remoteAddress, false, "", link, reader, writer, monkey}
|
||||||
|
proto.LogHandler = session.logf
|
||||||
|
proto.MessageReceivedHandler = session.acceptMessage
|
||||||
|
proto.ValidateSenderHandler = session.validateSender
|
||||||
|
proto.ValidateRecipientHandler = session.validateRecipient
|
||||||
|
proto.ValidateAuthenticationHandler = session.validateAuthentication
|
||||||
|
proto.GetAuthenticationMechanismsHandler = func() []string { return []string{"PLAIN"} }
|
||||||
|
|
||||||
|
session.logf("Starting session")
|
||||||
|
session.Write(proto.Start())
|
||||||
|
for session.Read() == true {
|
||||||
|
if monkey != nil && monkey.Disconnect != nil && monkey.Disconnect() {
|
||||||
|
session.conn.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
session.logf("Session ended")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Session) validateAuthentication(mechanism string, args ...string) (errorReply *smtp.Reply, ok bool) {
|
||||||
|
if c.monkey != nil {
|
||||||
|
ok := c.monkey.ValidAUTH(mechanism, args...)
|
||||||
|
if !ok {
|
||||||
|
// FIXME better error?
|
||||||
|
return smtp.ReplyUnrecognisedCommand(), false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Session) validateRecipient(to string) bool {
|
||||||
|
if c.monkey != nil {
|
||||||
|
ok := c.monkey.ValidRCPT(to)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Session) validateSender(from string) bool {
|
||||||
|
if c.monkey != nil {
|
||||||
|
ok := c.monkey.ValidMAIL(from)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Session) acceptMessage(msg *data.SMTPMessage) (id string, err error) {
|
||||||
|
m := msg.Parse(c.proto.Hostname)
|
||||||
|
c.logf("Storing message %s", m.ID)
|
||||||
|
id, err = c.storage.Store(m)
|
||||||
|
c.messageChan <- m
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Session) logf(message string, args ...interface{}) {
|
||||||
|
message = strings.Join([]string{"[SMTP %s]", message}, " ")
|
||||||
|
args = append([]interface{}{c.remoteAddress}, args...)
|
||||||
|
log.Printf(message, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read reads from the underlying net.TCPConn
|
||||||
|
func (c *Session) Read() bool {
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
n, err := c.reader.Read(buf)
|
||||||
|
|
||||||
|
if n == 0 {
|
||||||
|
c.logf("Connection closed by remote host\n")
|
||||||
|
io.Closer(c.conn).Close() // not sure this is necessary?
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.logf("Error reading from socket: %s\n", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
text := string(buf[0:n])
|
||||||
|
logText := strings.Replace(text, "\n", "\\n", -1)
|
||||||
|
logText = strings.Replace(logText, "\r", "\\r", -1)
|
||||||
|
c.logf("Received %d bytes: '%s'\n", n, logText)
|
||||||
|
|
||||||
|
c.line += text
|
||||||
|
|
||||||
|
for strings.Contains(c.line, "\r\n") {
|
||||||
|
line, reply := c.proto.Parse(c.line)
|
||||||
|
c.line = line
|
||||||
|
|
||||||
|
if reply != nil {
|
||||||
|
c.Write(reply)
|
||||||
|
if reply.Status == 221 {
|
||||||
|
io.Closer(c.conn).Close()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write writes a reply to the underlying net.TCPConn
|
||||||
|
func (c *Session) Write(reply *smtp.Reply) {
|
||||||
|
lines := reply.Lines()
|
||||||
|
for _, l := range lines {
|
||||||
|
logText := strings.Replace(l, "\n", "\\n", -1)
|
||||||
|
logText = strings.Replace(logText, "\r", "\\r", -1)
|
||||||
|
c.logf("Sent %d bytes: '%s'", len(l), logText)
|
||||||
|
c.writer.Write([]byte(l))
|
||||||
|
}
|
||||||
|
}
|
43
vendor/github.com/mailhog/MailHog-Server/smtp/smtp.go
generated
vendored
Normal file
43
vendor/github.com/mailhog/MailHog-Server/smtp/smtp.go
generated
vendored
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package smtp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/mailhog/MailHog-Server/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Listen(cfg *config.Config, exitCh chan int) *net.TCPListener {
|
||||||
|
log.Printf("[SMTP] Binding to address: %s\n", cfg.SMTPBindAddr)
|
||||||
|
ln, err := net.Listen("tcp", cfg.SMTPBindAddr)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[SMTP] Error listening on socket: %s\n", err)
|
||||||
|
}
|
||||||
|
defer ln.Close()
|
||||||
|
|
||||||
|
for {
|
||||||
|
conn, err := ln.Accept()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[SMTP] Error accepting connection: %s\n", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Monkey != nil {
|
||||||
|
ok := cfg.Monkey.Accept(conn)
|
||||||
|
if !ok {
|
||||||
|
conn.Close()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
go Accept(
|
||||||
|
conn.(*net.TCPConn).RemoteAddr().String(),
|
||||||
|
io.ReadWriteCloser(conn),
|
||||||
|
cfg.Storage,
|
||||||
|
cfg.MessageChan,
|
||||||
|
cfg.Hostname,
|
||||||
|
cfg.Monkey,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
73
vendor/github.com/mailhog/MailHog-Server/websockets/connection.go
generated
vendored
Normal file
73
vendor/github.com/mailhog/MailHog-Server/websockets/connection.go
generated
vendored
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
package websockets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Time allowed to write a message to the peer.
|
||||||
|
writeWait = 10 * time.Second
|
||||||
|
// Time allowed to read the next pong message from the peer.
|
||||||
|
pongWait = 60 * time.Second
|
||||||
|
// Send pings to peer with this period. Must be less than pongWait.
|
||||||
|
pingPeriod = (pongWait * 9) / 10
|
||||||
|
// Maximum message size allowed from peer. Set to minimum allowed value as we don't expect the client to send non-control messages.
|
||||||
|
maxMessageSize = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
type connection struct {
|
||||||
|
hub *Hub
|
||||||
|
ws *websocket.Conn
|
||||||
|
send chan interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *connection) readLoop() {
|
||||||
|
defer func() {
|
||||||
|
c.hub.unregisterChan <- c
|
||||||
|
c.ws.Close()
|
||||||
|
}()
|
||||||
|
c.ws.SetReadLimit(maxMessageSize)
|
||||||
|
c.ws.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
|
c.ws.SetPongHandler(func(string) error { c.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
||||||
|
for {
|
||||||
|
if _, _, err := c.ws.NextReader(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *connection) writeLoop() {
|
||||||
|
ticker := time.NewTicker(pingPeriod)
|
||||||
|
defer func() {
|
||||||
|
ticker.Stop()
|
||||||
|
c.ws.Close()
|
||||||
|
}()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case message, ok := <-c.send:
|
||||||
|
if !ok {
|
||||||
|
c.writeControl(websocket.CloseMessage)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := c.writeJSON(message); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case <-ticker.C:
|
||||||
|
if err := c.writeControl(websocket.PingMessage); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *connection) writeJSON(message interface{}) error {
|
||||||
|
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
|
return c.ws.WriteJSON(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *connection) writeControl(messageType int) error {
|
||||||
|
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
|
return c.ws.WriteMessage(messageType, []byte{})
|
||||||
|
}
|
76
vendor/github.com/mailhog/MailHog-Server/websockets/hub.go
generated
vendored
Normal file
76
vendor/github.com/mailhog/MailHog-Server/websockets/hub.go
generated
vendored
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
package websockets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/ian-kent/go-log/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Hub struct {
|
||||||
|
upgrader websocket.Upgrader
|
||||||
|
connections map[*connection]bool
|
||||||
|
messages chan interface{}
|
||||||
|
registerChan chan *connection
|
||||||
|
unregisterChan chan *connection
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHub() *Hub {
|
||||||
|
hub := &Hub{
|
||||||
|
upgrader: websocket.Upgrader{
|
||||||
|
ReadBufferSize: 256,
|
||||||
|
WriteBufferSize: 4096,
|
||||||
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
},
|
||||||
|
connections: make(map[*connection]bool),
|
||||||
|
messages: make(chan interface{}),
|
||||||
|
registerChan: make(chan *connection),
|
||||||
|
unregisterChan: make(chan *connection),
|
||||||
|
}
|
||||||
|
go hub.run()
|
||||||
|
return hub
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) run() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case c := <-h.registerChan:
|
||||||
|
h.connections[c] = true
|
||||||
|
case c := <-h.unregisterChan:
|
||||||
|
h.unregister(c)
|
||||||
|
case m := <-h.messages:
|
||||||
|
for c := range h.connections {
|
||||||
|
select {
|
||||||
|
case c.send <- m:
|
||||||
|
default:
|
||||||
|
h.unregister(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) unregister(c *connection) {
|
||||||
|
if _, ok := h.connections[c]; ok {
|
||||||
|
close(c.send)
|
||||||
|
delete(h.connections, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) Serve(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ws, err := h.upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c := &connection{hub: h, ws: ws, send: make(chan interface{}, 256)}
|
||||||
|
h.registerChan <- c
|
||||||
|
go c.writeLoop()
|
||||||
|
go c.readLoop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) Broadcast(data interface{}) {
|
||||||
|
h.messages <- data
|
||||||
|
}
|
21
vendor/github.com/mailhog/MailHog-UI/LICENSE.md
generated
vendored
Normal file
21
vendor/github.com/mailhog/MailHog-UI/LICENSE.md
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 - 2016 Ian Kent
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
684
vendor/github.com/mailhog/MailHog-UI/assets/assets.go
generated
vendored
Normal file
684
vendor/github.com/mailhog/MailHog-UI/assets/assets.go
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
32
vendor/github.com/mailhog/MailHog-UI/config/config.go
generated
vendored
Normal file
32
vendor/github.com/mailhog/MailHog-UI/config/config.go
generated
vendored
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
|
||||||
|
"github.com/ian-kent/envconf"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DefaultConfig() *Config {
|
||||||
|
return &Config{
|
||||||
|
APIHost: "",
|
||||||
|
UIBindAddr: "0.0.0.0:8025",
|
||||||
|
WebPath: "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
APIHost string
|
||||||
|
UIBindAddr string
|
||||||
|
WebPath string
|
||||||
|
}
|
||||||
|
|
||||||
|
var cfg = DefaultConfig()
|
||||||
|
|
||||||
|
func Configure() *Config {
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterFlags() {
|
||||||
|
flag.StringVar(&cfg.APIHost, "api-host", envconf.FromEnvP("MH_API_HOST", "").(string), "API URL for MailHog UI to connect to, e.g. http://some.host:1234")
|
||||||
|
flag.StringVar(&cfg.UIBindAddr, "ui-bind-addr", envconf.FromEnvP("MH_UI_BIND_ADDR", "0.0.0.0:8025").(string), "HTTP bind interface and port for UI, e.g. 0.0.0.0:8025 or just :8025")
|
||||||
|
}
|
117
vendor/github.com/mailhog/MailHog-UI/web/web.go
generated
vendored
Normal file
117
vendor/github.com/mailhog/MailHog-UI/web/web.go
generated
vendored
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"mime"
|
||||||
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gorilla/pat"
|
||||||
|
"github.com/mailhog/MailHog-UI/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
var APIHost string
|
||||||
|
var WebPath string
|
||||||
|
|
||||||
|
type Web struct {
|
||||||
|
config *config.Config
|
||||||
|
asset func(string) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateWeb(cfg *config.Config, pat *pat.Router, asset func(string) ([]byte, error)) *Web {
|
||||||
|
web := &Web{
|
||||||
|
config: cfg,
|
||||||
|
asset: asset,
|
||||||
|
}
|
||||||
|
|
||||||
|
WebPath = cfg.WebPath
|
||||||
|
|
||||||
|
log.Printf("Serving under http://%s%s/", cfg.UIBindAddr, WebPath)
|
||||||
|
|
||||||
|
pat.Path(WebPath + "/images/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/images/{{file}}"))
|
||||||
|
pat.Path(WebPath + "/css/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/css/{{file}}"))
|
||||||
|
pat.Path(WebPath + "/js/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/js/{{file}}"))
|
||||||
|
pat.Path(WebPath + "/fonts/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/fonts/{{file}}"))
|
||||||
|
pat.StrictSlash(true).Path(WebPath + "/").Methods("GET").HandlerFunc(web.Index())
|
||||||
|
|
||||||
|
return web
|
||||||
|
}
|
||||||
|
|
||||||
|
func (web Web) Static(pattern string) func(http.ResponseWriter, *http.Request) {
|
||||||
|
return func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
fp := strings.TrimSuffix(pattern, "{{file}}") + req.URL.Query().Get(":file")
|
||||||
|
if b, err := web.asset(fp); err == nil {
|
||||||
|
ext := filepath.Ext(fp)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
||||||
|
w.WriteHeader(200)
|
||||||
|
w.Write(b)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("[UI] File not found: %s", fp)
|
||||||
|
w.WriteHeader(404)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (web Web) Index() func(http.ResponseWriter, *http.Request) {
|
||||||
|
tmpl := template.New("index.html")
|
||||||
|
tmpl.Delims("[:", ":]")
|
||||||
|
|
||||||
|
asset, err := web.asset("assets/templates/index.html")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[UI] Error loading index.html: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err = tmpl.Parse(string(asset))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[UI] Error parsing index.html: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
layout := template.New("layout.html")
|
||||||
|
layout.Delims("[:", ":]")
|
||||||
|
|
||||||
|
asset, err = web.asset("assets/templates/layout.html")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[UI] Error loading layout.html: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
layout, err = layout.Parse(string(asset))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[UI] Error parsing layout.html: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"config": web.config,
|
||||||
|
"Page": "Browse",
|
||||||
|
"APIHost": APIHost,
|
||||||
|
}
|
||||||
|
|
||||||
|
b := new(bytes.Buffer)
|
||||||
|
err := tmpl.Execute(b, data)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[UI] Error executing template: %s", err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data["Content"] = template.HTML(b.String())
|
||||||
|
|
||||||
|
b = new(bytes.Buffer)
|
||||||
|
err = layout.Execute(b, data)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[UI] Error executing template: %s", err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
w.WriteHeader(200)
|
||||||
|
w.Write(b.Bytes())
|
||||||
|
}
|
||||||
|
}
|
21
vendor/github.com/mailhog/data/LICENSE.md
generated
vendored
Normal file
21
vendor/github.com/mailhog/data/LICENSE.md
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Ian Kent
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
10
vendor/github.com/mailhog/data/README.md
generated
vendored
Normal file
10
vendor/github.com/mailhog/data/README.md
generated
vendored
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
MailHog data library [![GoDoc](https://godoc.org/github.com/mailhog/data?status.svg)](https://godoc.org/github.com/mailhog/data) [![Build Status](https://travis-ci.org/mailhog/data.svg?branch=master)](https://travis-ci.org/mailhog/data)
|
||||||
|
=========
|
||||||
|
|
||||||
|
`github.com/mailhog/data` implements a data library
|
||||||
|
|
||||||
|
### Licence
|
||||||
|
|
||||||
|
Copyright © 2014-2015, Ian Kent (http://iankent.uk)
|
||||||
|
|
||||||
|
Released under MIT license, see [LICENSE](LICENSE.md) for details.
|
323
vendor/github.com/mailhog/data/message.go
generated
vendored
Normal file
323
vendor/github.com/mailhog/data/message.go
generated
vendored
Normal file
|
@ -0,0 +1,323 @@
|
||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"mime"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LogHandler is called for each log message. If nil, log messages will
|
||||||
|
// be output using log.Printf instead.
|
||||||
|
var LogHandler func(message string, args ...interface{})
|
||||||
|
|
||||||
|
func logf(message string, args ...interface{}) {
|
||||||
|
if LogHandler != nil {
|
||||||
|
LogHandler(message, args...)
|
||||||
|
} else {
|
||||||
|
log.Printf(message, args...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MessageID represents the ID of an SMTP message including the hostname part
|
||||||
|
type MessageID string
|
||||||
|
|
||||||
|
// NewMessageID generates a new message ID
|
||||||
|
func NewMessageID(hostname string) (MessageID, error) {
|
||||||
|
size := 32
|
||||||
|
|
||||||
|
rb := make([]byte, size)
|
||||||
|
_, err := rand.Read(rb)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return MessageID(""), err
|
||||||
|
}
|
||||||
|
|
||||||
|
rs := base64.URLEncoding.EncodeToString(rb)
|
||||||
|
|
||||||
|
return MessageID(rs + "@" + hostname), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Messages represents an array of Messages
|
||||||
|
// - TODO is this even required?
|
||||||
|
type Messages []Message
|
||||||
|
|
||||||
|
// Message represents a parsed SMTP message
|
||||||
|
type Message struct {
|
||||||
|
ID MessageID
|
||||||
|
From *Path
|
||||||
|
To []*Path
|
||||||
|
Content *Content
|
||||||
|
Created time.Time
|
||||||
|
MIME *MIMEBody // FIXME refactor to use Content.MIME
|
||||||
|
Raw *SMTPMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path represents an SMTP forward-path or return-path
|
||||||
|
type Path struct {
|
||||||
|
Relays []string
|
||||||
|
Mailbox string
|
||||||
|
Domain string
|
||||||
|
Params string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Content represents the body content of an SMTP message
|
||||||
|
type Content struct {
|
||||||
|
Headers map[string][]string
|
||||||
|
Body string
|
||||||
|
Size int
|
||||||
|
MIME *MIMEBody
|
||||||
|
}
|
||||||
|
|
||||||
|
// SMTPMessage represents a raw SMTP message
|
||||||
|
type SMTPMessage struct {
|
||||||
|
From string
|
||||||
|
To []string
|
||||||
|
Data string
|
||||||
|
Helo string
|
||||||
|
}
|
||||||
|
|
||||||
|
// MIMEBody represents a collection of MIME parts
|
||||||
|
type MIMEBody struct {
|
||||||
|
Parts []*Content
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse converts a raw SMTP message to a parsed MIME message
|
||||||
|
func (m *SMTPMessage) Parse(hostname string) *Message {
|
||||||
|
var arr []*Path
|
||||||
|
for _, path := range m.To {
|
||||||
|
arr = append(arr, PathFromString(path))
|
||||||
|
}
|
||||||
|
|
||||||
|
id, _ := NewMessageID(hostname)
|
||||||
|
msg := &Message{
|
||||||
|
ID: id,
|
||||||
|
From: PathFromString(m.From),
|
||||||
|
To: arr,
|
||||||
|
Content: ContentFromString(m.Data),
|
||||||
|
Created: time.Now(),
|
||||||
|
Raw: m,
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.Content.IsMIME() {
|
||||||
|
logf("Parsing MIME body")
|
||||||
|
msg.MIME = msg.Content.ParseMIMEBody()
|
||||||
|
}
|
||||||
|
|
||||||
|
// find headers
|
||||||
|
var hasMessageID bool
|
||||||
|
var receivedHeaderName string
|
||||||
|
var returnPathHeaderName string
|
||||||
|
|
||||||
|
for k := range msg.Content.Headers {
|
||||||
|
if strings.ToLower(k) == "message-id" {
|
||||||
|
hasMessageID = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.ToLower(k) == "received" {
|
||||||
|
receivedHeaderName = k
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.ToLower(k) == "return-path" {
|
||||||
|
returnPathHeaderName = k
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hasMessageID {
|
||||||
|
msg.Content.Headers["Message-ID"] = []string{string(id)}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(receivedHeaderName) > 0 {
|
||||||
|
msg.Content.Headers[receivedHeaderName] = append(msg.Content.Headers[receivedHeaderName], "from "+m.Helo+" by "+hostname+" (MailHog)\r\n id "+string(id)+"; "+time.Now().Format(time.RFC1123Z))
|
||||||
|
} else {
|
||||||
|
msg.Content.Headers["Received"] = []string{"from " + m.Helo + " by " + hostname + " (MailHog)\r\n id " + string(id) + "; " + time.Now().Format(time.RFC1123Z)}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(returnPathHeaderName) > 0 {
|
||||||
|
msg.Content.Headers[returnPathHeaderName] = append(msg.Content.Headers[returnPathHeaderName], "<"+m.From+">")
|
||||||
|
} else {
|
||||||
|
msg.Content.Headers["Return-Path"] = []string{"<" + m.From + ">"}
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bytes returns an io.Reader containing the raw message data
|
||||||
|
func (m *SMTPMessage) Bytes() io.Reader {
|
||||||
|
var b = new(bytes.Buffer)
|
||||||
|
|
||||||
|
b.WriteString("HELO:<" + m.Helo + ">\r\n")
|
||||||
|
b.WriteString("FROM:<" + m.From + ">\r\n")
|
||||||
|
for _, t := range m.To {
|
||||||
|
b.WriteString("TO:<" + t + ">\r\n")
|
||||||
|
}
|
||||||
|
b.WriteString("\r\n")
|
||||||
|
b.WriteString(m.Data)
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromBytes returns a SMTPMessage from raw message bytes (as output by SMTPMessage.Bytes())
|
||||||
|
func FromBytes(b []byte) *SMTPMessage {
|
||||||
|
msg := &SMTPMessage{}
|
||||||
|
for _, l := range strings.Split(string(b), "\n") {
|
||||||
|
if strings.HasPrefix(l, "HELO:<") {
|
||||||
|
l = strings.TrimPrefix(l, "HELO:<")
|
||||||
|
l = strings.TrimSuffix(l, ">\r")
|
||||||
|
msg.Helo = l
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(l, "FROM:<") {
|
||||||
|
l = strings.TrimPrefix(l, "FROM:<")
|
||||||
|
l = strings.TrimSuffix(l, ">\r")
|
||||||
|
msg.From = l
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(l, "TO:<") {
|
||||||
|
l = strings.TrimPrefix(l, "TO:<")
|
||||||
|
l = strings.TrimSuffix(l, ">\r")
|
||||||
|
msg.To = append(msg.To, l)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
msg.Data += l + "\n"
|
||||||
|
}
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bytes returns an io.Reader containing the raw message data
|
||||||
|
func (m *Message) Bytes() io.Reader {
|
||||||
|
var b = new(bytes.Buffer)
|
||||||
|
|
||||||
|
for k, vs := range m.Content.Headers {
|
||||||
|
for _, v := range vs {
|
||||||
|
b.WriteString(k + ": " + v + "\r\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
b.WriteString("\r\n")
|
||||||
|
b.WriteString(m.Content.Body)
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsMIME detects a valid MIME header
|
||||||
|
func (content *Content) IsMIME() bool {
|
||||||
|
header, ok := content.Headers["Content-Type"]
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return strings.HasPrefix(header[0], "multipart/")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseMIMEBody parses SMTP message content into multiple MIME parts
|
||||||
|
func (content *Content) ParseMIMEBody() *MIMEBody {
|
||||||
|
var parts []*Content
|
||||||
|
|
||||||
|
if hdr, ok := content.Headers["Content-Type"]; ok {
|
||||||
|
if len(hdr) > 0 {
|
||||||
|
boundary := extractBoundary(hdr[0])
|
||||||
|
var p []string
|
||||||
|
if len(boundary) > 0 {
|
||||||
|
p = strings.Split(content.Body, "--"+boundary)
|
||||||
|
logf("Got boundary: %s", boundary)
|
||||||
|
} else {
|
||||||
|
logf("Boundary not found: %s", hdr[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range p {
|
||||||
|
if len(s) > 0 {
|
||||||
|
part := ContentFromString(strings.Trim(s, "\r\n"))
|
||||||
|
if part.IsMIME() {
|
||||||
|
logf("Parsing inner MIME body")
|
||||||
|
part.MIME = part.ParseMIMEBody()
|
||||||
|
}
|
||||||
|
parts = append(parts, part)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &MIMEBody{
|
||||||
|
Parts: parts,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PathFromString parses a forward-path or reverse-path into its parts
|
||||||
|
func PathFromString(path string) *Path {
|
||||||
|
var relays []string
|
||||||
|
email := path
|
||||||
|
if strings.Contains(path, ":") {
|
||||||
|
x := strings.SplitN(path, ":", 2)
|
||||||
|
r, e := x[0], x[1]
|
||||||
|
email = e
|
||||||
|
relays = strings.Split(r, ",")
|
||||||
|
}
|
||||||
|
mailbox, domain := "", ""
|
||||||
|
if strings.Contains(email, "@") {
|
||||||
|
x := strings.SplitN(email, "@", 2)
|
||||||
|
mailbox, domain = x[0], x[1]
|
||||||
|
} else {
|
||||||
|
mailbox = email
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Path{
|
||||||
|
Relays: relays,
|
||||||
|
Mailbox: mailbox,
|
||||||
|
Domain: domain,
|
||||||
|
Params: "", // FIXME?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContentFromString parses SMTP content into separate headers and body
|
||||||
|
func ContentFromString(data string) *Content {
|
||||||
|
logf("Parsing Content from string: '%s'", data)
|
||||||
|
x := strings.SplitN(data, "\r\n\r\n", 2)
|
||||||
|
h := make(map[string][]string, 0)
|
||||||
|
|
||||||
|
// FIXME this fails if the message content has no headers - specifically,
|
||||||
|
// if it doesn't contain \r\n\r\n
|
||||||
|
|
||||||
|
if len(x) == 2 {
|
||||||
|
headers, body := x[0], x[1]
|
||||||
|
hdrs := strings.Split(headers, "\r\n")
|
||||||
|
var lastHdr = ""
|
||||||
|
for _, hdr := range hdrs {
|
||||||
|
if lastHdr != "" && (strings.HasPrefix(hdr, " ") || strings.HasPrefix(hdr, "\t")) {
|
||||||
|
h[lastHdr][len(h[lastHdr])-1] = h[lastHdr][len(h[lastHdr])-1] + hdr
|
||||||
|
} else if strings.Contains(hdr, ": ") {
|
||||||
|
y := strings.SplitN(hdr, ": ", 2)
|
||||||
|
key, value := y[0], y[1]
|
||||||
|
// TODO multiple header fields
|
||||||
|
h[key] = []string{value}
|
||||||
|
lastHdr = key
|
||||||
|
} else if len(hdr) > 0 {
|
||||||
|
logf("Found invalid header: '%s'", hdr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &Content{
|
||||||
|
Size: len(data),
|
||||||
|
Headers: h,
|
||||||
|
Body: body,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &Content{
|
||||||
|
Size: len(data),
|
||||||
|
Headers: h,
|
||||||
|
Body: x[0],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// extractBoundary extract boundary string in contentType.
|
||||||
|
// It returns empty string if no valid boundary found
|
||||||
|
func extractBoundary(contentType string) string {
|
||||||
|
_, params, err := mime.ParseMediaType(contentType)
|
||||||
|
if err == nil {
|
||||||
|
return params["boundary"]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
21
vendor/github.com/mailhog/http/LICENSE.md
generated
vendored
Normal file
21
vendor/github.com/mailhog/http/LICENSE.md
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Ian Kent
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
10
vendor/github.com/mailhog/http/README.md
generated
vendored
Normal file
10
vendor/github.com/mailhog/http/README.md
generated
vendored
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
MailHog HTTP utilities [![GoDoc](https://godoc.org/github.com/mailhog/http?status.svg)](https://godoc.org/github.com/mailhog/http) [![Build Status](https://travis-ci.org/mailhog/http.svg?branch=master)](https://travis-ci.org/mailhog/http)
|
||||||
|
=========
|
||||||
|
|
||||||
|
`github.com/mailhog/http` provides HTTP utilities used by MailHog-UI and MailHog-Server.
|
||||||
|
|
||||||
|
### Licence
|
||||||
|
|
||||||
|
Copyright © 2014-2015, Ian Kent (http://iankent.uk)
|
||||||
|
|
||||||
|
Released under MIT license, see [LICENSE](LICENSE.md) for details.
|
112
vendor/github.com/mailhog/http/server.go
generated
vendored
Normal file
112
vendor/github.com/mailhog/http/server.go
generated
vendored
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gorilla/pat"
|
||||||
|
"github.com/ian-kent/go-log/log"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Authorised should be given a function to enable HTTP Basic Authentication
|
||||||
|
var Authorised func(string, string) bool
|
||||||
|
var users map[string]string
|
||||||
|
|
||||||
|
// AuthFile sets Authorised to a function which validates against file
|
||||||
|
func AuthFile(file string) {
|
||||||
|
users = make(map[string]string)
|
||||||
|
|
||||||
|
b, err := ioutil.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[HTTP] Error reading auth-file: %s", err)
|
||||||
|
// FIXME - go-log
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer(b)
|
||||||
|
|
||||||
|
for {
|
||||||
|
l, err := buf.ReadString('\n')
|
||||||
|
l = strings.TrimSpace(l)
|
||||||
|
if len(l) > 0 {
|
||||||
|
p := strings.SplitN(l, ":", 2)
|
||||||
|
if len(p) < 2 {
|
||||||
|
log.Fatalf("[HTTP] Error reading auth-file, invalid line: %s", l)
|
||||||
|
// FIXME - go-log
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
users[p[0]] = p[1]
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case err == io.EOF:
|
||||||
|
break
|
||||||
|
case err != nil:
|
||||||
|
log.Fatalf("[HTTP] Error reading auth-file: %s", err)
|
||||||
|
// FIXME - go-log
|
||||||
|
os.Exit(1)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
} else if err != nil {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("[HTTP] Loaded %d users from %s", len(users), file)
|
||||||
|
|
||||||
|
Authorised = func(u, pw string) bool {
|
||||||
|
hpw, ok := users[u]
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
err := bcrypt.CompareHashAndPassword([]byte(hpw), []byte(pw))
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BasicAuthHandler is middleware to check HTTP Basic Authentication
|
||||||
|
// if an authorisation function is defined.
|
||||||
|
func BasicAuthHandler(h http.Handler) http.Handler {
|
||||||
|
f := func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
if Authorised == nil {
|
||||||
|
h.ServeHTTP(w, req)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
u, pw, ok := req.BasicAuth()
|
||||||
|
if !ok || !Authorised(u, pw) {
|
||||||
|
w.Header().Set("WWW-Authenticate", "Basic")
|
||||||
|
w.WriteHeader(401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.ServeHTTP(w, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
return http.HandlerFunc(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listen binds to httpBindAddr
|
||||||
|
func Listen(httpBindAddr string, Asset func(string) ([]byte, error), exitCh chan int, registerCallback func(http.Handler)) {
|
||||||
|
log.Info("[HTTP] Binding to address: %s", httpBindAddr)
|
||||||
|
|
||||||
|
pat := pat.New()
|
||||||
|
registerCallback(pat)
|
||||||
|
|
||||||
|
//compress := handlers.CompressHandler(pat)
|
||||||
|
auth := BasicAuthHandler(pat) //compress)
|
||||||
|
|
||||||
|
err := http.ListenAndServe(httpBindAddr, auth)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[HTTP] Error binding to address %s: %s", httpBindAddr, err)
|
||||||
|
}
|
||||||
|
}
|
21
vendor/github.com/mailhog/mhsendmail/LICENSE.md
generated
vendored
Normal file
21
vendor/github.com/mailhog/mhsendmail/LICENSE.md
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 - 2016 Ian Kent
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
77
vendor/github.com/mailhog/mhsendmail/cmd/cmd.go
generated
vendored
Normal file
77
vendor/github.com/mailhog/mhsendmail/cmd/cmd.go
generated
vendored
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/mail"
|
||||||
|
"net/smtp"
|
||||||
|
"os"
|
||||||
|
"os/user"
|
||||||
|
)
|
||||||
|
|
||||||
|
import flag "github.com/spf13/pflag"
|
||||||
|
|
||||||
|
// Go runs the MailHog sendmail replacement.
|
||||||
|
func Go() {
|
||||||
|
host, err := os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
host = "localhost"
|
||||||
|
}
|
||||||
|
|
||||||
|
username := "nobody"
|
||||||
|
user, err := user.Current()
|
||||||
|
if err == nil && user != nil && len(user.Username) > 0 {
|
||||||
|
username = user.Username
|
||||||
|
}
|
||||||
|
|
||||||
|
fromAddr := username + "@" + host
|
||||||
|
smtpAddr := "localhost:1025"
|
||||||
|
var recip []string
|
||||||
|
|
||||||
|
// defaults from envars if provided
|
||||||
|
if len(os.Getenv("MH_SENDMAIL_SMTP_ADDR")) > 0 {
|
||||||
|
smtpAddr = os.Getenv("MH_SENDMAIL_SMTP_ADDR")
|
||||||
|
}
|
||||||
|
if len(os.Getenv("MH_SENDMAIL_FROM")) > 0 {
|
||||||
|
fromAddr = os.Getenv("MH_SENDMAIL_FROM")
|
||||||
|
}
|
||||||
|
|
||||||
|
// override defaults from cli flags
|
||||||
|
flag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
|
||||||
|
flag.StringVarP(&fromAddr, "from", "f", fromAddr, "SMTP sender")
|
||||||
|
flag.BoolP("long-i", "i", true, "Ignored. This flag exists for sendmail compatibility.")
|
||||||
|
flag.BoolP("long-t", "t", true, "Ignored. This flag exists for sendmail compatibility.")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
// allow recipient to be passed as an argument
|
||||||
|
recip = flag.Args()
|
||||||
|
|
||||||
|
fmt.Fprintln(os.Stderr, smtpAddr, fromAddr)
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(os.Stdin)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "error reading stdin")
|
||||||
|
os.Exit(11)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err := mail.ReadMessage(bytes.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "error parsing message body")
|
||||||
|
os.Exit(11)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(recip) == 0 {
|
||||||
|
// We only need to parse the message to get a recipient if none where
|
||||||
|
// provided on the command line.
|
||||||
|
recip = append(recip, msg.Header.Get("To"))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = smtp.SendMail(smtpAddr, nil, fromAddr, recip, body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "error sending mail")
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
21
vendor/github.com/mailhog/smtp/LICENSE.md
generated
vendored
Normal file
21
vendor/github.com/mailhog/smtp/LICENSE.md
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Ian Kent
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
100
vendor/github.com/mailhog/smtp/README.md
generated
vendored
Normal file
100
vendor/github.com/mailhog/smtp/README.md
generated
vendored
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
MailHog SMTP Protocol [![GoDoc](https://godoc.org/github.com/mailhog/smtp?status.svg)](https://godoc.org/github.com/mailhog/smtp) [![Build Status](https://travis-ci.org/mailhog/smtp.svg?branch=master)](https://travis-ci.org/mailhog/smtp)
|
||||||
|
=========
|
||||||
|
|
||||||
|
`github.com/mailhog/smtp` implements an SMTP server state machine.
|
||||||
|
|
||||||
|
It attempts to encapsulate as much of the SMTP protocol (plus its extensions) as possible
|
||||||
|
without compromising configurability or requiring specific backend implementations.
|
||||||
|
|
||||||
|
* ESMTP server implementing [RFC5321](http://tools.ietf.org/html/rfc5321)
|
||||||
|
* Support for:
|
||||||
|
* AUTH [RFC4954](http://tools.ietf.org/html/rfc4954)
|
||||||
|
* PIPELINING [RFC2920](http://tools.ietf.org/html/rfc2920)
|
||||||
|
* STARTTLS [RFC3207](http://tools.ietf.org/html/rfc3207)
|
||||||
|
|
||||||
|
```go
|
||||||
|
proto := NewProtocol()
|
||||||
|
reply := proto.Start()
|
||||||
|
reply = proto.ProcessCommand("EHLO localhost")
|
||||||
|
// ...
|
||||||
|
```
|
||||||
|
|
||||||
|
See [MailHog-Server](https://github.com/mailhog/MailHog-Server) and [MailHog-MTA](https://github.com/mailhog/MailHog-MTA) for example implementations.
|
||||||
|
|
||||||
|
### Commands and replies
|
||||||
|
|
||||||
|
Interaction with the state machine is via:
|
||||||
|
* the `Parse` function
|
||||||
|
* the `ProcessCommand` and `ProcessData` functions
|
||||||
|
|
||||||
|
You can mix the use of all three functions as necessary.
|
||||||
|
|
||||||
|
#### Parse
|
||||||
|
|
||||||
|
`Parse` should be used on a raw text stream. It looks for an end of line (`\r\n`), and if found, processes a single command. Any unprocessed data is returned.
|
||||||
|
|
||||||
|
If any unprocessed data is returned, `Parse` should be
|
||||||
|
called again to process then next command.
|
||||||
|
|
||||||
|
```go
|
||||||
|
text := "EHLO localhost\r\nMAIL FROM:<test>\r\nDATA\r\nTest\r\n.\r\n"
|
||||||
|
|
||||||
|
var reply *smtp.Reply
|
||||||
|
for {
|
||||||
|
text, reply = proto.Parse(text)
|
||||||
|
if len(text) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### ProcessCommand and ProcessData
|
||||||
|
|
||||||
|
`ProcessCommand` should be used for an already parsed command (i.e., a complete
|
||||||
|
SMTP "line" excluding the line ending).
|
||||||
|
|
||||||
|
`ProcessData` should be used if the protocol is in `DATA` state.
|
||||||
|
|
||||||
|
```go
|
||||||
|
reply = proto.ProcessCommand("EHLO localhost")
|
||||||
|
reply = proto.ProcessCommand("MAIL FROM:<test>")
|
||||||
|
reply = proto.ProcessCommand("DATA")
|
||||||
|
reply = proto.ProcessData("Test\r\n.\r\n")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Hooks
|
||||||
|
|
||||||
|
The state machine provides hooks to manipulate its behaviour.
|
||||||
|
|
||||||
|
See [![GoDoc](https://godoc.org/github.com/mailhog/smtp?status.svg)](https://godoc.org/github.com/mailhog/smtp) for more information.
|
||||||
|
|
||||||
|
| Hook | Description
|
||||||
|
| ---------------------------------- | -----------
|
||||||
|
| LogHandler | Called for every log message
|
||||||
|
| MessageReceivedHandler | Called for each message received
|
||||||
|
| ValidateSenderHandler | Called after MAIL FROM
|
||||||
|
| ValidateRecipientHandler | Called after RCPT TO
|
||||||
|
| ValidateAuthenticationHandler | Called after AUTH
|
||||||
|
| SMTPVerbFilter | Called for every SMTP command processed
|
||||||
|
| TLSHandler | Callback mashup called after STARTTLS
|
||||||
|
| GetAuthenticationMechanismsHandler | Called for each EHLO command
|
||||||
|
|
||||||
|
### Behaviour flags
|
||||||
|
|
||||||
|
The state machine also exports variables to control its behaviour:
|
||||||
|
|
||||||
|
See [![GoDoc](https://godoc.org/github.com/mailhog/smtp?status.svg)](https://godoc.org/github.com/mailhog/smtp) for more information.
|
||||||
|
|
||||||
|
| Variable | Description
|
||||||
|
| ---------------------- | -----------
|
||||||
|
| RejectBrokenRCPTSyntax | Reject non-conforming RCPT syntax
|
||||||
|
| RejectBrokenMAILSyntax | Reject non-conforming MAIL syntax
|
||||||
|
| RequireTLS | Require STARTTLS before other commands
|
||||||
|
| MaximumRecipients | Maximum recipients per message
|
||||||
|
| MaximumLineLength | Maximum length of SMTP line
|
||||||
|
|
||||||
|
### Licence
|
||||||
|
|
||||||
|
Copyright © 2014-2015, Ian Kent (http://iankent.uk)
|
||||||
|
|
||||||
|
Released under MIT license, see [LICENSE](LICENSE.md) for details.
|
504
vendor/github.com/mailhog/smtp/protocol.go
generated
vendored
Normal file
504
vendor/github.com/mailhog/smtp/protocol.go
generated
vendored
Normal file
|
@ -0,0 +1,504 @@
|
||||||
|
package smtp
|
||||||
|
|
||||||
|
// http://www.rfc-editor.org/rfc/rfc5321.txt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/mailhog/data"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Command is a struct representing an SMTP command (verb + arguments)
|
||||||
|
type Command struct {
|
||||||
|
verb string
|
||||||
|
args string
|
||||||
|
orig string
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseCommand returns a Command from the line string
|
||||||
|
func ParseCommand(line string) *Command {
|
||||||
|
words := strings.Split(line, " ")
|
||||||
|
command := strings.ToUpper(words[0])
|
||||||
|
args := strings.Join(words[1:len(words)], " ")
|
||||||
|
|
||||||
|
return &Command{
|
||||||
|
verb: command,
|
||||||
|
args: args,
|
||||||
|
orig: line,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Protocol is a state machine representing an SMTP session
|
||||||
|
type Protocol struct {
|
||||||
|
lastCommand *Command
|
||||||
|
|
||||||
|
TLSPending bool
|
||||||
|
TLSUpgraded bool
|
||||||
|
|
||||||
|
State State
|
||||||
|
Message *data.SMTPMessage
|
||||||
|
|
||||||
|
Hostname string
|
||||||
|
Ident string
|
||||||
|
|
||||||
|
MaximumLineLength int
|
||||||
|
MaximumRecipients int
|
||||||
|
|
||||||
|
// LogHandler is called for each log message. If nil, log messages will
|
||||||
|
// be output using log.Printf instead.
|
||||||
|
LogHandler func(message string, args ...interface{})
|
||||||
|
// MessageReceivedHandler is called for each message accepted by the
|
||||||
|
// SMTP protocol. It must return a MessageID or error. If nil, messages
|
||||||
|
// will be rejected with an error.
|
||||||
|
MessageReceivedHandler func(*data.SMTPMessage) (string, error)
|
||||||
|
// ValidateSenderHandler should return true if the sender is valid,
|
||||||
|
// otherwise false. If nil, all senders will be accepted.
|
||||||
|
ValidateSenderHandler func(from string) bool
|
||||||
|
// ValidateRecipientHandler should return true if the recipient is valid,
|
||||||
|
// otherwise false. If nil, all recipients will be accepted.
|
||||||
|
ValidateRecipientHandler func(to string) bool
|
||||||
|
// ValidateAuthenticationhandler should return true if the authentication
|
||||||
|
// parameters are valid, otherwise false. If nil, all authentication
|
||||||
|
// attempts will be accepted.
|
||||||
|
ValidateAuthenticationHandler func(mechanism string, args ...string) (errorReply *Reply, ok bool)
|
||||||
|
// SMTPVerbFilter is called after each command is parsed, but before
|
||||||
|
// any code is executed. This provides an opportunity to reject unwanted verbs,
|
||||||
|
// e.g. to require AUTH before MAIL
|
||||||
|
SMTPVerbFilter func(verb string, args ...string) (errorReply *Reply)
|
||||||
|
// TLSHandler is called when a STARTTLS command is received.
|
||||||
|
//
|
||||||
|
// It should acknowledge the TLS request and set ok to true.
|
||||||
|
// It should also return a callback which will be invoked after the reply is
|
||||||
|
// sent. E.g., a TCP connection can only perform the upgrade after sending the reply
|
||||||
|
//
|
||||||
|
// Once the upgrade is complete, invoke the done function (e.g., from the returned callback)
|
||||||
|
//
|
||||||
|
// If TLS upgrade isn't possible, return an errorReply and set ok to false.
|
||||||
|
TLSHandler func(done func(ok bool)) (errorReply *Reply, callback func(), ok bool)
|
||||||
|
|
||||||
|
// GetAuthenticationMechanismsHandler should return an array of strings
|
||||||
|
// listing accepted authentication mechanisms
|
||||||
|
GetAuthenticationMechanismsHandler func() []string
|
||||||
|
|
||||||
|
// RejectBrokenRCPTSyntax controls whether the protocol accepts technically
|
||||||
|
// invalid syntax for the RCPT command. Set to true, the RCPT syntax requires
|
||||||
|
// no space between `TO:` and the opening `<`
|
||||||
|
RejectBrokenRCPTSyntax bool
|
||||||
|
// RejectBrokenMAILSyntax controls whether the protocol accepts technically
|
||||||
|
// invalid syntax for the MAIL command. Set to true, the MAIL syntax requires
|
||||||
|
// no space between `FROM:` and the opening `<`
|
||||||
|
RejectBrokenMAILSyntax bool
|
||||||
|
// RequireTLS controls whether TLS is required for a connection before other
|
||||||
|
// commands can be issued, applied at the protocol layer.
|
||||||
|
RequireTLS bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewProtocol returns a new SMTP state machine in INVALID state
|
||||||
|
// handler is called when a message is received and should return a message ID
|
||||||
|
func NewProtocol() *Protocol {
|
||||||
|
p := &Protocol{
|
||||||
|
Hostname: "mailhog.example",
|
||||||
|
Ident: "ESMTP MailHog",
|
||||||
|
State: INVALID,
|
||||||
|
MaximumLineLength: -1,
|
||||||
|
MaximumRecipients: -1,
|
||||||
|
}
|
||||||
|
p.resetState()
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (proto *Protocol) resetState() {
|
||||||
|
proto.Message = &data.SMTPMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (proto *Protocol) logf(message string, args ...interface{}) {
|
||||||
|
message = strings.Join([]string{"[PROTO: %s]", message}, " ")
|
||||||
|
args = append([]interface{}{StateMap[proto.State]}, args...)
|
||||||
|
|
||||||
|
if proto.LogHandler != nil {
|
||||||
|
proto.LogHandler(message, args...)
|
||||||
|
} else {
|
||||||
|
log.Printf(message, args...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start begins an SMTP conversation with a 220 reply, placing the state
|
||||||
|
// machine in ESTABLISH state.
|
||||||
|
func (proto *Protocol) Start() *Reply {
|
||||||
|
proto.logf("Started session, switching to ESTABLISH state")
|
||||||
|
proto.State = ESTABLISH
|
||||||
|
return ReplyIdent(proto.Hostname + " " + proto.Ident)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse parses a line string and returns any remaining line string
|
||||||
|
// and a reply, if a command was found. Parse does nothing until a
|
||||||
|
// new line is found.
|
||||||
|
// - TODO decide whether to move this to a buffer inside Protocol
|
||||||
|
// sort of like it this way, since it gives control back to the caller
|
||||||
|
func (proto *Protocol) Parse(line string) (string, *Reply) {
|
||||||
|
var reply *Reply
|
||||||
|
|
||||||
|
if !strings.Contains(line, "\r\n") {
|
||||||
|
return line, reply
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.SplitN(line, "\r\n", 2)
|
||||||
|
line = parts[1]
|
||||||
|
|
||||||
|
if proto.MaximumLineLength > -1 {
|
||||||
|
if len(parts[0]) > proto.MaximumLineLength {
|
||||||
|
return line, ReplyLineTooLong()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO collapse AUTH states into separate processing
|
||||||
|
if proto.State == DATA {
|
||||||
|
reply = proto.ProcessData(parts[0])
|
||||||
|
} else {
|
||||||
|
reply = proto.ProcessCommand(parts[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
return line, reply
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProcessData handles content received (with newlines stripped) while
|
||||||
|
// in the SMTP DATA state
|
||||||
|
func (proto *Protocol) ProcessData(line string) (reply *Reply) {
|
||||||
|
proto.Message.Data += line + "\r\n"
|
||||||
|
|
||||||
|
if strings.HasSuffix(proto.Message.Data, "\r\n.\r\n") {
|
||||||
|
proto.Message.Data = strings.Replace(proto.Message.Data, "\r\n..", "\r\n.", -1)
|
||||||
|
|
||||||
|
proto.logf("Got EOF, storing message and switching to MAIL state")
|
||||||
|
proto.Message.Data = strings.TrimSuffix(proto.Message.Data, "\r\n.\r\n")
|
||||||
|
proto.State = MAIL
|
||||||
|
|
||||||
|
defer proto.resetState()
|
||||||
|
|
||||||
|
if proto.MessageReceivedHandler == nil {
|
||||||
|
return ReplyStorageFailed("No storage backend")
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := proto.MessageReceivedHandler(proto.Message)
|
||||||
|
if err != nil {
|
||||||
|
proto.logf("Error storing message: %s", err)
|
||||||
|
return ReplyStorageFailed("Unable to store message")
|
||||||
|
}
|
||||||
|
return ReplyOk("Ok: queued as " + id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProcessCommand processes a line of text as a command
|
||||||
|
// It expects the line string to be a properly formed SMTP verb and arguments
|
||||||
|
func (proto *Protocol) ProcessCommand(line string) (reply *Reply) {
|
||||||
|
line = strings.Trim(line, "\r\n")
|
||||||
|
proto.logf("Processing line: %s", line)
|
||||||
|
|
||||||
|
words := strings.Split(line, " ")
|
||||||
|
command := strings.ToUpper(words[0])
|
||||||
|
args := strings.Join(words[1:len(words)], " ")
|
||||||
|
proto.logf("In state %d, got command '%s', args '%s'", proto.State, command, args)
|
||||||
|
|
||||||
|
cmd := ParseCommand(strings.TrimSuffix(line, "\r\n"))
|
||||||
|
return proto.Command(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command applies an SMTP verb and arguments to the state machine
|
||||||
|
func (proto *Protocol) Command(command *Command) (reply *Reply) {
|
||||||
|
defer func() {
|
||||||
|
proto.lastCommand = command
|
||||||
|
}()
|
||||||
|
if proto.SMTPVerbFilter != nil {
|
||||||
|
proto.logf("sending to SMTP verb filter")
|
||||||
|
r := proto.SMTPVerbFilter(command.verb)
|
||||||
|
if r != nil {
|
||||||
|
proto.logf("response returned by SMTP verb filter")
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case proto.TLSPending && !proto.TLSUpgraded:
|
||||||
|
proto.logf("Got command before TLS upgrade complete")
|
||||||
|
// FIXME what to do?
|
||||||
|
return ReplyBye()
|
||||||
|
case "RSET" == command.verb:
|
||||||
|
proto.logf("Got RSET command, switching to MAIL state")
|
||||||
|
proto.State = MAIL
|
||||||
|
proto.Message = &data.SMTPMessage{}
|
||||||
|
return ReplyOk()
|
||||||
|
case "NOOP" == command.verb:
|
||||||
|
proto.logf("Got NOOP verb, staying in %s state", StateMap[proto.State])
|
||||||
|
return ReplyOk()
|
||||||
|
case "QUIT" == command.verb:
|
||||||
|
proto.logf("Got QUIT verb, staying in %s state", StateMap[proto.State])
|
||||||
|
proto.State = DONE
|
||||||
|
return ReplyBye()
|
||||||
|
case ESTABLISH == proto.State:
|
||||||
|
proto.logf("In ESTABLISH state")
|
||||||
|
switch command.verb {
|
||||||
|
case "HELO":
|
||||||
|
return proto.HELO(command.args)
|
||||||
|
case "EHLO":
|
||||||
|
return proto.EHLO(command.args)
|
||||||
|
case "STARTTLS":
|
||||||
|
return proto.STARTTLS(command.args)
|
||||||
|
default:
|
||||||
|
proto.logf("Got unknown command for ESTABLISH state: '%s'", command.verb)
|
||||||
|
return ReplyUnrecognisedCommand()
|
||||||
|
}
|
||||||
|
case "STARTTLS" == command.verb:
|
||||||
|
proto.logf("Got STARTTLS command outside ESTABLISH state")
|
||||||
|
return proto.STARTTLS(command.args)
|
||||||
|
case proto.RequireTLS && !proto.TLSUpgraded:
|
||||||
|
proto.logf("RequireTLS set and not TLS not upgraded")
|
||||||
|
return ReplyMustIssueSTARTTLSFirst()
|
||||||
|
case AUTHPLAIN == proto.State:
|
||||||
|
proto.logf("Got PLAIN authentication response: '%s', switching to MAIL state", command.args)
|
||||||
|
proto.State = MAIL
|
||||||
|
if proto.ValidateAuthenticationHandler != nil {
|
||||||
|
// TODO error handling
|
||||||
|
val, _ := base64.StdEncoding.DecodeString(command.orig)
|
||||||
|
bits := strings.Split(string(val), string(rune(0)))
|
||||||
|
|
||||||
|
if len(bits) < 3 {
|
||||||
|
return ReplyError(errors.New("Badly formed parameter"))
|
||||||
|
}
|
||||||
|
|
||||||
|
user, pass := bits[1], bits[2]
|
||||||
|
|
||||||
|
if reply, ok := proto.ValidateAuthenticationHandler("PLAIN", user, pass); !ok {
|
||||||
|
return reply
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ReplyAuthOk()
|
||||||
|
case AUTHLOGIN == proto.State:
|
||||||
|
proto.logf("Got LOGIN authentication response: '%s', switching to AUTHLOGIN2 state", command.args)
|
||||||
|
proto.State = AUTHLOGIN2
|
||||||
|
return ReplyAuthResponse("UGFzc3dvcmQ6")
|
||||||
|
case AUTHLOGIN2 == proto.State:
|
||||||
|
proto.logf("Got LOGIN authentication response: '%s', switching to MAIL state", command.args)
|
||||||
|
proto.State = MAIL
|
||||||
|
if proto.ValidateAuthenticationHandler != nil {
|
||||||
|
if reply, ok := proto.ValidateAuthenticationHandler("LOGIN", proto.lastCommand.orig, command.orig); !ok {
|
||||||
|
return reply
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ReplyAuthOk()
|
||||||
|
case AUTHCRAMMD5 == proto.State:
|
||||||
|
proto.logf("Got CRAM-MD5 authentication response: '%s', switching to MAIL state", command.args)
|
||||||
|
proto.State = MAIL
|
||||||
|
if proto.ValidateAuthenticationHandler != nil {
|
||||||
|
if reply, ok := proto.ValidateAuthenticationHandler("CRAM-MD5", command.orig); !ok {
|
||||||
|
return reply
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ReplyAuthOk()
|
||||||
|
case MAIL == proto.State:
|
||||||
|
proto.logf("In MAIL state")
|
||||||
|
switch command.verb {
|
||||||
|
case "AUTH":
|
||||||
|
proto.logf("Got AUTH command, staying in MAIL state")
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(command.args, "PLAIN "):
|
||||||
|
proto.logf("Got PLAIN authentication: %s", strings.TrimPrefix(command.args, "PLAIN "))
|
||||||
|
if proto.ValidateAuthenticationHandler != nil {
|
||||||
|
val, _ := base64.StdEncoding.DecodeString(strings.TrimPrefix(command.args, "PLAIN "))
|
||||||
|
bits := strings.Split(string(val), string(rune(0)))
|
||||||
|
|
||||||
|
if len(bits) < 3 {
|
||||||
|
return ReplyError(errors.New("Badly formed parameter"))
|
||||||
|
}
|
||||||
|
|
||||||
|
user, pass := bits[1], bits[2]
|
||||||
|
|
||||||
|
if reply, ok := proto.ValidateAuthenticationHandler("PLAIN", user, pass); !ok {
|
||||||
|
return reply
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ReplyAuthOk()
|
||||||
|
case "LOGIN" == command.args:
|
||||||
|
proto.logf("Got LOGIN authentication, switching to AUTH state")
|
||||||
|
proto.State = AUTHLOGIN
|
||||||
|
return ReplyAuthResponse("VXNlcm5hbWU6")
|
||||||
|
case "PLAIN" == command.args:
|
||||||
|
proto.logf("Got PLAIN authentication (no args), switching to AUTH2 state")
|
||||||
|
proto.State = AUTHPLAIN
|
||||||
|
return ReplyAuthResponse("")
|
||||||
|
case "CRAM-MD5" == command.args:
|
||||||
|
proto.logf("Got CRAM-MD5 authentication, switching to AUTH state")
|
||||||
|
proto.State = AUTHCRAMMD5
|
||||||
|
return ReplyAuthResponse("PDQxOTI5NDIzNDEuMTI4Mjg0NzJAc291cmNlZm91ci5hbmRyZXcuY211LmVkdT4=")
|
||||||
|
case strings.HasPrefix(command.args, "EXTERNAL "):
|
||||||
|
proto.logf("Got EXTERNAL authentication: %s", strings.TrimPrefix(command.args, "EXTERNAL "))
|
||||||
|
if proto.ValidateAuthenticationHandler != nil {
|
||||||
|
if reply, ok := proto.ValidateAuthenticationHandler("EXTERNAL", strings.TrimPrefix(command.args, "EXTERNAL ")); !ok {
|
||||||
|
return reply
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ReplyAuthOk()
|
||||||
|
default:
|
||||||
|
return ReplyUnsupportedAuth()
|
||||||
|
}
|
||||||
|
case "MAIL":
|
||||||
|
proto.logf("Got MAIL command, switching to RCPT state")
|
||||||
|
from, err := proto.ParseMAIL(command.args)
|
||||||
|
if err != nil {
|
||||||
|
return ReplyError(err)
|
||||||
|
}
|
||||||
|
if proto.ValidateSenderHandler != nil {
|
||||||
|
if !proto.ValidateSenderHandler(from) {
|
||||||
|
// TODO correct sender error response
|
||||||
|
return ReplyError(errors.New("Invalid sender " + from))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
proto.Message.From = from
|
||||||
|
proto.State = RCPT
|
||||||
|
return ReplySenderOk(from)
|
||||||
|
case "HELO":
|
||||||
|
return proto.HELO(command.args)
|
||||||
|
case "EHLO":
|
||||||
|
return proto.EHLO(command.args)
|
||||||
|
default:
|
||||||
|
proto.logf("Got unknown command for MAIL state: '%s'", command)
|
||||||
|
return ReplyUnrecognisedCommand()
|
||||||
|
}
|
||||||
|
case RCPT == proto.State:
|
||||||
|
proto.logf("In RCPT state")
|
||||||
|
switch command.verb {
|
||||||
|
case "RCPT":
|
||||||
|
proto.logf("Got RCPT command")
|
||||||
|
if proto.MaximumRecipients > -1 && len(proto.Message.To) >= proto.MaximumRecipients {
|
||||||
|
return ReplyTooManyRecipients()
|
||||||
|
}
|
||||||
|
to, err := proto.ParseRCPT(command.args)
|
||||||
|
if err != nil {
|
||||||
|
return ReplyError(err)
|
||||||
|
}
|
||||||
|
if proto.ValidateRecipientHandler != nil {
|
||||||
|
if !proto.ValidateRecipientHandler(to) {
|
||||||
|
// TODO correct send error response
|
||||||
|
return ReplyError(errors.New("Invalid recipient " + to))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
proto.Message.To = append(proto.Message.To, to)
|
||||||
|
proto.State = RCPT
|
||||||
|
return ReplyRecipientOk(to)
|
||||||
|
case "HELO":
|
||||||
|
return proto.HELO(command.args)
|
||||||
|
case "EHLO":
|
||||||
|
return proto.EHLO(command.args)
|
||||||
|
case "DATA":
|
||||||
|
proto.logf("Got DATA command, switching to DATA state")
|
||||||
|
proto.State = DATA
|
||||||
|
return ReplyDataResponse()
|
||||||
|
default:
|
||||||
|
proto.logf("Got unknown command for RCPT state: '%s'", command)
|
||||||
|
return ReplyUnrecognisedCommand()
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
proto.logf("Command not recognised")
|
||||||
|
return ReplyUnrecognisedCommand()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HELO creates a reply to a HELO command
|
||||||
|
func (proto *Protocol) HELO(args string) (reply *Reply) {
|
||||||
|
proto.logf("Got HELO command, switching to MAIL state")
|
||||||
|
proto.State = MAIL
|
||||||
|
proto.Message.Helo = args
|
||||||
|
return ReplyOk("Hello " + args)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EHLO creates a reply to a EHLO command
|
||||||
|
func (proto *Protocol) EHLO(args string) (reply *Reply) {
|
||||||
|
proto.logf("Got EHLO command, switching to MAIL state")
|
||||||
|
proto.State = MAIL
|
||||||
|
proto.Message.Helo = args
|
||||||
|
replyArgs := []string{"Hello " + args, "PIPELINING"}
|
||||||
|
|
||||||
|
if proto.TLSHandler != nil && !proto.TLSPending && !proto.TLSUpgraded {
|
||||||
|
replyArgs = append(replyArgs, "STARTTLS")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !proto.RequireTLS || proto.TLSUpgraded {
|
||||||
|
if proto.GetAuthenticationMechanismsHandler != nil {
|
||||||
|
mechanisms := proto.GetAuthenticationMechanismsHandler()
|
||||||
|
if len(mechanisms) > 0 {
|
||||||
|
replyArgs = append(replyArgs, "AUTH "+strings.Join(mechanisms, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ReplyOk(replyArgs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// STARTTLS creates a reply to a STARTTLS command
|
||||||
|
func (proto *Protocol) STARTTLS(args string) (reply *Reply) {
|
||||||
|
if proto.TLSUpgraded {
|
||||||
|
return ReplyUnrecognisedCommand()
|
||||||
|
}
|
||||||
|
|
||||||
|
if proto.TLSHandler == nil {
|
||||||
|
proto.logf("tls handler not found")
|
||||||
|
return ReplyUnrecognisedCommand()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args) > 0 {
|
||||||
|
return ReplySyntaxError("no parameters allowed")
|
||||||
|
}
|
||||||
|
|
||||||
|
r, callback, ok := proto.TLSHandler(func(ok bool) {
|
||||||
|
proto.TLSUpgraded = ok
|
||||||
|
proto.TLSPending = ok
|
||||||
|
if ok {
|
||||||
|
proto.resetState()
|
||||||
|
proto.State = ESTABLISH
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if !ok {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
proto.TLSPending = true
|
||||||
|
return ReplyReadyToStartTLS(callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
var parseMailBrokenRegexp = regexp.MustCompile("(?i:From):\\s*<([^>]+)>")
|
||||||
|
var parseMailRFCRegexp = regexp.MustCompile("(?i:From):<([^>]+)>")
|
||||||
|
|
||||||
|
// ParseMAIL returns the forward-path from a MAIL command argument
|
||||||
|
func (proto *Protocol) ParseMAIL(mail string) (string, error) {
|
||||||
|
var match []string
|
||||||
|
if proto.RejectBrokenMAILSyntax {
|
||||||
|
match = parseMailRFCRegexp.FindStringSubmatch(mail)
|
||||||
|
} else {
|
||||||
|
match = parseMailBrokenRegexp.FindStringSubmatch(mail)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(match) != 2 {
|
||||||
|
return "", errors.New("Invalid syntax in MAIL command")
|
||||||
|
}
|
||||||
|
return match[1], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var parseRcptBrokenRegexp = regexp.MustCompile("(?i:To):\\s*<([^>]+)>")
|
||||||
|
var parseRcptRFCRegexp = regexp.MustCompile("(?i:To):<([^>]+)>")
|
||||||
|
|
||||||
|
// ParseRCPT returns the return-path from a RCPT command argument
|
||||||
|
func (proto *Protocol) ParseRCPT(rcpt string) (string, error) {
|
||||||
|
var match []string
|
||||||
|
if proto.RejectBrokenRCPTSyntax {
|
||||||
|
match = parseRcptRFCRegexp.FindStringSubmatch(rcpt)
|
||||||
|
} else {
|
||||||
|
match = parseRcptBrokenRegexp.FindStringSubmatch(rcpt)
|
||||||
|
}
|
||||||
|
if len(match) != 2 {
|
||||||
|
return "", errors.New("Invalid syntax in RCPT command")
|
||||||
|
}
|
||||||
|
return match[1], nil
|
||||||
|
}
|
111
vendor/github.com/mailhog/smtp/reply.go
generated
vendored
Normal file
111
vendor/github.com/mailhog/smtp/reply.go
generated
vendored
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
package smtp
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
// http://www.rfc-editor.org/rfc/rfc5321.txt
|
||||||
|
|
||||||
|
// Reply is a struct representing an SMTP reply (status code + lines)
|
||||||
|
type Reply struct {
|
||||||
|
Status int
|
||||||
|
lines []string
|
||||||
|
Done func()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lines returns the formatted SMTP reply
|
||||||
|
func (r Reply) Lines() []string {
|
||||||
|
var lines []string
|
||||||
|
|
||||||
|
if len(r.lines) == 0 {
|
||||||
|
l := strconv.Itoa(r.Status)
|
||||||
|
lines = append(lines, l+"\n")
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, line := range r.lines {
|
||||||
|
l := ""
|
||||||
|
if i == len(r.lines)-1 {
|
||||||
|
l = strconv.Itoa(r.Status) + " " + line + "\r\n"
|
||||||
|
} else {
|
||||||
|
l = strconv.Itoa(r.Status) + "-" + line + "\r\n"
|
||||||
|
}
|
||||||
|
lines = append(lines, l)
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplyIdent creates a 220 welcome reply
|
||||||
|
func ReplyIdent(ident string) *Reply { return &Reply{220, []string{ident}, nil} }
|
||||||
|
|
||||||
|
// ReplyReadyToStartTLS creates a 220 ready to start TLS reply
|
||||||
|
func ReplyReadyToStartTLS(callback func()) *Reply {
|
||||||
|
return &Reply{220, []string{"Ready to start TLS"}, callback}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplyBye creates a 221 Bye reply
|
||||||
|
func ReplyBye() *Reply { return &Reply{221, []string{"Bye"}, nil} }
|
||||||
|
|
||||||
|
// ReplyAuthOk creates a 235 authentication successful reply
|
||||||
|
func ReplyAuthOk() *Reply { return &Reply{235, []string{"Authentication successful"}, nil} }
|
||||||
|
|
||||||
|
// ReplyOk creates a 250 Ok reply
|
||||||
|
func ReplyOk(message ...string) *Reply {
|
||||||
|
if len(message) == 0 {
|
||||||
|
message = []string{"Ok"}
|
||||||
|
}
|
||||||
|
return &Reply{250, message, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplySenderOk creates a 250 Sender ok reply
|
||||||
|
func ReplySenderOk(sender string) *Reply {
|
||||||
|
return &Reply{250, []string{"Sender " + sender + " ok"}, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplyRecipientOk creates a 250 Sender ok reply
|
||||||
|
func ReplyRecipientOk(recipient string) *Reply {
|
||||||
|
return &Reply{250, []string{"Recipient " + recipient + " ok"}, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplyAuthResponse creates a 334 authentication reply
|
||||||
|
func ReplyAuthResponse(response string) *Reply { return &Reply{334, []string{response}, nil} }
|
||||||
|
|
||||||
|
// ReplyDataResponse creates a 354 data reply
|
||||||
|
func ReplyDataResponse() *Reply { return &Reply{354, []string{"End data with <CR><LF>.<CR><LF>"}, nil} }
|
||||||
|
|
||||||
|
// ReplyStorageFailed creates a 452 error reply
|
||||||
|
func ReplyStorageFailed(reason string) *Reply { return &Reply{452, []string{reason}, nil} }
|
||||||
|
|
||||||
|
// ReplyUnrecognisedCommand creates a 500 Unrecognised command reply
|
||||||
|
func ReplyUnrecognisedCommand() *Reply { return &Reply{500, []string{"Unrecognised command"}, nil} }
|
||||||
|
|
||||||
|
// ReplyLineTooLong creates a 500 Line too long reply
|
||||||
|
func ReplyLineTooLong() *Reply { return &Reply{500, []string{"Line too long"}, nil} }
|
||||||
|
|
||||||
|
// ReplySyntaxError creates a 501 Syntax error reply
|
||||||
|
func ReplySyntaxError(response string) *Reply {
|
||||||
|
if len(response) > 0 {
|
||||||
|
response = " (" + response + ")"
|
||||||
|
}
|
||||||
|
return &Reply{501, []string{"Syntax error" + response}, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplyUnsupportedAuth creates a 504 unsupported authentication reply
|
||||||
|
func ReplyUnsupportedAuth() *Reply {
|
||||||
|
return &Reply{504, []string{"Unsupported authentication mechanism"}, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplyMustIssueSTARTTLSFirst creates a 530 reply for RFC3207
|
||||||
|
func ReplyMustIssueSTARTTLSFirst() *Reply {
|
||||||
|
return &Reply{530, []string{"Must issue a STARTTLS command first"}, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplyInvalidAuth creates a 535 error reply
|
||||||
|
func ReplyInvalidAuth() *Reply {
|
||||||
|
return &Reply{535, []string{"Authentication credentials invalid"}, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplyError creates a 500 error reply
|
||||||
|
func ReplyError(err error) *Reply { return &Reply{550, []string{err.Error()}, nil} }
|
||||||
|
|
||||||
|
// ReplyTooManyRecipients creates a 552 too many recipients reply
|
||||||
|
func ReplyTooManyRecipients() *Reply { return &Reply{552, []string{"Too many recipients"}, nil} }
|
32
vendor/github.com/mailhog/smtp/state.go
generated
vendored
Normal file
32
vendor/github.com/mailhog/smtp/state.go
generated
vendored
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package smtp
|
||||||
|
|
||||||
|
// State represents the state of an SMTP conversation
|
||||||
|
type State int
|
||||||
|
|
||||||
|
// SMTP message conversation states
|
||||||
|
const (
|
||||||
|
INVALID = State(-1)
|
||||||
|
ESTABLISH = State(iota)
|
||||||
|
AUTHPLAIN
|
||||||
|
AUTHLOGIN
|
||||||
|
AUTHLOGIN2
|
||||||
|
AUTHCRAMMD5
|
||||||
|
MAIL
|
||||||
|
RCPT
|
||||||
|
DATA
|
||||||
|
DONE
|
||||||
|
)
|
||||||
|
|
||||||
|
// StateMap provides string representations of SMTP conversation states
|
||||||
|
var StateMap = map[State]string{
|
||||||
|
INVALID: "INVALID",
|
||||||
|
ESTABLISH: "ESTABLISH",
|
||||||
|
AUTHPLAIN: "AUTHPLAIN",
|
||||||
|
AUTHLOGIN: "AUTHLOGIN",
|
||||||
|
AUTHLOGIN2: "AUTHLOGIN2",
|
||||||
|
AUTHCRAMMD5: "AUTHCRAMMD5",
|
||||||
|
MAIL: "MAIL",
|
||||||
|
RCPT: "RCPT",
|
||||||
|
DATA: "DATA",
|
||||||
|
DONE: "DONE",
|
||||||
|
}
|
21
vendor/github.com/mailhog/storage/LICENSE.md
generated
vendored
Normal file
21
vendor/github.com/mailhog/storage/LICENSE.md
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 - 2016 Ian Kent
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
16
vendor/github.com/mailhog/storage/README.md
generated
vendored
Normal file
16
vendor/github.com/mailhog/storage/README.md
generated
vendored
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
MailHog storage backends [![GoDoc](https://godoc.org/github.com/mailhog/storage?status.svg)](https://godoc.org/github.com/mailhog/storage) [![Build Status](https://travis-ci.org/mailhog/storage.svg?branch=master)](https://travis-ci.org/mailhog/storage)
|
||||||
|
=========
|
||||||
|
|
||||||
|
`github.com/mailhog/storage` implements MailHog storage backends:
|
||||||
|
|
||||||
|
* In-memory
|
||||||
|
* MongoDB
|
||||||
|
|
||||||
|
You should implement `storage.Storage` interface to provide your
|
||||||
|
own storage backend.
|
||||||
|
|
||||||
|
### Licence
|
||||||
|
|
||||||
|
Copyright © 2014 - 2016, Ian Kent (http://iankent.uk)
|
||||||
|
|
||||||
|
Released under MIT license, see [LICENSE](LICENSE.md) for details.
|
184
vendor/github.com/mailhog/storage/maildir.go
generated
vendored
Normal file
184
vendor/github.com/mailhog/storage/maildir.go
generated
vendored
Normal file
|
@ -0,0 +1,184 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/mailhog/data"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Maildir is a maildir storage backend
|
||||||
|
type Maildir struct {
|
||||||
|
Path string
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateMaildir creates a new maildir storage backend
|
||||||
|
func CreateMaildir(path string) *Maildir {
|
||||||
|
if len(path) == 0 {
|
||||||
|
dir, err := ioutil.TempDir("", "mailhog")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
path = dir
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(path); err != nil {
|
||||||
|
err := os.MkdirAll(path, 0770)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Println("Maildir path is", path)
|
||||||
|
return &Maildir{
|
||||||
|
Path: path,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store stores a message and returns its storage ID
|
||||||
|
func (maildir *Maildir) Store(m *data.Message) (string, error) {
|
||||||
|
b, err := ioutil.ReadAll(m.Raw.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
err = ioutil.WriteFile(filepath.Join(maildir.Path, string(m.ID)), b, 0660)
|
||||||
|
return string(m.ID), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count returns the number of stored messages
|
||||||
|
func (maildir *Maildir) Count() int {
|
||||||
|
// FIXME may be wrong, ../. ?
|
||||||
|
// and handle error?
|
||||||
|
dir, err := os.Open(maildir.Path)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer dir.Close()
|
||||||
|
n, _ := dir.Readdirnames(0)
|
||||||
|
return len(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search finds messages matching the query
|
||||||
|
func (maildir *Maildir) Search(kind, query string, start, limit int) (*data.Messages, int, error) {
|
||||||
|
query = strings.ToLower(query)
|
||||||
|
var filteredMessages = make([]data.Message, 0)
|
||||||
|
|
||||||
|
var matched int
|
||||||
|
|
||||||
|
err := filepath.Walk(maildir.Path, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if limit > 0 && len(filteredMessages) >= limit {
|
||||||
|
return errors.New("reached limit")
|
||||||
|
}
|
||||||
|
|
||||||
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err := maildir.Load(info.Name())
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch kind {
|
||||||
|
case "to":
|
||||||
|
for _, t := range msg.To {
|
||||||
|
if strings.Contains(t.Mailbox+"@"+t.Domain, query) {
|
||||||
|
if start > matched {
|
||||||
|
matched++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
filteredMessages = append(filteredMessages, *msg)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "from":
|
||||||
|
if strings.Contains(msg.From.Mailbox+"@"+msg.From.Domain, query) {
|
||||||
|
if start > matched {
|
||||||
|
matched++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
filteredMessages = append(filteredMessages, *msg)
|
||||||
|
}
|
||||||
|
case "containing":
|
||||||
|
if strings.Contains(msg.Raw.Data, query) {
|
||||||
|
if start > matched {
|
||||||
|
matched++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
filteredMessages = append(filteredMessages, *msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
msgs := data.Messages(filteredMessages)
|
||||||
|
return &msgs, len(filteredMessages), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// List lists stored messages by index
|
||||||
|
func (maildir *Maildir) List(start, limit int) (*data.Messages, error) {
|
||||||
|
log.Println("Listing messages in", maildir.Path)
|
||||||
|
messages := make([]data.Message, 0)
|
||||||
|
|
||||||
|
dir, err := os.Open(maildir.Path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer dir.Close()
|
||||||
|
|
||||||
|
n, err := dir.Readdir(0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, fileinfo := range n {
|
||||||
|
b, err := ioutil.ReadFile(filepath.Join(maildir.Path, fileinfo.Name()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
msg := data.FromBytes(b)
|
||||||
|
// FIXME domain
|
||||||
|
m := *msg.Parse("mailhog.example")
|
||||||
|
m.ID = data.MessageID(fileinfo.Name())
|
||||||
|
m.Created = fileinfo.ModTime()
|
||||||
|
messages = append(messages, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Found %d messages", len(messages))
|
||||||
|
msgs := data.Messages(messages)
|
||||||
|
return &msgs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteOne deletes an individual message by storage ID
|
||||||
|
func (maildir *Maildir) DeleteOne(id string) error {
|
||||||
|
return os.Remove(filepath.Join(maildir.Path, id))
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteAll deletes all in memory messages
|
||||||
|
func (maildir *Maildir) DeleteAll() error {
|
||||||
|
err := os.RemoveAll(maildir.Path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.Mkdir(maildir.Path, 0770)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load returns an individual message by storage ID
|
||||||
|
func (maildir *Maildir) Load(id string) (*data.Message, error) {
|
||||||
|
b, err := ioutil.ReadFile(filepath.Join(maildir.Path, id))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// FIXME domain
|
||||||
|
m := data.FromBytes(b).Parse("mailhog.example")
|
||||||
|
m.ID = data.MessageID(id)
|
||||||
|
return m, nil
|
||||||
|
}
|
183
vendor/github.com/mailhog/storage/memory.go
generated
vendored
Normal file
183
vendor/github.com/mailhog/storage/memory.go
generated
vendored
Normal file
|
@ -0,0 +1,183 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/mailhog/data"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InMemory is an in memory storage backend
|
||||||
|
type InMemory struct {
|
||||||
|
MessageIDIndex map[string]int
|
||||||
|
Messages []*data.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateInMemory creates a new in memory storage backend
|
||||||
|
func CreateInMemory() *InMemory {
|
||||||
|
return &InMemory{
|
||||||
|
MessageIDIndex: make(map[string]int),
|
||||||
|
Messages: make([]*data.Message, 0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store stores a message and returns its storage ID
|
||||||
|
func (memory *InMemory) Store(m *data.Message) (string, error) {
|
||||||
|
memory.Messages = append(memory.Messages, m)
|
||||||
|
memory.MessageIDIndex[string(m.ID)] = len(memory.Messages) - 1
|
||||||
|
return string(m.ID), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count returns the number of stored messages
|
||||||
|
func (memory *InMemory) Count() int {
|
||||||
|
return len(memory.Messages)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search finds messages matching the query
|
||||||
|
func (memory *InMemory) Search(kind, query string, start, limit int) (*data.Messages, int, error) {
|
||||||
|
// FIXME needs optimising, or replacing with a proper db!
|
||||||
|
query = strings.ToLower(query)
|
||||||
|
var filteredMessages = make([]*data.Message, 0)
|
||||||
|
for _, m := range memory.Messages {
|
||||||
|
doAppend := false
|
||||||
|
|
||||||
|
switch kind {
|
||||||
|
case "to":
|
||||||
|
for _, to := range m.To {
|
||||||
|
if strings.Contains(strings.ToLower(to.Mailbox+"@"+to.Domain), query) {
|
||||||
|
doAppend = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !doAppend {
|
||||||
|
if hdr, ok := m.Content.Headers["To"]; ok {
|
||||||
|
for _, to := range hdr {
|
||||||
|
if strings.Contains(strings.ToLower(to), query) {
|
||||||
|
doAppend = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "from":
|
||||||
|
if strings.Contains(strings.ToLower(m.From.Mailbox+"@"+m.From.Domain), query) {
|
||||||
|
doAppend = true
|
||||||
|
}
|
||||||
|
if !doAppend {
|
||||||
|
if hdr, ok := m.Content.Headers["From"]; ok {
|
||||||
|
for _, from := range hdr {
|
||||||
|
if strings.Contains(strings.ToLower(from), query) {
|
||||||
|
doAppend = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "containing":
|
||||||
|
if strings.Contains(strings.ToLower(m.Content.Body), query) {
|
||||||
|
doAppend = true
|
||||||
|
}
|
||||||
|
if !doAppend {
|
||||||
|
for _, hdr := range m.Content.Headers {
|
||||||
|
for _, v := range hdr {
|
||||||
|
if strings.Contains(strings.ToLower(v), query) {
|
||||||
|
doAppend = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if doAppend {
|
||||||
|
filteredMessages = append(filteredMessages, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var messages = make([]data.Message, 0)
|
||||||
|
|
||||||
|
if len(filteredMessages) == 0 || start > len(filteredMessages) {
|
||||||
|
msgs := data.Messages(messages)
|
||||||
|
return &msgs, 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if start+limit > len(filteredMessages) {
|
||||||
|
limit = len(filteredMessages) - start
|
||||||
|
}
|
||||||
|
|
||||||
|
start = len(filteredMessages) - start - 1
|
||||||
|
end := start - limit
|
||||||
|
|
||||||
|
if start < 0 {
|
||||||
|
start = 0
|
||||||
|
}
|
||||||
|
if end < -1 {
|
||||||
|
end = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := start; i > end; i-- {
|
||||||
|
//for _, m := range memory.MessageIndex[start:end] {
|
||||||
|
messages = append(messages, *filteredMessages[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
msgs := data.Messages(messages)
|
||||||
|
return &msgs, len(filteredMessages), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// List lists stored messages by index
|
||||||
|
func (memory *InMemory) List(start int, limit int) (*data.Messages, error) {
|
||||||
|
var messages = make([]data.Message, 0)
|
||||||
|
|
||||||
|
if len(memory.Messages) == 0 || start > len(memory.Messages) {
|
||||||
|
msgs := data.Messages(messages)
|
||||||
|
return &msgs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if start+limit > len(memory.Messages) {
|
||||||
|
limit = len(memory.Messages) - start
|
||||||
|
}
|
||||||
|
|
||||||
|
start = len(memory.Messages) - start - 1
|
||||||
|
end := start - limit
|
||||||
|
|
||||||
|
if start < 0 {
|
||||||
|
start = 0
|
||||||
|
}
|
||||||
|
if end < -1 {
|
||||||
|
end = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := start; i > end; i-- {
|
||||||
|
//for _, m := range memory.MessageIndex[start:end] {
|
||||||
|
messages = append(messages, *memory.Messages[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
msgs := data.Messages(messages)
|
||||||
|
return &msgs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteOne deletes an individual message by storage ID
|
||||||
|
func (memory *InMemory) DeleteOne(id string) error {
|
||||||
|
index := memory.MessageIDIndex[id]
|
||||||
|
delete(memory.MessageIDIndex, id)
|
||||||
|
for k, v := range memory.MessageIDIndex {
|
||||||
|
if v > index {
|
||||||
|
memory.MessageIDIndex[k] = v - 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
memory.Messages = append(memory.Messages[:index], memory.Messages[index+1:]...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteAll deletes all in memory messages
|
||||||
|
func (memory *InMemory) DeleteAll() error {
|
||||||
|
memory.Messages = make([]*data.Message, 0)
|
||||||
|
memory.MessageIDIndex = make(map[string]int)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load returns an individual message by storage ID
|
||||||
|
func (memory *InMemory) Load(id string) (*data.Message, error) {
|
||||||
|
if idx, ok := memory.MessageIDIndex[id]; ok {
|
||||||
|
return memory.Messages[idx], nil
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
122
vendor/github.com/mailhog/storage/mongodb.go
generated
vendored
Normal file
122
vendor/github.com/mailhog/storage/mongodb.go
generated
vendored
Normal file
|
@ -0,0 +1,122 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"github.com/mailhog/data"
|
||||||
|
"gopkg.in/mgo.v2"
|
||||||
|
"gopkg.in/mgo.v2/bson"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MongoDB represents MongoDB backed storage backend
|
||||||
|
type MongoDB struct {
|
||||||
|
Session *mgo.Session
|
||||||
|
Collection *mgo.Collection
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateMongoDB creates a MongoDB backed storage backend
|
||||||
|
func CreateMongoDB(uri, db, coll string) *MongoDB {
|
||||||
|
log.Printf("Connecting to MongoDB: %s\n", uri)
|
||||||
|
session, err := mgo.Dial(uri)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error connecting to MongoDB: %s", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = session.DB(db).C(coll).EnsureIndexKey("created")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed creating index: %s", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &MongoDB{
|
||||||
|
Session: session,
|
||||||
|
Collection: session.DB(db).C(coll),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store stores a message in MongoDB and returns its storage ID
|
||||||
|
func (mongo *MongoDB) Store(m *data.Message) (string, error) {
|
||||||
|
err := mongo.Collection.Insert(m)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error inserting message: %s", err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(m.ID), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count returns the number of stored messages
|
||||||
|
func (mongo *MongoDB) Count() int {
|
||||||
|
c, _ := mongo.Collection.Count()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search finds messages matching the query
|
||||||
|
func (mongo *MongoDB) Search(kind, query string, start, limit int) (*data.Messages, int, error) {
|
||||||
|
messages := &data.Messages{}
|
||||||
|
var count = 0
|
||||||
|
var field = "raw.data"
|
||||||
|
switch kind {
|
||||||
|
case "to":
|
||||||
|
field = "raw.to"
|
||||||
|
case "from":
|
||||||
|
field = "raw.from"
|
||||||
|
}
|
||||||
|
err := mongo.Collection.Find(bson.M{field: bson.RegEx{Pattern: query, Options: "i"}}).Skip(start).Limit(limit).Sort("-created").Select(bson.M{
|
||||||
|
"id": 1,
|
||||||
|
"_id": 1,
|
||||||
|
"from": 1,
|
||||||
|
"to": 1,
|
||||||
|
"content.headers": 1,
|
||||||
|
"content.size": 1,
|
||||||
|
"created": 1,
|
||||||
|
"raw": 1,
|
||||||
|
}).All(messages)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error loading messages: %s", err)
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
count, _ = mongo.Collection.Find(bson.M{field: bson.RegEx{Pattern: query, Options: "i"}}).Count()
|
||||||
|
|
||||||
|
return messages, count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// List returns a list of messages by index
|
||||||
|
func (mongo *MongoDB) List(start int, limit int) (*data.Messages, error) {
|
||||||
|
messages := &data.Messages{}
|
||||||
|
err := mongo.Collection.Find(bson.M{}).Skip(start).Limit(limit).Sort("-created").Select(bson.M{
|
||||||
|
"id": 1,
|
||||||
|
"_id": 1,
|
||||||
|
"from": 1,
|
||||||
|
"to": 1,
|
||||||
|
"content.headers": 1,
|
||||||
|
"content.size": 1,
|
||||||
|
"created": 1,
|
||||||
|
"raw": 1,
|
||||||
|
}).All(messages)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error loading messages: %s", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return messages, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteOne deletes an individual message by storage ID
|
||||||
|
func (mongo *MongoDB) DeleteOne(id string) error {
|
||||||
|
_, err := mongo.Collection.RemoveAll(bson.M{"id": id})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteAll deletes all messages stored in MongoDB
|
||||||
|
func (mongo *MongoDB) DeleteAll() error {
|
||||||
|
_, err := mongo.Collection.RemoveAll(bson.M{})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load loads an individual message by storage ID
|
||||||
|
func (mongo *MongoDB) Load(id string) (*data.Message, error) {
|
||||||
|
result := &data.Message{}
|
||||||
|
err := mongo.Collection.Find(bson.M{"id": id}).One(&result)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error loading message: %s", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
14
vendor/github.com/mailhog/storage/storage.go
generated
vendored
Normal file
14
vendor/github.com/mailhog/storage/storage.go
generated
vendored
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import "github.com/mailhog/data"
|
||||||
|
|
||||||
|
// Storage represents a storage backend
|
||||||
|
type Storage interface {
|
||||||
|
Store(m *data.Message) (string, error)
|
||||||
|
List(start, limit int) (*data.Messages, error)
|
||||||
|
Search(kind, query string, start, limit int) (*data.Messages, int, error)
|
||||||
|
Count() int
|
||||||
|
DeleteOne(id string) error
|
||||||
|
DeleteAll() error
|
||||||
|
Load(id string) (*data.Message, error)
|
||||||
|
}
|
7
vendor/github.com/philhofer/fwd/LICENSE.md
generated
vendored
Normal file
7
vendor/github.com/philhofer/fwd/LICENSE.md
generated
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
Copyright (c) 2014-2015, Philip Hofer
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
315
vendor/github.com/philhofer/fwd/README.md
generated
vendored
Normal file
315
vendor/github.com/philhofer/fwd/README.md
generated
vendored
Normal file
|
@ -0,0 +1,315 @@
|
||||||
|
|
||||||
|
# fwd
|
||||||
|
import "github.com/philhofer/fwd"
|
||||||
|
|
||||||
|
The `fwd` package provides a buffered reader
|
||||||
|
and writer. Each has methods that help improve
|
||||||
|
the encoding/decoding performance of some binary
|
||||||
|
protocols.
|
||||||
|
|
||||||
|
The `fwd.Writer` and `fwd.Reader` type provide similar
|
||||||
|
functionality to their counterparts in `bufio`, plus
|
||||||
|
a few extra utility methods that simplify read-ahead
|
||||||
|
and write-ahead. I wrote this package to improve serialization
|
||||||
|
performance for <a href="http://github.com/tinylib/msgp">http://github.com/tinylib/msgp</a>,
|
||||||
|
where it provided about a 2x speedup over `bufio` for certain
|
||||||
|
workloads. However, care must be taken to understand the semantics of the
|
||||||
|
extra methods provided by this package, as they allow
|
||||||
|
the user to access and manipulate the buffer memory
|
||||||
|
directly.
|
||||||
|
|
||||||
|
The extra methods for `fwd.Reader` are `Peek`, `Skip`
|
||||||
|
and `Next`. `(*fwd.Reader).Peek`, unlike `(*bufio.Reader).Peek`,
|
||||||
|
will re-allocate the read buffer in order to accommodate arbitrarily
|
||||||
|
large read-ahead. `(*fwd.Reader).Skip` skips the next `n` bytes
|
||||||
|
in the stream, and uses the `io.Seeker` interface if the underlying
|
||||||
|
stream implements it. `(*fwd.Reader).Next` returns a slice pointing
|
||||||
|
to the next `n` bytes in the read buffer (like `Peek`), but also
|
||||||
|
increments the read position. This allows users to process streams
|
||||||
|
in arbitrary block sizes without having to manage appropriately-sized
|
||||||
|
slices. Additionally, obviating the need to copy the data from the
|
||||||
|
buffer to another location in memory can improve performance dramatically
|
||||||
|
in CPU-bound applications.
|
||||||
|
|
||||||
|
`fwd.Writer` only has one extra method, which is `(*fwd.Writer).Next`, which
|
||||||
|
returns a slice pointing to the next `n` bytes of the writer, and increments
|
||||||
|
the write position by the length of the returned slice. This allows users
|
||||||
|
to write directly to the end of the buffer.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Constants
|
||||||
|
``` go
|
||||||
|
const (
|
||||||
|
// DefaultReaderSize is the default size of the read buffer
|
||||||
|
DefaultReaderSize = 2048
|
||||||
|
)
|
||||||
|
```
|
||||||
|
``` go
|
||||||
|
const (
|
||||||
|
// DefaultWriterSize is the
|
||||||
|
// default write buffer size.
|
||||||
|
DefaultWriterSize = 2048
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## type Reader
|
||||||
|
``` go
|
||||||
|
type Reader struct {
|
||||||
|
// contains filtered or unexported fields
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Reader is a buffered look-ahead reader
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func NewReader
|
||||||
|
``` go
|
||||||
|
func NewReader(r io.Reader) *Reader
|
||||||
|
```
|
||||||
|
NewReader returns a new *Reader that reads from 'r'
|
||||||
|
|
||||||
|
|
||||||
|
### func NewReaderSize
|
||||||
|
``` go
|
||||||
|
func NewReaderSize(r io.Reader, n int) *Reader
|
||||||
|
```
|
||||||
|
NewReaderSize returns a new *Reader that
|
||||||
|
reads from 'r' and has a buffer size 'n'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Reader) BufferSize
|
||||||
|
``` go
|
||||||
|
func (r *Reader) BufferSize() int
|
||||||
|
```
|
||||||
|
BufferSize returns the total size of the buffer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Reader) Buffered
|
||||||
|
``` go
|
||||||
|
func (r *Reader) Buffered() int
|
||||||
|
```
|
||||||
|
Buffered returns the number of bytes currently in the buffer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Reader) Next
|
||||||
|
``` go
|
||||||
|
func (r *Reader) Next(n int) ([]byte, error)
|
||||||
|
```
|
||||||
|
Next returns the next 'n' bytes in the stream.
|
||||||
|
Unlike Peek, Next advances the reader position.
|
||||||
|
The returned bytes point to the same
|
||||||
|
data as the buffer, so the slice is
|
||||||
|
only valid until the next reader method call.
|
||||||
|
An EOF is considered an unexpected error.
|
||||||
|
If an the returned slice is less than the
|
||||||
|
length asked for, an error will be returned,
|
||||||
|
and the reader position will not be incremented.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Reader) Peek
|
||||||
|
``` go
|
||||||
|
func (r *Reader) Peek(n int) ([]byte, error)
|
||||||
|
```
|
||||||
|
Peek returns the next 'n' buffered bytes,
|
||||||
|
reading from the underlying reader if necessary.
|
||||||
|
It will only return a slice shorter than 'n' bytes
|
||||||
|
if it also returns an error. Peek does not advance
|
||||||
|
the reader. EOF errors are *not* returned as
|
||||||
|
io.ErrUnexpectedEOF.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Reader) Read
|
||||||
|
``` go
|
||||||
|
func (r *Reader) Read(b []byte) (int, error)
|
||||||
|
```
|
||||||
|
Read implements `io.Reader`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Reader) ReadByte
|
||||||
|
``` go
|
||||||
|
func (r *Reader) ReadByte() (byte, error)
|
||||||
|
```
|
||||||
|
ReadByte implements `io.ByteReader`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Reader) ReadFull
|
||||||
|
``` go
|
||||||
|
func (r *Reader) ReadFull(b []byte) (int, error)
|
||||||
|
```
|
||||||
|
ReadFull attempts to read len(b) bytes into
|
||||||
|
'b'. It returns the number of bytes read into
|
||||||
|
'b', and an error if it does not return len(b).
|
||||||
|
EOF is considered an unexpected error.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Reader) Reset
|
||||||
|
``` go
|
||||||
|
func (r *Reader) Reset(rd io.Reader)
|
||||||
|
```
|
||||||
|
Reset resets the underlying reader
|
||||||
|
and the read buffer.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Reader) Skip
|
||||||
|
``` go
|
||||||
|
func (r *Reader) Skip(n int) (int, error)
|
||||||
|
```
|
||||||
|
Skip moves the reader forward 'n' bytes.
|
||||||
|
Returns the number of bytes skipped and any
|
||||||
|
errors encountered. It is analogous to Seek(n, 1).
|
||||||
|
If the underlying reader implements io.Seeker, then
|
||||||
|
that method will be used to skip forward.
|
||||||
|
|
||||||
|
If the reader encounters
|
||||||
|
an EOF before skipping 'n' bytes, it
|
||||||
|
returns io.ErrUnexpectedEOF. If the
|
||||||
|
underlying reader implements io.Seeker, then
|
||||||
|
those rules apply instead. (Many implementations
|
||||||
|
will not return `io.EOF` until the next call
|
||||||
|
to Read.)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Reader) WriteTo
|
||||||
|
``` go
|
||||||
|
func (r *Reader) WriteTo(w io.Writer) (int64, error)
|
||||||
|
```
|
||||||
|
WriteTo implements `io.WriterTo`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## type Writer
|
||||||
|
``` go
|
||||||
|
type Writer struct {
|
||||||
|
// contains filtered or unexported fields
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Writer is a buffered writer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func NewWriter
|
||||||
|
``` go
|
||||||
|
func NewWriter(w io.Writer) *Writer
|
||||||
|
```
|
||||||
|
NewWriter returns a new writer
|
||||||
|
that writes to 'w' and has a buffer
|
||||||
|
that is `DefaultWriterSize` bytes.
|
||||||
|
|
||||||
|
|
||||||
|
### func NewWriterSize
|
||||||
|
``` go
|
||||||
|
func NewWriterSize(w io.Writer, size int) *Writer
|
||||||
|
```
|
||||||
|
NewWriterSize returns a new writer
|
||||||
|
that writes to 'w' and has a buffer
|
||||||
|
that is 'size' bytes.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Writer) BufferSize
|
||||||
|
``` go
|
||||||
|
func (w *Writer) BufferSize() int
|
||||||
|
```
|
||||||
|
BufferSize returns the maximum size of the buffer.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Writer) Buffered
|
||||||
|
``` go
|
||||||
|
func (w *Writer) Buffered() int
|
||||||
|
```
|
||||||
|
Buffered returns the number of buffered bytes
|
||||||
|
in the reader.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Writer) Flush
|
||||||
|
``` go
|
||||||
|
func (w *Writer) Flush() error
|
||||||
|
```
|
||||||
|
Flush flushes any buffered bytes
|
||||||
|
to the underlying writer.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Writer) Next
|
||||||
|
``` go
|
||||||
|
func (w *Writer) Next(n int) ([]byte, error)
|
||||||
|
```
|
||||||
|
Next returns the next 'n' free bytes
|
||||||
|
in the write buffer, flushing the writer
|
||||||
|
as necessary. Next will return `io.ErrShortBuffer`
|
||||||
|
if 'n' is greater than the size of the write buffer.
|
||||||
|
Calls to 'next' increment the write position by
|
||||||
|
the size of the returned buffer.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Writer) ReadFrom
|
||||||
|
``` go
|
||||||
|
func (w *Writer) ReadFrom(r io.Reader) (int64, error)
|
||||||
|
```
|
||||||
|
ReadFrom implements `io.ReaderFrom`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Writer) Write
|
||||||
|
``` go
|
||||||
|
func (w *Writer) Write(p []byte) (int, error)
|
||||||
|
```
|
||||||
|
Write implements `io.Writer`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Writer) WriteByte
|
||||||
|
``` go
|
||||||
|
func (w *Writer) WriteByte(b byte) error
|
||||||
|
```
|
||||||
|
WriteByte implements `io.ByteWriter`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### func (\*Writer) WriteString
|
||||||
|
``` go
|
||||||
|
func (w *Writer) WriteString(s string) (int, error)
|
||||||
|
```
|
||||||
|
WriteString is analogous to Write, but it takes a string.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- - -
|
||||||
|
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)
|
379
vendor/github.com/philhofer/fwd/reader.go
generated
vendored
Normal file
379
vendor/github.com/philhofer/fwd/reader.go
generated
vendored
Normal file
|
@ -0,0 +1,379 @@
|
||||||
|
// The `fwd` package provides a buffered reader
|
||||||
|
// and writer. Each has methods that help improve
|
||||||
|
// the encoding/decoding performance of some binary
|
||||||
|
// protocols.
|
||||||
|
//
|
||||||
|
// The `fwd.Writer` and `fwd.Reader` type provide similar
|
||||||
|
// functionality to their counterparts in `bufio`, plus
|
||||||
|
// a few extra utility methods that simplify read-ahead
|
||||||
|
// and write-ahead. I wrote this package to improve serialization
|
||||||
|
// performance for http://github.com/tinylib/msgp,
|
||||||
|
// where it provided about a 2x speedup over `bufio` for certain
|
||||||
|
// workloads. However, care must be taken to understand the semantics of the
|
||||||
|
// extra methods provided by this package, as they allow
|
||||||
|
// the user to access and manipulate the buffer memory
|
||||||
|
// directly.
|
||||||
|
//
|
||||||
|
// The extra methods for `fwd.Reader` are `Peek`, `Skip`
|
||||||
|
// and `Next`. `(*fwd.Reader).Peek`, unlike `(*bufio.Reader).Peek`,
|
||||||
|
// will re-allocate the read buffer in order to accommodate arbitrarily
|
||||||
|
// large read-ahead. `(*fwd.Reader).Skip` skips the next `n` bytes
|
||||||
|
// in the stream, and uses the `io.Seeker` interface if the underlying
|
||||||
|
// stream implements it. `(*fwd.Reader).Next` returns a slice pointing
|
||||||
|
// to the next `n` bytes in the read buffer (like `Peek`), but also
|
||||||
|
// increments the read position. This allows users to process streams
|
||||||
|
// in arbitrary block sizes without having to manage appropriately-sized
|
||||||
|
// slices. Additionally, obviating the need to copy the data from the
|
||||||
|
// buffer to another location in memory can improve performance dramatically
|
||||||
|
// in CPU-bound applications.
|
||||||
|
//
|
||||||
|
// `fwd.Writer` only has one extra method, which is `(*fwd.Writer).Next`, which
|
||||||
|
// returns a slice pointing to the next `n` bytes of the writer, and increments
|
||||||
|
// the write position by the length of the returned slice. This allows users
|
||||||
|
// to write directly to the end of the buffer.
|
||||||
|
//
|
||||||
|
package fwd
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DefaultReaderSize is the default size of the read buffer
|
||||||
|
DefaultReaderSize = 2048
|
||||||
|
|
||||||
|
// minimum read buffer; straight from bufio
|
||||||
|
minReaderSize = 16
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewReader returns a new *Reader that reads from 'r'
|
||||||
|
func NewReader(r io.Reader) *Reader {
|
||||||
|
return NewReaderSize(r, DefaultReaderSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewReaderSize returns a new *Reader that
|
||||||
|
// reads from 'r' and has a buffer size 'n'
|
||||||
|
func NewReaderSize(r io.Reader, n int) *Reader {
|
||||||
|
rd := &Reader{
|
||||||
|
r: r,
|
||||||
|
data: make([]byte, 0, max(minReaderSize, n)),
|
||||||
|
}
|
||||||
|
if s, ok := r.(io.Seeker); ok {
|
||||||
|
rd.rs = s
|
||||||
|
}
|
||||||
|
return rd
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reader is a buffered look-ahead reader
|
||||||
|
type Reader struct {
|
||||||
|
r io.Reader // underlying reader
|
||||||
|
|
||||||
|
// data[n:len(data)] is buffered data; data[len(data):cap(data)] is free buffer space
|
||||||
|
data []byte // data
|
||||||
|
n int // read offset
|
||||||
|
state error // last read error
|
||||||
|
|
||||||
|
// if the reader past to NewReader was
|
||||||
|
// also an io.Seeker, this is non-nil
|
||||||
|
rs io.Seeker
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset resets the underlying reader
|
||||||
|
// and the read buffer.
|
||||||
|
func (r *Reader) Reset(rd io.Reader) {
|
||||||
|
r.r = rd
|
||||||
|
r.data = r.data[0:0]
|
||||||
|
r.n = 0
|
||||||
|
r.state = nil
|
||||||
|
if s, ok := rd.(io.Seeker); ok {
|
||||||
|
r.rs = s
|
||||||
|
} else {
|
||||||
|
r.rs = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// more() does one read on the underlying reader
|
||||||
|
func (r *Reader) more() {
|
||||||
|
// move data backwards so that
|
||||||
|
// the read offset is 0; this way
|
||||||
|
// we can supply the maximum number of
|
||||||
|
// bytes to the reader
|
||||||
|
if r.n != 0 {
|
||||||
|
if r.n < len(r.data) {
|
||||||
|
r.data = r.data[:copy(r.data[0:], r.data[r.n:])]
|
||||||
|
} else {
|
||||||
|
r.data = r.data[:0]
|
||||||
|
}
|
||||||
|
r.n = 0
|
||||||
|
}
|
||||||
|
var a int
|
||||||
|
a, r.state = r.r.Read(r.data[len(r.data):cap(r.data)])
|
||||||
|
if a == 0 && r.state == nil {
|
||||||
|
r.state = io.ErrNoProgress
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r.data = r.data[:len(r.data)+a]
|
||||||
|
}
|
||||||
|
|
||||||
|
// pop error
|
||||||
|
func (r *Reader) err() (e error) {
|
||||||
|
e, r.state = r.state, nil
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// pop error; EOF -> io.ErrUnexpectedEOF
|
||||||
|
func (r *Reader) noEOF() (e error) {
|
||||||
|
e, r.state = r.state, nil
|
||||||
|
if e == io.EOF {
|
||||||
|
e = io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// buffered bytes
|
||||||
|
func (r *Reader) buffered() int { return len(r.data) - r.n }
|
||||||
|
|
||||||
|
// Buffered returns the number of bytes currently in the buffer
|
||||||
|
func (r *Reader) Buffered() int { return len(r.data) - r.n }
|
||||||
|
|
||||||
|
// BufferSize returns the total size of the buffer
|
||||||
|
func (r *Reader) BufferSize() int { return cap(r.data) }
|
||||||
|
|
||||||
|
// Peek returns the next 'n' buffered bytes,
|
||||||
|
// reading from the underlying reader if necessary.
|
||||||
|
// It will only return a slice shorter than 'n' bytes
|
||||||
|
// if it also returns an error. Peek does not advance
|
||||||
|
// the reader. EOF errors are *not* returned as
|
||||||
|
// io.ErrUnexpectedEOF.
|
||||||
|
func (r *Reader) Peek(n int) ([]byte, error) {
|
||||||
|
// in the degenerate case,
|
||||||
|
// we may need to realloc
|
||||||
|
// (the caller asked for more
|
||||||
|
// bytes than the size of the buffer)
|
||||||
|
if cap(r.data) < n {
|
||||||
|
old := r.data[r.n:]
|
||||||
|
r.data = make([]byte, n+r.buffered())
|
||||||
|
r.data = r.data[:copy(r.data, old)]
|
||||||
|
r.n = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// keep filling until
|
||||||
|
// we hit an error or
|
||||||
|
// read enough bytes
|
||||||
|
for r.buffered() < n && r.state == nil {
|
||||||
|
r.more()
|
||||||
|
}
|
||||||
|
|
||||||
|
// we must have hit an error
|
||||||
|
if r.buffered() < n {
|
||||||
|
return r.data[r.n:], r.err()
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.data[r.n : r.n+n], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip moves the reader forward 'n' bytes.
|
||||||
|
// Returns the number of bytes skipped and any
|
||||||
|
// errors encountered. It is analogous to Seek(n, 1).
|
||||||
|
// If the underlying reader implements io.Seeker, then
|
||||||
|
// that method will be used to skip forward.
|
||||||
|
//
|
||||||
|
// If the reader encounters
|
||||||
|
// an EOF before skipping 'n' bytes, it
|
||||||
|
// returns io.ErrUnexpectedEOF. If the
|
||||||
|
// underlying reader implements io.Seeker, then
|
||||||
|
// those rules apply instead. (Many implementations
|
||||||
|
// will not return `io.EOF` until the next call
|
||||||
|
// to Read.)
|
||||||
|
func (r *Reader) Skip(n int) (int, error) {
|
||||||
|
|
||||||
|
// fast path
|
||||||
|
if r.buffered() >= n {
|
||||||
|
r.n += n
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// use seeker implementation
|
||||||
|
// if we can
|
||||||
|
if r.rs != nil {
|
||||||
|
return r.skipSeek(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// loop on filling
|
||||||
|
// and then erasing
|
||||||
|
o := n
|
||||||
|
for r.buffered() < n && r.state == nil {
|
||||||
|
r.more()
|
||||||
|
// we can skip forward
|
||||||
|
// up to r.buffered() bytes
|
||||||
|
step := min(r.buffered(), n)
|
||||||
|
r.n += step
|
||||||
|
n -= step
|
||||||
|
}
|
||||||
|
// at this point, n should be
|
||||||
|
// 0 if everything went smoothly
|
||||||
|
return o - n, r.noEOF()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next returns the next 'n' bytes in the stream.
|
||||||
|
// Unlike Peek, Next advances the reader position.
|
||||||
|
// The returned bytes point to the same
|
||||||
|
// data as the buffer, so the slice is
|
||||||
|
// only valid until the next reader method call.
|
||||||
|
// An EOF is considered an unexpected error.
|
||||||
|
// If an the returned slice is less than the
|
||||||
|
// length asked for, an error will be returned,
|
||||||
|
// and the reader position will not be incremented.
|
||||||
|
func (r *Reader) Next(n int) ([]byte, error) {
|
||||||
|
|
||||||
|
// in case the buffer is too small
|
||||||
|
if cap(r.data) < n {
|
||||||
|
old := r.data[r.n:]
|
||||||
|
r.data = make([]byte, n+r.buffered())
|
||||||
|
r.data = r.data[:copy(r.data, old)]
|
||||||
|
r.n = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// fill at least 'n' bytes
|
||||||
|
for r.buffered() < n && r.state == nil {
|
||||||
|
r.more()
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.buffered() < n {
|
||||||
|
return r.data[r.n:], r.noEOF()
|
||||||
|
}
|
||||||
|
out := r.data[r.n : r.n+n]
|
||||||
|
r.n += n
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// skipSeek uses the io.Seeker to seek forward.
|
||||||
|
// only call this function when n > r.buffered()
|
||||||
|
func (r *Reader) skipSeek(n int) (int, error) {
|
||||||
|
o := r.buffered()
|
||||||
|
// first, clear buffer
|
||||||
|
n -= o
|
||||||
|
r.n = 0
|
||||||
|
r.data = r.data[:0]
|
||||||
|
|
||||||
|
// then seek forward remaning bytes
|
||||||
|
i, err := r.rs.Seek(int64(n), 1)
|
||||||
|
return int(i) + o, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read implements `io.Reader`
|
||||||
|
func (r *Reader) Read(b []byte) (int, error) {
|
||||||
|
// if we have data in the buffer, just
|
||||||
|
// return that.
|
||||||
|
if r.buffered() != 0 {
|
||||||
|
x := copy(b, r.data[r.n:])
|
||||||
|
r.n += x
|
||||||
|
return x, nil
|
||||||
|
}
|
||||||
|
var n int
|
||||||
|
// we have no buffered data; determine
|
||||||
|
// whether or not to buffer or call
|
||||||
|
// the underlying reader directly
|
||||||
|
if len(b) >= cap(r.data) {
|
||||||
|
n, r.state = r.r.Read(b)
|
||||||
|
} else {
|
||||||
|
r.more()
|
||||||
|
n = copy(b, r.data)
|
||||||
|
r.n = n
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return 0, r.err()
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadFull attempts to read len(b) bytes into
|
||||||
|
// 'b'. It returns the number of bytes read into
|
||||||
|
// 'b', and an error if it does not return len(b).
|
||||||
|
// EOF is considered an unexpected error.
|
||||||
|
func (r *Reader) ReadFull(b []byte) (int, error) {
|
||||||
|
var n int // read into b
|
||||||
|
var nn int // scratch
|
||||||
|
l := len(b)
|
||||||
|
// either read buffered data,
|
||||||
|
// or read directly for the underlying
|
||||||
|
// buffer, or fetch more buffered data.
|
||||||
|
for n < l && r.state == nil {
|
||||||
|
if r.buffered() != 0 {
|
||||||
|
nn = copy(b[n:], r.data[r.n:])
|
||||||
|
n += nn
|
||||||
|
r.n += nn
|
||||||
|
} else if l-n > cap(r.data) {
|
||||||
|
nn, r.state = r.r.Read(b[n:])
|
||||||
|
n += nn
|
||||||
|
} else {
|
||||||
|
r.more()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if n < l {
|
||||||
|
return n, r.noEOF()
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadByte implements `io.ByteReader`
|
||||||
|
func (r *Reader) ReadByte() (byte, error) {
|
||||||
|
for r.buffered() < 1 && r.state == nil {
|
||||||
|
r.more()
|
||||||
|
}
|
||||||
|
if r.buffered() < 1 {
|
||||||
|
return 0, r.err()
|
||||||
|
}
|
||||||
|
b := r.data[r.n]
|
||||||
|
r.n++
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo implements `io.WriterTo`
|
||||||
|
func (r *Reader) WriteTo(w io.Writer) (int64, error) {
|
||||||
|
var (
|
||||||
|
i int64
|
||||||
|
ii int
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
// first, clear buffer
|
||||||
|
if r.buffered() > 0 {
|
||||||
|
ii, err = w.Write(r.data[r.n:])
|
||||||
|
i += int64(ii)
|
||||||
|
if err != nil {
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
r.data = r.data[0:0]
|
||||||
|
r.n = 0
|
||||||
|
}
|
||||||
|
for r.state == nil {
|
||||||
|
// here we just do
|
||||||
|
// 1:1 reads and writes
|
||||||
|
r.more()
|
||||||
|
if r.buffered() > 0 {
|
||||||
|
ii, err = w.Write(r.data)
|
||||||
|
i += int64(ii)
|
||||||
|
if err != nil {
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
r.data = r.data[0:0]
|
||||||
|
r.n = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if r.state != io.EOF {
|
||||||
|
return i, r.err()
|
||||||
|
}
|
||||||
|
return i, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func min(a int, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func max(a int, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
224
vendor/github.com/philhofer/fwd/writer.go
generated
vendored
Normal file
224
vendor/github.com/philhofer/fwd/writer.go
generated
vendored
Normal file
|
@ -0,0 +1,224 @@
|
||||||
|
package fwd
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DefaultWriterSize is the
|
||||||
|
// default write buffer size.
|
||||||
|
DefaultWriterSize = 2048
|
||||||
|
|
||||||
|
minWriterSize = minReaderSize
|
||||||
|
)
|
||||||
|
|
||||||
|
// Writer is a buffered writer
|
||||||
|
type Writer struct {
|
||||||
|
w io.Writer // writer
|
||||||
|
buf []byte // 0:len(buf) is bufered data
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWriter returns a new writer
|
||||||
|
// that writes to 'w' and has a buffer
|
||||||
|
// that is `DefaultWriterSize` bytes.
|
||||||
|
func NewWriter(w io.Writer) *Writer {
|
||||||
|
if wr, ok := w.(*Writer); ok {
|
||||||
|
return wr
|
||||||
|
}
|
||||||
|
return &Writer{
|
||||||
|
w: w,
|
||||||
|
buf: make([]byte, 0, DefaultWriterSize),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWriterSize returns a new writer
|
||||||
|
// that writes to 'w' and has a buffer
|
||||||
|
// that is 'size' bytes.
|
||||||
|
func NewWriterSize(w io.Writer, size int) *Writer {
|
||||||
|
if wr, ok := w.(*Writer); ok && cap(wr.buf) >= size {
|
||||||
|
return wr
|
||||||
|
}
|
||||||
|
return &Writer{
|
||||||
|
w: w,
|
||||||
|
buf: make([]byte, 0, max(size, minWriterSize)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Buffered returns the number of buffered bytes
|
||||||
|
// in the reader.
|
||||||
|
func (w *Writer) Buffered() int { return len(w.buf) }
|
||||||
|
|
||||||
|
// BufferSize returns the maximum size of the buffer.
|
||||||
|
func (w *Writer) BufferSize() int { return cap(w.buf) }
|
||||||
|
|
||||||
|
// Flush flushes any buffered bytes
|
||||||
|
// to the underlying writer.
|
||||||
|
func (w *Writer) Flush() error {
|
||||||
|
l := len(w.buf)
|
||||||
|
if l > 0 {
|
||||||
|
n, err := w.w.Write(w.buf)
|
||||||
|
|
||||||
|
// if we didn't write the whole
|
||||||
|
// thing, copy the unwritten
|
||||||
|
// bytes to the beginnning of the
|
||||||
|
// buffer.
|
||||||
|
if n < l && n > 0 {
|
||||||
|
w.pushback(n)
|
||||||
|
if err == nil {
|
||||||
|
err = io.ErrShortWrite
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.buf = w.buf[:0]
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write implements `io.Writer`
|
||||||
|
func (w *Writer) Write(p []byte) (int, error) {
|
||||||
|
c, l, ln := cap(w.buf), len(w.buf), len(p)
|
||||||
|
avail := c - l
|
||||||
|
|
||||||
|
// requires flush
|
||||||
|
if avail < ln {
|
||||||
|
if err := w.Flush(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
l = len(w.buf)
|
||||||
|
}
|
||||||
|
// too big to fit in buffer;
|
||||||
|
// write directly to w.w
|
||||||
|
if c < ln {
|
||||||
|
return w.w.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// grow buf slice; copy; return
|
||||||
|
w.buf = w.buf[:l+ln]
|
||||||
|
return copy(w.buf[l:], p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteString is analogous to Write, but it takes a string.
|
||||||
|
func (w *Writer) WriteString(s string) (int, error) {
|
||||||
|
c, l, ln := cap(w.buf), len(w.buf), len(s)
|
||||||
|
avail := c - l
|
||||||
|
|
||||||
|
// requires flush
|
||||||
|
if avail < ln {
|
||||||
|
if err := w.Flush(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
l = len(w.buf)
|
||||||
|
}
|
||||||
|
// too big to fit in buffer;
|
||||||
|
// write directly to w.w
|
||||||
|
//
|
||||||
|
// yes, this is unsafe. *but*
|
||||||
|
// io.Writer is not allowed
|
||||||
|
// to mutate its input or
|
||||||
|
// maintain a reference to it,
|
||||||
|
// per the spec in package io.
|
||||||
|
//
|
||||||
|
// plus, if the string is really
|
||||||
|
// too big to fit in the buffer, then
|
||||||
|
// creating a copy to write it is
|
||||||
|
// expensive (and, strictly speaking,
|
||||||
|
// unnecessary)
|
||||||
|
if c < ln {
|
||||||
|
return w.w.Write(unsafestr(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
// grow buf slice; copy; return
|
||||||
|
w.buf = w.buf[:l+ln]
|
||||||
|
return copy(w.buf[l:], s), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteByte implements `io.ByteWriter`
|
||||||
|
func (w *Writer) WriteByte(b byte) error {
|
||||||
|
if len(w.buf) == cap(w.buf) {
|
||||||
|
if err := w.Flush(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.buf = append(w.buf, b)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next returns the next 'n' free bytes
|
||||||
|
// in the write buffer, flushing the writer
|
||||||
|
// as necessary. Next will return `io.ErrShortBuffer`
|
||||||
|
// if 'n' is greater than the size of the write buffer.
|
||||||
|
// Calls to 'next' increment the write position by
|
||||||
|
// the size of the returned buffer.
|
||||||
|
func (w *Writer) Next(n int) ([]byte, error) {
|
||||||
|
c, l := cap(w.buf), len(w.buf)
|
||||||
|
if n > c {
|
||||||
|
return nil, io.ErrShortBuffer
|
||||||
|
}
|
||||||
|
avail := c - l
|
||||||
|
if avail < n {
|
||||||
|
if err := w.Flush(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
l = len(w.buf)
|
||||||
|
}
|
||||||
|
w.buf = w.buf[:l+n]
|
||||||
|
return w.buf[l:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// take the bytes from w.buf[n:len(w.buf)]
|
||||||
|
// and put them at the beginning of w.buf,
|
||||||
|
// and resize to the length of the copied segment.
|
||||||
|
func (w *Writer) pushback(n int) {
|
||||||
|
w.buf = w.buf[:copy(w.buf, w.buf[n:])]
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadFrom implements `io.ReaderFrom`
|
||||||
|
func (w *Writer) ReadFrom(r io.Reader) (int64, error) {
|
||||||
|
// anticipatory flush
|
||||||
|
if err := w.Flush(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
w.buf = w.buf[0:cap(w.buf)] // expand buffer
|
||||||
|
|
||||||
|
var nn int64 // written
|
||||||
|
var err error // error
|
||||||
|
var x int // read
|
||||||
|
|
||||||
|
// 1:1 reads and writes
|
||||||
|
for err == nil {
|
||||||
|
x, err = r.Read(w.buf)
|
||||||
|
if x > 0 {
|
||||||
|
n, werr := w.w.Write(w.buf[:x])
|
||||||
|
nn += int64(n)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if n < x && n > 0 {
|
||||||
|
w.pushback(n - x)
|
||||||
|
}
|
||||||
|
return nn, werr
|
||||||
|
}
|
||||||
|
if n < x {
|
||||||
|
w.pushback(n - x)
|
||||||
|
return nn, io.ErrShortWrite
|
||||||
|
}
|
||||||
|
} else if err == nil {
|
||||||
|
err = io.ErrNoProgress
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != io.EOF {
|
||||||
|
return nn, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// we only clear here
|
||||||
|
// because we are sure
|
||||||
|
// the writes have
|
||||||
|
// succeeded. otherwise,
|
||||||
|
// we retain the data in case
|
||||||
|
// future writes succeed.
|
||||||
|
w.buf = w.buf[0:0]
|
||||||
|
|
||||||
|
return nn, nil
|
||||||
|
}
|
5
vendor/github.com/philhofer/fwd/writer_appengine.go
generated
vendored
Normal file
5
vendor/github.com/philhofer/fwd/writer_appengine.go
generated
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
// +build appengine
|
||||||
|
|
||||||
|
package fwd
|
||||||
|
|
||||||
|
func unsafestr(s string) []byte { return []byte(s) }
|
18
vendor/github.com/philhofer/fwd/writer_unsafe.go
generated
vendored
Normal file
18
vendor/github.com/philhofer/fwd/writer_unsafe.go
generated
vendored
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
// +build !appengine
|
||||||
|
|
||||||
|
package fwd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// unsafe cast string as []byte
|
||||||
|
func unsafestr(b string) []byte {
|
||||||
|
l := len(b)
|
||||||
|
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||||
|
Len: l,
|
||||||
|
Cap: l,
|
||||||
|
Data: (*reflect.StringHeader)(unsafe.Pointer(&b)).Data,
|
||||||
|
}))
|
||||||
|
}
|
28
vendor/github.com/spf13/pflag/LICENSE
generated
vendored
Normal file
28
vendor/github.com/spf13/pflag/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
Copyright (c) 2012 Alex Ogier. All rights reserved.
|
||||||
|
Copyright (c) 2012 The Go Authors. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following disclaimer
|
||||||
|
in the documentation and/or other materials provided with the
|
||||||
|
distribution.
|
||||||
|
* Neither the name of Google Inc. nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
256
vendor/github.com/spf13/pflag/README.md
generated
vendored
Normal file
256
vendor/github.com/spf13/pflag/README.md
generated
vendored
Normal file
|
@ -0,0 +1,256 @@
|
||||||
|
[![Build Status](https://travis-ci.org/spf13/pflag.svg?branch=master)](https://travis-ci.org/spf13/pflag)
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
pflag is a drop-in replacement for Go's flag package, implementing
|
||||||
|
POSIX/GNU-style --flags.
|
||||||
|
|
||||||
|
pflag is compatible with the [GNU extensions to the POSIX recommendations
|
||||||
|
for command-line options][1]. For a more precise description, see the
|
||||||
|
"Command-line flag syntax" section below.
|
||||||
|
|
||||||
|
[1]: http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
|
||||||
|
|
||||||
|
pflag is available under the same style of BSD license as the Go language,
|
||||||
|
which can be found in the LICENSE file.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
pflag is available using the standard `go get` command.
|
||||||
|
|
||||||
|
Install by running:
|
||||||
|
|
||||||
|
go get github.com/spf13/pflag
|
||||||
|
|
||||||
|
Run tests by running:
|
||||||
|
|
||||||
|
go test github.com/spf13/pflag
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
pflag is a drop-in replacement of Go's native flag package. If you import
|
||||||
|
pflag under the name "flag" then all code should continue to function
|
||||||
|
with no changes.
|
||||||
|
|
||||||
|
``` go
|
||||||
|
import flag "github.com/spf13/pflag"
|
||||||
|
```
|
||||||
|
|
||||||
|
There is one exception to this: if you directly instantiate the Flag struct
|
||||||
|
there is one more field "Shorthand" that you will need to set.
|
||||||
|
Most code never instantiates this struct directly, and instead uses
|
||||||
|
functions such as String(), BoolVar(), and Var(), and is therefore
|
||||||
|
unaffected.
|
||||||
|
|
||||||
|
Define flags using flag.String(), Bool(), Int(), etc.
|
||||||
|
|
||||||
|
This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
|
||||||
|
|
||||||
|
``` go
|
||||||
|
var ip *int = flag.Int("flagname", 1234, "help message for flagname")
|
||||||
|
```
|
||||||
|
|
||||||
|
If you like, you can bind the flag to a variable using the Var() functions.
|
||||||
|
|
||||||
|
``` go
|
||||||
|
var flagvar int
|
||||||
|
func init() {
|
||||||
|
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Or you can create custom flags that satisfy the Value interface (with
|
||||||
|
pointer receivers) and couple them to flag parsing by
|
||||||
|
|
||||||
|
``` go
|
||||||
|
flag.Var(&flagVal, "name", "help message for flagname")
|
||||||
|
```
|
||||||
|
|
||||||
|
For such flags, the default value is just the initial value of the variable.
|
||||||
|
|
||||||
|
After all flags are defined, call
|
||||||
|
|
||||||
|
``` go
|
||||||
|
flag.Parse()
|
||||||
|
```
|
||||||
|
|
||||||
|
to parse the command line into the defined flags.
|
||||||
|
|
||||||
|
Flags may then be used directly. If you're using the flags themselves,
|
||||||
|
they are all pointers; if you bind to variables, they're values.
|
||||||
|
|
||||||
|
``` go
|
||||||
|
fmt.Println("ip has value ", *ip)
|
||||||
|
fmt.Println("flagvar has value ", flagvar)
|
||||||
|
```
|
||||||
|
|
||||||
|
There are helpers function to get values later if you have the FlagSet but
|
||||||
|
it was difficult to keep up with all of the flag pointers in your code.
|
||||||
|
If you have a pflag.FlagSet with a flag called 'flagname' of type int you
|
||||||
|
can use GetInt() to get the int value. But notice that 'flagname' must exist
|
||||||
|
and it must be an int. GetString("flagname") will fail.
|
||||||
|
|
||||||
|
``` go
|
||||||
|
i, err := flagset.GetInt("flagname")
|
||||||
|
```
|
||||||
|
|
||||||
|
After parsing, the arguments after the flag are available as the
|
||||||
|
slice flag.Args() or individually as flag.Arg(i).
|
||||||
|
The arguments are indexed from 0 through flag.NArg()-1.
|
||||||
|
|
||||||
|
The pflag package also defines some new functions that are not in flag,
|
||||||
|
that give one-letter shorthands for flags. You can use these by appending
|
||||||
|
'P' to the name of any function that defines a flag.
|
||||||
|
|
||||||
|
``` go
|
||||||
|
var ip = flag.IntP("flagname", "f", 1234, "help message")
|
||||||
|
var flagvar bool
|
||||||
|
func init() {
|
||||||
|
flag.BoolVarP("boolname", "b", true, "help message")
|
||||||
|
}
|
||||||
|
flag.VarP(&flagVar, "varname", "v", 1234, "help message")
|
||||||
|
```
|
||||||
|
|
||||||
|
Shorthand letters can be used with single dashes on the command line.
|
||||||
|
Boolean shorthand flags can be combined with other shorthand flags.
|
||||||
|
|
||||||
|
The default set of command-line flags is controlled by
|
||||||
|
top-level functions. The FlagSet type allows one to define
|
||||||
|
independent sets of flags, such as to implement subcommands
|
||||||
|
in a command-line interface. The methods of FlagSet are
|
||||||
|
analogous to the top-level functions for the command-line
|
||||||
|
flag set.
|
||||||
|
|
||||||
|
## Setting no option default values for flags
|
||||||
|
|
||||||
|
After you create a flag it is possible to set the pflag.NoOptDefVal for
|
||||||
|
the given flag. Doing this changes the meaning of the flag slightly. If
|
||||||
|
a flag has a NoOptDefVal and the flag is set on the command line without
|
||||||
|
an option the flag will be set to the NoOptDefVal. For example given:
|
||||||
|
|
||||||
|
``` go
|
||||||
|
var ip = flag.IntP("flagname", "f", 1234, "help message")
|
||||||
|
flag.Lookup("flagname").NoOptDefVal = "4321"
|
||||||
|
```
|
||||||
|
|
||||||
|
Would result in something like
|
||||||
|
|
||||||
|
| Parsed Arguments | Resulting Value |
|
||||||
|
| ------------- | ------------- |
|
||||||
|
| --flagname=1357 | ip=1357 |
|
||||||
|
| --flagname | ip=4321 |
|
||||||
|
| [nothing] | ip=1234 |
|
||||||
|
|
||||||
|
## Command line flag syntax
|
||||||
|
|
||||||
|
```
|
||||||
|
--flag // boolean flags, or flags with no option default values
|
||||||
|
--flag x // only on flags without a default value
|
||||||
|
--flag=x
|
||||||
|
```
|
||||||
|
|
||||||
|
Unlike the flag package, a single dash before an option means something
|
||||||
|
different than a double dash. Single dashes signify a series of shorthand
|
||||||
|
letters for flags. All but the last shorthand letter must be boolean flags
|
||||||
|
or a flag with a default value
|
||||||
|
|
||||||
|
```
|
||||||
|
// boolean or flags where the 'no option default value' is set
|
||||||
|
-f
|
||||||
|
-f=true
|
||||||
|
-abc
|
||||||
|
but
|
||||||
|
-b true is INVALID
|
||||||
|
|
||||||
|
// non-boolean and flags without a 'no option default value'
|
||||||
|
-n 1234
|
||||||
|
-n=1234
|
||||||
|
-n1234
|
||||||
|
|
||||||
|
// mixed
|
||||||
|
-abcs "hello"
|
||||||
|
-absd="hello"
|
||||||
|
-abcs1234
|
||||||
|
```
|
||||||
|
|
||||||
|
Flag parsing stops after the terminator "--". Unlike the flag package,
|
||||||
|
flags can be interspersed with arguments anywhere on the command line
|
||||||
|
before this terminator.
|
||||||
|
|
||||||
|
Integer flags accept 1234, 0664, 0x1234 and may be negative.
|
||||||
|
Boolean flags (in their long form) accept 1, 0, t, f, true, false,
|
||||||
|
TRUE, FALSE, True, False.
|
||||||
|
Duration flags accept any input valid for time.ParseDuration.
|
||||||
|
|
||||||
|
## Mutating or "Normalizing" Flag names
|
||||||
|
|
||||||
|
It is possible to set a custom flag name 'normalization function.' It allows flag names to be mutated both when created in the code and when used on the command line to some 'normalized' form. The 'normalized' form is used for comparison. Two examples of using the custom normalization func follow.
|
||||||
|
|
||||||
|
**Example #1**: You want -, _, and . in flags to compare the same. aka --my-flag == --my_flag == --my.flag
|
||||||
|
|
||||||
|
``` go
|
||||||
|
func wordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
|
||||||
|
from := []string{"-", "_"}
|
||||||
|
to := "."
|
||||||
|
for _, sep := range from {
|
||||||
|
name = strings.Replace(name, sep, to, -1)
|
||||||
|
}
|
||||||
|
return pflag.NormalizedName(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
myFlagSet.SetNormalizeFunc(wordSepNormalizeFunc)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example #2**: You want to alias two flags. aka --old-flag-name == --new-flag-name
|
||||||
|
|
||||||
|
``` go
|
||||||
|
func aliasNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
|
||||||
|
switch name {
|
||||||
|
case "old-flag-name":
|
||||||
|
name = "new-flag-name"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return pflag.NormalizedName(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
myFlagSet.SetNormalizeFunc(aliasNormalizeFunc)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Deprecating a flag or its shorthand
|
||||||
|
It is possible to deprecate a flag, or just its shorthand. Deprecating a flag/shorthand hides it from help text and prints a usage message when the deprecated flag/shorthand is used.
|
||||||
|
|
||||||
|
**Example #1**: You want to deprecate a flag named "badflag" as well as inform the users what flag they should use instead.
|
||||||
|
```go
|
||||||
|
// deprecate a flag by specifying its name and a usage message
|
||||||
|
flags.MarkDeprecated("badflag", "please use --good-flag instead")
|
||||||
|
```
|
||||||
|
This hides "badflag" from help text, and prints `Flag --badflag has been deprecated, please use --good-flag instead` when "badflag" is used.
|
||||||
|
|
||||||
|
**Example #2**: You want to keep a flag name "noshorthandflag" but deprecate its shortname "n".
|
||||||
|
```go
|
||||||
|
// deprecate a flag shorthand by specifying its flag name and a usage message
|
||||||
|
flags.MarkShorthandDeprecated("noshorthandflag", "please use --noshorthandflag only")
|
||||||
|
```
|
||||||
|
This hides the shortname "n" from help text, and prints `Flag shorthand -n has been deprecated, please use --noshorthandflag only` when the shorthand "n" is used.
|
||||||
|
|
||||||
|
Note that usage message is essential here, and it should not be empty.
|
||||||
|
|
||||||
|
## Hidden flags
|
||||||
|
It is possible to mark a flag as hidden, meaning it will still function as normal, however will not show up in usage/help text.
|
||||||
|
|
||||||
|
**Example**: You have a flag named "secretFlag" that you need for internal use only and don't want it showing up in help text, or for its usage text to be available.
|
||||||
|
```go
|
||||||
|
// hide a flag by specifying its name
|
||||||
|
flags.MarkHidden("secretFlag")
|
||||||
|
```
|
||||||
|
|
||||||
|
## More info
|
||||||
|
|
||||||
|
You can see the full reference documentation of the pflag package
|
||||||
|
[at godoc.org][3], or through go's standard documentation system by
|
||||||
|
running `godoc -http=:6060` and browsing to
|
||||||
|
[http://localhost:6060/pkg/github.com/ogier/pflag][2] after
|
||||||
|
installation.
|
||||||
|
|
||||||
|
[2]: http://localhost:6060/pkg/github.com/ogier/pflag
|
||||||
|
[3]: http://godoc.org/github.com/ogier/pflag
|
97
vendor/github.com/spf13/pflag/bool.go
generated
vendored
Normal file
97
vendor/github.com/spf13/pflag/bool.go
generated
vendored
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
package pflag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// optional interface to indicate boolean flags that can be
|
||||||
|
// supplied without "=value" text
|
||||||
|
type boolFlag interface {
|
||||||
|
Value
|
||||||
|
IsBoolFlag() bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- bool Value
|
||||||
|
type boolValue bool
|
||||||
|
|
||||||
|
func newBoolValue(val bool, p *bool) *boolValue {
|
||||||
|
*p = val
|
||||||
|
return (*boolValue)(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *boolValue) Set(s string) error {
|
||||||
|
v, err := strconv.ParseBool(s)
|
||||||
|
*b = boolValue(v)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *boolValue) Type() string {
|
||||||
|
return "bool"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }
|
||||||
|
|
||||||
|
func (b *boolValue) IsBoolFlag() bool { return true }
|
||||||
|
|
||||||
|
func boolConv(sval string) (interface{}, error) {
|
||||||
|
return strconv.ParseBool(sval)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBool return the bool value of a flag with the given name
|
||||||
|
func (f *FlagSet) GetBool(name string) (bool, error) {
|
||||||
|
val, err := f.getFlagType(name, "bool", boolConv)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return val.(bool), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to a bool variable in which to store the value of the flag.
|
||||||
|
func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
|
||||||
|
f.BoolVarP(p, name, "", value, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
|
||||||
|
flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage)
|
||||||
|
flag.NoOptDefVal = "true"
|
||||||
|
}
|
||||||
|
|
||||||
|
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to a bool variable in which to store the value of the flag.
|
||||||
|
func BoolVar(p *bool, name string, value bool, usage string) {
|
||||||
|
BoolVarP(p, name, "", value, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
|
||||||
|
flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage)
|
||||||
|
flag.NoOptDefVal = "true"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bool defines a bool flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of a bool variable that stores the value of the flag.
|
||||||
|
func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
|
||||||
|
return f.BoolP(name, "", value, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) BoolP(name, shorthand string, value bool, usage string) *bool {
|
||||||
|
p := new(bool)
|
||||||
|
f.BoolVarP(p, name, shorthand, value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bool defines a bool flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of a bool variable that stores the value of the flag.
|
||||||
|
func Bool(name string, value bool, usage string) *bool {
|
||||||
|
return BoolP(name, "", value, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func BoolP(name, shorthand string, value bool, usage string) *bool {
|
||||||
|
b := CommandLine.BoolP(name, shorthand, value, usage)
|
||||||
|
return b
|
||||||
|
}
|
97
vendor/github.com/spf13/pflag/count.go
generated
vendored
Normal file
97
vendor/github.com/spf13/pflag/count.go
generated
vendored
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
package pflag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// -- count Value
|
||||||
|
type countValue int
|
||||||
|
|
||||||
|
func newCountValue(val int, p *int) *countValue {
|
||||||
|
*p = val
|
||||||
|
return (*countValue)(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *countValue) Set(s string) error {
|
||||||
|
v, err := strconv.ParseInt(s, 0, 64)
|
||||||
|
// -1 means that no specific value was passed, so increment
|
||||||
|
if v == -1 {
|
||||||
|
*i = countValue(*i + 1)
|
||||||
|
} else {
|
||||||
|
*i = countValue(v)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *countValue) Type() string {
|
||||||
|
return "count"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *countValue) String() string { return fmt.Sprintf("%v", *i) }
|
||||||
|
|
||||||
|
func countConv(sval string) (interface{}, error) {
|
||||||
|
i, err := strconv.Atoi(sval)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return i, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCount return the int value of a flag with the given name
|
||||||
|
func (f *FlagSet) GetCount(name string) (int, error) {
|
||||||
|
val, err := f.getFlagType(name, "count", countConv)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return val.(int), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountVar defines a count flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to an int variable in which to store the value of the flag.
|
||||||
|
// A count flag will add 1 to its value evey time it is found on the command line
|
||||||
|
func (f *FlagSet) CountVar(p *int, name string, usage string) {
|
||||||
|
f.CountVarP(p, name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountVarP is like CountVar only take a shorthand for the flag name.
|
||||||
|
func (f *FlagSet) CountVarP(p *int, name, shorthand string, usage string) {
|
||||||
|
flag := f.VarPF(newCountValue(0, p), name, shorthand, usage)
|
||||||
|
flag.NoOptDefVal = "-1"
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountVar like CountVar only the flag is placed on the CommandLine instead of a given flag set
|
||||||
|
func CountVar(p *int, name string, usage string) {
|
||||||
|
CommandLine.CountVar(p, name, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountVarP is like CountVar only take a shorthand for the flag name.
|
||||||
|
func CountVarP(p *int, name, shorthand string, usage string) {
|
||||||
|
CommandLine.CountVarP(p, name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count defines a count flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of an int variable that stores the value of the flag.
|
||||||
|
// A count flag will add 1 to its value evey time it is found on the command line
|
||||||
|
func (f *FlagSet) Count(name string, usage string) *int {
|
||||||
|
p := new(int)
|
||||||
|
f.CountVarP(p, name, "", usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountP is like Count only takes a shorthand for the flag name.
|
||||||
|
func (f *FlagSet) CountP(name, shorthand string, usage string) *int {
|
||||||
|
p := new(int)
|
||||||
|
f.CountVarP(p, name, shorthand, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count like Count only the flag is placed on the CommandLine isntead of a given flag set
|
||||||
|
func Count(name string, usage string) *int {
|
||||||
|
return CommandLine.CountP(name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountP is like Count only takes a shorthand for the flag name.
|
||||||
|
func CountP(name, shorthand string, usage string) *int {
|
||||||
|
return CommandLine.CountP(name, shorthand, usage)
|
||||||
|
}
|
86
vendor/github.com/spf13/pflag/duration.go
generated
vendored
Normal file
86
vendor/github.com/spf13/pflag/duration.go
generated
vendored
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
package pflag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// -- time.Duration Value
|
||||||
|
type durationValue time.Duration
|
||||||
|
|
||||||
|
func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
|
||||||
|
*p = val
|
||||||
|
return (*durationValue)(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *durationValue) Set(s string) error {
|
||||||
|
v, err := time.ParseDuration(s)
|
||||||
|
*d = durationValue(v)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *durationValue) Type() string {
|
||||||
|
return "duration"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *durationValue) String() string { return (*time.Duration)(d).String() }
|
||||||
|
|
||||||
|
func durationConv(sval string) (interface{}, error) {
|
||||||
|
return time.ParseDuration(sval)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDuration return the duration value of a flag with the given name
|
||||||
|
func (f *FlagSet) GetDuration(name string) (time.Duration, error) {
|
||||||
|
val, err := f.getFlagType(name, "duration", durationConv)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return val.(time.Duration), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to a time.Duration variable in which to store the value of the flag.
|
||||||
|
func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
|
||||||
|
f.VarP(newDurationValue(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) {
|
||||||
|
f.VarP(newDurationValue(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to a time.Duration variable in which to store the value of the flag.
|
||||||
|
func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
|
||||||
|
CommandLine.VarP(newDurationValue(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) {
|
||||||
|
CommandLine.VarP(newDurationValue(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Duration defines a time.Duration flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of a time.Duration variable that stores the value of the flag.
|
||||||
|
func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {
|
||||||
|
p := new(time.Duration)
|
||||||
|
f.DurationVarP(p, name, "", value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration {
|
||||||
|
p := new(time.Duration)
|
||||||
|
f.DurationVarP(p, name, shorthand, value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Duration defines a time.Duration flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of a time.Duration variable that stores the value of the flag.
|
||||||
|
func Duration(name string, value time.Duration, usage string) *time.Duration {
|
||||||
|
return CommandLine.DurationP(name, "", value, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration {
|
||||||
|
return CommandLine.DurationP(name, shorthand, value, usage)
|
||||||
|
}
|
934
vendor/github.com/spf13/pflag/flag.go
generated
vendored
Normal file
934
vendor/github.com/spf13/pflag/flag.go
generated
vendored
Normal file
|
@ -0,0 +1,934 @@
|
||||||
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package pflag is a drop-in replacement for Go's flag package, implementing
|
||||||
|
POSIX/GNU-style --flags.
|
||||||
|
|
||||||
|
pflag is compatible with the GNU extensions to the POSIX recommendations
|
||||||
|
for command-line options. See
|
||||||
|
http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
pflag is a drop-in replacement of Go's native flag package. If you import
|
||||||
|
pflag under the name "flag" then all code should continue to function
|
||||||
|
with no changes.
|
||||||
|
|
||||||
|
import flag "github.com/ogier/pflag"
|
||||||
|
|
||||||
|
There is one exception to this: if you directly instantiate the Flag struct
|
||||||
|
there is one more field "Shorthand" that you will need to set.
|
||||||
|
Most code never instantiates this struct directly, and instead uses
|
||||||
|
functions such as String(), BoolVar(), and Var(), and is therefore
|
||||||
|
unaffected.
|
||||||
|
|
||||||
|
Define flags using flag.String(), Bool(), Int(), etc.
|
||||||
|
|
||||||
|
This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
|
||||||
|
var ip = flag.Int("flagname", 1234, "help message for flagname")
|
||||||
|
If you like, you can bind the flag to a variable using the Var() functions.
|
||||||
|
var flagvar int
|
||||||
|
func init() {
|
||||||
|
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
|
||||||
|
}
|
||||||
|
Or you can create custom flags that satisfy the Value interface (with
|
||||||
|
pointer receivers) and couple them to flag parsing by
|
||||||
|
flag.Var(&flagVal, "name", "help message for flagname")
|
||||||
|
For such flags, the default value is just the initial value of the variable.
|
||||||
|
|
||||||
|
After all flags are defined, call
|
||||||
|
flag.Parse()
|
||||||
|
to parse the command line into the defined flags.
|
||||||
|
|
||||||
|
Flags may then be used directly. If you're using the flags themselves,
|
||||||
|
they are all pointers; if you bind to variables, they're values.
|
||||||
|
fmt.Println("ip has value ", *ip)
|
||||||
|
fmt.Println("flagvar has value ", flagvar)
|
||||||
|
|
||||||
|
After parsing, the arguments after the flag are available as the
|
||||||
|
slice flag.Args() or individually as flag.Arg(i).
|
||||||
|
The arguments are indexed from 0 through flag.NArg()-1.
|
||||||
|
|
||||||
|
The pflag package also defines some new functions that are not in flag,
|
||||||
|
that give one-letter shorthands for flags. You can use these by appending
|
||||||
|
'P' to the name of any function that defines a flag.
|
||||||
|
var ip = flag.IntP("flagname", "f", 1234, "help message")
|
||||||
|
var flagvar bool
|
||||||
|
func init() {
|
||||||
|
flag.BoolVarP("boolname", "b", true, "help message")
|
||||||
|
}
|
||||||
|
flag.VarP(&flagVar, "varname", "v", 1234, "help message")
|
||||||
|
Shorthand letters can be used with single dashes on the command line.
|
||||||
|
Boolean shorthand flags can be combined with other shorthand flags.
|
||||||
|
|
||||||
|
Command line flag syntax:
|
||||||
|
--flag // boolean flags only
|
||||||
|
--flag=x
|
||||||
|
|
||||||
|
Unlike the flag package, a single dash before an option means something
|
||||||
|
different than a double dash. Single dashes signify a series of shorthand
|
||||||
|
letters for flags. All but the last shorthand letter must be boolean flags.
|
||||||
|
// boolean flags
|
||||||
|
-f
|
||||||
|
-abc
|
||||||
|
// non-boolean flags
|
||||||
|
-n 1234
|
||||||
|
-Ifile
|
||||||
|
// mixed
|
||||||
|
-abcs "hello"
|
||||||
|
-abcn1234
|
||||||
|
|
||||||
|
Flag parsing stops after the terminator "--". Unlike the flag package,
|
||||||
|
flags can be interspersed with arguments anywhere on the command line
|
||||||
|
before this terminator.
|
||||||
|
|
||||||
|
Integer flags accept 1234, 0664, 0x1234 and may be negative.
|
||||||
|
Boolean flags (in their long form) accept 1, 0, t, f, true, false,
|
||||||
|
TRUE, FALSE, True, False.
|
||||||
|
Duration flags accept any input valid for time.ParseDuration.
|
||||||
|
|
||||||
|
The default set of command-line flags is controlled by
|
||||||
|
top-level functions. The FlagSet type allows one to define
|
||||||
|
independent sets of flags, such as to implement subcommands
|
||||||
|
in a command-line interface. The methods of FlagSet are
|
||||||
|
analogous to the top-level functions for the command-line
|
||||||
|
flag set.
|
||||||
|
*/
|
||||||
|
package pflag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.
|
||||||
|
var ErrHelp = errors.New("pflag: help requested")
|
||||||
|
|
||||||
|
// ErrorHandling defines how to handle flag parsing errors.
|
||||||
|
type ErrorHandling int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ContinueOnError will return an err from Parse() if an error is found
|
||||||
|
ContinueOnError ErrorHandling = iota
|
||||||
|
// ExitOnError will call os.Exit(2) if an error is found when parsing
|
||||||
|
ExitOnError
|
||||||
|
// PanicOnError will panic() if an error is found when parsing flags
|
||||||
|
PanicOnError
|
||||||
|
)
|
||||||
|
|
||||||
|
// NormalizedName is a flag name that has been normalized according to rules
|
||||||
|
// for the FlagSet (e.g. making '-' and '_' equivalent).
|
||||||
|
type NormalizedName string
|
||||||
|
|
||||||
|
// A FlagSet represents a set of defined flags.
|
||||||
|
type FlagSet struct {
|
||||||
|
// Usage is the function called when an error occurs while parsing flags.
|
||||||
|
// The field is a function (not a method) that may be changed to point to
|
||||||
|
// a custom error handler.
|
||||||
|
Usage func()
|
||||||
|
|
||||||
|
name string
|
||||||
|
parsed bool
|
||||||
|
actual map[NormalizedName]*Flag
|
||||||
|
formal map[NormalizedName]*Flag
|
||||||
|
shorthands map[byte]*Flag
|
||||||
|
args []string // arguments after flags
|
||||||
|
argsLenAtDash int // len(args) when a '--' was located when parsing, or -1 if no --
|
||||||
|
exitOnError bool // does the program exit if there's an error?
|
||||||
|
errorHandling ErrorHandling
|
||||||
|
output io.Writer // nil means stderr; use out() accessor
|
||||||
|
interspersed bool // allow interspersed option/non-option args
|
||||||
|
normalizeNameFunc func(f *FlagSet, name string) NormalizedName
|
||||||
|
}
|
||||||
|
|
||||||
|
// A Flag represents the state of a flag.
|
||||||
|
type Flag struct {
|
||||||
|
Name string // name as it appears on command line
|
||||||
|
Shorthand string // one-letter abbreviated flag
|
||||||
|
Usage string // help message
|
||||||
|
Value Value // value as set
|
||||||
|
DefValue string // default value (as text); for usage message
|
||||||
|
Changed bool // If the user set the value (or if left to default)
|
||||||
|
NoOptDefVal string //default value (as text); if the flag is on the command line without any options
|
||||||
|
Deprecated string // If this flag is deprecated, this string is the new or now thing to use
|
||||||
|
Hidden bool // used by cobra.Command to allow flags to be hidden from help/usage text
|
||||||
|
ShorthandDeprecated string // If the shorthand of this flag is deprecated, this string is the new or now thing to use
|
||||||
|
Annotations map[string][]string // used by cobra.Command bash autocomple code
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value is the interface to the dynamic value stored in a flag.
|
||||||
|
// (The default value is represented as a string.)
|
||||||
|
type Value interface {
|
||||||
|
String() string
|
||||||
|
Set(string) error
|
||||||
|
Type() string
|
||||||
|
}
|
||||||
|
|
||||||
|
// sortFlags returns the flags as a slice in lexicographical sorted order.
|
||||||
|
func sortFlags(flags map[NormalizedName]*Flag) []*Flag {
|
||||||
|
list := make(sort.StringSlice, len(flags))
|
||||||
|
i := 0
|
||||||
|
for k := range flags {
|
||||||
|
list[i] = string(k)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
list.Sort()
|
||||||
|
result := make([]*Flag, len(list))
|
||||||
|
for i, name := range list {
|
||||||
|
result[i] = flags[NormalizedName(name)]
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetNormalizeFunc allows you to add a function which can translate flag names.
|
||||||
|
// Flags added to the FlagSet will be translated and then when anything tries to
|
||||||
|
// look up the flag that will also be translated. So it would be possible to create
|
||||||
|
// a flag named "getURL" and have it translated to "geturl". A user could then pass
|
||||||
|
// "--getUrl" which may also be translated to "geturl" and everything will work.
|
||||||
|
func (f *FlagSet) SetNormalizeFunc(n func(f *FlagSet, name string) NormalizedName) {
|
||||||
|
f.normalizeNameFunc = n
|
||||||
|
for k, v := range f.formal {
|
||||||
|
delete(f.formal, k)
|
||||||
|
nname := f.normalizeFlagName(string(k))
|
||||||
|
f.formal[nname] = v
|
||||||
|
v.Name = string(nname)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNormalizeFunc returns the previously set NormalizeFunc of a function which
|
||||||
|
// does no translation, if not set previously.
|
||||||
|
func (f *FlagSet) GetNormalizeFunc() func(f *FlagSet, name string) NormalizedName {
|
||||||
|
if f.normalizeNameFunc != nil {
|
||||||
|
return f.normalizeNameFunc
|
||||||
|
}
|
||||||
|
return func(f *FlagSet, name string) NormalizedName { return NormalizedName(name) }
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FlagSet) normalizeFlagName(name string) NormalizedName {
|
||||||
|
n := f.GetNormalizeFunc()
|
||||||
|
return n(f, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FlagSet) out() io.Writer {
|
||||||
|
if f.output == nil {
|
||||||
|
return os.Stderr
|
||||||
|
}
|
||||||
|
return f.output
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetOutput sets the destination for usage and error messages.
|
||||||
|
// If output is nil, os.Stderr is used.
|
||||||
|
func (f *FlagSet) SetOutput(output io.Writer) {
|
||||||
|
f.output = output
|
||||||
|
}
|
||||||
|
|
||||||
|
// VisitAll visits the flags in lexicographical order, calling fn for each.
|
||||||
|
// It visits all flags, even those not set.
|
||||||
|
func (f *FlagSet) VisitAll(fn func(*Flag)) {
|
||||||
|
for _, flag := range sortFlags(f.formal) {
|
||||||
|
fn(flag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasFlags returns a bool to indicate if the FlagSet has any flags definied.
|
||||||
|
func (f *FlagSet) HasFlags() bool {
|
||||||
|
return len(f.formal) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasAvailableFlags returns a bool to indicate if the FlagSet has any flags
|
||||||
|
// definied that are not hidden or deprecated.
|
||||||
|
func (f *FlagSet) HasAvailableFlags() bool {
|
||||||
|
for _, flag := range f.formal {
|
||||||
|
if !flag.Hidden && len(flag.Deprecated) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// VisitAll visits the command-line flags in lexicographical order, calling
|
||||||
|
// fn for each. It visits all flags, even those not set.
|
||||||
|
func VisitAll(fn func(*Flag)) {
|
||||||
|
CommandLine.VisitAll(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Visit visits the flags in lexicographical order, calling fn for each.
|
||||||
|
// It visits only those flags that have been set.
|
||||||
|
func (f *FlagSet) Visit(fn func(*Flag)) {
|
||||||
|
for _, flag := range sortFlags(f.actual) {
|
||||||
|
fn(flag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Visit visits the command-line flags in lexicographical order, calling fn
|
||||||
|
// for each. It visits only those flags that have been set.
|
||||||
|
func Visit(fn func(*Flag)) {
|
||||||
|
CommandLine.Visit(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lookup returns the Flag structure of the named flag, returning nil if none exists.
|
||||||
|
func (f *FlagSet) Lookup(name string) *Flag {
|
||||||
|
return f.lookup(f.normalizeFlagName(name))
|
||||||
|
}
|
||||||
|
|
||||||
|
// lookup returns the Flag structure of the named flag, returning nil if none exists.
|
||||||
|
func (f *FlagSet) lookup(name NormalizedName) *Flag {
|
||||||
|
return f.formal[name]
|
||||||
|
}
|
||||||
|
|
||||||
|
// func to return a given type for a given flag name
|
||||||
|
func (f *FlagSet) getFlagType(name string, ftype string, convFunc func(sval string) (interface{}, error)) (interface{}, error) {
|
||||||
|
flag := f.Lookup(name)
|
||||||
|
if flag == nil {
|
||||||
|
err := fmt.Errorf("flag accessed but not defined: %s", name)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if flag.Value.Type() != ftype {
|
||||||
|
err := fmt.Errorf("trying to get %s value of flag of type %s", ftype, flag.Value.Type())
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sval := flag.Value.String()
|
||||||
|
result, err := convFunc(sval)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ArgsLenAtDash will return the length of f.Args at the moment when a -- was
|
||||||
|
// found during arg parsing. This allows your program to know which args were
|
||||||
|
// before the -- and which came after.
|
||||||
|
func (f *FlagSet) ArgsLenAtDash() int {
|
||||||
|
return f.argsLenAtDash
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkDeprecated indicated that a flag is deprecated in your program. It will
|
||||||
|
// continue to function but will not show up in help or usage messages. Using
|
||||||
|
// this flag will also print the given usageMessage.
|
||||||
|
func (f *FlagSet) MarkDeprecated(name string, usageMessage string) error {
|
||||||
|
flag := f.Lookup(name)
|
||||||
|
if flag == nil {
|
||||||
|
return fmt.Errorf("flag %q does not exist", name)
|
||||||
|
}
|
||||||
|
if len(usageMessage) == 0 {
|
||||||
|
return fmt.Errorf("deprecated message for flag %q must be set", name)
|
||||||
|
}
|
||||||
|
flag.Deprecated = usageMessage
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkShorthandDeprecated will mark the shorthand of a flag deprecated in your
|
||||||
|
// program. It will continue to function but will not show up in help or usage
|
||||||
|
// messages. Using this flag will also print the given usageMessage.
|
||||||
|
func (f *FlagSet) MarkShorthandDeprecated(name string, usageMessage string) error {
|
||||||
|
flag := f.Lookup(name)
|
||||||
|
if flag == nil {
|
||||||
|
return fmt.Errorf("flag %q does not exist", name)
|
||||||
|
}
|
||||||
|
if len(usageMessage) == 0 {
|
||||||
|
return fmt.Errorf("deprecated message for flag %q must be set", name)
|
||||||
|
}
|
||||||
|
flag.ShorthandDeprecated = usageMessage
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkHidden sets a flag to 'hidden' in your program. It will continue to
|
||||||
|
// function but will not show up in help or usage messages.
|
||||||
|
func (f *FlagSet) MarkHidden(name string) error {
|
||||||
|
flag := f.Lookup(name)
|
||||||
|
if flag == nil {
|
||||||
|
return fmt.Errorf("flag %q does not exist", name)
|
||||||
|
}
|
||||||
|
flag.Hidden = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lookup returns the Flag structure of the named command-line flag,
|
||||||
|
// returning nil if none exists.
|
||||||
|
func Lookup(name string) *Flag {
|
||||||
|
return CommandLine.Lookup(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set sets the value of the named flag.
|
||||||
|
func (f *FlagSet) Set(name, value string) error {
|
||||||
|
normalName := f.normalizeFlagName(name)
|
||||||
|
flag, ok := f.formal[normalName]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("no such flag -%v", name)
|
||||||
|
}
|
||||||
|
err := flag.Value.Set(value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if f.actual == nil {
|
||||||
|
f.actual = make(map[NormalizedName]*Flag)
|
||||||
|
}
|
||||||
|
f.actual[normalName] = flag
|
||||||
|
flag.Changed = true
|
||||||
|
if len(flag.Deprecated) > 0 {
|
||||||
|
fmt.Fprintf(os.Stderr, "Flag --%s has been deprecated, %s\n", flag.Name, flag.Deprecated)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAnnotation allows one to set arbitrary annotations on a flag in the FlagSet.
|
||||||
|
// This is sometimes used by spf13/cobra programs which want to generate additional
|
||||||
|
// bash completion information.
|
||||||
|
func (f *FlagSet) SetAnnotation(name, key string, values []string) error {
|
||||||
|
normalName := f.normalizeFlagName(name)
|
||||||
|
flag, ok := f.formal[normalName]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("no such flag -%v", name)
|
||||||
|
}
|
||||||
|
if flag.Annotations == nil {
|
||||||
|
flag.Annotations = map[string][]string{}
|
||||||
|
}
|
||||||
|
flag.Annotations[key] = values
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Changed returns true if the flag was explicitly set during Parse() and false
|
||||||
|
// otherwise
|
||||||
|
func (f *FlagSet) Changed(name string) bool {
|
||||||
|
flag := f.Lookup(name)
|
||||||
|
// If a flag doesn't exist, it wasn't changed....
|
||||||
|
if flag == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return flag.Changed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set sets the value of the named command-line flag.
|
||||||
|
func Set(name, value string) error {
|
||||||
|
return CommandLine.Set(name, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrintDefaults prints, to standard error unless configured
|
||||||
|
// otherwise, the default values of all defined flags in the set.
|
||||||
|
func (f *FlagSet) PrintDefaults() {
|
||||||
|
usages := f.FlagUsages()
|
||||||
|
fmt.Fprintf(f.out(), "%s", usages)
|
||||||
|
}
|
||||||
|
|
||||||
|
// isZeroValue guesses whether the string represents the zero
|
||||||
|
// value for a flag. It is not accurate but in practice works OK.
|
||||||
|
func isZeroValue(value string) bool {
|
||||||
|
switch value {
|
||||||
|
case "false":
|
||||||
|
return true
|
||||||
|
case "<nil>":
|
||||||
|
return true
|
||||||
|
case "":
|
||||||
|
return true
|
||||||
|
case "0":
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnquoteUsage extracts a back-quoted name from the usage
|
||||||
|
// string for a flag and returns it and the un-quoted usage.
|
||||||
|
// Given "a `name` to show" it returns ("name", "a name to show").
|
||||||
|
// If there are no back quotes, the name is an educated guess of the
|
||||||
|
// type of the flag's value, or the empty string if the flag is boolean.
|
||||||
|
func UnquoteUsage(flag *Flag) (name string, usage string) {
|
||||||
|
// Look for a back-quoted name, but avoid the strings package.
|
||||||
|
usage = flag.Usage
|
||||||
|
for i := 0; i < len(usage); i++ {
|
||||||
|
if usage[i] == '`' {
|
||||||
|
for j := i + 1; j < len(usage); j++ {
|
||||||
|
if usage[j] == '`' {
|
||||||
|
name = usage[i+1 : j]
|
||||||
|
usage = usage[:i] + name + usage[j+1:]
|
||||||
|
return name, usage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break // Only one back quote; use type name.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// No explicit name, so use type if we can find one.
|
||||||
|
name = "value"
|
||||||
|
switch flag.Value.(type) {
|
||||||
|
case boolFlag:
|
||||||
|
name = ""
|
||||||
|
case *durationValue:
|
||||||
|
name = "duration"
|
||||||
|
case *float64Value:
|
||||||
|
name = "float"
|
||||||
|
case *intValue, *int64Value:
|
||||||
|
name = "int"
|
||||||
|
case *stringValue:
|
||||||
|
name = "string"
|
||||||
|
case *uintValue, *uint64Value:
|
||||||
|
name = "uint"
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// FlagUsages Returns a string containing the usage information for all flags in
|
||||||
|
// the FlagSet
|
||||||
|
func (f *FlagSet) FlagUsages() string {
|
||||||
|
x := new(bytes.Buffer)
|
||||||
|
|
||||||
|
lines := make([]string, 0, len(f.formal))
|
||||||
|
|
||||||
|
maxlen := 0
|
||||||
|
f.VisitAll(func(flag *Flag) {
|
||||||
|
if len(flag.Deprecated) > 0 || flag.Hidden {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
line := ""
|
||||||
|
if len(flag.Shorthand) > 0 && len(flag.ShorthandDeprecated) == 0 {
|
||||||
|
line = fmt.Sprintf(" -%s, --%s", flag.Shorthand, flag.Name)
|
||||||
|
} else {
|
||||||
|
line = fmt.Sprintf(" --%s", flag.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
varname, usage := UnquoteUsage(flag)
|
||||||
|
if len(varname) > 0 {
|
||||||
|
line += " " + varname
|
||||||
|
}
|
||||||
|
if len(flag.NoOptDefVal) > 0 {
|
||||||
|
switch flag.Value.Type() {
|
||||||
|
case "string":
|
||||||
|
line += fmt.Sprintf("[=%q]", flag.NoOptDefVal)
|
||||||
|
case "bool":
|
||||||
|
if flag.NoOptDefVal != "true" {
|
||||||
|
line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This special character will be replaced with spacing once the
|
||||||
|
// correct alignment is calculated
|
||||||
|
line += "\x00"
|
||||||
|
if len(line) > maxlen {
|
||||||
|
maxlen = len(line)
|
||||||
|
}
|
||||||
|
|
||||||
|
line += usage
|
||||||
|
if !isZeroValue(flag.DefValue) {
|
||||||
|
if flag.Value.Type() == "string" {
|
||||||
|
line += fmt.Sprintf(" (default %q)", flag.DefValue)
|
||||||
|
} else {
|
||||||
|
line += fmt.Sprintf(" (default %s)", flag.DefValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lines = append(lines, line)
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
sidx := strings.Index(line, "\x00")
|
||||||
|
spacing := strings.Repeat(" ", maxlen-sidx)
|
||||||
|
fmt.Fprintln(x, line[:sidx], spacing, line[sidx+1:])
|
||||||
|
}
|
||||||
|
|
||||||
|
return x.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrintDefaults prints to standard error the default values of all defined command-line flags.
|
||||||
|
func PrintDefaults() {
|
||||||
|
CommandLine.PrintDefaults()
|
||||||
|
}
|
||||||
|
|
||||||
|
// defaultUsage is the default function to print a usage message.
|
||||||
|
func defaultUsage(f *FlagSet) {
|
||||||
|
fmt.Fprintf(f.out(), "Usage of %s:\n", f.name)
|
||||||
|
f.PrintDefaults()
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: Usage is not just defaultUsage(CommandLine)
|
||||||
|
// because it serves (via godoc flag Usage) as the example
|
||||||
|
// for how to write your own usage function.
|
||||||
|
|
||||||
|
// Usage prints to standard error a usage message documenting all defined command-line flags.
|
||||||
|
// The function is a variable that may be changed to point to a custom function.
|
||||||
|
// By default it prints a simple header and calls PrintDefaults; for details about the
|
||||||
|
// format of the output and how to control it, see the documentation for PrintDefaults.
|
||||||
|
var Usage = func() {
|
||||||
|
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
||||||
|
PrintDefaults()
|
||||||
|
}
|
||||||
|
|
||||||
|
// NFlag returns the number of flags that have been set.
|
||||||
|
func (f *FlagSet) NFlag() int { return len(f.actual) }
|
||||||
|
|
||||||
|
// NFlag returns the number of command-line flags that have been set.
|
||||||
|
func NFlag() int { return len(CommandLine.actual) }
|
||||||
|
|
||||||
|
// Arg returns the i'th argument. Arg(0) is the first remaining argument
|
||||||
|
// after flags have been processed.
|
||||||
|
func (f *FlagSet) Arg(i int) string {
|
||||||
|
if i < 0 || i >= len(f.args) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return f.args[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
|
||||||
|
// after flags have been processed.
|
||||||
|
func Arg(i int) string {
|
||||||
|
return CommandLine.Arg(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NArg is the number of arguments remaining after flags have been processed.
|
||||||
|
func (f *FlagSet) NArg() int { return len(f.args) }
|
||||||
|
|
||||||
|
// NArg is the number of arguments remaining after flags have been processed.
|
||||||
|
func NArg() int { return len(CommandLine.args) }
|
||||||
|
|
||||||
|
// Args returns the non-flag arguments.
|
||||||
|
func (f *FlagSet) Args() []string { return f.args }
|
||||||
|
|
||||||
|
// Args returns the non-flag command-line arguments.
|
||||||
|
func Args() []string { return CommandLine.args }
|
||||||
|
|
||||||
|
// Var defines a flag with the specified name and usage string. The type and
|
||||||
|
// value of the flag are represented by the first argument, of type Value, which
|
||||||
|
// typically holds a user-defined implementation of Value. For instance, the
|
||||||
|
// caller could create a flag that turns a comma-separated string into a slice
|
||||||
|
// of strings by giving the slice the methods of Value; in particular, Set would
|
||||||
|
// decompose the comma-separated string into the slice.
|
||||||
|
func (f *FlagSet) Var(value Value, name string, usage string) {
|
||||||
|
f.VarP(value, name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// VarPF is like VarP, but returns the flag created
|
||||||
|
func (f *FlagSet) VarPF(value Value, name, shorthand, usage string) *Flag {
|
||||||
|
// Remember the default value as a string; it won't change.
|
||||||
|
flag := &Flag{
|
||||||
|
Name: name,
|
||||||
|
Shorthand: shorthand,
|
||||||
|
Usage: usage,
|
||||||
|
Value: value,
|
||||||
|
DefValue: value.String(),
|
||||||
|
}
|
||||||
|
f.AddFlag(flag)
|
||||||
|
return flag
|
||||||
|
}
|
||||||
|
|
||||||
|
// VarP is like Var, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) VarP(value Value, name, shorthand, usage string) {
|
||||||
|
_ = f.VarPF(value, name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddFlag will add the flag to the FlagSet
|
||||||
|
func (f *FlagSet) AddFlag(flag *Flag) {
|
||||||
|
// Call normalizeFlagName function only once
|
||||||
|
normalizedFlagName := f.normalizeFlagName(flag.Name)
|
||||||
|
|
||||||
|
_, alreadythere := f.formal[normalizedFlagName]
|
||||||
|
if alreadythere {
|
||||||
|
msg := fmt.Sprintf("%s flag redefined: %s", f.name, flag.Name)
|
||||||
|
fmt.Fprintln(f.out(), msg)
|
||||||
|
panic(msg) // Happens only if flags are declared with identical names
|
||||||
|
}
|
||||||
|
if f.formal == nil {
|
||||||
|
f.formal = make(map[NormalizedName]*Flag)
|
||||||
|
}
|
||||||
|
|
||||||
|
flag.Name = string(normalizedFlagName)
|
||||||
|
f.formal[normalizedFlagName] = flag
|
||||||
|
|
||||||
|
if len(flag.Shorthand) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(flag.Shorthand) > 1 {
|
||||||
|
fmt.Fprintf(f.out(), "%s shorthand more than ASCII character: %s\n", f.name, flag.Shorthand)
|
||||||
|
panic("shorthand is more than one character")
|
||||||
|
}
|
||||||
|
if f.shorthands == nil {
|
||||||
|
f.shorthands = make(map[byte]*Flag)
|
||||||
|
}
|
||||||
|
c := flag.Shorthand[0]
|
||||||
|
old, alreadythere := f.shorthands[c]
|
||||||
|
if alreadythere {
|
||||||
|
fmt.Fprintf(f.out(), "%s shorthand reused: %q for %s already used for %s\n", f.name, c, flag.Name, old.Name)
|
||||||
|
panic("shorthand redefinition")
|
||||||
|
}
|
||||||
|
f.shorthands[c] = flag
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddFlagSet adds one FlagSet to another. If a flag is already present in f
|
||||||
|
// the flag from newSet will be ignored
|
||||||
|
func (f *FlagSet) AddFlagSet(newSet *FlagSet) {
|
||||||
|
if newSet == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
newSet.VisitAll(func(flag *Flag) {
|
||||||
|
if f.Lookup(flag.Name) == nil {
|
||||||
|
f.AddFlag(flag)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Var defines a flag with the specified name and usage string. The type and
|
||||||
|
// value of the flag are represented by the first argument, of type Value, which
|
||||||
|
// typically holds a user-defined implementation of Value. For instance, the
|
||||||
|
// caller could create a flag that turns a comma-separated string into a slice
|
||||||
|
// of strings by giving the slice the methods of Value; in particular, Set would
|
||||||
|
// decompose the comma-separated string into the slice.
|
||||||
|
func Var(value Value, name string, usage string) {
|
||||||
|
CommandLine.VarP(value, name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// VarP is like Var, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func VarP(value Value, name, shorthand, usage string) {
|
||||||
|
CommandLine.VarP(value, name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// failf prints to standard error a formatted error and usage message and
|
||||||
|
// returns the error.
|
||||||
|
func (f *FlagSet) failf(format string, a ...interface{}) error {
|
||||||
|
err := fmt.Errorf(format, a...)
|
||||||
|
fmt.Fprintln(f.out(), err)
|
||||||
|
f.usage()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// usage calls the Usage method for the flag set, or the usage function if
|
||||||
|
// the flag set is CommandLine.
|
||||||
|
func (f *FlagSet) usage() {
|
||||||
|
if f == CommandLine {
|
||||||
|
Usage()
|
||||||
|
} else if f.Usage == nil {
|
||||||
|
defaultUsage(f)
|
||||||
|
} else {
|
||||||
|
f.Usage()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FlagSet) setFlag(flag *Flag, value string, origArg string) error {
|
||||||
|
if err := flag.Value.Set(value); err != nil {
|
||||||
|
return f.failf("invalid argument %q for %s: %v", value, origArg, err)
|
||||||
|
}
|
||||||
|
// mark as visited for Visit()
|
||||||
|
if f.actual == nil {
|
||||||
|
f.actual = make(map[NormalizedName]*Flag)
|
||||||
|
}
|
||||||
|
f.actual[f.normalizeFlagName(flag.Name)] = flag
|
||||||
|
flag.Changed = true
|
||||||
|
if len(flag.Deprecated) > 0 {
|
||||||
|
fmt.Fprintf(os.Stderr, "Flag --%s has been deprecated, %s\n", flag.Name, flag.Deprecated)
|
||||||
|
}
|
||||||
|
if len(flag.ShorthandDeprecated) > 0 && containsShorthand(origArg, flag.Shorthand) {
|
||||||
|
fmt.Fprintf(os.Stderr, "Flag shorthand -%s has been deprecated, %s\n", flag.Shorthand, flag.ShorthandDeprecated)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsShorthand(arg, shorthand string) bool {
|
||||||
|
// filter out flags --<flag_name>
|
||||||
|
if strings.HasPrefix(arg, "-") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
arg = strings.SplitN(arg, "=", 2)[0]
|
||||||
|
return strings.Contains(arg, shorthand)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FlagSet) parseLongArg(s string, args []string) (a []string, err error) {
|
||||||
|
a = args
|
||||||
|
name := s[2:]
|
||||||
|
if len(name) == 0 || name[0] == '-' || name[0] == '=' {
|
||||||
|
err = f.failf("bad flag syntax: %s", s)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
split := strings.SplitN(name, "=", 2)
|
||||||
|
name = split[0]
|
||||||
|
flag, alreadythere := f.formal[f.normalizeFlagName(name)]
|
||||||
|
if !alreadythere {
|
||||||
|
if name == "help" { // special case for nice help message.
|
||||||
|
f.usage()
|
||||||
|
return a, ErrHelp
|
||||||
|
}
|
||||||
|
err = f.failf("unknown flag: --%s", name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var value string
|
||||||
|
if len(split) == 2 {
|
||||||
|
// '--flag=arg'
|
||||||
|
value = split[1]
|
||||||
|
} else if len(flag.NoOptDefVal) > 0 {
|
||||||
|
// '--flag' (arg was optional)
|
||||||
|
value = flag.NoOptDefVal
|
||||||
|
} else if len(a) > 0 {
|
||||||
|
// '--flag arg'
|
||||||
|
value = a[0]
|
||||||
|
a = a[1:]
|
||||||
|
} else {
|
||||||
|
// '--flag' (arg was required)
|
||||||
|
err = f.failf("flag needs an argument: %s", s)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = f.setFlag(flag, value, s)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FlagSet) parseSingleShortArg(shorthands string, args []string) (outShorts string, outArgs []string, err error) {
|
||||||
|
if strings.HasPrefix(shorthands, "test.") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
outArgs = args
|
||||||
|
outShorts = shorthands[1:]
|
||||||
|
c := shorthands[0]
|
||||||
|
|
||||||
|
flag, alreadythere := f.shorthands[c]
|
||||||
|
if !alreadythere {
|
||||||
|
if c == 'h' { // special case for nice help message.
|
||||||
|
f.usage()
|
||||||
|
err = ErrHelp
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//TODO continue on error
|
||||||
|
err = f.failf("unknown shorthand flag: %q in -%s", c, shorthands)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var value string
|
||||||
|
if len(shorthands) > 2 && shorthands[1] == '=' {
|
||||||
|
value = shorthands[2:]
|
||||||
|
outShorts = ""
|
||||||
|
} else if len(flag.NoOptDefVal) > 0 {
|
||||||
|
value = flag.NoOptDefVal
|
||||||
|
} else if len(shorthands) > 1 {
|
||||||
|
value = shorthands[1:]
|
||||||
|
outShorts = ""
|
||||||
|
} else if len(args) > 0 {
|
||||||
|
value = args[0]
|
||||||
|
outArgs = args[1:]
|
||||||
|
} else {
|
||||||
|
err = f.failf("flag needs an argument: %q in -%s", c, shorthands)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = f.setFlag(flag, value, shorthands)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FlagSet) parseShortArg(s string, args []string) (a []string, err error) {
|
||||||
|
a = args
|
||||||
|
shorthands := s[1:]
|
||||||
|
|
||||||
|
for len(shorthands) > 0 {
|
||||||
|
shorthands, a, err = f.parseSingleShortArg(shorthands, args)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FlagSet) parseArgs(args []string) (err error) {
|
||||||
|
for len(args) > 0 {
|
||||||
|
s := args[0]
|
||||||
|
args = args[1:]
|
||||||
|
if len(s) == 0 || s[0] != '-' || len(s) == 1 {
|
||||||
|
if !f.interspersed {
|
||||||
|
f.args = append(f.args, s)
|
||||||
|
f.args = append(f.args, args...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
f.args = append(f.args, s)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if s[1] == '-' {
|
||||||
|
if len(s) == 2 { // "--" terminates the flags
|
||||||
|
f.argsLenAtDash = len(f.args)
|
||||||
|
f.args = append(f.args, args...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
args, err = f.parseLongArg(s, args)
|
||||||
|
} else {
|
||||||
|
args, err = f.parseShortArg(s, args)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse parses flag definitions from the argument list, which should not
|
||||||
|
// include the command name. Must be called after all flags in the FlagSet
|
||||||
|
// are defined and before flags are accessed by the program.
|
||||||
|
// The return value will be ErrHelp if -help was set but not defined.
|
||||||
|
func (f *FlagSet) Parse(arguments []string) error {
|
||||||
|
f.parsed = true
|
||||||
|
f.args = make([]string, 0, len(arguments))
|
||||||
|
err := f.parseArgs(arguments)
|
||||||
|
if err != nil {
|
||||||
|
switch f.errorHandling {
|
||||||
|
case ContinueOnError:
|
||||||
|
return err
|
||||||
|
case ExitOnError:
|
||||||
|
os.Exit(2)
|
||||||
|
case PanicOnError:
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parsed reports whether f.Parse has been called.
|
||||||
|
func (f *FlagSet) Parsed() bool {
|
||||||
|
return f.parsed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse parses the command-line flags from os.Args[1:]. Must be called
|
||||||
|
// after all flags are defined and before flags are accessed by the program.
|
||||||
|
func Parse() {
|
||||||
|
// Ignore errors; CommandLine is set for ExitOnError.
|
||||||
|
CommandLine.Parse(os.Args[1:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetInterspersed sets whether to support interspersed option/non-option arguments.
|
||||||
|
func SetInterspersed(interspersed bool) {
|
||||||
|
CommandLine.SetInterspersed(interspersed)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parsed returns true if the command-line flags have been parsed.
|
||||||
|
func Parsed() bool {
|
||||||
|
return CommandLine.Parsed()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CommandLine is the default set of command-line flags, parsed from os.Args.
|
||||||
|
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
|
||||||
|
|
||||||
|
// NewFlagSet returns a new, empty flag set with the specified name and
|
||||||
|
// error handling property.
|
||||||
|
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
|
||||||
|
f := &FlagSet{
|
||||||
|
name: name,
|
||||||
|
errorHandling: errorHandling,
|
||||||
|
argsLenAtDash: -1,
|
||||||
|
interspersed: true,
|
||||||
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetInterspersed sets whether to support interspersed option/non-option arguments.
|
||||||
|
func (f *FlagSet) SetInterspersed(interspersed bool) {
|
||||||
|
f.interspersed = interspersed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init sets the name and error handling property for a flag set.
|
||||||
|
// By default, the zero FlagSet uses an empty name and the
|
||||||
|
// ContinueOnError error handling policy.
|
||||||
|
func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
|
||||||
|
f.name = name
|
||||||
|
f.errorHandling = errorHandling
|
||||||
|
f.argsLenAtDash = -1
|
||||||
|
}
|
91
vendor/github.com/spf13/pflag/float32.go
generated
vendored
Normal file
91
vendor/github.com/spf13/pflag/float32.go
generated
vendored
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
package pflag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// -- float32 Value
|
||||||
|
type float32Value float32
|
||||||
|
|
||||||
|
func newFloat32Value(val float32, p *float32) *float32Value {
|
||||||
|
*p = val
|
||||||
|
return (*float32Value)(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *float32Value) Set(s string) error {
|
||||||
|
v, err := strconv.ParseFloat(s, 32)
|
||||||
|
*f = float32Value(v)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *float32Value) Type() string {
|
||||||
|
return "float32"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *float32Value) String() string { return fmt.Sprintf("%v", *f) }
|
||||||
|
|
||||||
|
func float32Conv(sval string) (interface{}, error) {
|
||||||
|
v, err := strconv.ParseFloat(sval, 32)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return float32(v), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFloat32 return the float32 value of a flag with the given name
|
||||||
|
func (f *FlagSet) GetFloat32(name string) (float32, error) {
|
||||||
|
val, err := f.getFlagType(name, "float32", float32Conv)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return val.(float32), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float32Var defines a float32 flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to a float32 variable in which to store the value of the flag.
|
||||||
|
func (f *FlagSet) Float32Var(p *float32, name string, value float32, usage string) {
|
||||||
|
f.VarP(newFloat32Value(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float32VarP is like Float32Var, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) Float32VarP(p *float32, name, shorthand string, value float32, usage string) {
|
||||||
|
f.VarP(newFloat32Value(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float32Var defines a float32 flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to a float32 variable in which to store the value of the flag.
|
||||||
|
func Float32Var(p *float32, name string, value float32, usage string) {
|
||||||
|
CommandLine.VarP(newFloat32Value(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float32VarP is like Float32Var, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func Float32VarP(p *float32, name, shorthand string, value float32, usage string) {
|
||||||
|
CommandLine.VarP(newFloat32Value(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float32 defines a float32 flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of a float32 variable that stores the value of the flag.
|
||||||
|
func (f *FlagSet) Float32(name string, value float32, usage string) *float32 {
|
||||||
|
p := new(float32)
|
||||||
|
f.Float32VarP(p, name, "", value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float32P is like Float32, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) Float32P(name, shorthand string, value float32, usage string) *float32 {
|
||||||
|
p := new(float32)
|
||||||
|
f.Float32VarP(p, name, shorthand, value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float32 defines a float32 flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of a float32 variable that stores the value of the flag.
|
||||||
|
func Float32(name string, value float32, usage string) *float32 {
|
||||||
|
return CommandLine.Float32P(name, "", value, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float32P is like Float32, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func Float32P(name, shorthand string, value float32, usage string) *float32 {
|
||||||
|
return CommandLine.Float32P(name, shorthand, value, usage)
|
||||||
|
}
|
87
vendor/github.com/spf13/pflag/float64.go
generated
vendored
Normal file
87
vendor/github.com/spf13/pflag/float64.go
generated
vendored
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
package pflag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// -- float64 Value
|
||||||
|
type float64Value float64
|
||||||
|
|
||||||
|
func newFloat64Value(val float64, p *float64) *float64Value {
|
||||||
|
*p = val
|
||||||
|
return (*float64Value)(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *float64Value) Set(s string) error {
|
||||||
|
v, err := strconv.ParseFloat(s, 64)
|
||||||
|
*f = float64Value(v)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *float64Value) Type() string {
|
||||||
|
return "float64"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) }
|
||||||
|
|
||||||
|
func float64Conv(sval string) (interface{}, error) {
|
||||||
|
return strconv.ParseFloat(sval, 64)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFloat64 return the float64 value of a flag with the given name
|
||||||
|
func (f *FlagSet) GetFloat64(name string) (float64, error) {
|
||||||
|
val, err := f.getFlagType(name, "float64", float64Conv)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return val.(float64), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float64Var defines a float64 flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to a float64 variable in which to store the value of the flag.
|
||||||
|
func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) {
|
||||||
|
f.VarP(newFloat64Value(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float64VarP is like Float64Var, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) Float64VarP(p *float64, name, shorthand string, value float64, usage string) {
|
||||||
|
f.VarP(newFloat64Value(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float64Var defines a float64 flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to a float64 variable in which to store the value of the flag.
|
||||||
|
func Float64Var(p *float64, name string, value float64, usage string) {
|
||||||
|
CommandLine.VarP(newFloat64Value(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float64VarP is like Float64Var, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func Float64VarP(p *float64, name, shorthand string, value float64, usage string) {
|
||||||
|
CommandLine.VarP(newFloat64Value(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float64 defines a float64 flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of a float64 variable that stores the value of the flag.
|
||||||
|
func (f *FlagSet) Float64(name string, value float64, usage string) *float64 {
|
||||||
|
p := new(float64)
|
||||||
|
f.Float64VarP(p, name, "", value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float64P is like Float64, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) Float64P(name, shorthand string, value float64, usage string) *float64 {
|
||||||
|
p := new(float64)
|
||||||
|
f.Float64VarP(p, name, shorthand, value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float64 defines a float64 flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of a float64 variable that stores the value of the flag.
|
||||||
|
func Float64(name string, value float64, usage string) *float64 {
|
||||||
|
return CommandLine.Float64P(name, "", value, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float64P is like Float64, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func Float64P(name, shorthand string, value float64, usage string) *float64 {
|
||||||
|
return CommandLine.Float64P(name, shorthand, value, usage)
|
||||||
|
}
|
104
vendor/github.com/spf13/pflag/golangflag.go
generated
vendored
Normal file
104
vendor/github.com/spf13/pflag/golangflag.go
generated
vendored
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package pflag
|
||||||
|
|
||||||
|
import (
|
||||||
|
goflag "flag"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = fmt.Print
|
||||||
|
|
||||||
|
// flagValueWrapper implements pflag.Value around a flag.Value. The main
|
||||||
|
// difference here is the addition of the Type method that returns a string
|
||||||
|
// name of the type. As this is generally unknown, we approximate that with
|
||||||
|
// reflection.
|
||||||
|
type flagValueWrapper struct {
|
||||||
|
inner goflag.Value
|
||||||
|
flagType string
|
||||||
|
}
|
||||||
|
|
||||||
|
// We are just copying the boolFlag interface out of goflag as that is what
|
||||||
|
// they use to decide if a flag should get "true" when no arg is given.
|
||||||
|
type goBoolFlag interface {
|
||||||
|
goflag.Value
|
||||||
|
IsBoolFlag() bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func wrapFlagValue(v goflag.Value) Value {
|
||||||
|
// If the flag.Value happens to also be a pflag.Value, just use it directly.
|
||||||
|
if pv, ok := v.(Value); ok {
|
||||||
|
return pv
|
||||||
|
}
|
||||||
|
|
||||||
|
pv := &flagValueWrapper{
|
||||||
|
inner: v,
|
||||||
|
}
|
||||||
|
|
||||||
|
t := reflect.TypeOf(v)
|
||||||
|
if t.Kind() == reflect.Interface || t.Kind() == reflect.Ptr {
|
||||||
|
t = t.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
pv.flagType = strings.TrimSuffix(t.Name(), "Value")
|
||||||
|
return pv
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *flagValueWrapper) String() string {
|
||||||
|
return v.inner.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *flagValueWrapper) Set(s string) error {
|
||||||
|
return v.inner.Set(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *flagValueWrapper) Type() string {
|
||||||
|
return v.flagType
|
||||||
|
}
|
||||||
|
|
||||||
|
// PFlagFromGoFlag will return a *pflag.Flag given a *flag.Flag
|
||||||
|
// If the *flag.Flag.Name was a single character (ex: `v`) it will be accessiblei
|
||||||
|
// with both `-v` and `--v` in flags. If the golang flag was more than a single
|
||||||
|
// character (ex: `verbose`) it will only be accessible via `--verbose`
|
||||||
|
func PFlagFromGoFlag(goflag *goflag.Flag) *Flag {
|
||||||
|
// Remember the default value as a string; it won't change.
|
||||||
|
flag := &Flag{
|
||||||
|
Name: goflag.Name,
|
||||||
|
Usage: goflag.Usage,
|
||||||
|
Value: wrapFlagValue(goflag.Value),
|
||||||
|
// Looks like golang flags don't set DefValue correctly :-(
|
||||||
|
//DefValue: goflag.DefValue,
|
||||||
|
DefValue: goflag.Value.String(),
|
||||||
|
}
|
||||||
|
// Ex: if the golang flag was -v, allow both -v and --v to work
|
||||||
|
if len(flag.Name) == 1 {
|
||||||
|
flag.Shorthand = flag.Name
|
||||||
|
}
|
||||||
|
if fv, ok := goflag.Value.(goBoolFlag); ok && fv.IsBoolFlag() {
|
||||||
|
flag.NoOptDefVal = "true"
|
||||||
|
}
|
||||||
|
return flag
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddGoFlag will add the given *flag.Flag to the pflag.FlagSet
|
||||||
|
func (f *FlagSet) AddGoFlag(goflag *goflag.Flag) {
|
||||||
|
if f.Lookup(goflag.Name) != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
newflag := PFlagFromGoFlag(goflag)
|
||||||
|
f.AddFlag(newflag)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddGoFlagSet will add the given *flag.FlagSet to the pflag.FlagSet
|
||||||
|
func (f *FlagSet) AddGoFlagSet(newSet *goflag.FlagSet) {
|
||||||
|
if newSet == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
newSet.VisitAll(func(goflag *goflag.Flag) {
|
||||||
|
f.AddGoFlag(goflag)
|
||||||
|
})
|
||||||
|
}
|
87
vendor/github.com/spf13/pflag/int.go
generated
vendored
Normal file
87
vendor/github.com/spf13/pflag/int.go
generated
vendored
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
package pflag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// -- int Value
|
||||||
|
type intValue int
|
||||||
|
|
||||||
|
func newIntValue(val int, p *int) *intValue {
|
||||||
|
*p = val
|
||||||
|
return (*intValue)(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *intValue) Set(s string) error {
|
||||||
|
v, err := strconv.ParseInt(s, 0, 64)
|
||||||
|
*i = intValue(v)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *intValue) Type() string {
|
||||||
|
return "int"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *intValue) String() string { return fmt.Sprintf("%v", *i) }
|
||||||
|
|
||||||
|
func intConv(sval string) (interface{}, error) {
|
||||||
|
return strconv.Atoi(sval)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInt return the int value of a flag with the given name
|
||||||
|
func (f *FlagSet) GetInt(name string) (int, error) {
|
||||||
|
val, err := f.getFlagType(name, "int", intConv)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return val.(int), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntVar defines an int flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to an int variable in which to store the value of the flag.
|
||||||
|
func (f *FlagSet) IntVar(p *int, name string, value int, usage string) {
|
||||||
|
f.VarP(newIntValue(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntVarP is like IntVar, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) IntVarP(p *int, name, shorthand string, value int, usage string) {
|
||||||
|
f.VarP(newIntValue(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntVar defines an int flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to an int variable in which to store the value of the flag.
|
||||||
|
func IntVar(p *int, name string, value int, usage string) {
|
||||||
|
CommandLine.VarP(newIntValue(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntVarP is like IntVar, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func IntVarP(p *int, name, shorthand string, value int, usage string) {
|
||||||
|
CommandLine.VarP(newIntValue(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int defines an int flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of an int variable that stores the value of the flag.
|
||||||
|
func (f *FlagSet) Int(name string, value int, usage string) *int {
|
||||||
|
p := new(int)
|
||||||
|
f.IntVarP(p, name, "", value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntP is like Int, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) IntP(name, shorthand string, value int, usage string) *int {
|
||||||
|
p := new(int)
|
||||||
|
f.IntVarP(p, name, shorthand, value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int defines an int flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of an int variable that stores the value of the flag.
|
||||||
|
func Int(name string, value int, usage string) *int {
|
||||||
|
return CommandLine.IntP(name, "", value, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntP is like Int, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func IntP(name, shorthand string, value int, usage string) *int {
|
||||||
|
return CommandLine.IntP(name, shorthand, value, usage)
|
||||||
|
}
|
91
vendor/github.com/spf13/pflag/int32.go
generated
vendored
Normal file
91
vendor/github.com/spf13/pflag/int32.go
generated
vendored
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
package pflag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// -- int32 Value
|
||||||
|
type int32Value int32
|
||||||
|
|
||||||
|
func newInt32Value(val int32, p *int32) *int32Value {
|
||||||
|
*p = val
|
||||||
|
return (*int32Value)(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *int32Value) Set(s string) error {
|
||||||
|
v, err := strconv.ParseInt(s, 0, 32)
|
||||||
|
*i = int32Value(v)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *int32Value) Type() string {
|
||||||
|
return "int32"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *int32Value) String() string { return fmt.Sprintf("%v", *i) }
|
||||||
|
|
||||||
|
func int32Conv(sval string) (interface{}, error) {
|
||||||
|
v, err := strconv.ParseInt(sval, 0, 32)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return int32(v), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInt32 return the int32 value of a flag with the given name
|
||||||
|
func (f *FlagSet) GetInt32(name string) (int32, error) {
|
||||||
|
val, err := f.getFlagType(name, "int32", int32Conv)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return val.(int32), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int32Var defines an int32 flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to an int32 variable in which to store the value of the flag.
|
||||||
|
func (f *FlagSet) Int32Var(p *int32, name string, value int32, usage string) {
|
||||||
|
f.VarP(newInt32Value(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int32VarP is like Int32Var, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) Int32VarP(p *int32, name, shorthand string, value int32, usage string) {
|
||||||
|
f.VarP(newInt32Value(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int32Var defines an int32 flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to an int32 variable in which to store the value of the flag.
|
||||||
|
func Int32Var(p *int32, name string, value int32, usage string) {
|
||||||
|
CommandLine.VarP(newInt32Value(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int32VarP is like Int32Var, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func Int32VarP(p *int32, name, shorthand string, value int32, usage string) {
|
||||||
|
CommandLine.VarP(newInt32Value(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int32 defines an int32 flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of an int32 variable that stores the value of the flag.
|
||||||
|
func (f *FlagSet) Int32(name string, value int32, usage string) *int32 {
|
||||||
|
p := new(int32)
|
||||||
|
f.Int32VarP(p, name, "", value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int32P is like Int32, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) Int32P(name, shorthand string, value int32, usage string) *int32 {
|
||||||
|
p := new(int32)
|
||||||
|
f.Int32VarP(p, name, shorthand, value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int32 defines an int32 flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of an int32 variable that stores the value of the flag.
|
||||||
|
func Int32(name string, value int32, usage string) *int32 {
|
||||||
|
return CommandLine.Int32P(name, "", value, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int32P is like Int32, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func Int32P(name, shorthand string, value int32, usage string) *int32 {
|
||||||
|
return CommandLine.Int32P(name, shorthand, value, usage)
|
||||||
|
}
|
87
vendor/github.com/spf13/pflag/int64.go
generated
vendored
Normal file
87
vendor/github.com/spf13/pflag/int64.go
generated
vendored
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
package pflag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// -- int64 Value
|
||||||
|
type int64Value int64
|
||||||
|
|
||||||
|
func newInt64Value(val int64, p *int64) *int64Value {
|
||||||
|
*p = val
|
||||||
|
return (*int64Value)(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *int64Value) Set(s string) error {
|
||||||
|
v, err := strconv.ParseInt(s, 0, 64)
|
||||||
|
*i = int64Value(v)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *int64Value) Type() string {
|
||||||
|
return "int64"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) }
|
||||||
|
|
||||||
|
func int64Conv(sval string) (interface{}, error) {
|
||||||
|
return strconv.ParseInt(sval, 0, 64)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInt64 return the int64 value of a flag with the given name
|
||||||
|
func (f *FlagSet) GetInt64(name string) (int64, error) {
|
||||||
|
val, err := f.getFlagType(name, "int64", int64Conv)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return val.(int64), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int64Var defines an int64 flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to an int64 variable in which to store the value of the flag.
|
||||||
|
func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) {
|
||||||
|
f.VarP(newInt64Value(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int64VarP is like Int64Var, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) Int64VarP(p *int64, name, shorthand string, value int64, usage string) {
|
||||||
|
f.VarP(newInt64Value(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int64Var defines an int64 flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to an int64 variable in which to store the value of the flag.
|
||||||
|
func Int64Var(p *int64, name string, value int64, usage string) {
|
||||||
|
CommandLine.VarP(newInt64Value(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int64VarP is like Int64Var, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func Int64VarP(p *int64, name, shorthand string, value int64, usage string) {
|
||||||
|
CommandLine.VarP(newInt64Value(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int64 defines an int64 flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of an int64 variable that stores the value of the flag.
|
||||||
|
func (f *FlagSet) Int64(name string, value int64, usage string) *int64 {
|
||||||
|
p := new(int64)
|
||||||
|
f.Int64VarP(p, name, "", value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int64P is like Int64, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) Int64P(name, shorthand string, value int64, usage string) *int64 {
|
||||||
|
p := new(int64)
|
||||||
|
f.Int64VarP(p, name, shorthand, value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int64 defines an int64 flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of an int64 variable that stores the value of the flag.
|
||||||
|
func Int64(name string, value int64, usage string) *int64 {
|
||||||
|
return CommandLine.Int64P(name, "", value, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int64P is like Int64, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func Int64P(name, shorthand string, value int64, usage string) *int64 {
|
||||||
|
return CommandLine.Int64P(name, shorthand, value, usage)
|
||||||
|
}
|
91
vendor/github.com/spf13/pflag/int8.go
generated
vendored
Normal file
91
vendor/github.com/spf13/pflag/int8.go
generated
vendored
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
package pflag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// -- int8 Value
|
||||||
|
type int8Value int8
|
||||||
|
|
||||||
|
func newInt8Value(val int8, p *int8) *int8Value {
|
||||||
|
*p = val
|
||||||
|
return (*int8Value)(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *int8Value) Set(s string) error {
|
||||||
|
v, err := strconv.ParseInt(s, 0, 8)
|
||||||
|
*i = int8Value(v)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *int8Value) Type() string {
|
||||||
|
return "int8"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *int8Value) String() string { return fmt.Sprintf("%v", *i) }
|
||||||
|
|
||||||
|
func int8Conv(sval string) (interface{}, error) {
|
||||||
|
v, err := strconv.ParseInt(sval, 0, 8)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return int8(v), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInt8 return the int8 value of a flag with the given name
|
||||||
|
func (f *FlagSet) GetInt8(name string) (int8, error) {
|
||||||
|
val, err := f.getFlagType(name, "int8", int8Conv)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return val.(int8), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int8Var defines an int8 flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to an int8 variable in which to store the value of the flag.
|
||||||
|
func (f *FlagSet) Int8Var(p *int8, name string, value int8, usage string) {
|
||||||
|
f.VarP(newInt8Value(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int8VarP is like Int8Var, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) Int8VarP(p *int8, name, shorthand string, value int8, usage string) {
|
||||||
|
f.VarP(newInt8Value(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int8Var defines an int8 flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to an int8 variable in which to store the value of the flag.
|
||||||
|
func Int8Var(p *int8, name string, value int8, usage string) {
|
||||||
|
CommandLine.VarP(newInt8Value(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int8VarP is like Int8Var, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func Int8VarP(p *int8, name, shorthand string, value int8, usage string) {
|
||||||
|
CommandLine.VarP(newInt8Value(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int8 defines an int8 flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of an int8 variable that stores the value of the flag.
|
||||||
|
func (f *FlagSet) Int8(name string, value int8, usage string) *int8 {
|
||||||
|
p := new(int8)
|
||||||
|
f.Int8VarP(p, name, "", value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int8P is like Int8, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) Int8P(name, shorthand string, value int8, usage string) *int8 {
|
||||||
|
p := new(int8)
|
||||||
|
f.Int8VarP(p, name, shorthand, value, usage)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int8 defines an int8 flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of an int8 variable that stores the value of the flag.
|
||||||
|
func Int8(name string, value int8, usage string) *int8 {
|
||||||
|
return CommandLine.Int8P(name, "", value, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int8P is like Int8, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func Int8P(name, shorthand string, value int8, usage string) *int8 {
|
||||||
|
return CommandLine.Int8P(name, shorthand, value, usage)
|
||||||
|
}
|
128
vendor/github.com/spf13/pflag/int_slice.go
generated
vendored
Normal file
128
vendor/github.com/spf13/pflag/int_slice.go
generated
vendored
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
package pflag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// -- intSlice Value
|
||||||
|
type intSliceValue struct {
|
||||||
|
value *[]int
|
||||||
|
changed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newIntSliceValue(val []int, p *[]int) *intSliceValue {
|
||||||
|
isv := new(intSliceValue)
|
||||||
|
isv.value = p
|
||||||
|
*isv.value = val
|
||||||
|
return isv
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *intSliceValue) Set(val string) error {
|
||||||
|
ss := strings.Split(val, ",")
|
||||||
|
out := make([]int, len(ss))
|
||||||
|
for i, d := range ss {
|
||||||
|
var err error
|
||||||
|
out[i], err = strconv.Atoi(d)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if !s.changed {
|
||||||
|
*s.value = out
|
||||||
|
} else {
|
||||||
|
*s.value = append(*s.value, out...)
|
||||||
|
}
|
||||||
|
s.changed = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *intSliceValue) Type() string {
|
||||||
|
return "intSlice"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *intSliceValue) String() string {
|
||||||
|
out := make([]string, len(*s.value))
|
||||||
|
for i, d := range *s.value {
|
||||||
|
out[i] = fmt.Sprintf("%d", d)
|
||||||
|
}
|
||||||
|
return "[" + strings.Join(out, ",") + "]"
|
||||||
|
}
|
||||||
|
|
||||||
|
func intSliceConv(val string) (interface{}, error) {
|
||||||
|
val = strings.Trim(val, "[]")
|
||||||
|
// Empty string would cause a slice with one (empty) entry
|
||||||
|
if len(val) == 0 {
|
||||||
|
return []int{}, nil
|
||||||
|
}
|
||||||
|
ss := strings.Split(val, ",")
|
||||||
|
out := make([]int, len(ss))
|
||||||
|
for i, d := range ss {
|
||||||
|
var err error
|
||||||
|
out[i], err = strconv.Atoi(d)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetIntSlice return the []int value of a flag with the given name
|
||||||
|
func (f *FlagSet) GetIntSlice(name string) ([]int, error) {
|
||||||
|
val, err := f.getFlagType(name, "intSlice", intSliceConv)
|
||||||
|
if err != nil {
|
||||||
|
return []int{}, err
|
||||||
|
}
|
||||||
|
return val.([]int), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntSliceVar defines a intSlice flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to a []int variable in which to store the value of the flag.
|
||||||
|
func (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string) {
|
||||||
|
f.VarP(newIntSliceValue(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
|
||||||
|
f.VarP(newIntSliceValue(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntSliceVar defines a int[] flag with specified name, default value, and usage string.
|
||||||
|
// The argument p points to a int[] variable in which to store the value of the flag.
|
||||||
|
func IntSliceVar(p *[]int, name string, value []int, usage string) {
|
||||||
|
CommandLine.VarP(newIntSliceValue(value, p), name, "", usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
|
||||||
|
CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntSlice defines a []int flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of a []int variable that stores the value of the flag.
|
||||||
|
func (f *FlagSet) IntSlice(name string, value []int, usage string) *[]int {
|
||||||
|
p := []int{}
|
||||||
|
f.IntSliceVarP(&p, name, "", value, usage)
|
||||||
|
return &p
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func (f *FlagSet) IntSliceP(name, shorthand string, value []int, usage string) *[]int {
|
||||||
|
p := []int{}
|
||||||
|
f.IntSliceVarP(&p, name, shorthand, value, usage)
|
||||||
|
return &p
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntSlice defines a []int flag with specified name, default value, and usage string.
|
||||||
|
// The return value is the address of a []int variable that stores the value of the flag.
|
||||||
|
func IntSlice(name string, value []int, usage string) *[]int {
|
||||||
|
return CommandLine.IntSliceP(name, "", value, usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.
|
||||||
|
func IntSliceP(name, shorthand string, value []int, usage string) *[]int {
|
||||||
|
return CommandLine.IntSliceP(name, shorthand, value, usage)
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue