Created
December 20, 2023 18:38
-
-
Save nir9/368b64f9e3105c2682568d86b8575e00 to your computer and use it in GitHub Desktop.
Minimalist HTTPS Web Server in C - not for production use, only for fun :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <openssl/ssl.h> | |
#include <stdio.h> | |
#include <string.h> | |
void main() { | |
int sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
struct sockaddr_in addr = { | |
AF_INET, | |
htons(8080), | |
0 | |
}; | |
bind(sockfd, &addr, sizeof(addr)); | |
listen(sockfd, 10); | |
int clientfd = accept(sockfd, NULL, NULL); | |
SSL_CTX* ctx = SSL_CTX_new(TLS_server_method()); | |
SSL* ssl = SSL_new(ctx); | |
SSL_set_fd(ssl, clientfd); | |
SSL_use_certificate_chain_file(ssl, "fullChain"); | |
SSL_use_PrivateKey_file(ssl, "theKey", SSL_FILETYPE_PEM); | |
SSL_accept(ssl); | |
char buffer[1024] = {0}; | |
SSL_read(ssl, buffer, 1023); | |
// GET /file .... | |
char* file_request = buffer + 5; | |
char response[1024] = {0}; | |
char* metadata = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; | |
memcpy(response, metadata, strlen(metadata)); | |
if (strncmp(file_request, "index.html ", 11) == 0) { | |
FILE* f = fopen("index.html", "r"); | |
fread(response + strlen(metadata), 1024 - strlen(metadata) - 1, 1, f); | |
fclose(f); | |
} else { | |
char* error = "No page found"; | |
memcpy(response + strlen(metadata), error, strlen(error)); | |
} | |
SSL_write(ssl, response, 1024); | |
SSL_shutdown(ssl); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment