Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

use evbuffer_add_reference send json data have a problem #140

Open
dx01259 opened this issue Jul 26, 2019 · 2 comments
Open

use evbuffer_add_reference send json data have a problem #140

dx01259 opened this issue Jul 26, 2019 · 2 comments

Comments

@dx01259
Copy link

dx01259 commented Jul 26, 2019

there is a problem when use evbuffer_add_reference function to send json data ,such as send the data of {"code":20000,"messsage":"success"},the client receive data is 20000,"messsage":"success"}, why?? thanks for you answer。

image

@git4won
Copy link

git4won commented Jul 26, 2019

I think you should show your code.

@dx01259
Copy link
Author

dx01259 commented Jul 27, 2019

I think you should show your code.

The key code is as follows,thanks!Replacing evbuffer_add_reference with evbuffer_add_printf can solve the problem. why?? Thanks for your answer.

int HttpService::startServer(const char *addr, uint16_t port, int backlog)
{
/signal(SIGTERM, HttpService::signalHandler);
signal(SIGHUP, HttpService::signalHandler);
signal(SIGQUIT, HttpService::signalHandler);
signal(SIGINT, HttpService::signalHandler);
/
evbase_t *evbase = event_base_new();
evhtp_t *htp = evhtp_new(evbase, NULL);

    /** Set callback function */
    request.batchSetupRequest(htp);
    /**
     * No matching requests
     */
    evhtp_set_gencb(htp, [](evhtp_request_t * req, void * arg){
        evbuffer_add_reference(req->buffer_out, "This is error request !\n", 18, NULL, NULL);
        evhtp_send_reply(req, EVHTP_RES_OK);
    }, NULL);

#ifndef EVHTP_DISABLE_EVTHR
evhtp_use_threads_wexit(htp, NULL, NULL, 4, NULL);
#endif
evhtp_bind_socket(htp, addr, port, backlog);
event_base_loop(evbase, 0);

    evhtp_unbind_socket(htp);
    evhtp_free(htp);
    event_base_free(evbase);
    return 0;
}

void HttpRequest::login(evhtp_request_t *req, void *arg) {
evhtp_connection_t *conn;

    evhtp_assert(req != NULL);

    conn = evhtp_request_get_connection(req);
    evhtp_assert(conn != NULL);

    htp_sslutil_add_xheaders(req->headers_out, conn->ssl, HTP_SSLUTILS_XHDR_ALL);

    htp_method methodType = evhtp_request_get_method(req);

    if (methodType == htp_method_OPTIONS) {
        return dump_request_cb(req, arg);
    } else {
        setRequestHead(req, arg);
        LoginRequest::login(req, arg);
    }
}

void LoginRequest::login(evhtp_request_t *req, void *arg)
{
try {
size_t length = evhtp_request_content_len(req);
if (length > 0 ) {
std::string requestBuf = std::string(length, '\0');
evbuffer_copyout(req->buffer_in, &requestBuf[0], length);
BServerPkg pkg = BServerPkg(requestBuf);
std::string username = pkg.getString("username");
std::string password = pkg.getString("password");
std::string res_data = std::string(pkg.response(DEFINE_HTTP_CODE::DEF_RES_OK, "success").c_str());
evbuffer_add_printf(req->buffer_out, "%s", res_data.c_str());
//int ret = evbuffer_add_reference(req->buffer_out, res_data.c_str(), res_data.size(), NULL, NULL);
//evbuffer_add_reference(req->buffer_out, "This is a HTTP server response.", 32, NULL, NULL);
evhtp_send_reply(req, EVHTP_RES_OK);

        } else {
            evhtp_send_reply(req, EVHTP_RES_OK);
        }
    } catch (std::exception &ex) {
        printf(ex.what());
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants