github.com/cloudwego/hertz@v0.9.3/pkg/app/server/binding/internal/decoder/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/pkg/protocol" 45 "github.com/cloudwego/hertz/pkg/route/param" 46 ) 47 48 type getter func(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret string, exist bool) 49 50 func path(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret string, exist bool) { 51 if params != nil { 52 ret, exist = params.Get(key) 53 } 54 55 if len(ret) == 0 && len(defaultValue) != 0 { 56 ret = defaultValue[0] 57 } 58 return ret, exist 59 } 60 61 func postForm(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret string, exist bool) { 62 if ret, exist = req.PostArgs().PeekExists(key); exist { 63 return 64 } 65 66 mf, err := req.MultipartForm() 67 if err == nil && mf.Value != nil { 68 for k, v := range mf.Value { 69 if k == key && len(v) > 0 { 70 ret = v[0] 71 } 72 } 73 } 74 75 if len(ret) != 0 { 76 return ret, true 77 } 78 if ret, exist = req.URI().QueryArgs().PeekExists(key); exist { 79 return 80 } 81 82 if len(ret) == 0 && len(defaultValue) != 0 { 83 ret = defaultValue[0] 84 } 85 86 return ret, false 87 } 88 89 func query(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret string, exist bool) { 90 if ret, exist = req.URI().QueryArgs().PeekExists(key); exist { 91 return 92 } 93 94 if len(ret) == 0 && len(defaultValue) != 0 { 95 ret = defaultValue[0] 96 } 97 98 return 99 } 100 101 func cookie(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret string, exist bool) { 102 if val := req.Header.Cookie(key); val != nil { 103 ret = string(val) 104 return ret, true 105 } 106 107 if len(ret) == 0 && len(defaultValue) != 0 { 108 ret = defaultValue[0] 109 } 110 111 return ret, false 112 } 113 114 func header(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret string, exist bool) { 115 if val := req.Header.Peek(key); val != nil { 116 ret = string(val) 117 return ret, true 118 } 119 120 if len(ret) == 0 && len(defaultValue) != 0 { 121 ret = defaultValue[0] 122 } 123 124 return ret, false 125 } 126 127 func rawBody(req *protocol.Request, params param.Params, key string, defaultValue ...string) (ret string, exist bool) { 128 exist = false 129 if req.Header.ContentLength() > 0 { 130 ret = string(req.Body()) 131 exist = true 132 } 133 return 134 }