[Intro]]
I have been practicing C for 4 Months 5 times a Week, I am attempting to make the best possible guess for moving into web server/client programming for HTTP2. I have worked with HTML5 & Javascript I also work with HTTP2 & have used SPDY on a low level.
[Info]
Debian GNU/Linux (Buster), V.10
Compiler – Clang
headers HTTP2(?)
Headers HTTP(sys/socket.h, sys/types.h, signal.h, arpa/inet.h)
Network Protocol – TCP, HTTP2, HTTP.
[Question]
When playing around with Java you notice a lot of HTTP2
being used in Game engines. HTTP2
is being standardized more & more in current languages as of 2021.(FOCUS POINT->) C has a standard Library for working with HTTP
& TCP
.
HTTP2
uses Encryption & as of my knowledge is based in "TCP
". Is there a standard for C working with, "HTTP2 TCP
" types. I need to be able to read the Text from HTTP2
& easily preform tasks(send(), recv().(<-FOCUS POINT END).
[FINAL]
Can I Realistically develop HTTP2 Programs Using C standard POSIX Library’s.
How would I go about using HTTP2 in C & is it even posible.
2
Answers
HTTP is a protocol. It defines the way computers communicate with each other. There a lot of different protocols out there; HTTP/2 and HTTP is based on TCP/IP, which almost all computers use to connect to servers. TCP/IP specifies how to create a connection between a client and a specific server, and send raw bytes. HTTP(2) is a layer over this, which defines how a client should tell the server what resource it wants.
The API provided by base POSIX extends to only raw TCP/IP. Here’s an example of what an application could send to a web server to request a web page (this is an HTTP/1 request, HTTP/2 is binary and encrypted, making it not human-readable)
However, it’s kind of inconvenient to assemble these requests manually in your program, especially when you start doing things like sending parameters, encryption, etc. In this case, people should use some libraries on the machine. It’s important that these are nonstandard. Probably one of the most common ones is
libcurl
.Here’s a libcurl tutorial. To summarize:
You can compile your program with libcurl by providing some flags to the compiler and the linker. You can get these by running
curl-config --cflags
andcurl-config --libs
.Initialize libcurl with
curl_global_init(bitfield)
. The bitfield can haveCURL_GLOBAL_WIN32
andCURL_GLOBAL_SSL
as its values. You probably needCURL_GLOBAL_SSL
since you want to use encryption.libcurl has two interfaces:
easy
andmulti
. I am going to covereasy
.Get an easy handle by calling
curl_easy_init()
. This handle allows you to make requests.Set options on the handle by using
curl_easy_setopt(handle, option, value)
.handle
is the "easy handle" you obtained,option
is a constant – for exampleCURLOPT_URL
for setting the URL to request – andvalue
is the value you want to set it to. It’s often a string.Set a callback function with
option
asCURLOPT_WRITEFUNCTION
. It should have the signaturesize_t(void *buffer, size_t size, size_t nmemb, void *userp)
. It’s called when libcurl receives data from the server. Usually, it’s called many times.nmemb
points out the size of the data inbuffer
. It should return the count of bytes it successfully processed. Normally this isnmemb
.Execute the request with
curl_easy_perform(handle)
.When you’re done with every request you want to make, call
curl_global_cleanup()
.Within C standard library there is no part dedicated to
http2
.However, working with
http2
inC
is definitely possible, well documented and widely used. For example:More info about
http2
and various programming languages can be found here: https://github.com/httpwg/http2-spec/wiki/Implementations