Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site











SourceForge.net Logo

Https

 

Simple HTTPS server (certificates for localhost included)

 

 

Https.cpp

 

#include <Core/Core.h>

 

using namespace Upp;

 

// To run, OpenSSL libraries must be installed and accessible

// After starting, type "https://localhost:4000" into your browser

 

CONSOLE_APP_MAIN

{

    StdLogSetup(LOG_COUT|LOG_FILE);

 

    TcpSocket   server;

 

    if(!server.Listen(4000, 10)) {

        LOG("Cannot open server port for listening\r\n");

        return;

    }

 

    for(;;) {

        TcpSocket socket;

        LOG("===================================================== Waiting...");

        if(socket.Accept(server)) {

            LOG("Connection accepted");

            socket.SSLCertificate(LoadFile(GetDataFile("server.crt")),

                                  LoadFile(GetDataFile("server.key")),

                                  false);

            if(!socket.StartSSL()) {

                LOG("Failed to start SSL: " << socket.GetErrorDesc());

                continue;

            }

            while(socket.SSLHandshake());

            if(socket.IsError()) {

                LOG("SSL handshake failed: " << socket.GetErrorDesc());

                continue;

            }

            LOG("SSL established");

            HttpHeader http;

            if(!http.Read(socket)) {

                LOG("Failed to obtain HTTP header");

                continue;

            }

            LOG("Got HTTP header: " << http.GetMethod() << ' ' << http.GetURI());

            String html;

            html << "<html>"

                 << "<b>Method:</b> " << http.GetMethod() << "<br>"

                 << "<b>URI:</b> " << http.GetURI() << "<br>";

            for(int i = 0; i < http.fields.GetCount(); i++)

                html << "<b>" << http.fields.GetKey(i) << ":</b> " << http.fields[i] << "<br>";

            int len = (int)http.GetContentLength();

            if(len > 0)

                socket.GetAll(len);

            html << "<b><i>Current time:</i></b> " << GetSysTime() << "</html>";

            HttpResponse(socket, http.scgi, 200, "OK", "text/html", html);

            LOG("Request finished");

        }

    }

}

 

 

 

server.key

 

-----BEGIN RSA PRIVATE KEY-----

MIICXQIBAAKBgQDioZOQ8350EYK+GSg2uxEdcpqiRLgVVzwpDGYS2w0ongU/wE6k

pWqvVoq8tYg+19uUQRQnTl1AE9XGvvfcPeO1cr1tmjDse85TJMwzOPjEcbFXV4Va

UdR04NbsZR9gXy7kXMDu1pwfuOuWsUkUor1QeeQygeIo4hMw+HGEs1GVZwIDAQAB

AoGBAIGsiB4JZRKOrmuNQ3hFg16BI7+zaE01tAOcx48EbbwO5tv6ckU9LRUUhJ/D

kyi6JDbpIdn3ud2fO9PTk0ke1Hn7ehHn47k39Ady/4TZUlYohjrNjBbSQs4FD3A8

npq5wiHg4Bokfz2Bd9HzSaaIC8gZvDXg6OBBby4HzQ4KoFlRAkEA88bqfDVZ4d7m

7rjppvTI5IleH2rmAm7tQSRg9j6qZxw0ZYiOCyyNMnzb0TjdEhPH0b6SZqWMYh3b

MDzGFu7JSQJBAO3+lDK7Zkia9UtOm17qUvTcMzi5rmW8kfeog5Tzwx80mUsvC7Gk

MZX7XDajKMz5rhW8ex4iDcMQM8/RQEu+mS8CQCoEfyYpi/SClwtOk/dS+c1qX+7K

XhrNQ9eWjZ/j7TsYgW9+ufzzEONC4iCNzIzxG9/rGRDYN/oxazZFQ19wiQECQHc3

iIslPqZuISP5G3FfVfDcgTgQI2FJJaF2o3sw5HQ0R/nWdjglNMWZpWalMqIDDIZM

5SuCCXzGF/4pJtkKf/cCQQDM09LC+cJMVPyzDmgEUu+izq8HtN7LYoxL2ikf1gLD

qTGUlTl9/+b5UVc95OpWykU7y6C+F3Vi2US1a3tU214u

-----END RSA PRIVATE KEY-----

 

 

 

 

Do you want to contribute?