nghttp2 icon indicating copy to clipboard operation
nghttp2 copied to clipboard

Why is on_data being called twice for a single request in asio http2 server

Open dnkanga opened this issue 4 years ago • 1 comments

I am running a HTTP2 server by using nghttp2. I am trying to figure out why the on_data handler in the server.cpp is called twice. I know it's called twice because when I send a request that contains data, I get the following log output for a single request in the server.

1
2

But if I don't send data in the request. The log output for single request is what I would expect

1

I send data to the server by using the client and with this line of code

auto req = sess.submit(ec, "GET", "http://127.0.0.1:3000/", "aaaaaaaaaa");

To not send data I just remove the third parameter.

server.cpp

Compiled with g++ server.cpp -o server.out -lnghttp2_asio -lboost_system -lcrypto -lpthread -lssl -lboost_thread

#include <iostream>
#include <nghttp2/asio_http2_server.h>

using namespace nghttp2::asio_http2;
using namespace nghttp2::asio_http2::server;

int main() {
    std::string hostname = "127.0.0.1";
    std::string port = "3000";
    boost::system::error_code ec;
    http2 server;

    int count = 0;
    server.handle("/", [&count](const request &req, const response &res) {
        req.on_data([&res, &count](const uint8_t *data, std::size_t len) {
            std::cerr << ++count << '\n';
            res.write_head(200);
            res.end("done");
        });
    });

    if (server.listen_and_serve(ec, hostname, port)) {
        std::cerr << "error: " << ec.message() << std::endl;
    }
}

Compiled with g++ client.cpp -o client.out -lnghttp2_asio -lboost_system -lcrypto -lpthread -lssl -lboost_thread

#include <iostream>

#include <nghttp2/asio_http2_client.h>

using boost::asio::ip::tcp;

using namespace nghttp2::asio_http2;
using namespace nghttp2::asio_http2::client;

int main(int argc, char *argv[]) {
    boost::system::error_code ec;
    boost::asio::io_service io_service;

    // connect to localhost:3000
    session sess(io_service, "127.0.0.1", "3000");

    sess.on_connect([&sess](tcp::resolver::iterator endpoint_it) {
        boost::system::error_code ec;

        auto req = sess.submit(ec, "GET", "http://127.0.0.1:3000/", "aaaaaaaaaa");
        req->on_response([](const response &res) {
            // print status code and response header fields.
            std::cerr << "HTTP/2 " << res.status_code() << std::endl;
            for (auto &kv : res.header()) {
                std::cerr << kv.first << ": " << kv.second.value << "\n";
            }
            std::cerr << std::endl;

            res.on_data([](const uint8_t *data, std::size_t len) {
                std::cerr.write(reinterpret_cast<const char *>(data), len);
                std::cerr << std::endl;
            });
        });
        req->on_close([&sess](uint32_t error_code) {
            // shutdown session after first request was done.
            sess.shutdown();
        });
    });

    sess.on_error([](const boost::system::error_code &ec) {
        std::cerr << "error: " << ec.message() << std::endl;
    });

    io_service.run();
}

dnkanga avatar Aug 26 '21 21:08 dnkanga

Hi, I think it's because when flag END_STREAM is set in DATA frame, there will be an additional call to on_data callback with nullptr and 0.

    if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
      strm->request().impl().call_on_data(nullptr, 0);
    }

Please also check RFC 7540, section 6.1: https://datatracker.ietf.org/doc/html/rfc7540#section-6.1

JackyYin avatar Aug 28 '21 16:08 JackyYin

Closed since asio library has been removed.

tatsuhiro-t avatar Mar 23 '24 06:03 tatsuhiro-t