Skip to content

Commit

Permalink
Modify user agent and add timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Sep 5, 2024
1 parent befb116 commit ced9830
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
8 changes: 5 additions & 3 deletions fileserver/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"io"
"net/http"
"strings"
"time"
)

var HttpReqContext, HttpReqCancel = context.WithCancel(context.Background())

func GetAuthorizationToken(h http.Header) string {
auth := h.Get("Authorization")
splitResult := strings.Split(auth, " ")
Expand All @@ -20,7 +19,10 @@ func GetAuthorizationToken(h http.Header) string {
}

func HttpCommon(method, url string, header map[string][]string, reader io.Reader) (int, []byte, error) {
req, err := http.NewRequestWithContext(HttpReqContext, method, url, reader)
header["Content-Type"] = []string{"application/json"}
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, method, url, reader)
if err != nil {
return -1, nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions server/http-tx-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ http_post (Connection *conn, const char *url, const char *token,

curl = conn->curl;

headers = curl_slist_append (headers, "User-Agent: Seafile/"SEAFILE_CLIENT_VERSION" ("USER_AGENT_OS")");
headers = curl_slist_append (headers, "User-Agent: Seafile Server/"SEAFILE_CLIENT_VERSION" ("USER_AGENT_OS")");

if (token) {
token_header = g_strdup_printf ("Authorization: Token %s", token);
Expand Down Expand Up @@ -506,7 +506,7 @@ http_tx_manager_get_nickname (const char *modifier)
json_decref (content);

curl = conn->curl;
headers = curl_slist_append (headers, "User-Agent: Seafile/"SEAFILE_CLIENT_VERSION" ("USER_AGENT_OS")");
headers = curl_slist_append (headers, "User-Agent: Seafile Server/"SEAFILE_CLIENT_VERSION" ("USER_AGENT_OS")");
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,16 +608,16 @@ 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/"SEAFILE_CLIENT_VERSION" ("USER_AGENT_OS")");
headers = curl_slist_append (headers, "User-Agent: Seafile Server/"SEAFILE_CLIENT_VERSION" ("USER_AGENT_OS")");
token_header = g_strdup_printf ("Authorization: Token %s", jwt_token);
headers = curl_slist_append (headers, token_header);
g_free (token_header);
if (cookie) {
cookie_header = g_strdup_printf ("Cookie: %s", cookie);
headers = curl_slist_append (headers, cookie_header);
g_free (cookie_header);
}
headers = curl_slist_append (headers, token_header);
headers = curl_slist_append (headers, "Content-Type: application/json");
g_free (token_header);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

url = g_strdup_printf("%s/check-share-link-access/?token=%s&type=%s", seaf->seahub_url, token, type);
Expand Down

0 comments on commit ced9830

Please sign in to comment.