mirror of
https://github.com/haiwen/seafile-server.git
synced 2025-09-07 10:20: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:
@@ -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))
|
status, body, err := utils.HttpCommon("POST", url, header, bytes.NewReader(msg))
|
||||||
if err != nil {
|
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)
|
err := fmt.Errorf("failed to get access token info: %v", err)
|
||||||
return "", &appError{err, "", http.StatusInternalServerError}
|
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)
|
info := new(UserInfo)
|
||||||
@@ -3651,12 +3652,12 @@ func queryShareLinkInfo(token, cookie, opType string) (*ShareLinkInfo, *appError
|
|||||||
}
|
}
|
||||||
status, body, err := utils.HttpCommon("GET", url, header, nil)
|
status, body, err := utils.HttpCommon("GET", url, header, nil)
|
||||||
if err != 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)
|
err := fmt.Errorf("failed to get share link info: %v", err)
|
||||||
return nil, &appError{err, "", http.StatusInternalServerError}
|
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)
|
info := new(ShareLinkInfo)
|
||||||
|
@@ -2,6 +2,7 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -25,22 +26,19 @@ func HttpCommon(method, url string, header map[string][]string, reader io.Reader
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
req, err := http.NewRequestWithContext(ctx, method, url, reader)
|
req, err := http.NewRequestWithContext(ctx, method, url, reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, nil, err
|
return http.StatusInternalServerError, nil, err
|
||||||
}
|
}
|
||||||
req.Header = header
|
req.Header = header
|
||||||
|
|
||||||
rsp, err := http.DefaultClient.Do(req)
|
rsp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, nil, err
|
return http.StatusInternalServerError, nil, err
|
||||||
}
|
}
|
||||||
defer rsp.Body.Close()
|
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 {
|
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)
|
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, 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
|
||||||
}
|
}
|
||||||
|
@@ -1823,11 +1823,22 @@ access_link_cb(evhtp_request_t *req, void *arg)
|
|||||||
token = parts[1];
|
token = parts[1];
|
||||||
|
|
||||||
const char *cookie = evhtp_kv_find (req->headers_in, "Cookie");
|
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) {
|
if (!info) {
|
||||||
error_str = "Link token not found\n";
|
g_strfreev (parts);
|
||||||
error_code = EVHTP_RES_FORBIDDEN;
|
if (status != HTTP_OK) {
|
||||||
goto out;
|
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);
|
repo_id = seafile_share_link_info_get_repo_id (info);
|
||||||
|
@@ -567,8 +567,39 @@ out:
|
|||||||
return info;
|
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 *
|
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;
|
Connection *conn = NULL;
|
||||||
char *cookie_header;
|
char *cookie_header;
|
||||||
@@ -609,7 +640,9 @@ http_tx_manager_query_share_link_info (const char *token, const char *cookie, co
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*status = rsp_status;
|
||||||
if (rsp_status != HTTP_OK) {
|
if (rsp_status != HTTP_OK) {
|
||||||
|
*err_msg = parse_error_message (rsp_content, rsp_size);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -50,7 +50,8 @@ char *
|
|||||||
http_tx_manager_get_nickname (const char *modifier);
|
http_tx_manager_get_nickname (const char *modifier);
|
||||||
|
|
||||||
SeafileShareLinkInfo *
|
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
|
int
|
||||||
http_tx_manager_check_file_access (const char *repo_id, const char *token, const char *cookie,
|
http_tx_manager_check_file_access (const char *repo_id, const char *token, const char *cookie,
|
||||||
|
Reference in New Issue
Block a user