Skip to content
Maru Berezin edited this page Feb 15, 2018 · 2 revisions

This week was all about coding the response.

What I learned this week:

  • sendfile() system call: copies data between one file descriptor and another, within the kernel. This operation is more efficient (and so, faster) than using a combination of read()s and write()s, transferring data to and from user space.

Difference between sendfile() and read()+write()

Difference between sendfile() and read()+write()

HTTP/1 allows only one request-response transaction per connection, and this is very well reflected in the definition of MHD_Connection struct. HTTP/2 can have multiple of these request-response transactions (streams) per connection (session) at a time, so several changes were introduced:

  • A stream has one MHD_Response object (multiple responses per connection).
  • Writing the response then depends on the stream, so the stream keeps track of the current write position in the actual response.
  • In order to use the sendfile() primitive, a nghttp2_send_data_callback function is implemented, so we can send a file directly, without having intermediate buffers.

What happens when receiving a request:

  • http2_call_connection_handler() calls the MHD_AccessHandlerCallback callback function for all the requests, defined in the MHD application.

    This function will be called:

    • After a HEADERS stream, if the HTTP request method is e.g. GET or HEAD.
    • After a HEADERS + DATA stream, if the HTTP request method is POST or PUT.
  • When the application calls MHD_queue_response(), MHD_http2_queue_response() executes and we build the response headers.

  • Then response_read_callback() is called, and depending on how the application created the response:

    • MHD_create_response_from_data(): the response body is a data buffer, the data is copied into the buffer (which will be sent later).
    • MHD_create_response_from_fd(): the response body is a file, we will either:
      • Read with the sendfile_adapter function.
      • Read with the response.c:file_reader().
    • MHD_create_response_from_callback(): the response body is created within an application callback response->crc().
  • The response is sent immediately (HEADERS + DATA frames).

Clone this wiki locally