ci: update go version and refactor code (#508)

This commit is contained in:
zekin 2023-01-04 19:02:51 +08:00 committed by GitHub
parent 36682f8027
commit 16de10c02f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
39 changed files with 216 additions and 208 deletions

View File

@ -22,7 +22,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.19
- uses: actions/cache@v3
with:

View File

@ -4,6 +4,9 @@ on: [ push, pull_request ]
jobs:
lint-and-ut:
strategy:
matrix:
version: [ 1.18, 1.19 ]
runs-on: [ self-hosted, X64 ]
steps:
- uses: actions/checkout@v3
@ -11,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: ${{ matrix.version }}
- uses: actions/cache@v3
with:

View File

@ -19,7 +19,7 @@ package generator
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -142,7 +142,7 @@ func (pkgGen *HttpPackageGenerator) updateHandler(handler interface{}, handlerTp
return pkgGen.TemplateGenerator.Generate(handler, handlerTpl, filePath, noRepeat)
}
file, err := ioutil.ReadFile(filePath)
file, err := os.ReadFile(filePath)
if err != nil {
return err
}

View File

@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"reflect"
"github.com/cloudwego/hertz/cmd/hz/util"
@ -55,7 +56,7 @@ func (lg *LayoutGenerator) Init() error {
config := layoutConfig
// unmarshal from user-defined config file if it exists
if lg.ConfigPath != "" {
cdata, err := ioutil.ReadFile(lg.ConfigPath)
cdata, err := os.ReadFile(lg.ConfigPath)
if err != nil {
return fmt.Errorf("read layout config from %s failed, err: %v", lg.ConfigPath, err.Error())
}

View File

@ -19,7 +19,7 @@ package generator
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"text/template"
@ -70,7 +70,7 @@ func (pkgGen *HttpPackageGenerator) Init() error {
config := packageConfig
// unmarshal from user-defined config file if it exists
if pkgGen.ConfigPath != "" {
cdata, err := ioutil.ReadFile(pkgGen.ConfigPath)
cdata, err := os.ReadFile(pkgGen.ConfigPath)
if err != nil {
return fmt.Errorf("read layout config from %s failed, err: %v", pkgGen.ConfigPath, err.Error())
}

View File

@ -18,7 +18,6 @@ package meta
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
@ -43,7 +42,7 @@ func init() {
var regVersion = regexp.MustCompile(`hz version: v([0-9a-z.]*)`)
func (manifest *Manifest) Validate(dir string) error {
rd, err := ioutil.ReadFile(filepath.Join(dir, ManifestFile))
rd, err := os.ReadFile(filepath.Join(dir, ManifestFile))
if err != nil {
return err
}

View File

@ -17,7 +17,7 @@
package protobuf
import (
"io/ioutil"
"os"
"strings"
"testing"
@ -27,7 +27,7 @@ import (
)
func TestPlugin_Handle(t *testing.T) {
in, err := ioutil.ReadFile("../testdata/request_protoc.out")
in, err := os.ReadFile("../testdata/request_protoc.out")
if err != nil {
t.Fatal(err)
}

View File

@ -17,7 +17,7 @@
package protobuf
import (
"io/ioutil"
"os"
"strings"
"testing"
@ -104,7 +104,7 @@ func TestTagGenerate(t *testing.T) {
},
}
in, err := ioutil.ReadFile("./test_data/protobuf_tag_test.out")
in, err := os.ReadFile("./test_data/protobuf_tag_test.out")
if err != nil {
t.Fatal(err)
}

View File

@ -17,7 +17,7 @@
package thrift
import (
"io/ioutil"
"os"
"testing"
"github.com/cloudwego/hertz/cmd/hz/generator"
@ -27,7 +27,7 @@ import (
)
func TestRun(t *testing.T) {
data, err := ioutil.ReadFile("../testdata/request_thrift.out")
data, err := os.ReadFile("../testdata/request_thrift.out")
if err != nil {
t.Fatal(err)
}

View File

@ -17,7 +17,7 @@
package thrift
import (
"io/ioutil"
"os"
"strings"
"testing"
@ -26,7 +26,7 @@ import (
)
func TestInsertTag(t *testing.T) {
data, err := ioutil.ReadFile("./test_data/thrift_tag_test.out")
data, err := os.ReadFile("./test_data/thrift_tag_test.out")
if err != nil {
t.Fatal(err)
}

View File

@ -20,7 +20,6 @@ import (
"bytes"
"fmt"
"go/build"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -85,7 +84,7 @@ var goModReg = regexp.MustCompile(`^\s*module\s+(\S+)\s*`)
func SearchGoMod(cwd string, recurse bool) (moduleName, path string, found bool) {
for {
path = filepath.Join(cwd, "go.mod")
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err == nil {
for _, line := range strings.Split(string(data), "\n") {
m := goModReg.FindStringSubmatch(line)

View File

@ -187,7 +187,6 @@ func DoRedirects(ctx context.Context, req *protocol.Request, resp *protocol.Resp
// is too small a new slice will be allocated.
//
// The function follows redirects. Use Do* for manually handling redirects.
//
func Get(ctx context.Context, dst []byte, url string, requestOptions ...config.RequestOption) (statusCode int, body []byte, err error) {
return defaultClient.Get(ctx, dst, url, requestOptions...)
}

View File

@ -48,7 +48,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"net/http"
@ -1504,7 +1503,7 @@ func TestClientReadResponseBodyStream(t *testing.T) {
if string(p) != part1 {
t.Errorf("read len=%v, read content=%v; want len=%v, want content=%v", r, string(p), len(part1), part1)
}
left, _ := ioutil.ReadAll(bodyStream)
left, _ := io.ReadAll(bodyStream)
if string(left) != part2 {
t.Errorf("left len=%v, left content=%v; want len=%v, want content=%v", len(left), string(left), len(part2), part2)
}
@ -1871,13 +1870,13 @@ func TestClientReadResponseBodyStreamWithDoubleRequest(t *testing.T) {
if bodyStream1 == nil {
t.Errorf("bodystream1 is nil")
}
data, _ := ioutil.ReadAll(bodyStream1)
data, _ := io.ReadAll(bodyStream1)
if string(data) != part1+part2 {
t.Errorf("read len=%v, read content=%v; want len=%v, want content=%v", len(data), data, len(part1+part2), part1+part2)
}
// read left bodystream
left, _ := ioutil.ReadAll(bodyStream)
left, _ := io.ReadAll(bodyStream)
if string(left) != part2 {
t.Errorf("left len=%v, left content=%v; want len=%v, want content=%v", len(left), string(left), len(part2), part2)
}

View File

@ -275,8 +275,8 @@ type HijackHandler func(c network.Conn)
//
// The server skips calling the handler in the following cases:
//
// * 'Connection: close' header exists in either request or response.
// * Unexpected error during response writing to the connection.
// - 'Connection: close' header exists in either request or response.
// - Unexpected error during response writing to the connection.
//
// The server stops processing requests from hijacked connections.
//
@ -288,9 +288,8 @@ type HijackHandler func(c network.Conn)
// Arbitrary 'Connection: Upgrade' protocols may be implemented
// with HijackHandler. For instance,
//
// * WebSocket ( https://en.wikipedia.org/wiki/WebSocket )
// * HTTP/2.0 ( https://en.wikipedia.org/wiki/HTTP/2 )
//
// - WebSocket ( https://en.wikipedia.org/wiki/WebSocket )
// - HTTP/2.0 ( https://en.wikipedia.org/wiki/HTTP/2 )
func (ctx *RequestContext) Hijack(handler HijackHandler) {
ctx.hijackHandler = handler
}
@ -441,9 +440,10 @@ func (ctx *RequestContext) String(code int, format string, values ...interface{}
// FullPath returns a matched route full path. For not found routes
// returns an empty string.
// router.GET("/user/:id", func(c *hertz.RequestContext) {
// c.FullPath() == "/user/:id" // true
// })
//
// router.GET("/user/:id", func(c *hertz.RequestContext) {
// c.FullPath() == "/user/:id" // true
// })
func (ctx *RequestContext) FullPath() string {
return ctx.fullPath
}
@ -507,15 +507,15 @@ func (ctx *RequestContext) FormFile(name string) (*multipart.FileHeader, error)
//
// The value is searched in the following places:
//
// * Query string.
// * POST or PUT body.
// - Query string.
// - POST or PUT body.
//
// There are more fine-grained methods for obtaining form values:
//
// * QueryArgs for obtaining values from query string.
// * PostArgs for obtaining values from POST or PUT body.
// * MultipartForm for obtaining values from multipart form.
// * FormFile for obtaining uploaded files.
// - QueryArgs for obtaining values from query string.
// - PostArgs for obtaining values from POST or PUT body.
// - MultipartForm for obtaining values from multipart form.
// - FormFile for obtaining uploaded files.
//
// The returned value is valid until returning from RequestHandler.
// Use engine.SetCustomFormValueFunc to change action of FormValue.
@ -678,7 +678,7 @@ func (ctx *RequestContext) SetHandlers(hc HandlersChain) {
// HandlerName returns the main handler's name.
//
//For example if the handler is "handleGetUsers()", this function will return "main.handleGetUsers".
// For example if the handler is "handleGetUsers()", this function will return "main.handleGetUsers".
func (ctx *RequestContext) HandlerName() string {
return utils.NameOfFunction(ctx.handlers.Last())
}
@ -889,10 +889,11 @@ func (ctx *RequestContext) GetStringMapStringSlice(key string) (smss map[string]
// Param returns the value of the URL param.
// It is a shortcut for c.Params.ByName(key)
// router.GET("/user/:id", func(c *hertz.RequestContext) {
// // a GET request to /user/john
// id := c.Param("id") // id == "john"
// })
//
// router.GET("/user/:id", func(c *hertz.RequestContext) {
// // a GET request to /user/john
// id := c.Param("id") // id == "john"
// })
func (ctx *RequestContext) Param(key string) string {
return ctx.Params.ByName(key)
}
@ -1046,22 +1047,23 @@ func (ctx *RequestContext) Cookie(key string) []byte {
}
// SetCookie adds a Set-Cookie header to the Response's headers.
// Parameter introduce:
// name and value is used to set cookie's name and value, eg. Set-Cookie: name=value
// maxAge is use to set cookie's expiry date, eg. Set-Cookie: name=value; max-age=1
// path and domain is used to set the scope of a cookie, eg. Set-Cookie: name=value;domain=localhost; path=/;
// secure and httpOnly is used to sent cookies securely; eg. Set-Cookie: name=value;HttpOnly; secure;
// sameSite let servers specify whether/when cookies are sent with cross-site requests; eg. Set-Cookie: name=value;HttpOnly; secure; SameSite=Lax;
//
// For example:
// 1. ctx.SetCookie("user", "hertz", 1, "/", "localhost",protocol.CookieSameSiteLaxMode, true, true)
// add response header ---> Set-Cookie: user=hertz; max-age=1; domain=localhost; path=/; HttpOnly; secure; SameSite=Lax;
// 2. ctx.SetCookie("user", "hertz", 10, "/", "localhost",protocol.CookieSameSiteLaxMode, false, false)
// add response header ---> Set-Cookie: user=hertz; max-age=10; domain=localhost; path=/; SameSite=Lax;
// 3. ctx.SetCookie("", "hertz", 10, "/", "localhost",protocol.CookieSameSiteLaxMode, false, false)
// add response header ---> Set-Cookie: hertz; max-age=10; domain=localhost; path=/; SameSite=Lax;
// 4. ctx.SetCookie("user", "", 10, "/", "localhost",protocol.CookieSameSiteLaxMode, false, false)
// add response header ---> Set-Cookie: user=; max-age=10; domain=localhost; path=/; SameSite=Lax;
// Parameter introduce:
// name and value is used to set cookie's name and value, eg. Set-Cookie: name=value
// maxAge is use to set cookie's expiry date, eg. Set-Cookie: name=value; max-age=1
// path and domain is used to set the scope of a cookie, eg. Set-Cookie: name=value;domain=localhost; path=/;
// secure and httpOnly is used to sent cookies securely; eg. Set-Cookie: name=value;HttpOnly; secure;
// sameSite let servers specify whether/when cookies are sent with cross-site requests; eg. Set-Cookie: name=value;HttpOnly; secure; SameSite=Lax;
//
// For example:
// 1. ctx.SetCookie("user", "hertz", 1, "/", "localhost",protocol.CookieSameSiteLaxMode, true, true)
// add response header ---> Set-Cookie: user=hertz; max-age=1; domain=localhost; path=/; HttpOnly; secure; SameSite=Lax;
// 2. ctx.SetCookie("user", "hertz", 10, "/", "localhost",protocol.CookieSameSiteLaxMode, false, false)
// add response header ---> Set-Cookie: user=hertz; max-age=10; domain=localhost; path=/; SameSite=Lax;
// 3. ctx.SetCookie("", "hertz", 10, "/", "localhost",protocol.CookieSameSiteLaxMode, false, false)
// add response header ---> Set-Cookie: hertz; max-age=10; domain=localhost; path=/; SameSite=Lax;
// 4. ctx.SetCookie("user", "", 10, "/", "localhost",protocol.CookieSameSiteLaxMode, false, false)
// add response header ---> Set-Cookie: user=; max-age=10; domain=localhost; path=/; SameSite=Lax;
func (ctx *RequestContext) SetCookie(name, value string, maxAge int, path, domain string, sameSite protocol.CookieSameSite, secure, httpOnly bool) {
if path == "" {
path = "/"
@ -1135,11 +1137,12 @@ func (ctx *RequestContext) PostArgs() *protocol.Args {
// Query returns the keyed url query value if it exists, otherwise it returns an empty string `("")`.
//
// For example:
// GET /path?id=1234&name=Manu&value=
// c.Query("id") == "1234"
// c.Query("name") == "Manu"
// c.Query("value") == ""
// c.Query("wtf") == ""
//
// GET /path?id=1234&name=Manu&value=
// c.Query("id") == "1234"
// c.Query("name") == "Manu"
// c.Query("value") == ""
// c.Query("wtf") == ""
func (ctx *RequestContext) Query(key string) string {
value, _ := ctx.GetQuery(key)
return value
@ -1159,10 +1162,11 @@ func (ctx *RequestContext) DefaultQuery(key, defaultValue string) string {
// if it exists `(value, true)` (even when the value is an empty string) will be returned,
// otherwise it returns `("", false)`.
// For example:
// GET /?name=Manu&lastname=
// ("Manu", true) == c.GetQuery("name")
// ("", false) == c.GetQuery("id")
// ("", true) == c.GetQuery("lastname")
//
// GET /?name=Manu&lastname=
// ("Manu", true) == c.GetQuery("name")
// ("", false) == c.GetQuery("id")
// ("", true) == c.GetQuery("lastname")
func (ctx *RequestContext) GetQuery(key string) (string, bool) {
return ctx.QueryArgs().PeekExists(key)
}
@ -1190,9 +1194,10 @@ func (ctx *RequestContext) DefaultPostForm(key, defaultValue string) string {
// otherwise it returns ("", false).
//
// For example, during a PATCH request to update the user's email:
// email=mail@example.com --> ("mail@example.com", true) := GetPostForm("email") // set email to "mail@example.com"
// email= --> ("", true) := GetPostForm("email") // set email to ""
// --> ("", false) := GetPostForm("email") // do nothing with email
//
// email=mail@example.com --> ("mail@example.com", true) := GetPostForm("email") // set email to "mail@example.com"
// email= --> ("", true) := GetPostForm("email") // set email to ""
// --> ("", false) := GetPostForm("email") // do nothing with email
func (ctx *RequestContext) GetPostForm(key string) (string, bool) {
if v, exists := ctx.PostArgs().PeekExists(key); exists {
return v, exists

View File

@ -20,7 +20,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"reflect"
"strings"
"testing"
@ -401,7 +401,7 @@ tailfoobar`
if err := req.Read(&ctx.Request, mr); err != nil {
t.Fatalf("unexpected error: %s", err)
}
tail, err := ioutil.ReadAll(mr)
tail, err := io.ReadAll(mr)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

View File

@ -48,7 +48,6 @@ import (
"fmt"
"html"
"io"
"io/ioutil"
"mime"
"net/http"
"os"
@ -270,8 +269,8 @@ func (r *fsSmallFileReader) WriteTo(w io.Writer) (int64, error) {
//
// HTTP response may contain uncompressed file contents in the following cases:
//
// * Missing 'Accept-Encoding: gzip' request header.
// * No write access to directory containing the file.
// - Missing 'Accept-Encoding: gzip' request header.
// - No write access to directory containing the file.
//
// Directory contents is returned if path points to directory.
//
@ -1077,7 +1076,7 @@ func readFileHeader(f *os.File, compressed bool) ([]byte, error) {
R: r,
N: 512,
}
data, err := ioutil.ReadAll(lr)
data, err := io.ReadAll(lr)
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}
@ -1155,12 +1154,11 @@ func ParseByteRange(byteRange []byte, contentLength int) (startPos, endPos int,
//
// Examples:
//
// * host=foobar.com, slashesCount=0, original path="/foo/bar".
// - host=foobar.com, slashesCount=0, original path="/foo/bar".
// Resulting path: "/foobar.com/foo/bar"
//
// * host=img.aaa.com, slashesCount=1, original path="/images/123/456.jpg"
// - host=img.aaa.com, slashesCount=1, original path="/images/123/456.jpg"
// Resulting path: "/img.aaa.com/123/456.jpg"
//
func NewVHostPathRewriter(slashesCount int) PathRewriteFunc {
return func(ctx *RequestContext) []byte {
path := stripLeadingSlashes(ctx.Path(), slashesCount)
@ -1217,9 +1215,9 @@ func ServeFileUncompressed(ctx *RequestContext, path string) {
//
// Examples:
//
// * slashesCount = 0, original path: "/foo/bar", result: "/foo/bar"
// * slashesCount = 1, original path: "/foo/bar", result: "/bar"
// * slashesCount = 2, original path: "/foo/bar", result: ""
// - slashesCount = 0, original path: "/foo/bar", result: "/foo/bar"
// - slashesCount = 1, original path: "/foo/bar", result: "/bar"
// - slashesCount = 2, original path: "/foo/bar", result: ""
//
// The returned path rewriter may be used as FS.PathRewrite .
func NewPathSlashesStripper(slashesCount int) PathRewriteFunc {

View File

@ -46,7 +46,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path"
@ -185,13 +184,13 @@ func TestServeFileSmallNoReadFrom(t *testing.T) {
teststr := "hello, world!"
tempdir, err := ioutil.TempDir("", "httpexpect")
tempdir, err := os.MkdirTemp("", "httpexpect")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempdir)
if err := ioutil.WriteFile(
if err := os.WriteFile(
path.Join(tempdir, "hello"), []byte(teststr), 0o666); err != nil {
t.Fatal(err)
}
@ -399,7 +398,7 @@ func getFileContents(path string) ([]byte, error) {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
return io.ReadAll(f)
}
func TestParseByteRangeSuccess(t *testing.T) {

View File

@ -20,7 +20,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"runtime"
"github.com/cloudwego/hertz/pkg/app"
@ -66,7 +66,7 @@ func stack(skip int) []byte {
// Print this much at least. If we can't find the source, it won't show.
fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
if file != lastFile {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
continue
}

View File

@ -36,22 +36,25 @@ var defaultBinder = binding.Default()
// BindAndValidate binds data from *protocol.Request to obj and validates them if needed.
// NOTE:
// obj should be a pointer.
//
// obj should be a pointer.
func BindAndValidate(req *protocol.Request, obj interface{}, pathParams param.Params) error {
return defaultBinder.IBindAndValidate(obj, wrapRequest(req), pathParams)
}
// Bind binds data from *protocol.Request to obj.
// NOTE:
// obj should be a pointer.
//
// obj should be a pointer.
func Bind(req *protocol.Request, obj interface{}, pathParams param.Params) error {
return defaultBinder.IBind(obj, wrapRequest(req), pathParams)
}
// Validate validates obj with "vd" tag
// NOTE:
// obj should be a pointer.
// Validate should be called after Bind.
//
// obj should be a pointer.
// Validate should be called after Bind.
func Validate(obj interface{}) error {
return defaultBinder.Validate(obj)
}
@ -59,54 +62,61 @@ func Validate(obj interface{}) error {
// SetLooseZeroMode if set to true,
// the empty string request parameter is bound to the zero value of parameter.
// NOTE:
// The default is false.
// Suitable for these parameter types: query/header/cookie/form .
//
// The default is false.
// Suitable for these parameter types: query/header/cookie/form .
func SetLooseZeroMode(enable bool) {
defaultBinder.SetLooseZeroMode(enable)
}
// SetErrorFactory customizes the factory of validation error.
// NOTE:
// If errFactory==nil, the default is used.
// SetErrorFactory will remain in effect once it has been called.
//
// If errFactory==nil, the default is used.
// SetErrorFactory will remain in effect once it has been called.
func SetErrorFactory(bindErrFactory, validatingErrFactory func(failField, msg string) error) {
defaultBinder.SetErrorFactory(bindErrFactory, validatingErrFactory)
}
// MustRegTypeUnmarshal registers unmarshal function of type.
// NOTE:
// It will panic if exist error.
// MustRegTypeUnmarshal will remain in effect once it has been called.
//
// It will panic if exist error.
// MustRegTypeUnmarshal will remain in effect once it has been called.
func MustRegTypeUnmarshal(t reflect.Type, fn func(v string, emptyAsZero bool) (reflect.Value, error)) {
binding.MustRegTypeUnmarshal(t, fn)
}
// MustRegValidateFunc registers validator function expression.
// NOTE:
// If force=true, allow to cover the existed same funcName.
// MustRegValidateFunc will remain in effect once it has been called.
//
// If force=true, allow to cover the existed same funcName.
// MustRegValidateFunc will remain in effect once it has been called.
func MustRegValidateFunc(funcName string, fn func(args ...interface{}) error, force ...bool) {
validator.RegFunc(funcName, fn, force...)
}
// UseStdJSONUnmarshaler uses encoding/json as json library
// NOTE:
// The current version uses encoding/json by default.
// UseStdJSONUnmarshaler will remain in effect once it has been called.
//
// The current version uses encoding/json by default.
// UseStdJSONUnmarshaler will remain in effect once it has been called.
func UseStdJSONUnmarshaler() {
binding.ResetJSONUnmarshaler(json.Unmarshal)
}
// UseGJSONUnmarshaler uses github.com/bytedance/go-tagexpr/v2/binding/gjson as json library
// NOTE:
// UseGJSONUnmarshaler will remain in effect once it has been called.
//
// UseGJSONUnmarshaler will remain in effect once it has been called.
func UseGJSONUnmarshaler() {
gjson.UseJSONUnmarshaler()
}
// UseThirdPartyJSONUnmarshaler uses third-party json library for binding
// NOTE:
// UseThirdPartyJSONUnmarshaler will remain in effect once it has been called.
//
// UseThirdPartyJSONUnmarshaler will remain in effect once it has been called.
func UseThirdPartyJSONUnmarshaler(unmarshaler func(data []byte, v interface{}) error) {
binding.ResetJSONUnmarshaler(unmarshaler)
}

View File

@ -23,7 +23,6 @@ import (
"fmt"
"html/template"
"io"
"io/ioutil"
"net"
"net/http"
"os"
@ -397,7 +396,7 @@ func TestNotEnoughBodySize(t *testing.T) {
resp, err := http.Post("http://127.0.0.1:8889/test", "application/x-www-form-urlencoded", body)
assert.Nil(t, err)
assert.DeepEqual(t, 413, resp.StatusCode)
bodyBytes, _ := ioutil.ReadAll(resp.Body)
bodyBytes, _ := io.ReadAll(resp.Body)
assert.DeepEqual(t, "Request Entity Too Large", string(bodyBytes))
}

View File

@ -18,7 +18,7 @@ package adaptor
import (
"context"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
@ -87,7 +87,7 @@ func makeACall(t *testing.T, method, url string, header http.Header, body string
}
}
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Read body error: %s", err)
}
@ -116,7 +116,7 @@ func handlerAndCheck(t *testing.T, writer http.ResponseWriter, request *http.Req
}
}
body, err := ioutil.ReadAll(request.Body)
body, err := io.ReadAll(request.Body)
if err != nil {
t.Fatalf("Read body error: %s", err)
}

View File

@ -155,11 +155,11 @@ func AppendGzipBytes(dst, src []byte) []byte {
//
// Supported compression levels are:
//
// * CompressNoCompression
// * CompressBestSpeed
// * CompressBestCompression
// * CompressDefaultCompression
// * CompressHuffmanOnly
// - CompressNoCompression
// - CompressBestSpeed
// - CompressBestCompression
// - CompressDefaultCompression
// - CompressHuffmanOnly
func AppendGzipBytesLevel(dst, src []byte, level int) []byte {
w := &byteSliceWriter{dst}
WriteGzipLevel(w, src, level) //nolint:errcheck
@ -219,11 +219,11 @@ func normalizeCompressLevel(level int) int {
//
// Supported compression levels are:
//
// * CompressNoCompression
// * CompressBestSpeed
// * CompressBestCompression
// * CompressDefaultCompression
// * CompressHuffmanOnly
// - CompressNoCompression
// - CompressBestSpeed
// - CompressBestCompression
// - CompressDefaultCompression
// - CompressHuffmanOnly
func WriteGzipLevel(w io.Writer, p []byte, level int) (int, error) {
switch w.(type) {
case *byteSliceWriter,

View File

@ -155,10 +155,11 @@ func (msg *Error) JSON() interface{} {
// Errors returns an array will all the error messages.
// Example:
// c.Error(errors.New("first"))
// c.Error(errors.New("second"))
// c.Error(errors.New("third"))
// c.Errors.Errors() // == []string{"first", "second", "third"}
//
// c.Error(errors.New("first"))
// c.Error(errors.New("second"))
// c.Error(errors.New("third"))
// c.Errors.Errors() // == []string{"first", "second", "third"}
func (a ErrorChain) Errors() []string {
if len(a) == 0 {
return nil

View File

@ -53,9 +53,9 @@ import (
// The wrapper may save a lot of stack space if the following conditions
// are met:
//
// - f doesn't contain blocking calls on network, I/O or channels;
// - f uses a lot of stack space;
// - the wrapper is called from high number of concurrent goroutines.
// - f doesn't contain blocking calls on network, I/O or channels;
// - f uses a lot of stack space;
// - the wrapper is called from high number of concurrent goroutines.
//
// The stackless wrapper returns false if the call cannot be processed
// at the moment due to high load.

View File

@ -47,7 +47,6 @@ import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"testing"
"time"
)
@ -129,7 +128,7 @@ func testWriterReuse(w Writer, r io.Reader, newReader func(io.Reader) io.Reader)
w.Close()
zr := newReader(r)
data, err := ioutil.ReadAll(zr)
data, err := io.ReadAll(zr)
if err != nil {
return fmt.Errorf("unexpected error: %s, data=%q", err, data)
}

View File

@ -19,7 +19,6 @@ package assert
import (
"bytes"
"io"
"io/ioutil"
"reflect"
"testing"
)
@ -117,7 +116,7 @@ func NotPanic(t testing.TB, fn func()) {
}
func VerifyTrailer(t *testing.T, r io.Reader, expectedTrailer string) {
trailer, err := ioutil.ReadAll(r)
trailer, err := io.ReadAll(r)
if err != nil {
t.Fatalf("Cannot read trailer: %s", err)
}

View File

@ -20,7 +20,6 @@ package ut
import (
"context"
"io"
"io/ioutil"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/route"
@ -40,7 +39,7 @@ type Body struct {
// PerformRequest send a constructed request to given engine without network transporting
//
// Url can be a standard relative URI or a simple absolute path
// # Url can be a standard relative URI or a simple absolute path
//
// If engine.streamRequestBody is true, it sets body as bodyStream
// if not, it sets body as bodyBytes
@ -58,7 +57,7 @@ func PerformRequest(engine *route.Engine, method, url string, body *Body, header
if engine.IsStreamRequestBody() || body.Len == -1 {
ctx.Request.SetBodyStream(body.Body, body.Len)
} else {
buf, err := ioutil.ReadAll(&io.LimitedReader{R: body.Body, N: int64(body.Len)})
buf, err := io.ReadAll(&io.LimitedReader{R: body.Body, N: int64(body.Len)})
ctx.Request.SetBody(buf)
if err != nil && err != io.EOF {
panic(err)

View File

@ -47,12 +47,12 @@ import "strings"
//
// The following rules are applied iteratively until no further processing can
// be done:
// 1. Replace multiple slashes with a single slash.
// 2. Eliminate each . path name element (the current directory).
// 3. Eliminate each inner .. path name element (the parent directory)
// along with the non-.. element that precedes it.
// 4. Eliminate .. elements that begin a rooted path:
// that is, replace "/.." by "/" at the beginning of a path.
// 1. Replace multiple slashes with a single slash.
// 2. Eliminate each . path name element (the current directory).
// 3. Eliminate each inner .. path name element (the parent directory)
// along with the non-.. element that precedes it.
// 4. Eliminate .. elements that begin a rooted path:
// that is, replace "/.." by "/" at the beginning of a path.
//
// If the result of this process is an empty string, "/" is returned
func CleanPath(p string) string {

View File

@ -232,7 +232,6 @@ func (h *ResponseHeader) CopyTo(dst *ResponseHeader) {
dst.cookies = copyArgs(dst.cookies, h.cookies)
}
//
// Multiple headers with the same key may be added with this function.
// Use Set for setting a single header for the given key.
//
@ -610,13 +609,13 @@ func (h *ResponseHeader) PeekLocation() []byte {
// This doesn't work for a cookie with specific domain or path,
// you should delete it manually like:
//
// c := AcquireCookie()
// c.SetKey(key)
// c.SetDomain("example.com")
// c.SetPath("/path")
// c.SetExpire(CookieExpireDelete)
// h.SetCookie(c)
// ReleaseCookie(c)
// c := AcquireCookie()
// c.SetKey(key)
// c.SetDomain("example.com")
// c.SetPath("/path")
// c.SetExpire(CookieExpireDelete)
// h.SetCookie(c)
// ReleaseCookie(c)
//
// Use DelCookie if you want just removing the cookie from response header.
func (h *ResponseHeader) DelClientCookie(key string) {
@ -633,13 +632,13 @@ func (h *ResponseHeader) DelClientCookie(key string) {
// This doesn't work for a cookie with specific domain or path,
// you should delete it manually like:
//
// c := AcquireCookie()
// c.SetKey(key)
// c.SetDomain("example.com")
// c.SetPath("/path")
// c.SetExpire(CookieExpireDelete)
// h.SetCookie(c)
// ReleaseCookie(c)
// c := AcquireCookie()
// c.SetKey(key)
// c.SetDomain("example.com")
// c.SetPath("/path")
// c.SetExpire(CookieExpireDelete)
// h.SetCookie(c)
// ReleaseCookie(c)
//
// Use DelCookieBytes if you want just removing the cookie from response header.
func (h *ResponseHeader) DelClientCookieBytes(key []byte) {
@ -907,8 +906,8 @@ func (h *RequestHeader) Reset() {
// SetByteRange sets 'Range: bytes=startPos-endPos' header.
//
// * If startPos is negative, then 'bytes=-startPos' value is set.
// * If endPos is negative, then 'bytes=startPos-' value is set.
// - If startPos is negative, then 'bytes=-startPos' value is set.
// - If endPos is negative, then 'bytes=startPos-' value is set.
func (h *RequestHeader) SetByteRange(startPos, endPos int) {
b := h.bufKV.value[:0]
b = append(b, bytestr.StrBytes...)
@ -1336,9 +1335,9 @@ func (h *RequestHeader) UserAgent() []byte {
// while lowercasing all the other letters.
// Examples:
//
// * CONNECTION -> Connection
// * conteNT-tYPE -> Content-Type
// * foo-bar-baz -> Foo-Bar-Baz
// - CONNECTION -> Connection
// - conteNT-tYPE -> Content-Type
// - foo-bar-baz -> Foo-Bar-Baz
//
// Disable header names' normalization only if you know what are you doing.
func (h *RequestHeader) DisableNormalizing() {
@ -1509,9 +1508,9 @@ func (h *RequestHeader) SetMethodBytes(method []byte) {
// while lowercasing all the other letters.
// Examples:
//
// * CONNECTION -> Connection
// * conteNT-tYPE -> Content-Type
// * foo-bar-baz -> Foo-Bar-Baz
// - CONNECTION -> Connection
// - conteNT-tYPE -> Content-Type
// - foo-bar-baz -> Foo-Bar-Baz
//
// Disable header names' normalization only if you know what are you doing.
func (h *ResponseHeader) DisableNormalizing() {

View File

@ -47,7 +47,7 @@ import (
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"strings"
"sync"
@ -187,7 +187,7 @@ func testContinueReadResponseBodyStream(t *testing.T, header, body string, maxBo
t.Fatalf("should read %d from stream body, but got %d", firstRead, sR)
}
leftB, _ := ioutil.ReadAll(r.BodyStream())
leftB, _ := io.ReadAll(r.BodyStream())
if len(leftB) != leftBytes {
t.Fatalf("should left %d bytes from stream body, but left %d", leftBytes, len(leftB))
}

View File

@ -102,11 +102,11 @@ func GetHTTP1Request(req *protocol.Request) fmt.Stringer {
//
// If MayContinue returns true, the caller must:
//
// - Either send StatusExpectationFailed response if request headers don't
// satisfy the caller.
// - Or send StatusContinue response before reading request body
// with ContinueReadBody.
// - Or close the connection.
// - Either send StatusExpectationFailed response if request headers don't
// satisfy the caller.
// - Or send StatusContinue response before reading request body
// with ContinueReadBody.
// - Or close the connection.
//
// io.EOF is returned if r is closed before reading the first header byte.
func ReadHeaderAndLimitBody(req *protocol.Request, r network.Reader, maxBodySize int, preParse ...bool) error {
@ -133,11 +133,11 @@ func ReadHeaderAndLimitBody(req *protocol.Request, r network.Reader, maxBodySize
//
// If MayContinue returns true, the caller must:
//
// - Either send StatusExpectationFailed response if request headers don't
// satisfy the caller.
// - Or send StatusContinue response before reading request body
// with ContinueReadBody.
// - Or close the connection.
// - Either send StatusExpectationFailed response if request headers don't
// satisfy the caller.
// - Or send StatusContinue response before reading request body
// with ContinueReadBody.
// - Or close the connection.
//
// io.EOF is returned if r is closed before reading the first header byte.
func Read(req *protocol.Request, r network.Reader, preParse ...bool) error {

View File

@ -47,7 +47,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/url"
"strings"
@ -657,7 +656,7 @@ func TestRequestReadPostNoBody(t *testing.T) {
t.Fatalf("unexpected content-length: %d. Expecting 0", r.Header.ContentLength())
}
tail, err := ioutil.ReadAll(zr)
tail, err := io.ReadAll(zr)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
@ -1049,7 +1048,7 @@ tailfoobar`
t.Fatalf("unexpected error: %s", err)
}
tail, err := ioutil.ReadAll(mr)
tail, err := io.ReadAll(mr)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
@ -1130,7 +1129,7 @@ func testReadIncompleteStream(t *testing.T, header, body string) {
if err := ContinueReadBodyStream(&r, mr, 1, true); err != nil {
t.Fatalf("error when reading request body stream: %s", err)
}
readBody, err := ioutil.ReadAll(r.BodyStream())
readBody, err := io.ReadAll(r.BodyStream())
if !bytes.Equal(readBody, []byte(body)) || len(readBody) != len(body) {
t.Fatalf("readBody is not equal to the rawBody: %b(len: %d)", readBody, len(readBody))
}
@ -1160,7 +1159,7 @@ func testReadChunked(t *testing.T, header, body string, firstRead, leftBytes int
if fr != firstRead {
t.Fatalf("should read %d from stream body, but got %d", streamRead, fr)
}
leftB, _ := ioutil.ReadAll(r.BodyStream())
leftB, _ := io.ReadAll(r.BodyStream())
if len(leftB) != leftBytes {
t.Fatalf("should left %d bytes from stream body, but left %d", leftBytes, len(leftB))
}
@ -1183,7 +1182,7 @@ func testContinueReadBodyStream(t *testing.T, header, body string, maxBodySize,
t.Fatalf("should read %d from stream body, but got %d", firstRead, sR)
}
leftB, _ := ioutil.ReadAll(r.BodyStream())
leftB, _ := io.ReadAll(r.BodyStream())
if len(leftB) != leftBytes {
t.Fatalf("should left %d bytes from stream body, but left %d", leftBytes, len(leftB))
}
@ -1255,7 +1254,7 @@ tailfoobar`
t.Fatalf("unexpected error: %s", err)
}
tail, err := ioutil.ReadAll(mr)
tail, err := io.ReadAll(mr)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

View File

@ -158,11 +158,11 @@ func (req *Request) BodyBuffer() *bytebufferpool.ByteBuffer {
//
// The caller must do one of the following actions if MayContinue returns true:
//
// - Either send StatusExpectationFailed response if request headers don't
// satisfy the caller.
// - Or send StatusContinue response before reading request body
// with ContinueReadBody.
// - Or close the connection.
// - Either send StatusExpectationFailed response if request headers don't
// satisfy the caller.
// - Or send StatusContinue response before reading request body
// with ContinueReadBody.
// - Or close the connection.
func (req *Request) MayContinue() bool {
return bytes.Equal(req.Header.peek(bytestr.StrExpect), bytestr.Str100Continue)
}
@ -563,13 +563,15 @@ func parseBasicAuth(auth []byte) (username, password string, ok bool) {
}
// SetAuthToken sets the auth token header(Default Scheme: Bearer) in the current HTTP request. Header example:
// Authorization: Bearer <auth-token-value-comes-here>
//
// Authorization: Bearer <auth-token-value-comes-here>
func (req *Request) SetAuthToken(token string) {
req.SetHeader(consts.HeaderAuthorization, "Bearer "+token)
}
// SetAuthSchemeToken sets the auth token scheme type in the HTTP request. For Example:
// Authorization: <auth-scheme-value-set-here> <auth-token-value>
//
// Authorization: <auth-scheme-value-set-here> <auth-token-value>
func (req *Request) SetAuthSchemeToken(scheme, token string) {
req.SetHeader(consts.HeaderAuthorization, scheme+" "+token)
}
@ -827,7 +829,7 @@ func ReleaseRequest(req *Request) {
// NewRequest makes a new Request given a method, URL, and
// optional body.
//
// Method's default value is GET
// # Method's default value is GET
//
// Url must contain fully qualified uri, i.e. with scheme and host,
// and http is assumed if scheme is omitted.

View File

@ -46,7 +46,6 @@ import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"math"
"mime/multipart"
"strings"
@ -148,7 +147,7 @@ tailfoobar`
defer r.RemoveMultipartFormFiles()
// verify tail
tail, err := ioutil.ReadAll(mr)
tail, err := io.ReadAll(mr)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
@ -230,7 +229,7 @@ tailfoobar`
defer r.RemoveMultipartFormFiles()
// all data must be consumed if the content length is unknown
tail, err := ioutil.ReadAll(mr)
tail, err := io.ReadAll(mr)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

View File

@ -278,12 +278,12 @@ func (engine *Engine) NewContext() *app.RequestContext {
// Shutdown starts the server's graceful exit by next steps:
//
// 1. Trigger OnShutdown hooks concurrently and wait them until wait timeout or finish
// 2. Close the net listener, which means new connection won't be accepted
// 3. Wait all connections get closed:
// One connection gets closed after reaching out the shorter time of processing
// one request (in hand or next incoming), idleTimeout or ExitWaitTime
// 4. Exit
// 1. Trigger OnShutdown hooks concurrently and wait them until wait timeout or finish
// 2. Close the net listener, which means new connection won't be accepted
// 3. Wait all connections get closed:
// One connection gets closed after reaching out the shorter time of processing
// one request (in hand or next incoming), idleTimeout or ExitWaitTime
// 4. Exit
func (engine *Engine) Shutdown(ctx context.Context) (err error) {
if atomic.LoadUint32(&engine.status) != statusRunning {
return errStatusNotRunning

View File

@ -46,7 +46,7 @@ import (
"errors"
"fmt"
"html/template"
"io/ioutil"
"io"
"net"
"net/http"
"sync/atomic"
@ -412,7 +412,7 @@ func TestRenderHtml(t *testing.T) {
})
})
rr := performRequest(e, "GET", "/templateName")
b, _ := ioutil.ReadAll(rr.Body)
b, _ := io.ReadAll(rr.Body)
assert.DeepEqual(t, consts.StatusOK, rr.Code)
assert.DeepEqual(t, []byte("<h1>Date: 2017/07/01</h1>"), b)
assert.DeepEqual(t, "text/html; charset=utf-8", rr.Header().Get("Content-Type"))
@ -460,7 +460,7 @@ func TestRenderHtmlOfGlobWithAutoRender(t *testing.T) {
})
})
rr := performRequest(e, "GET", "/templateName")
b, _ := ioutil.ReadAll(rr.Body)
b, _ := io.ReadAll(rr.Body)
assert.DeepEqual(t, consts.StatusOK, rr.Code)
assert.DeepEqual(t, []byte("<h1>Date: 2017/07/01</h1>"), b)
assert.DeepEqual(t, "text/html; charset=utf-8", rr.Header().Get("Content-Type"))
@ -498,7 +498,7 @@ func TestRenderHtmlOfFilesWithAutoRender(t *testing.T) {
})
})
rr := performRequest(e, "GET", "/templateName")
b, _ := ioutil.ReadAll(rr.Body)
b, _ := io.ReadAll(rr.Body)
assert.DeepEqual(t, consts.StatusOK, rr.Code)
assert.DeepEqual(t, []byte("<h1>Date: 2017/07/01</h1>"), b)
assert.DeepEqual(t, "text/html; charset=utf-8", rr.Header().Get("Content-Type"))

View File

@ -200,7 +200,8 @@ func (group *RouterGroup) StaticFile(relativePath, filepath string) IRoutes {
// Static serves files from the given file system root.
// To use the operating system's file system implementation,
// use :
// router.Static("/static", "/var/www")
//
// router.Static("/static", "/var/www")
func (group *RouterGroup) Static(relativePath, root string) IRoutes {
return group.StaticFS(relativePath, &app.FS{Root: root})
}

View File

@ -42,7 +42,7 @@ package route
import (
"context"
"io/ioutil"
"io"
"net/http"
"os"
"testing"
@ -137,7 +137,7 @@ func TestRouterGroupStatic(t *testing.T) {
}
assert.DeepEqual(t, http.StatusOK, w.Code)
defer fd.Close()
content, err := ioutil.ReadAll(fd)
content, err := io.ReadAll(fd)
if err != nil {
panic(err)
}
@ -154,7 +154,7 @@ func TestRouterGroupStaticFile(t *testing.T) {
panic(err)
}
defer fd.Close()
content, err := ioutil.ReadAll(fd)
content, err := io.ReadAll(fd)
if err != nil {
panic(err)
}

View File

@ -43,7 +43,6 @@ package route
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
@ -398,7 +397,7 @@ func TestRouteParamsByNameWithExtraSlash(t *testing.T) {
func TestRouteStaticFile(t *testing.T) {
// SETUP file
testRoot, _ := os.Getwd()
f, err := ioutil.TempFile(testRoot, "")
f, err := os.CreateTemp(testRoot, "")
if err != nil {
t.Error(err)
}