1
0
mirror of https://github.com/haiwen/seafile-server.git synced 2025-09-03 16:34:33 +00:00

Return error message when query share link info (#698)

* Return rsp content when query share link info

* Add parse error message

---------

Co-authored-by: 杨赫然 <heran.yang@seafile.com>
This commit is contained in:
feiniks
2024-09-13 18:33:41 +08:00
committed by GitHub
parent c80bf17efb
commit f27ab0847b
5 changed files with 88 additions and 26 deletions

View File

@@ -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 {
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}
}
if status != http.StatusOK {
msg := "No permission to access file\n"
return "", &appError{nil, msg, http.StatusForbidden}
}
info := new(UserInfo)
@@ -3651,12 +3652,12 @@ func queryShareLinkInfo(token, cookie, opType string) (*ShareLinkInfo, *appError
}
status, body, err := utils.HttpCommon("GET", url, header, nil)
if err != nil {
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}
}
if status != http.StatusOK {
msg := "Link token not found"
return nil, &appError{nil, msg, http.StatusForbidden}
}
info := new(ShareLinkInfo)

View File

@@ -2,6 +2,7 @@ package utils
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
@@ -25,22 +26,19 @@ 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)
errMsg := parseErrorMessage(rsp.Body)
return rsp.StatusCode, errMsg, fmt.Errorf("bad response %d for %s", rsp.StatusCode, url)
}
body, err := io.ReadAll(rsp.Body)
@@ -48,5 +46,23 @@ func HttpCommon(method, url string, header map[string][]string, reader io.Reader
return rsp.StatusCode, nil, err
}
return rsp.StatusCode, body, nil
return http.StatusOK, body, nil
}
func parseErrorMessage(r io.Reader) []byte {
body, err := io.ReadAll(r)
if err != nil {
return nil
}
var objs map[string]string
err = json.Unmarshal(body, &objs)
if err != nil {
return body
}
errMsg, ok := objs["error_msg"]
if ok {
return []byte(errMsg)
}
return body
}

View File

@@ -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);

View File

@@ -567,8 +567,39 @@ out:
return info;
}
char *
parse_error_message (const char *rsp_content, int rsp_size)
{
json_t *object;
json_error_t jerror;
const char *err_msg = NULL;
char *ret = NULL;
if (!rsp_content) {
return NULL;
}
object = json_loadb (rsp_content, rsp_size, 0, &jerror);
if (!object) {
ret = g_strdup (rsp_content);
return ret;
}
err_msg = json_object_get_string_member (object, "error_msg");
if (!err_msg) {
ret = g_strdup (rsp_content);
goto out;
}
ret = g_strdup (err_msg);
out:
json_decref (object);
return ret;
}
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;
@@ -609,7 +640,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 = parse_error_message (rsp_content, rsp_size);
goto out;
}

View File

@@ -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,