switch to go vendoring

This commit is contained in:
Michael Barz
2023-04-19 20:10:09 +02:00
parent 632fa05ef9
commit afc6ed1e41
8527 changed files with 3004916 additions and 2 deletions
+28
View File
@@ -0,0 +1,28 @@
Copyright (c) 2017 Simon Eisenmann. 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.
+137
View File
@@ -0,0 +1,137 @@
package loggedwriter
import (
"net/http"
)
// LoggedResponseWriter define http.ResponseWriter with Status
type LoggedResponseWriter interface {
http.ResponseWriter
Status() int
}
// loggedResponseWriter is a http.ResponseWriter with Status.
type loggedResponseWriter struct {
http.ResponseWriter
status int
}
// loggedResponseWriterHijacker is a http.ResponseWriter and http.Hijacker
// with Status.
type loggedResponseWriterHijacker struct {
LoggedResponseWriter
http.Hijacker
}
// loggedResponseWriterHijacker is a http.ResponseWriter and http.Flusher
// with Status.
type loggedResponseWriterFlusher struct {
LoggedResponseWriter
http.Flusher
}
// loggedResponseWriterHijacker is a http.ResponseWriter and http.Pusher
// with Status.
type loggedResponseWriterPusher struct {
LoggedResponseWriter
http.Pusher
}
// loggedResponseWriterHijackerFlusherPusher is a http.ResponseWriter, http.Hijacker,
// http.Flusher and http.Pusher with Status.
type loggedResponseWriterHijackerFlusherPusher struct {
LoggedResponseWriter
http.Hijacker
http.Flusher
http.Pusher
}
// loggedResponseWriterHijackerFlusher is a http.ResponseWriter, http.Hijacker,
// and http.Flusher with Status.
type loggedResponseWriterHijackerFlusher struct {
LoggedResponseWriter
http.Hijacker
http.Flusher
}
// loggedResponseWriterHijackerPusher is a http.ResponseWriter, http.Hijacker,
// and http.Pusher with Status.
type loggedResponseWriterHijackerPusher struct {
LoggedResponseWriter
http.Hijacker
http.Pusher
}
// loggedResponseWriterFlusherPusher is a http.ResponseWriter, http.Flusher and
// http.Pusher with Status.
type loggedResponseWriterFlusherPusher struct {
LoggedResponseWriter
http.Flusher
http.Pusher
}
// NewLoggedResponseWriter wraps the provided http.ResponseWriter with Status
// preserving the support to hijack the connection if supported by the provided
// http.ResponseWriter.
func NewLoggedResponseWriter(w http.ResponseWriter) LoggedResponseWriter {
lw := &loggedResponseWriter{ResponseWriter: w}
hj, hj_ok := w.(http.Hijacker)
fl, fl_ok := w.(http.Flusher)
ps, ps_ok := w.(http.Pusher)
if hj_ok {
if fl_ok && ps_ok {
return &loggedResponseWriterHijackerFlusherPusher{
LoggedResponseWriter: lw,
Hijacker: hj,
Flusher: fl,
Pusher: ps,
}
}
if fl_ok {
return &loggedResponseWriterHijackerFlusher{
LoggedResponseWriter: lw,
Hijacker: hj,
Flusher: fl,
}
}
if ps_ok {
return &loggedResponseWriterHijackerPusher{
LoggedResponseWriter: lw,
Hijacker: hj,
Pusher: ps,
}
}
return &loggedResponseWriterHijacker{LoggedResponseWriter: lw, Hijacker: hj}
} else if fl_ok {
if ps_ok {
return &loggedResponseWriterFlusherPusher{
LoggedResponseWriter: lw,
Flusher: fl,
Pusher: ps,
}
}
return &loggedResponseWriterFlusher{LoggedResponseWriter: lw, Flusher: fl}
} else if ps_ok {
return &loggedResponseWriterPusher{LoggedResponseWriter: lw, Pusher: ps}
}
return lw
}
// WriteHeader sends an HTTP response header with status code.
func (w *loggedResponseWriter) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
}
// Status returns the written HTTP response header status code or http.StatusOK
// if none was written.
func (w *loggedResponseWriter) Status() int {
status := w.status
if status == 0 {
status = http.StatusOK
}
return status
}
+69
View File
@@ -0,0 +1,69 @@
package timing
import (
"context"
"time"
)
// key is an unexported type for keys defined in this package.
type key int
// elapsedKey is the key for elapsedRecord which holds start end elapsed time
// in Contexts.
const elapsedKey key = 0
type elapsedRecord struct {
start time.Time
elapsed time.Duration
done chan bool
cancel context.CancelFunc
}
// NewContext returns a new Context that carries the start time and calls the
// provided stopped function then the created Context is cancelled. The stopped
// function can be nil in which case nothing is called.
func NewContext(parent context.Context, stopped func(elapsed time.Duration)) context.Context {
ctx, cancel := context.WithCancel(parent)
recordPtr := &elapsedRecord{
start: time.Now(),
done: make(chan bool),
cancel: cancel,
}
ctx = context.WithValue(ctx, elapsedKey, recordPtr)
go func() {
<-ctx.Done()
recordPtr.elapsed = time.Since(recordPtr.start)
close(recordPtr.done)
if stopped != nil {
stopped(recordPtr.elapsed)
}
}()
return ctx
}
// StartFromContext returns the start time from the provided Context.
func StartFromContext(ctx context.Context) time.Time {
return ctx.Value(elapsedKey).(*elapsedRecord).start
}
// ElapsedFromContext returns the elapsed time from the provided Context. If the
// provided context is not yet cancelled the duration from now since start is
// returned.
func ElapsedFromContext(ctx context.Context) time.Duration {
elapsed := ctx.Value(elapsedKey).(*elapsedRecord).elapsed
if elapsed > 0 {
return elapsed
}
return time.Since(StartFromContext(ctx))
}
// CancelContext cancels the provided Context if it carries start time.
func CancelContext(ctx context.Context) {
recordPtr := ctx.Value(elapsedKey).(*elapsedRecord)
if recordPtr != nil {
recordPtr.cancel()
<-recordPtr.done // Wait until done is complete.
}
}
+12
View File
@@ -0,0 +1,12 @@
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
charset = utf-8
indent_style = tab
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
+18
View File
@@ -0,0 +1,18 @@
language: go
go:
- '1.13'
- '1.14'
- '1.15'
- '1.16'
- '1.17'
- '1.18'
- '1.19'
- tip
- master
go_import_path: github.com/longsleep/rndm
matrix:
allow_failures:
- go: tip master
+19
View File
@@ -0,0 +1,19 @@
Copyright (c) 2017 - 2022 Kopano
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
View File
@@ -0,0 +1,10 @@
# rndm [![GoDoc](https://godoc.org/github.com/longsleep/rndm?status.svg)](https://godoc.org/github.com/longsleep/rndm)
Rndm is a Go package to provide helper functions which create cryptographically
secure random values out of random data.
## Installation
```text
go get github.com/longsleep/rndm
```
+32
View File
@@ -0,0 +1,32 @@
/*
* Copyright 2017 Kopano
*
* Use of this source code is governed by a MIT license
* that can be found in the LICENSE.txt file.
*
*/
package rndm
import (
"crypto/rand"
)
// GenerateRandomBytes returns securely generated random bytes. It will panic
// when the system fails to provide enough secure random data.
func GenerateRandomBytes(n int) []byte {
b := make([]byte, n)
_, err := ReadRandomBytes(b)
if err != nil {
panic("unable to read enough random bytes")
}
return b
}
// ReadRandomBytes is a helper function that reads random data into the provided
// []byte. Tt returns the number of random bytes read and an error if fewer
// bytes were read.
func ReadRandomBytes(b []byte) (n int, err error) {
return rand.Read(b)
}
+9
View File
@@ -0,0 +1,9 @@
/*
* Copyright 2017 Kopano
*
* Use of this source code is governed by a MIT license
* that can be found in the LICENSE.txt file.
*
*/
package rndm // import "github.com/longsleep/rndm"
+22
View File
@@ -0,0 +1,22 @@
/*
* Copyright 2017 Kopano
*
* Use of this source code is governed by a MIT license
* that can be found in the LICENSE.txt file.
*
*/
package rndm
import (
"encoding/base64"
)
// GenerateRandomString returns a URL-safe, base64 encoded securely generated
// random string. It will panic if the system fails to provide secure random
// data.
func GenerateRandomString(s int) string {
b := GenerateRandomBytes(s)
return base64.RawURLEncoding.EncodeToString(b)
}