Skip to content

Commit

Permalink
Set Content-Type and User-Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Sep 6, 2024
1 parent a3e56c3 commit e98294c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 46 deletions.
27 changes: 1 addition & 26 deletions fileserver/fileop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:]
Expand Down
1 change: 0 additions & 1 deletion fileserver/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down
6 changes: 6 additions & 0 deletions fileserver/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
22 changes: 3 additions & 19 deletions server/http-tx-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e98294c

Please sign in to comment.