From e98294cd82c06673f7975ce634a81b6f051119c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E8=B5=AB=E7=84=B6?= Date: Fri, 6 Sep 2024 11:32:59 +0800 Subject: [PATCH] Set Content-Type and User-Agent --- fileserver/fileop.go | 27 +-------------------------- fileserver/merge.go | 1 - fileserver/utils/http.go | 6 ++++++ server/http-tx-mgr.c | 22 +++------------------- 4 files changed, 10 insertions(+), 46 deletions(-) diff --git a/fileserver/fileop.go b/fileserver/fileop.go index 489b9ebf..a30ef171 100644 --- a/fileserver/fileop.go +++ b/fileserver/fileop.go @@ -1948,9 +1948,8 @@ func notifRepoUpdate(repoID string, commitID string) error { } header := map[string][]string{ "Authorization": {"Token " + token}, - "Content-Type": {"application/json"}, } - _, _, err = httpCommon("POST", url, header, bytes.NewReader(msg)) + _, _, err = utils.HttpCommon("POST", url, header, bytes.NewReader(msg)) if err != nil { log.Printf("failed to send repo update event: %v", err) return err @@ -1959,30 +1958,6 @@ func notifRepoUpdate(repoID string, commitID string) error { return nil } -func httpCommon(method, url string, header map[string][]string, reader io.Reader) (int, []byte, error) { - req, err := http.NewRequest(method, url, reader) - if err != nil { - return -1, nil, err - } - req.Header = header - - rsp, err := http.DefaultClient.Do(req) - if err != nil { - return -1, nil, err - } - defer rsp.Body.Close() - - if rsp.StatusCode != http.StatusOK { - return rsp.StatusCode, nil, fmt.Errorf("bad response %d for %s", rsp.StatusCode, url) - } - body, err := io.ReadAll(rsp.Body) - if err != nil { - return rsp.StatusCode, nil, err - } - - return rsp.StatusCode, body, nil -} - func doPostMultiFiles(repo *repomgr.Repo, rootID, parentDir string, dents []*fsmgr.SeafDirent, user string, replace bool, names *[]string) (string, error) { if parentDir[0] == '/' { parentDir = parentDir[1:] diff --git a/fileserver/merge.go b/fileserver/merge.go index d57a74af..dcd6b488 100644 --- a/fileserver/merge.go +++ b/fileserver/merge.go @@ -419,7 +419,6 @@ func postGetNickName(modifier string) string { header := map[string][]string{ "Authorization": {"Token " + tokenString}, - "Content-Type": {"application/json"}, } data, err := json.Marshal(map[string]interface{}{ diff --git a/fileserver/utils/http.go b/fileserver/utils/http.go index 008d8678..bde68b2e 100644 --- a/fileserver/utils/http.go +++ b/fileserver/utils/http.go @@ -20,6 +20,7 @@ func GetAuthorizationToken(h http.Header) string { func HttpCommon(method, url string, header map[string][]string, reader io.Reader) (int, []byte, error) { header["Content-Type"] = []string{"application/json"} + header["User-Agent"] = []string{"Seafile Server"} ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second) defer cancel() req, err := http.NewRequestWithContext(ctx, method, url, reader) @@ -37,6 +38,11 @@ func HttpCommon(method, url string, header map[string][]string, reader io.Reader if rsp.StatusCode == http.StatusNotFound { return rsp.StatusCode, nil, fmt.Errorf("url %s not found", url) } + + if rsp.StatusCode != http.StatusOK { + return rsp.StatusCode, nil, fmt.Errorf("bad response %d for %s", rsp.StatusCode, url) + } + body, err := io.ReadAll(rsp.Body) if err != nil { return rsp.StatusCode, nil, err diff --git a/server/http-tx-mgr.c b/server/http-tx-mgr.c index e2de9a4a..d99c51d2 100644 --- a/server/http-tx-mgr.c +++ b/server/http-tx-mgr.c @@ -17,22 +17,6 @@ #define DEBUG_FLAG SEAFILE_DEBUG_TRANSFER #include "log.h" -#ifndef SEAFILE_CLIENT_VERSION -#define SEAFILE_CLIENT_VERSION PACKAGE_VERSION -#endif - -#ifdef WIN32 -#define USER_AGENT_OS "Windows NT" -#endif - -#ifdef __APPLE__ -#define USER_AGENT_OS "Apple OS X" -#endif - -#ifdef __linux__ -#define USER_AGENT_OS "Linux" -#endif - /* Http connection and connection pool. */ struct _Connection { @@ -365,7 +349,7 @@ http_post (Connection *conn, const char *url, const char *token, curl = conn->curl; - headers = curl_slist_append (headers, "User-Agent: Seafile Server/"SEAFILE_CLIENT_VERSION" ("USER_AGENT_OS")"); + headers = curl_slist_append (headers, "User-Agent: Seafile Server"); if (token) { token_header = g_strdup_printf ("Authorization: Token %s", token); @@ -506,7 +490,7 @@ http_tx_manager_get_nickname (const char *modifier) json_decref (content); curl = conn->curl; - headers = curl_slist_append (headers, "User-Agent: Seafile Server/"SEAFILE_CLIENT_VERSION" ("USER_AGENT_OS")"); + headers = curl_slist_append (headers, "User-Agent: Seafile Server"); token_header = g_strdup_printf ("Authorization: Token %s", jwt_token); headers = curl_slist_append (headers, token_header); headers = curl_slist_append (headers, "Content-Type: application/json"); @@ -608,7 +592,7 @@ http_tx_manager_query_share_link_info (const char *token, const char *cookie, co } curl = conn->curl; - headers = curl_slist_append (headers, "User-Agent: Seafile Server/"SEAFILE_CLIENT_VERSION" ("USER_AGENT_OS")"); + headers = curl_slist_append (headers, "User-Agent: Seafile Server"); token_header = g_strdup_printf ("Authorization: Token %s", jwt_token); headers = curl_slist_append (headers, token_header); g_free (token_header);