Skip to content

Commit

Permalink
Return rsp content when query share link info
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Sep 12, 2024
1 parent b2bde11 commit a9c65a5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
25 changes: 13 additions & 12 deletions fileserver/fileop.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,13 @@ func checkFileAccess(repoID, token, cookie, filePath, op string) (string, *appEr
}
status, body, err := utils.HttpCommon("POST", url, header, bytes.NewReader(msg))
if err != nil {
err := fmt.Errorf("failed to get access token info: %v", err)
return "", &appError{err, "", http.StatusInternalServerError}
}
if status != http.StatusOK {
msg := "No permission to access file\n"
return "", &appError{nil, msg, http.StatusForbidden}
if status != http.StatusInternalServerError {
msg := "No permission to access file\n"
return "", &appError{nil, msg, http.StatusForbidden}
} else {
err := fmt.Errorf("failed to get access token info: %v", err)
return "", &appError{err, "", http.StatusInternalServerError}
}
}

info := new(UserInfo)
Expand Down Expand Up @@ -3637,12 +3638,12 @@ func queryShareLinkInfo(token, cookie, opType string) (*ShareLinkInfo, *appError
}
status, body, err := utils.HttpCommon("GET", url, header, nil)
if err != nil {
err := fmt.Errorf("failed to get share link info: %v", err)
return nil, &appError{err, "", http.StatusInternalServerError}
}
if status != http.StatusOK {
msg := "Link token not found"
return nil, &appError{nil, msg, http.StatusForbidden}
if status != http.StatusInternalServerError {
return nil, &appError{nil, string(body), status}
} else {
err := fmt.Errorf("failed to get share link info: %v", err)
return nil, &appError{err, "", http.StatusInternalServerError}
}
}

info := new(ShareLinkInfo)
Expand Down
13 changes: 5 additions & 8 deletions fileserver/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,25 @@ func HttpCommon(method, url string, header map[string][]string, reader io.Reader
defer cancel()
req, err := http.NewRequestWithContext(ctx, method, url, reader)
if err != nil {
return -1, nil, err
return http.StatusInternalServerError, nil, err
}
req.Header = header

rsp, err := http.DefaultClient.Do(req)
if err != nil {
return -1, nil, err
return http.StatusInternalServerError, nil, err
}
defer rsp.Body.Close()

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, _ := io.ReadAll(rsp.Body)
return rsp.StatusCode, body, 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
return http.StatusOK, body, nil
}
19 changes: 15 additions & 4 deletions server/access-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1823,11 +1823,22 @@ access_link_cb(evhtp_request_t *req, void *arg)
token = parts[1];

const char *cookie = evhtp_kv_find (req->headers_in, "Cookie");
info = http_tx_manager_query_share_link_info (token, cookie, "file");
int status = HTTP_OK;
char *err_msg = NULL;
info = http_tx_manager_query_share_link_info (token, cookie, "file", &status, &err_msg);
if (!info) {
error_str = "Link token not found\n";
error_code = EVHTP_RES_FORBIDDEN;
goto out;
g_strfreev (parts);
if (status != HTTP_OK) {
evbuffer_add_printf(req->buffer_out, "%s\n", err_msg);
evhtp_send_reply(req, status);
} else {
error_str = "Internal server error\n";
error_code = EVHTP_RES_SERVERR;
evbuffer_add_printf(req->buffer_out, "%s\n", error_str);
evhtp_send_reply(req, error_code);
}
g_free (err_msg);
return;
}

repo_id = seafile_share_link_info_get_repo_id (info);
Expand Down
4 changes: 3 additions & 1 deletion server/http-tx-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ parse_share_link_info (const char *rsp_content, int rsp_size)
}

SeafileShareLinkInfo *
http_tx_manager_query_share_link_info (const char *token, const char *cookie, const char *type)
http_tx_manager_query_share_link_info (const char *token, const char *cookie, const char *type, int *status, char **err_msg)
{
Connection *conn = NULL;
char *cookie_header;
Expand Down Expand Up @@ -609,7 +609,9 @@ http_tx_manager_query_share_link_info (const char *token, const char *cookie, co
goto out;
}

*status = rsp_status;
if (rsp_status != HTTP_OK) {
*err_msg = g_strdup (rsp_content);
goto out;
}

Expand Down
3 changes: 2 additions & 1 deletion server/http-tx-mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ char *
http_tx_manager_get_nickname (const char *modifier);

SeafileShareLinkInfo *
http_tx_manager_query_share_link_info (const char *token, const char *cookie, const char *type);
http_tx_manager_query_share_link_info (const char *token, const char *cookie, const char *type,
int *status, char **err_msg);

int
http_tx_manager_check_file_access (const char *repo_id, const char *token, const char *cookie,
Expand Down

0 comments on commit a9c65a5

Please sign in to comment.