2014-05-06 21:20:31 +05:30
|
|
|
// Copyright 2013 The Beego Authors. All rights reserved.
|
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2014-05-06 21:20:31 +05:30
|
|
|
|
|
|
|
package httplib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-07-14 20:13:13 +05:30
|
|
|
"context"
|
2014-05-06 21:20:31 +05:30
|
|
|
"crypto/tls"
|
Refactor internal API for git commands, use meaningful messages instead of "Internal Server Error" (#23687)
# Why this PR comes
At first, I'd like to help users like #23636 (there are a lot)
The unclear "Internal Server Error" is quite anonying, scare users,
frustrate contributors, nobody knows what happens.
So, it's always good to provide meaningful messages to end users (of
course, do not leak sensitive information).
When I started working on the "response message to end users", I found
that the related code has a lot of technical debt. A lot of copy&paste
code, unclear fields and usages.
So I think it's good to make everything clear.
# Tech Backgrounds
Gitea has many sub-commands, some are used by admins, some are used by
SSH servers or Git Hooks. Many sub-commands use "internal API" to
communicate with Gitea web server.
Before, Gitea server always use `StatusCode + Json "err" field` to
return messages.
* The CLI sub-commands: they expect to show all error related messages
to site admin
* The Serv/Hook sub-commands (for git clients): they could only show
safe messages to end users, the error log could only be recorded by
"SSHLog" to Gitea web server.
In the old design, it assumes that:
* If the StatusCode is 500 (in some functions), then the "err" field is
error log, shouldn't be exposed to git client.
* If the StatusCode is 40x, then the "err" field could be exposed. And
some functions always read the "err" no matter what the StatusCode is.
The old code is not strict, and it's difficult to distinguish the
messages clearly and then output them correctly.
# This PR
To help to remove duplicate code and make everything clear, this PR
introduces `ResponseExtra` and `requestJSONResp`.
* `ResponseExtra` is a struct which contains "extra" information of a
internal API response, including StatusCode, UserMsg, Error
* `requestJSONResp` is a generic function which can be used for all
cases to help to simplify the calls.
* Remove all `map["err"]`, always use `private.Response{Err}` to
construct error messages.
* User messages and error messages are separated clearly, the `fail` and
`handleCliResponseExtra` will output correct messages.
* Replace all `Internal Server Error` messages with meaningful (still
safe) messages.
This PR saves more than 300 lines, while makes the git client messages
more clear.
Many gitea-serv/git-hook related essential functions are covered by
tests.
---------
Co-authored-by: delvh <dev.lh@web.de>
2023-03-29 12:02:26 +05:30
|
|
|
"fmt"
|
2014-05-06 21:20:31 +05:30
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-01-20 06:01:39 +05:30
|
|
|
var defaultSetting = Settings{"GiteaServer", 60 * time.Second, 60 * time.Second, nil, nil}
|
2014-08-23 18:43:55 +05:30
|
|
|
|
2016-11-25 12:02:09 +05:30
|
|
|
// newRequest returns *Request with specific method
|
2015-10-26 18:46:24 +05:30
|
|
|
func newRequest(url, method string) *Request {
|
2014-08-23 18:43:55 +05:30
|
|
|
var resp http.Response
|
|
|
|
req := http.Request{
|
|
|
|
Method: method,
|
|
|
|
Header: make(http.Header),
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
}
|
2022-01-20 06:01:39 +05:30
|
|
|
return &Request{url, &req, map[string]string{}, defaultSetting, &resp, nil}
|
2014-08-23 18:43:55 +05:30
|
|
|
}
|
2014-05-06 21:20:31 +05:30
|
|
|
|
2017-04-19 09:15:01 +05:30
|
|
|
// NewRequest returns *Request with specific method
|
|
|
|
func NewRequest(url, method string) *Request {
|
|
|
|
return newRequest(url, method)
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:02:09 +05:30
|
|
|
// Settings is the default settings for http client
|
2015-08-27 20:36:14 +05:30
|
|
|
type Settings struct {
|
2014-08-23 18:43:55 +05:30
|
|
|
UserAgent string
|
|
|
|
ConnectTimeout time.Duration
|
|
|
|
ReadWriteTimeout time.Duration
|
2016-11-25 12:02:09 +05:30
|
|
|
TLSClientConfig *tls.Config
|
2014-08-23 18:43:55 +05:30
|
|
|
Transport http.RoundTripper
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
|
|
|
|
2016-11-25 12:02:09 +05:30
|
|
|
// Request provides more useful methods for requesting one url than http.Request.
|
2015-08-27 20:36:14 +05:30
|
|
|
type Request struct {
|
2014-08-23 18:43:55 +05:30
|
|
|
url string
|
|
|
|
req *http.Request
|
|
|
|
params map[string]string
|
2015-08-27 20:36:14 +05:30
|
|
|
setting Settings
|
2014-08-23 18:43:55 +05:30
|
|
|
resp *http.Response
|
|
|
|
body []byte
|
|
|
|
}
|
|
|
|
|
2021-07-14 20:13:13 +05:30
|
|
|
// SetContext sets the request's Context
|
|
|
|
func (r *Request) SetContext(ctx context.Context) *Request {
|
|
|
|
r.req = r.req.WithContext(ctx)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-05-06 21:20:31 +05:30
|
|
|
// SetTimeout sets connect time out and read-write time out for BeegoRequest.
|
2015-08-27 20:36:14 +05:30
|
|
|
func (r *Request) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *Request {
|
|
|
|
r.setting.ConnectTimeout = connectTimeout
|
|
|
|
r.setting.ReadWriteTimeout = readWriteTimeout
|
|
|
|
return r
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
|
|
|
|
Refactor internal API for git commands, use meaningful messages instead of "Internal Server Error" (#23687)
# Why this PR comes
At first, I'd like to help users like #23636 (there are a lot)
The unclear "Internal Server Error" is quite anonying, scare users,
frustrate contributors, nobody knows what happens.
So, it's always good to provide meaningful messages to end users (of
course, do not leak sensitive information).
When I started working on the "response message to end users", I found
that the related code has a lot of technical debt. A lot of copy&paste
code, unclear fields and usages.
So I think it's good to make everything clear.
# Tech Backgrounds
Gitea has many sub-commands, some are used by admins, some are used by
SSH servers or Git Hooks. Many sub-commands use "internal API" to
communicate with Gitea web server.
Before, Gitea server always use `StatusCode + Json "err" field` to
return messages.
* The CLI sub-commands: they expect to show all error related messages
to site admin
* The Serv/Hook sub-commands (for git clients): they could only show
safe messages to end users, the error log could only be recorded by
"SSHLog" to Gitea web server.
In the old design, it assumes that:
* If the StatusCode is 500 (in some functions), then the "err" field is
error log, shouldn't be exposed to git client.
* If the StatusCode is 40x, then the "err" field could be exposed. And
some functions always read the "err" no matter what the StatusCode is.
The old code is not strict, and it's difficult to distinguish the
messages clearly and then output them correctly.
# This PR
To help to remove duplicate code and make everything clear, this PR
introduces `ResponseExtra` and `requestJSONResp`.
* `ResponseExtra` is a struct which contains "extra" information of a
internal API response, including StatusCode, UserMsg, Error
* `requestJSONResp` is a generic function which can be used for all
cases to help to simplify the calls.
* Remove all `map["err"]`, always use `private.Response{Err}` to
construct error messages.
* User messages and error messages are separated clearly, the `fail` and
`handleCliResponseExtra` will output correct messages.
* Replace all `Internal Server Error` messages with meaningful (still
safe) messages.
This PR saves more than 300 lines, while makes the git client messages
more clear.
Many gitea-serv/git-hook related essential functions are covered by
tests.
---------
Co-authored-by: delvh <dev.lh@web.de>
2023-03-29 12:02:26 +05:30
|
|
|
func (r *Request) SetReadWriteTimeout(readWriteTimeout time.Duration) *Request {
|
|
|
|
r.setting.ReadWriteTimeout = readWriteTimeout
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-05-06 21:20:31 +05:30
|
|
|
// SetTLSClientConfig sets tls connection configurations if visiting https url.
|
2015-08-27 20:36:14 +05:30
|
|
|
func (r *Request) SetTLSClientConfig(config *tls.Config) *Request {
|
2016-11-25 12:02:09 +05:30
|
|
|
r.setting.TLSClientConfig = config
|
2015-08-27 20:36:14 +05:30
|
|
|
return r
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Header add header item string in request.
|
2015-08-27 20:36:14 +05:30
|
|
|
func (r *Request) Header(key, value string) *Request {
|
|
|
|
r.req.Header.Set(key, value)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:02:09 +05:30
|
|
|
// SetTransport sets transport to
|
2015-08-27 20:36:14 +05:30
|
|
|
func (r *Request) SetTransport(transport http.RoundTripper) *Request {
|
|
|
|
r.setting.Transport = transport
|
|
|
|
return r
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Param adds query param in to request.
|
|
|
|
// params build query string as ?key1=value1&key2=value2...
|
2015-08-27 20:36:14 +05:30
|
|
|
func (r *Request) Param(key, value string) *Request {
|
|
|
|
r.params[key] = value
|
|
|
|
return r
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Body adds request raw body.
|
|
|
|
// it supports string and []byte.
|
2015-08-27 20:36:14 +05:30
|
|
|
func (r *Request) Body(data interface{}) *Request {
|
2014-05-06 21:20:31 +05:30
|
|
|
switch t := data.(type) {
|
|
|
|
case string:
|
|
|
|
bf := bytes.NewBufferString(t)
|
2021-09-22 11:08:34 +05:30
|
|
|
r.req.Body = io.NopCloser(bf)
|
2015-08-27 20:36:14 +05:30
|
|
|
r.req.ContentLength = int64(len(t))
|
2014-05-06 21:20:31 +05:30
|
|
|
case []byte:
|
|
|
|
bf := bytes.NewBuffer(t)
|
2021-09-22 11:08:34 +05:30
|
|
|
r.req.Body = io.NopCloser(bf)
|
2015-08-27 20:36:14 +05:30
|
|
|
r.req.ContentLength = int64(len(t))
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
2015-08-27 20:36:14 +05:30
|
|
|
return r
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
|
|
|
|
2015-08-27 20:36:14 +05:30
|
|
|
func (r *Request) getResponse() (*http.Response, error) {
|
|
|
|
if r.resp.StatusCode != 0 {
|
|
|
|
return r.resp, nil
|
2014-08-23 18:43:55 +05:30
|
|
|
}
|
2022-01-20 06:01:39 +05:30
|
|
|
|
2014-05-06 21:20:31 +05:30
|
|
|
var paramBody string
|
2015-08-27 20:36:14 +05:30
|
|
|
if len(r.params) > 0 {
|
2014-05-06 21:20:31 +05:30
|
|
|
var buf bytes.Buffer
|
2015-08-27 20:36:14 +05:30
|
|
|
for k, v := range r.params {
|
2014-05-06 21:20:31 +05:30
|
|
|
buf.WriteString(url.QueryEscape(k))
|
|
|
|
buf.WriteByte('=')
|
|
|
|
buf.WriteString(url.QueryEscape(v))
|
|
|
|
buf.WriteByte('&')
|
|
|
|
}
|
|
|
|
paramBody = buf.String()
|
|
|
|
paramBody = paramBody[0 : len(paramBody)-1]
|
|
|
|
}
|
|
|
|
|
2015-08-27 20:36:14 +05:30
|
|
|
if r.req.Method == "GET" && len(paramBody) > 0 {
|
2019-06-13 01:11:28 +05:30
|
|
|
if strings.Contains(r.url, "?") {
|
2015-08-27 20:36:14 +05:30
|
|
|
r.url += "&" + paramBody
|
2014-05-06 21:20:31 +05:30
|
|
|
} else {
|
2015-08-27 20:36:14 +05:30
|
|
|
r.url = r.url + "?" + paramBody
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
2022-01-20 06:01:39 +05:30
|
|
|
} else if r.req.Method == "POST" && r.req.Body == nil && len(paramBody) > 0 {
|
|
|
|
r.Header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
r.Body(paramBody)
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
|
|
|
|
Refactor internal API for git commands, use meaningful messages instead of "Internal Server Error" (#23687)
# Why this PR comes
At first, I'd like to help users like #23636 (there are a lot)
The unclear "Internal Server Error" is quite anonying, scare users,
frustrate contributors, nobody knows what happens.
So, it's always good to provide meaningful messages to end users (of
course, do not leak sensitive information).
When I started working on the "response message to end users", I found
that the related code has a lot of technical debt. A lot of copy&paste
code, unclear fields and usages.
So I think it's good to make everything clear.
# Tech Backgrounds
Gitea has many sub-commands, some are used by admins, some are used by
SSH servers or Git Hooks. Many sub-commands use "internal API" to
communicate with Gitea web server.
Before, Gitea server always use `StatusCode + Json "err" field` to
return messages.
* The CLI sub-commands: they expect to show all error related messages
to site admin
* The Serv/Hook sub-commands (for git clients): they could only show
safe messages to end users, the error log could only be recorded by
"SSHLog" to Gitea web server.
In the old design, it assumes that:
* If the StatusCode is 500 (in some functions), then the "err" field is
error log, shouldn't be exposed to git client.
* If the StatusCode is 40x, then the "err" field could be exposed. And
some functions always read the "err" no matter what the StatusCode is.
The old code is not strict, and it's difficult to distinguish the
messages clearly and then output them correctly.
# This PR
To help to remove duplicate code and make everything clear, this PR
introduces `ResponseExtra` and `requestJSONResp`.
* `ResponseExtra` is a struct which contains "extra" information of a
internal API response, including StatusCode, UserMsg, Error
* `requestJSONResp` is a generic function which can be used for all
cases to help to simplify the calls.
* Remove all `map["err"]`, always use `private.Response{Err}` to
construct error messages.
* User messages and error messages are separated clearly, the `fail` and
`handleCliResponseExtra` will output correct messages.
* Replace all `Internal Server Error` messages with meaningful (still
safe) messages.
This PR saves more than 300 lines, while makes the git client messages
more clear.
Many gitea-serv/git-hook related essential functions are covered by
tests.
---------
Co-authored-by: delvh <dev.lh@web.de>
2023-03-29 12:02:26 +05:30
|
|
|
var err error
|
|
|
|
r.req.URL, err = url.Parse(r.url)
|
2014-05-06 21:20:31 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-27 20:36:14 +05:30
|
|
|
trans := r.setting.Transport
|
2014-05-06 21:20:31 +05:30
|
|
|
if trans == nil {
|
|
|
|
// create default transport
|
|
|
|
trans = &http.Transport{
|
2016-11-25 12:02:09 +05:30
|
|
|
TLSClientConfig: r.setting.TLSClientConfig,
|
2022-01-20 06:01:39 +05:30
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2021-07-14 20:13:13 +05:30
|
|
|
DialContext: TimeoutDialer(r.setting.ConnectTimeout),
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
2019-06-13 01:11:28 +05:30
|
|
|
} else if t, ok := trans.(*http.Transport); ok {
|
|
|
|
if t.TLSClientConfig == nil {
|
|
|
|
t.TLSClientConfig = r.setting.TLSClientConfig
|
|
|
|
}
|
2021-07-14 20:13:13 +05:30
|
|
|
if t.DialContext == nil {
|
|
|
|
t.DialContext = TimeoutDialer(r.setting.ConnectTimeout)
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: trans,
|
2021-04-26 00:18:12 +05:30
|
|
|
Timeout: r.setting.ReadWriteTimeout,
|
2014-08-23 18:43:55 +05:30
|
|
|
}
|
|
|
|
|
2015-08-27 20:36:14 +05:30
|
|
|
if len(r.setting.UserAgent) > 0 && len(r.req.Header.Get("User-Agent")) == 0 {
|
|
|
|
r.req.Header.Set("User-Agent", r.setting.UserAgent)
|
2014-08-23 18:43:55 +05:30
|
|
|
}
|
|
|
|
|
2015-08-27 20:36:14 +05:30
|
|
|
resp, err := client.Do(r.req)
|
2014-05-06 21:20:31 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-27 20:36:14 +05:30
|
|
|
r.resp = resp
|
2014-05-06 21:20:31 +05:30
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2017-03-15 06:22:01 +05:30
|
|
|
// Response executes request client gets response manually.
|
2015-08-27 20:36:14 +05:30
|
|
|
func (r *Request) Response() (*http.Response, error) {
|
|
|
|
return r.getResponse()
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.
|
2021-07-14 20:13:13 +05:30
|
|
|
func TimeoutDialer(cTimeout time.Duration) func(ctx context.Context, net, addr string) (c net.Conn, err error) {
|
|
|
|
return func(ctx context.Context, netw, addr string) (net.Conn, error) {
|
|
|
|
d := net.Dialer{Timeout: cTimeout}
|
|
|
|
conn, err := d.DialContext(ctx, netw, addr)
|
2014-05-06 21:20:31 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-26 00:18:12 +05:30
|
|
|
return conn, nil
|
2014-05-06 21:20:31 +05:30
|
|
|
}
|
|
|
|
}
|
Refactor internal API for git commands, use meaningful messages instead of "Internal Server Error" (#23687)
# Why this PR comes
At first, I'd like to help users like #23636 (there are a lot)
The unclear "Internal Server Error" is quite anonying, scare users,
frustrate contributors, nobody knows what happens.
So, it's always good to provide meaningful messages to end users (of
course, do not leak sensitive information).
When I started working on the "response message to end users", I found
that the related code has a lot of technical debt. A lot of copy&paste
code, unclear fields and usages.
So I think it's good to make everything clear.
# Tech Backgrounds
Gitea has many sub-commands, some are used by admins, some are used by
SSH servers or Git Hooks. Many sub-commands use "internal API" to
communicate with Gitea web server.
Before, Gitea server always use `StatusCode + Json "err" field` to
return messages.
* The CLI sub-commands: they expect to show all error related messages
to site admin
* The Serv/Hook sub-commands (for git clients): they could only show
safe messages to end users, the error log could only be recorded by
"SSHLog" to Gitea web server.
In the old design, it assumes that:
* If the StatusCode is 500 (in some functions), then the "err" field is
error log, shouldn't be exposed to git client.
* If the StatusCode is 40x, then the "err" field could be exposed. And
some functions always read the "err" no matter what the StatusCode is.
The old code is not strict, and it's difficult to distinguish the
messages clearly and then output them correctly.
# This PR
To help to remove duplicate code and make everything clear, this PR
introduces `ResponseExtra` and `requestJSONResp`.
* `ResponseExtra` is a struct which contains "extra" information of a
internal API response, including StatusCode, UserMsg, Error
* `requestJSONResp` is a generic function which can be used for all
cases to help to simplify the calls.
* Remove all `map["err"]`, always use `private.Response{Err}` to
construct error messages.
* User messages and error messages are separated clearly, the `fail` and
`handleCliResponseExtra` will output correct messages.
* Replace all `Internal Server Error` messages with meaningful (still
safe) messages.
This PR saves more than 300 lines, while makes the git client messages
more clear.
Many gitea-serv/git-hook related essential functions are covered by
tests.
---------
Co-authored-by: delvh <dev.lh@web.de>
2023-03-29 12:02:26 +05:30
|
|
|
|
|
|
|
func (r *Request) GoString() string {
|
|
|
|
return fmt.Sprintf("%s %s", r.req.Method, r.url)
|
|
|
|
}
|