github.com/cloudwego/hertz@v0.9.3/pkg/app/server/binding/internal/decoder/slice_getter.go (about) 1 /* 2 * Copyright 2023 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * MIT License 16 * 17 * Copyright (c) 2019-present Fenny and Contributors 18 * 19 * Permission is hereby granted, free of charge, to any person obtaining a copy 20 * of this software and associated documentation files (the "Software"), to deal 21 * in the Software without restriction, including without limitation the rights 22 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 23 * copies of the Software, and to permit persons to whom the Software is 24 * furnished to do so, subject to the following conditions: 25 * 26 * The above copyright notice and this permission notice shall be included in all 27 * copies or substantial portions of the Software. 28 * 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 34 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 35 * SOFTWARE. 36 * 37 * This file may have been modified by CloudWeGo authors. All CloudWeGo 38 * Modifications are Copyright 2023 CloudWeGo Authors 39 */ 40 41 package decoder 42 43 import ( 44 "github.com/cloudwego/hertz/internal/bytesconv" 45 "github.com/cloudwego/hertz/pkg/protocol" 46 "github.com/cloudwego/hertz/pkg/route/param" 47 ) 48 49 type sliceGetter func(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret []string) 50 51 func pathSlice(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret []string) { 52 var value string 53 if params != nil { 54 value, _ = params.Get(key) 55 } 56 57 if len(value) == 0 && len(defaultValue) != 0 { 58 value = defaultValue[0] 59 } 60 if len(value) != 0 { 61 ret = append(ret, value) 62 } 63 64 return 65 } 66 67 func postFormSlice(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret []string) { 68 req.PostArgs().VisitAll(func(formKey, value []byte) { 69 if bytesconv.B2s(formKey) == key { 70 ret = append(ret, string(value)) 71 } 72 }) 73 if len(ret) > 0 { 74 return 75 } 76 77 mf, err := req.MultipartForm() 78 if err == nil && mf.Value != nil { 79 for k, v := range mf.Value { 80 if k == key && len(v) > 0 { 81 ret = append(ret, v...) 82 } 83 } 84 } 85 if len(ret) > 0 { 86 return 87 } 88 89 if len(ret) == 0 && len(defaultValue) != 0 { 90 ret = append(ret, defaultValue...) 91 } 92 93 return 94 } 95 96 func querySlice(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret []string) { 97 req.URI().QueryArgs().VisitAll(func(queryKey, value []byte) { 98 if key == bytesconv.B2s(queryKey) { 99 ret = append(ret, string(value)) 100 } 101 }) 102 103 if len(ret) == 0 && len(defaultValue) != 0 { 104 ret = append(ret, defaultValue...) 105 } 106 107 return 108 } 109 110 func cookieSlice(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret []string) { 111 req.Header.VisitAllCookie(func(cookieKey, value []byte) { 112 if bytesconv.B2s(cookieKey) == key { 113 ret = append(ret, string(value)) 114 } 115 }) 116 117 if len(ret) == 0 && len(defaultValue) != 0 { 118 ret = append(ret, defaultValue...) 119 } 120 121 return 122 } 123 124 func headerSlice(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret []string) { 125 req.Header.VisitAll(func(headerKey, value []byte) { 126 if bytesconv.B2s(headerKey) == key { 127 ret = append(ret, string(value)) 128 } 129 }) 130 131 if len(ret) == 0 && len(defaultValue) != 0 { 132 ret = append(ret, defaultValue...) 133 } 134 135 return 136 } 137 138 func rawBodySlice(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret []string) { 139 if req.Header.ContentLength() > 0 { 140 ret = append(ret, string(req.Body())) 141 } 142 return 143 }