Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deshmukhrajvardhan/3714c07c4216395c02a165d128c1bedd to your computer and use it in GitHub Desktop.
Save deshmukhrajvardhan/3714c07c4216395c02a165d128c1bedd to your computer and use it in GitHub Desktop.
Application to use HTTP2 multiplexing with libcurl
Pre-req:
https://serversforhackers.com/c/curl-with-http2-support
Code:libcurl-client-app.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* curl stuff */
#include <curl/curl.h>
int main(){
//check nghttp2 supprt
const curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
if(data->features & CURL_VERSION_HTTP2){
fprintf(stdout, "This libcurl DOES have HTTP2 support!\n");
} else {
fprintf(stdout, "This libcurl does NOT have HTTP/2 support!\n");
}
// Create multi handle with multiplex over a single connection
CURLM *multi_handle = curl_multi_init();
curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, (long) 1L);
curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_HTTP1 | CURLPIPE_MULTIPLEX);
// Add some requests
int NUM_HANDLES = 3;
CURL *easy[NUM_HANDLES];
for(int i = 0; i < 3; i++){
char url[1024];
snprintf(url, 1024, "https://10.10.3.2/www-itec.uni-klu.ac.at/ftp/datasets/\
DASHDataset2014/BigBuckBunny/2sec/bunny_4219897bps/BigBuckBunny_2s144.m4s");
//snprintf(url, 1024, "https://http2.akamai.com/demo");
easy[i] = curl_easy_init();
curl_easy_setopt(easy[i], CURLOPT_VERBOSE, 1L);
curl_easy_setopt(easy[i], CURLOPT_URL, url);
curl_easy_setopt(easy[i], CURLOPT_SSLCERTTYPE, "PEM");
curl_easy_setopt(easy[i], CURLOPT_CAINFO, "cert.pem");
curl_easy_setopt(easy[i], CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_easy_setopt(easy[i], CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_multi_add_handle(multi_handle, easy[i]);
}
//perform requests
int still_running = 1;
while(still_running){
curl_multi_perform(multi_handle, &still_running);
}
//cleanups
for(int i = 0; i < NUM_HANDLES; i++)
curl_easy_cleanup(easy[i]);
curl_multi_cleanup(multi_handle);
exit(0);
}
Compilation:
gcc libcurl-client-app.c -o libcurl-client-app.o -std=c99 -lcurl
./libcurl-client-app.o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment