diff --git a/fileserver/fileop.go b/fileserver/fileop.go index a118b427..e896fc17 100644 --- a/fileserver/fileop.go +++ b/fileserver/fileop.go @@ -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) @@ -3651,12 +3652,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) diff --git a/fileserver/utils/http.go b/fileserver/utils/http.go index bde68b2e..ed764405 100644 --- a/fileserver/utils/http.go +++ b/fileserver/utils/http.go @@ -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 } diff --git a/server/access-file.c b/server/access-file.c index 48414a6d..7a500be3 100644 --- a/server/access-file.c +++ b/server/access-file.c @@ -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); diff --git a/server/http-tx-mgr.c b/server/http-tx-mgr.c index 45557e53..c3323a13 100644 --- a/server/http-tx-mgr.c +++ b/server/http-tx-mgr.c @@ -567,8 +567,39 @@ parse_share_link_info (const char *rsp_content, int rsp_size) 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; } diff --git a/server/http-tx-mgr.h b/server/http-tx-mgr.h index d74e64e8..75bf0913 100644 --- a/server/http-tx-mgr.h +++ b/server/http-tx-mgr.h @@ -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,