### Server Run Function Source: https://nghttp2.org/documentation/tutorial-server.html Orchestrates the server setup, including SSL context creation, event base initialization, application context setup, and starting the network listener. ```c static void run(const char *service, const char *key_file, const char *cert_file) { SSL_CTX *ssl_ctx; app_context app_ctx; struct event_base *evbase; ssl_ctx = create_ssl_ctx(key_file, cert_file); evbase = event_base_new(); initialize_app_context(&app_ctx, ssl_ctx, evbase); start_listen(evbase, service, &app_ctx); event_base_loop(evbase, 0); event_base_free(evbase); SSL_CTX_free(ssl_ctx); } ``` -------------------------------- ### Build and Install OpenSSL Source: https://nghttp2.org/documentation/building-android-binary.html Build and install OpenSSL without documentation. This command should be run after configuring OpenSSL. ```bash #!/bin/sh . $NGHTTP2/android-env export PATH=$TOOLCHAIN/bin:$PATH make install_sw ``` -------------------------------- ### Example: libevent-client.c for HTTP/2 client Source: https://nghttp2.org/documentation A client implementation using libevent to demonstrate HTTP/2 communication. This example showcases basic client functionality. ```c #include "examples/libevent-client.c" ``` -------------------------------- ### Example: libevent-server.c for HTTP/2 server Source: https://nghttp2.org/documentation A server implementation using libevent to demonstrate HTTP/2 communication. This example showcases basic server functionality. ```c #include "examples/libevent-server.c" ``` -------------------------------- ### Install nghttp2 Dependencies on Ubuntu 22.04 LTS Source: https://nghttp2.org/documentation/package_README.html Run this command to install all required development packages for nghttp2 on Ubuntu 22.04 LTS. Ensure you have sudo privileges. ```bash sudo apt-get install g++ clang make binutils autoconf automake \n autotools-dev libtool pkg-config \n zlib1g-dev libssl-dev libxml2-dev libev-dev \n libevent-dev libjansson-dev \n libc-ares-dev libjemalloc-dev libsystemd-dev \n ruby-dev bison libelf-dev ``` -------------------------------- ### Start Listening for Connections Source: https://nghttp2.org/documentation/tutorial-server.html Sets up a listener to accept incoming TCP connections on a specified service port. It resolves the address and binds to a socket, preparing to accept callbacks. ```c static void start_listen(struct event_base *evbase, const char *service, app_context *app_ctx) { int rv; struct addrinfo hints; struct addrinfo *res, *rp; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; #ifdef AI_ADDRCONFIG hints.ai_flags |= AI_ADDRCONFIG; #endif /* defined(AI_ADDRCONFIG) */ rv = getaddrinfo(NULL, service, &hints, &res); if (rv != 0) { errx(1, "Could not resolve server address"); } for (rp = res; rp; rp = rp->ai_next) { struct evconnlistener *listener; listener = evconnlistener_new_bind( evbase, acceptcb, app_ctx, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, 16, rp->ai_addr, (int)rp->ai_addrlen); if (listener) { freeaddrinfo(res); return; } } errx(1, "Could not start listener"); } ``` -------------------------------- ### nghttp2_session_callbacks_set_data_source_read_length_callback2 Source: https://nghttp2.org/documentation/nghttp2_session_callbacks_set_data_source_read_length_callback2.html Sets the callback function to get the length of data source. This callback is invoked when the library needs to know the total size of data to be read from a data source, for example, when sending a DATA frame. ```APIDOC ## nghttp2_session_callbacks_set_data_source_read_length_callback2 ### Description Sets the callback function to get the length of data source. This callback is invoked when the library needs to know the total size of data to be read from a data source, for example, when sending a DATA frame. ### Callback Signature ```c typedef int64_t (*nghttp2_data_source_read_length_callback)( nghttp2_session *session, int32_t stream_id, void *user_data); ``` ### Parameters * **session** (`nghttp2_session *`) - The nghttp2 session object. * **stream_id** (`int32_t`) - The ID of the stream for which the data length is requested. * **user_data** (`void *`) - User-defined data passed to the callback. ### Return Value Returns the length of the data source in bytes. Returns a negative value on error. ``` -------------------------------- ### Starting the network listener Source: https://nghttp2.org/documentation/tutorial-server.html The `start_listen` function initializes a libevent listener to accept incoming TCP connections. It uses `getaddrinfo` to resolve the service address and `evconnlistener_new_bind` to create the listener, specifying the `acceptcb` callback. ```c static void start_listen(struct event_base *evbase, const char *service, app_context *app_ctx) { int rv; struct addrinfo hints; struct addrinfo *res, *rp; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; #ifdef AI_ADDRCONFIG hints.ai_flags |= AI_ADDRCONFIG; #endif /* AI_ADDRCONFIG */ rv = getaddrinfo(NULL, service, &hints, &res); if (rv != 0) { errx(1, NULL); } for (rp = res; rp; rp = rp->ai_next) { struct evconnlistener *listener; listener = evconnlistener_new_bind( evbase, acceptcb, app_ctx, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, 16, rp->ai_addr, (int)rp->ai_addrlen); if (listener) { freeaddrinfo(res); return; } } errx(1, "Could not start listener"); } ``` -------------------------------- ### Example: Implementing Custom Memory Callbacks Source: https://nghttp2.org/documentation/nghttp2.h.html Illustrates how to implement custom memory allocation callbacks and integrate them with nghttp2_session_client_new3. ```c void *my_malloc_cb(size_t size, void *mem_user_data) { return my_malloc(size); } void my_free_cb(void *ptr, void *mem_user_data) { my_free(ptr); } void *my_calloc_cb(size_t nmemb, size_t size, void *mem_user_data) { return my_calloc(nmemb, size); } void *my_realloc_cb(void *ptr, size_t size, void *mem_user_data) { return my_realloc(ptr, size); } void session_new() { nghttp2_session *session; nghttp2_session_callbacks *callbacks; nghttp2_mem mem = {NULL, my_malloc_cb, my_free_cb, my_calloc_cb, my_realloc_cb}; ... nghttp2_session_client_new3(&session, callbacks, NULL, NULL, &mem); ... } ``` -------------------------------- ### Initialize Application Context Source: https://nghttp2.org/documentation/tutorial-server.html Initializes the application context structure, setting up SSL context and event base. ```c static void initialize_app_context(app_context *app_ctx, SSL_CTX *ssl_ctx, struct event_base *evbase) { memset(app_ctx, 0, sizeof(app_context)); app_ctx->ssl_ctx = ssl_ctx; app_ctx->evbase = evbase; } ``` -------------------------------- ### Begin Headers Reception Callback for HTTP/2 Source: https://nghttp2.org/documentation/tutorial-client.html Called when the nghttp2 library starts receiving a header block for a frame. This implementation prints a message indicating the start of response headers for a specific stream. ```c static int on_begin_headers_callback(nghttp2_session *session, const nghttp2_frame *frame, void *user_data) { http2_session_data *session_data = (http2_session_data *)user_data; (void)session; switch (frame->hd.type) { case NGHTTP2_HEADERS: if (frame->headers.cat == NGHTTP2_HCAT_RESPONSE && session_data->stream_data->stream_id == frame->hd.stream_id) { fprintf(stderr, "Response headers for stream ID=%d:\n", frame->hd.stream_id); } break; } return 0; } ``` -------------------------------- ### Command-line usage for libevent-server Source: https://nghttp2.org/documentation/tutorial-server.html This is the command-line synopsis for running the libevent-server. It requires the port number, path to the SSL/TLS private key, and the path to the certificate file. ```bash $ libevent-server PORT /path/to/server.key /path/to/server.crt ``` -------------------------------- ### Initiate Network Connection with SSL/TLS Source: https://nghttp2.org/documentation/tutorial-client.html Establishes a network connection using libevent's bufferevent, integrating with OpenSSL for TLS. Sets up callbacks for read, write, and event handling. Ensure host and port are valid. ```c static void initiate_connection(struct event_base *evbase, SSL_CTX *ssl_ctx, const char *host, uint16_t port, http2_session_data *session_data) { int rv; struct bufferevent *bev; SSL *ssl; ssl = create_ssl(ssl_ctx); bev = bufferevent_openssl_socket_new( evbase, -1, ssl, BUFFEREVENT_SSL_CONNECTING, BEV_OPT_DEFER_CALLBACKS | BEV_OPT_CLOSE_ON_FREE); bufferevent_enable(bev, EV_READ | EV_WRITE); bufferevent_setcb(bev, readcb, writecb, eventcb, session_data); rv = bufferevent_socket_connect_hostname(bev, session_data->dnsbase, AF_UNSPEC, host, port); if (rv != 0) { errx(1, "Could not connect to the remote host %s", host); } session_data->bev = bev; } ``` -------------------------------- ### nghttp2_session_get_stream_state Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the state of a stream. ```APIDOC ## nghttp2_session_get_stream_state ### Description Gets the state of a stream. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. * **stream_id** (int32_t) - The ID of the stream. ### Returns The state of the stream. ``` -------------------------------- ### Build libbpf from source Source: https://nghttp2.org/documentation/package_README.html Builds and installs libbpf from source if a suitable version is not available on the distribution. Requires git and make. ```bash $ git clone --depth 1 -b v1.7.0 https://github.com/libbpf/libbpf $ cd libbpf $ PREFIX=$PWD/build make -C src install $ cd .. ``` -------------------------------- ### nghttp2_session_get_stream_weight Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the weight of a stream. ```APIDOC ## nghttp2_session_get_stream_weight ### Description Gets the weight of a stream. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. * **stream_id** (int32_t) - The ID of the stream. ### Returns The weight of the stream. ``` -------------------------------- ### nghttp2_session_get_stream_priority Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the priority of a stream. ```APIDOC ## nghttp2_session_get_stream_priority ### Description Gets the priority of a stream. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. * **stream_id** (int32_t) - The ID of the stream. * **pri_spec** (nghttp2_priority_spec *) - Pointer to the nghttp2_priority_spec structure to be filled with the stream's priority. ### Returns On success, 0 is returned. On failure, a negative error code is returned. ``` -------------------------------- ### Create SSL/TLS Context for Server Source: https://nghttp2.org/documentation/tutorial-server.html Initializes and configures an SSL_CTX for a TLS server, including setting options, cipher suites, and loading certificate/key files. ```c static SSL_CTX *create_ssl_ctx(const char *key_file, const char *cert_file) { SSL_CTX *ssl_ctx; ssl_ctx = SSL_CTX_new(TLS_server_method()); if (!ssl_ctx) { errx(1, "Could not create SSL/TLS context: %s", ERR_error_string(ERR_get_error(), NULL)); } SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); #if OPENSSL_VERSION_NUMBER >= 0x30000000L if (SSL_CTX_set1_groups_list(ssl_ctx, "P-256") != 1) { errx(1, "SSL_CTX_set1_groups_list failed: %s", ERR_error_string(ERR_get_error(), NULL)); } #else /* OPENSSL_VERSION_NUMBER < 0x30000000L */ { EC_KEY *ecdh; ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); if (!ecdh) { errx(1, "EC_KEY_new_by_curv_name failed: %s", ERR_error_string(ERR_get_error(), NULL)); } SSL_CTX_set_tmp_ecdh(ssl_ctx, ecdh); EC_KEY_free(ecdh); } #endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */ if (SSL_CTX_use_PrivateKey_file(ssl_ctx, key_file, SSL_FILETYPE_PEM) != 1) { errx(1, "Could not read private key file %s", key_file); } if (SSL_CTX_use_certificate_chain_file(ssl_ctx, cert_file) != 1) { errx(1, "Could not read certificate file %s", cert_file); } SSL_CTX_set_alpn_select_cb(ssl_ctx, alpn_select_proto_cb, NULL); return ssl_ctx; } ``` -------------------------------- ### nghttp2_stream_get_weight Source: https://nghttp2.org/documentation/macros.html Gets the weight of a stream. ```APIDOC ## nghttp2_stream_get_weight ### Description Returns the weight assigned to a specific stream, used in priority calculations. ### Method (Not applicable, this is a C function) ### Parameters - **stream** (*nghttp2_stream*) - Pointer to the stream. ### Request Example (Not applicable) ### Response - **return value** (uint32_t) - The weight of the stream. ``` -------------------------------- ### nghttp2_session_get_stream_option Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the value of a stream option. ```APIDOC ## nghttp2_session_get_stream_option ### Description Gets the value of a stream option. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. * **stream_id** (int32_t) - The ID of the stream. * **option_name** (nghttp2_option_name) - The name of the option. * **value** (int64_t *) - Pointer to a variable to store the option value. ### Returns On success, 0 is returned. On failure, a negative error code is returned. ``` -------------------------------- ### Configure zlib for Android Source: https://nghttp2.org/documentation/building-android-binary.html Configure zlib for Android, specifying the installation prefix, library directory, include directory, and building it as a static library. ```bash #!/bin/sh -e . $NGHTTP2/android-env export HOST=$TARGET ./configure \ --prefix=$PREFIX \ --libdir=$PREFIX/lib \ --includedir=$PREFIX/include \ --static ``` -------------------------------- ### nghttp2_session_get_stream_raw_option Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the value of a stream option. ```APIDOC ## nghttp2_session_get_stream_raw_option ### Description Gets the value of a stream option. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. * **stream_id** (int32_t) - The ID of the stream. * **option_name** (nghttp2_option_name) - The name of the option. * **value** (int64_t *) - Pointer to a variable to store the option value. ### Returns On success, 0 is returned. On failure, a negative error code is returned. ``` -------------------------------- ### nghttp2_session_get_option Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the value of a session option. ```APIDOC ## nghttp2_session_get_option ### Description Gets the value of a session option. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. * **option_name** (nghttp2_option_name) - The name of the option. * **value** (int64_t *) - Pointer to a variable to store the option value. ### Returns On success, 0 is returned. On failure, a negative error code is returned. ``` -------------------------------- ### Test nghttpx Server with nghttp Client Source: https://nghttp2.org/documentation/nghttpx-howto.html After setting up nghttpx, use this command with the nghttp client to send a GET request to the proxy. This verifies that the proxy is correctly handling incoming HTTP/2 connections. ```bash $ nghttp -nv https://localhost:8443/ ``` -------------------------------- ### nghttp2_session_get_buffer_limit Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the buffer limit for the session. ```APIDOC ## nghttp2_session_get_buffer_limit ### Description Gets the buffer limit for the session. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. ### Returns The buffer limit for the session. ``` -------------------------------- ### Example: Integrating Custom Memory Allocators with nghttp2_session Source: https://nghttp2.org/documentation/types.html Demonstrates how to define and use custom memory allocation functions (malloc, free, calloc, realloc) by creating an nghttp2_mem structure and passing it during nghttp2_session client creation. ```c void *my_malloc_cb(size_t size, void *mem_user_data) { return my_malloc(size); } void my_free_cb(void *ptr, void *mem_user_data) { my_free(ptr); } void *my_calloc_cb(size_t nmemb, size_t size, void *mem_user_data) { return my_calloc(nmemb, size); } void *my_realloc_cb(void *ptr, size_t size, void *mem_user_data) { return my_realloc(ptr, size); } void session_new() { nghttp2_session *session; nghttp2_session_callbacks *callbacks; nghttp2_mem mem = {NULL, my_malloc_cb, my_free_cb, my_calloc_cb, my_realloc_cb}; ... nghttp2_session_client_new3(&session, callbacks, NULL, NULL, &mem); ... } ``` -------------------------------- ### nghttp2_session_get_stream_dependency Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the stream dependency information. ```APIDOC ## nghttp2_session_get_stream_dependency ### Description Gets the stream dependency information. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. * **stream_id** (int32_t) - The ID of the stream. * **dep_spec** (nghttp2_priority_spec *) - Pointer to the nghttp2_priority_spec structure to be filled with the stream's dependency information. ### Returns On success, 0 is returned. On failure, a negative error code is returned. ``` -------------------------------- ### Enable HTTP/3 Frontend Source: https://nghttp2.org/documentation/nghttpx-howto.html Configure nghttpx to listen on a UDP port for HTTP/3 traffic by adding the 'quic' parameter to the --frontend option. ```shell frontend=*,443;quic ``` -------------------------------- ### nghttp2_session_get_stream_state Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the current state of a stream. ```APIDOC ## nghttp2_session_get_stream_state ### Description Gets the current state of a stream. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. * **stream_id** (int32_t) - The ID of the stream. ### Returns The current state of the stream, or NGHTTP2_STREAM_STATE_CLOSED if the stream does not exist. ``` -------------------------------- ### ALPN Protocol Selection Callback Example Source: https://nghttp2.org/documentation/nghttp2_select_next_protocol.html Demonstrates how to use nghttp2_select_next_protocol within an SSL_CTX_set_alpn_select_cb callback to handle ALPN negotiation. ```c static int alpn_select_proto_cb(SSL* ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { int rv; rv = nghttp2_select_next_protocol((unsigned char**)out, outlen, in, inlen); if (rv == -1) { return SSL_TLSEXT_ERR_NOACK; } if (rv == 1) { ((MyType*)arg)->http2_selected = 1; } return SSL_TLSEXT_ERR_OK; } ... SSL_CTX_set_alpn_select_cb(ssl_ctx, alpn_select_proto_cb, my_obj); ``` -------------------------------- ### Build nghttp2 Documentation Source: https://nghttp2.org/documentation/package_README.html Generate HTML documentation for nghttp2. The output will be placed in the doc/manual/html/ directory and is not installed by default. ```bash make html ``` -------------------------------- ### nghttp2_session_get_state Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the current state of the session. ```APIDOC ## nghttp2_session_get_state ### Description Gets the current state of the session. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. ### Returns The current state of the session. ``` -------------------------------- ### nghttp Client with HTTP Upgrade Source: https://nghttp2.org/documentation/package_README.html This command demonstrates how to initiate an HTTP/2 connection using the HTTP Upgrade mechanism. The -nvu flags enable verbose output and show the upgrade process. ```bash $ nghttp -nvu http://nghttp2.org [ 0.011] Connected [ 0.011] HTTP Upgrade request GET / HTTP/1.1 Host: nghttp2.org Connection: Upgrade, HTTP2-Settings Upgrade: h2c HTTP2-Settings: AAMAAABkAAQAAP__ Accept: */* User-Agent: nghttp2/1.0.1-DEV [ 0.018] HTTP Upgrade response HTTP/1.1 101 Switching Protocols Connection: Upgrade Upgrade: h2c [ 0.018] HTTP Upgrade success [ 0.018] recv SETTINGS frame (niv=2) [SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100] [SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535] [ 0.018] send SETTINGS frame (niv=2) [SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100] [SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535] [ 0.018] send SETTINGS frame ``` -------------------------------- ### nghttp2_stream_get_state Source: https://nghttp2.org/documentation/macros.html Gets the current state of a stream. ```APIDOC ## nghttp2_stream_get_state ### Description Returns the current state of a specific stream within the nghttp2 session. ### Method (Not applicable, this is a C function) ### Parameters - **stream** (*nghttp2_stream*) - Pointer to the stream. ### Request Example (Not applicable) ### Response - **return value** (int) - An integer representing the current state of the stream (e.g., `NGHTTP2_STREAM_STATE_IDLE`, `NGHTTP2_STREAM_STATE_OPEN`, etc.). ``` -------------------------------- ### Configure Chromium to Use HTTP/2 Proxy Source: https://nghttp2.org/documentation/nghttpx-howto.html Launch Chromium with command-line arguments to use a specified PAC file for proxy configuration and enable NPN. ```bash $ google-chrome --proxy-pac-url=file:///path/to/proxy.pac --use-npn ``` -------------------------------- ### nghttp2_session_get_root_stream Source: https://nghttp2.org/documentation/macros.html Gets the root stream of the session. ```APIDOC ## nghttp2_session_get_root_stream ### Description Retrieves a pointer to the root stream object of the nghttp2 session. ### Method (Not applicable, this is a C function) ### Parameters - **session** (*nghttp2_session*) - Pointer to the nghttp2 session. ### Request Example (Not applicable) ### Response - **return value** (*nghttp2_stream*) - Pointer to the root stream object. ``` -------------------------------- ### nghttp2_session_get_remote_settings Source: https://nghttp2.org/documentation/macros.html Gets the remote settings of the session. ```APIDOC ## nghttp2_session_get_remote_settings ### Description Retrieves the current settings received from the remote peer for the nghttp2 session. ### Method (Not applicable, this is a C function) ### Parameters - **session** (*nghttp2_session*) - Pointer to the nghttp2 session. ### Request Example (Not applicable) ### Response - **return value** (*const nghttp2_settings*) - Pointer to the remote settings structure. ``` -------------------------------- ### nghttp2_session_get_local_settings Source: https://nghttp2.org/documentation/macros.html Gets the local settings of the session. ```APIDOC ## nghttp2_session_get_local_settings ### Description Retrieves the current local settings configured for the nghttp2 session. ### Method (Not applicable, this is a C function) ### Parameters - **session** (*nghttp2_session*) - Pointer to the nghttp2 session. ### Request Example (Not applicable) ### Response - **return value** (*const nghttp2_settings*) - Pointer to the local settings structure. ``` -------------------------------- ### Configure HTTP/2 Backend Connection Source: https://nghttp2.org/documentation/nghttpx-howto.html Use the 'proto=h2;tls' parameters in the --backend option to configure an HTTP/2 backend connection with TLS. ```shell backend=,;;proto=h2;tls ``` -------------------------------- ### nghttp2_session_get_extpri_stream_priority Source: https://nghttp2.org/documentation/macros.html Gets the extended priority of a stream. ```APIDOC ## nghttp2_session_get_extpri_stream_priority ### Description Retrieves the extended priority information for a specific stream within the nghttp2 session. ### Method (Not applicable, this is a C function) ### Parameters - **session** (*nghttp2_session*) - Pointer to the nghttp2 session. - **stream_id** (int) - The ID of the stream. ### Request Example (Not applicable) ### Response - **return value** (*nghttp2_priority_spec*) - Pointer to the priority specification structure, or NULL if the stream does not exist. ``` -------------------------------- ### Initialize nghttp2 Session with Callbacks Source: https://nghttp2.org/documentation/tutorial-server.html Sets up the nghttp2 session by creating callbacks and assigning them to specific events like send, frame reception, stream closure, and header processing. ```c static void initialize_nghttp2_session(http2_session_data *session_data) { nghttp2_session_callbacks *callbacks; nghttp2_session_callbacks_new(&callbacks); nghttp2_session_callbacks_set_send_callback2(callbacks, send_callback); nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks, on_frame_recv_callback); nghttp2_session_callbacks_set_on_stream_close_callback( callbacks, on_stream_close_callback); nghttp2_session_callbacks_set_on_header_callback(callbacks, on_header_callback); nghttp2_session_callbacks_set_on_begin_headers_callback( callbacks, on_begin_headers_callback); nghttp2_session_server_new(&session_data->session, callbacks, session_data); nghttp2_session_callbacks_del(callbacks); } ``` -------------------------------- ### nghttp2_session_get_remote_settings_values Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the values of the remote SETTINGS parameters. ```APIDOC ## nghttp2_session_get_remote_settings_values ### Description Gets the values of the remote SETTINGS parameters. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. ### Returns A pointer to the nghttp2_settings structure containing the remote SETTINGS values. ``` -------------------------------- ### nghttp Client with Prior Knowledge Source: https://nghttp2.org/documentation/package_README.html Use this command to connect to an HTTP/2 server with prior knowledge. The -nv flags enable verbose output for framing information. ```bash $ nghttp -nv https://nghttp2.org [ 0.190] Connected The negotiated protocol: h2 [ 0.212] recv SETTINGS frame (niv=2) [SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100] [SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535] [ 0.212] send SETTINGS frame (niv=2) [SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100] [SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535] [ 0.212] send SETTINGS frame ; ACK (niv=0) [ 0.212] send PRIORITY frame (dep_stream_id=0, weight=201, exclusive=0) [ 0.212] send PRIORITY frame (dep_stream_id=0, weight=101, exclusive=0) [ 0.212] send PRIORITY frame (dep_stream_id=0, weight=1, exclusive=0) [ 0.212] send PRIORITY frame (dep_stream_id=7, weight=1, exclusive=0) [ 0.212] send PRIORITY frame (dep_stream_id=3, weight=1, exclusive=0) [ 0.212] send HEADERS frame ; END_STREAM | END_HEADERS | PRIORITY (padlen=0, dep_stream_id=11, weight=16, exclusive=0) ; Open new stream :method: GET :path: / :scheme: https :authority: nghttp2.org accept: */* accept-encoding: gzip, deflate user-agent: nghttp2/1.0.1-DEV [ 0.221] recv SETTINGS frame ; ACK (niv=0) [ 0.221] recv (stream_id=13) :method: GET [ 0.221] recv (stream_id=13) :scheme: https [ 0.221] recv (stream_id=13) :path: /stylesheets/screen.css [ 0.221] recv (stream_id=13) :authority: nghttp2.org [ 0.221] recv (stream_id=13) accept-encoding: gzip, deflate [ 0.221] recv (stream_id=13) user-agent: nghttp2/1.0.1-DEV [ 0.222] recv PUSH_PROMISE frame ; END_HEADERS (padlen=0, promised_stream_id=2) [ 0.222] recv (stream_id=13) :status: 200 [ 0.222] recv (stream_id=13) date: Thu, 21 May 2015 16:38:14 GMT [ 0.222] recv (stream_id=13) content-type: text/html [ 0.222] recv (stream_id=13) last-modified: Fri, 15 May 2015 15:38:06 GMT [ 0.222] recv (stream_id=13) etag: W/"555612de-19f6" [ 0.222] recv (stream_id=13) link: ; rel=preload; as=stylesheet [ 0.222] recv (stream_id=13) content-encoding: gzip [ 0.222] recv (stream_id=13) server: nghttpx nghttp2/1.0.1-DEV [ 0.222] recv (stream_id=13) via: 1.1 nghttpx [ 0.222] recv (stream_id=13) strict-transport-security: max-age=31536000 [ 0.222] recv HEADERS frame ; END_HEADERS (padlen=0) ; First response header [ 0.222] recv DATA frame ; END_STREAM [ 0.222] recv (stream_id=2) :status: 200 [ 0.222] recv (stream_id=2) date: Thu, 21 May 2015 16:38:14 GMT [ 0.222] recv (stream_id=2) content-type: text/css [ 0.222] recv (stream_id=2) last-modified: Fri, 15 May 2015 15:38:06 GMT [ 0.222] recv (stream_id=2) etag: W/"555612de-9845" [ 0.222] recv (stream_id=2) content-encoding: gzip [ 0.222] recv (stream_id=2) server: nghttpx nghttp2/1.0.1-DEV [ 0.222] recv (stream_id=2) via: 1.1 nghttpx [ 0.222] recv (stream_id=2) strict-transport-security: max-age=31536000 [ 0.222] recv HEADERS frame ; END_HEADERS (padlen=0) ; First push response header [ 0.228] recv DATA frame ; END_STREAM [ 0.228] send GOAWAY frame (last_stream_id=2, error_code=NO_ERROR(0x00), opaque_data(0)=[]) ``` -------------------------------- ### Create SSL Context for HTTP/2 Client Source: https://nghttp2.org/documentation/tutorial-client.html Initializes an SSL_CTX for TLS connections, configuring it for HTTP/2 by setting options and specifying ALPN protocols. ```c static SSL_CTX *create_ssl_ctx(void) { SSL_CTX *ssl_ctx; ssl_ctx = SSL_CTX_new(TLS_client_method()); if (!ssl_ctx) { errx(1, "Could not create SSL/TLS context: %s", ERR_error_string(ERR_get_error(), NULL)); } SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); SSL_CTX_set_alpn_protos(ssl_ctx, (const unsigned char *)"\x02h2", 3); return ssl_ctx; } ``` -------------------------------- ### nghttp2_session_get_local_settings_values Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the values of the local SETTINGS parameters. ```APIDOC ## nghttp2_session_get_local_settings_values ### Description Gets the values of the local SETTINGS parameters. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. ### Returns A pointer to the nghttp2_settings structure containing the local SETTINGS values. ``` -------------------------------- ### nghttp2_session_get_closed_stream_id Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the ID of the last closed stream. ```APIDOC ## nghttp2_session_get_closed_stream_id ### Description Gets the ID of the last closed stream. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. ### Returns The ID of the last closed stream. ``` -------------------------------- ### Initialize nghttp2 Session Callbacks Source: https://nghttp2.org/documentation/tutorial-client.html Sets up the necessary callbacks for the nghttp2 session, including data reception, stream closure, and header handling. ```c void setup_callbacks(http2_session_data *session_data) { nghttp2_session_callbacks *callbacks; nghttp2_session_callbacks_new(&callbacks); nghttp2_session_callbacks_set_on_frame_recv_callback( callbacks, on_frame_recv_callback); nghttp2_session_callbacks_set_on_data_chunk_recv_callback( callbacks, on_data_chunk_recv_callback); nghttp2_session_callbacks_set_on_stream_close_callback( callbacks, on_stream_close_callback); nghttp2_session_callbacks_set_on_header_callback(callbacks, on_header_callback); nghttp2_session_callbacks_set_on_begin_headers_callback( callbacks, on_begin_headers_callback); nghttp2_session_client_new(&session_data->session, callbacks, session_data); nghttp2_session_callbacks_del(callbacks); } ``` -------------------------------- ### Configure Frontend without TLS and HTTP/2 Backend with TLS Source: https://nghttp2.org/documentation/nghttpx-howto.html Set up a frontend connection without TLS and an HTTP/2 backend connection with TLS using the specified options. ```shell frontend=,;no-tls backend=,;;proto=h2;tls ``` -------------------------------- ### nghttp2_session_get_user_data Source: https://nghttp2.org/documentation/nghttp2_session_want_write.html Gets the user data associated with the session. ```APIDOC ## nghttp2_session_get_user_data ### Description Gets the user data associated with the session. ### Parameters * **session** (nghttp2_session *) - Pointer to the nghttp2 session. ### Returns Pointer to the user data associated with the session. ``` -------------------------------- ### Send Client Connection Settings Source: https://nghttp2.org/documentation/tutorial-client.html Submits the initial SETTINGS frame to the remote peer, configuring parameters like maximum concurrent streams. ```c static void send_client_connection_header(http2_session_data *session_data) { nghttp2_settings_entry iv[1] = { {NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100}}; int rv; /* client 24 bytes magic string will be sent by nghttp2 library */ rv = nghttp2_submit_settings(session_data->session, NGHTTP2_FLAG_NONE, iv, ARRLEN(iv)); if (rv != 0) { errx(1, "Could not submit SETTINGS: %s", nghttp2_strerror(rv)); } } ``` -------------------------------- ### nghttp2_session_get_next_obey_flags Source: https://nghttp2.org/documentation/nghttp2_session_set_local_window_size.html Gets the next obey flags for the session. ```APIDOC ## nghttp2_session_get_next_obey_flags ### Description Gets the next obey flags for the session. These flags control how the session behaves regarding certain protocol aspects. ### Parameters * `session` (nghttp2_session *) - Pointer to the `nghttp2_session` structure. ### Returns The next obey flags. ``` -------------------------------- ### nghttp2_stream_get_sum_dependency_weight Source: https://nghttp2.org/documentation/macros.html Gets the sum of weights of dependent streams. ```APIDOC ## nghttp2_stream_get_sum_dependency_weight ### Description Calculates and returns the sum of weights of all streams that depend on the given stream. ### Method (Not applicable, this is a C function) ### Parameters - **stream** (*nghttp2_stream*) - Pointer to the stream. ### Request Example (Not applicable) ### Response - **return value** (uint32_t) - The sum of weights of dependent streams. ``` -------------------------------- ### Run integration tests Source: https://nghttp2.org/documentation/package_README.html Executes integration tests for the nghttpx proxy server. Assumes tests are in the 'integration-tests' directory and Go dependencies are managed. ```bash $ make it ``` -------------------------------- ### nghttp2_stream_get_parent Source: https://nghttp2.org/documentation/macros.html Gets the parent stream of a given stream. ```APIDOC ## nghttp2_stream_get_parent ### Description Retrieves a pointer to the parent stream of a specified stream in the nghttp2 session's stream hierarchy. ### Method (Not applicable, this is a C function) ### Parameters - **stream** (*nghttp2_stream*) - Pointer to the child stream. ### Request Example (Not applicable) ### Response - **return value** (*nghttp2_stream*) - Pointer to the parent stream, or NULL if it's the root stream. ``` -------------------------------- ### Build HTTP/3 enabled h2load and nghttpx Source: https://nghttp2.org/documentation Instructions for building h2load and nghttpx with HTTP/3 support. Ensure you have the necessary dependencies installed. ```bash autoreconf -fi ./configure --enable-http3 make ``` -------------------------------- ### nghttp2_session_get_state Source: https://nghttp2.org/documentation/nghttp2_session_find_stream.html Gets the current state of the nghttp2 session. ```APIDOC ## nghttp2_session_get_state ### Description Gets the current state of the nghttp2 session. ### Parameters - **session** (nghttp2_session *) - Pointer to the session. ### Returns - **nghttp2_session_state** - The current state of the session. ```