Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a HTTP connection using C?

Tags:

c

http

comet

How do I make an HTTP connection using C? Any references or sample code out there? Further, how do I implement opening a Comet connection from client written in C? (Any additional info about opening an HTTPS connection would also be appreciated.) Thanks!

like image 806
ikevin8me Avatar asked Feb 21 '23 09:02

ikevin8me


2 Answers

Try this, a good reference

http://beej.us/guide/bgnet/

The above is tutorial for socket programming, you can use those to built an http client of your own, otherwise you can choose built in libraries like curl. If you use plain sockets then you need to encode and decode the header information along with each request, also so many others factors about http protocol need to be considered http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

like image 132
Akhil Thayyil Avatar answered Feb 23 '23 22:02

Akhil Thayyil


This is an old topic but for sure one that is Googled a lot. After many weeks of my own research, reading about this topic and going over some complicated examples and tutorials. I boiled it down to the bare minimum needed to make it work.

Important note: this code doesn't check if the public key was signed by a valid authority. Meaning I don't use root certificates for validation.

The code below is also a part of a bigger project that I have build, You can check the README.md hear where you can find additional information how to install openSSL, and how to compile the code.

#include <stdio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>

#define APIKEY "YOUR_API_KEY"
#define HOST "YOUR_WEB_SERVER_URI"
#define PORT "443"

int main() {

    //
    //  Initialize Variables
    //
    BIO* bio;
    SSL* ssl;
    SSL_CTX* ctx;

    //
    //   Registers the available SSL/TLS ciphers and digests.
    //
    //   Basically start the security layer.
    //
    SSL_library_init();

    //
    //  Creates a new SSL_CTX object as framework to establish TLS/SSL
    //  or DTLS enabled connections
    //
    ctx = SSL_CTX_new(SSLv23_client_method());

    //
    //  -> Error check
    //
    if (ctx == NULL)
    {
        printf("Ctx is null\n");
    }

    //
    //   Creates a new BIO chain consisting of an SSL BIO
    //
    bio = BIO_new_ssl_connect(ctx);

    //
    //  uses the string name to set the hostname
    //
    BIO_set_conn_hostname(bio, HOST ":" PORT);

    //
    //   Attempts to connect the supplied BIO
    //
    if(BIO_do_connect(bio) <= 0)
    {
        printf("Failed connection\n");
        return 1;
    }
    else
    {
        printf("Connected\n");
    }

    //
    //  Data to send to create a HTTP request.
    //
    char* write_buf = "POST / HTTP/1.1\r\n"
                      "Host: " HOST "\r\n"
                      "Authorization: Basic " APIKEY "\r\n"
                      "Connection: close\r\n"
                      "\r\n";

    //
    //   Attempts to write len bytes from buf to BIO
    //
    if(BIO_write(bio, write_buf, strlen(write_buf)) <= 0)
    {
        //
        //  Handle failed write here
        //
        if(!BIO_should_retry(bio))
        {
            // Not worth implementing, but worth knowing.
        }

        //
        //  -> Let us know about the failed write
        //
        printf("Failed write\n");
    }

    //
    //  Variables used to read the response from the server
    //
    int size;
    char buf[1024];

    //
    //  Read the response message
    //
    for(;;)
    {
        //
        //  Put response in a buffer of size.
        //
        size = BIO_read(bio, buf, 1023);

        //
        //  If no more data, then exit the loop
        //
        if(size <= 0)
        {
            break;
        }

        //
        //  Terminate the string with a 0 so the system knows where
        //  the end is.
        //
        buf[size] = 0;

        printf("%s", buf);
    }

    //
    //  Clean after ourselves
    //
    BIO_free_all(bio);
    SSL_CTX_free(ctx);

    return 0;
}
like image 41
David Gatti Avatar answered Feb 23 '23 23:02

David Gatti