Skip to content

Commit

Permalink
Return error message when query share link info (#698)
Browse files Browse the repository at this point in the history
* Return rsp content when query share link info

* Add parse error message

---------

Co-authored-by: 杨赫然 <[email protected]>
  • Loading branch information
feiniks and 杨赫然 authored Sep 13, 2024
1 parent c80bf17 commit f27ab08
Show file tree
Hide file tree
Showing 5 changed files with 88 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 @@ -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)
Expand Down
32 changes: 24 additions & 8 deletions fileserver/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -25,28 +26,43 @@ 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)
if err != nil {
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
}
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
35 changes: 34 additions & 1 deletion server/http-tx-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

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 f27ab08

Please sign in to comment.