Sagui library v3.5.0
Cross-platform library which helps to develop web servers or frameworks.
Loading...
Searching...
No Matches
example_httpsrv_tls.c

Simple "hello world" HTTPS server example.

/* _
* ___ __ _ __ _ _ _(_)
* / __|/ _` |/ _` | | | | |
* \__ \ (_| | (_| | |_| | |
* |___/\__,_|\__, |\__,_|_|
* |___/
*
* Cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (C) 2016-2019 Silvio Clecio <silvioprog@gmail.com>
*
* Sagui library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Sagui library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sagui library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sagui.h>
/* NOTE: Error checking has been omitted to make it clear. */
/*
* Very simple insecure HTTPS server. For total security, use: https://help.ubuntu.com/community/OpenSSL.
*
* Client example using cURL:
*
* curl -k https://localhost:<PORT>
*
* Certificate generation:
*
* # Private key
* certtool --generate-privkey --outfile server.key
* echo 'organization = GnuTLS test server' > server.tmpl
* echo 'cn = test.gnutls.org' >> server.tmpl
* echo 'tls_www_server' >> server.tmpl
* echo 'expiration_days = 3650' >> server.tmpl
*
* # Certificate
* certtool --generate-self-signed --load-privkey server.key --template server.tmpl --outfile server.pem
*/
const char private_key[] =
"-----BEGIN RSA PRIVATE KEY-----\n"
"MIIG5QIBAAKCAYEA1qzaG2QbwnBzbRYENJbh17BoNM1XIV4PH030FJL77BR+KfL/\n"
"qfUwY6Nkzy/iDtn1BYrUmd8Mus34obsC0ad/sIarailK//mAYVyrVHuLgOJ4c1Fu\n"
"sqMvafEB67LLFxeItwK1X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u/eQJ/JMSVBwxHuKQXm502fjJnMPMDuB5Y8E5nZd42Qrl/5Qs5+m/FmxhFoJIM\n"
"s+Ijb/DCMu9KWPrPSaY6eJ+vg0V2n94V89A8KgcCAwEAAaNEMEIwDAYDVR0TAQH/\n"
"BAIwADATBgNVHSUEDDAKBggrBgEFBQcDATAdBgNVHQ4EFgQUxH1HUKpvYFEHrPeJ\n"
"sY0I7HQDbIIwDQYJKoZIhvcNAQELBQADggGBABDTlhiKuuh51Rx+mpt5vjJ7zXRJ\n"
"1RoHY92AmZY49hfdpUp6mMLvbRdD6REv4pe1IORGsqgPk4MPCQsaFGbpZS1CokLz\n"
"Sex4LZruHYWtv18fUFliJIZZSITnArNB29lXAem5T20D04bCCYLJJB3VTcPilbkf\n"
"ipT/h1CyhbWX14ZtkzzpWMAwLgod6uZvJqJXTpjwdWA7Anp4yfh2QxBYC5/us4xP\n"
"wHa0euWOBZ+Q9ZNZ/fFdLESLSbBob9736hBglNSgBFCMNezqs18/EGIJcS7w96PN\n"
"YJtVsVhcQJMMT4dnaSM/Ri4CPv7j8/zll6uq4kHpxZLhuxeRSeuKBn6jl0wHQFd1\n"
"7bpHrRLBuRyDhPWrzdmMY2dyJ5DkO39auisAyJza8IddfNnCa7howSjp/ZvZN9Sf\n"
"gi1klZeSpe+iijWQaxjIKAr/g8Rn+ALfeMAitm6DjCcTUkXdKtXVqTwdFZRNxrNH\n"
"lqt+H07raUsv/p50oVS6/Euv8fBm3EKPwxC64w==\n"
"-----END CERTIFICATE-----";
static void req_cb(__SG_UNUSED void *cls, __SG_UNUSED struct sg_httpreq *req,
struct sg_httpres *res) {
"<html><head><title>Hello world</title></head><body>Hello "
"<font color=\"green\">HTTPS</font></body></html>",
"text/html; charset=utf-8", 200);
}
int main(int argc, const char *argv[]) {
struct sg_httpsrv *srv;
uint16_t port;
if (argc != 2) {
printf("%s <PORT>\n", argv[0]);
return EXIT_FAILURE;
}
port = strtol(argv[1], NULL, 10);
srv = sg_httpsrv_new(req_cb, NULL);
if (!sg_httpsrv_tls_listen(srv, private_key, certificate, port, false)) {
return EXIT_FAILURE;
}
fprintf(stdout, "Server running at https://localhost:%d\n",
fflush(stdout);
getchar();
return EXIT_SUCCESS;
}
struct sg_httpsrv * sg_httpsrv_new(sg_httpreq_cb cb, void *cls) __attribute__((malloc))
bool sg_httpsrv_tls_listen(struct sg_httpsrv *srv, const char *key, const char *cert, uint16_t port, bool threaded)
void sg_httpsrv_free(struct sg_httpsrv *srv)
#define sg_httpres_send(res, val, content_type, status)
Definition sagui.h:1032
uint16_t sg_httpsrv_port(struct sg_httpsrv *srv)