2025-01-04 08:40:31 +01:00
// Written by retoor@molodetz.nl
// The source code provides functionality for making HTTP POST and GET requests over SSL/TLS using OpenSSL. It includes initialization and cleanup of the OpenSSL library, creation of SSL context, socket creation and connection, and sending requests with handling responses. Furthermore, it interfaces with JSON and handles authentication using an external "auth.h" file.
// Includes: "auth.h", <json-c/json.h>
// MIT License
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
# ifndef CALPACA_HTTP_H
# define CALPACA_HTTP_H
2025-01-04 08:40:31 +01:00
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <unistd.h>
# include <arpa/inet.h>
# include <netdb.h>
# include <openssl/ssl.h>
# include <openssl/err.h>
# include "auth.h"
# include <json-c/json.h>
2025-01-04 08:40:31 +01:00
void init_openssl ( ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
SSL_load_error_strings ( ) ;
OpenSSL_add_ssl_algorithms ( ) ;
}
2025-01-04 08:40:31 +01:00
void cleanup_openssl ( ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
EVP_cleanup ( ) ;
}
2025-01-04 08:40:31 +01:00
SSL_CTX * create_context ( ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
const SSL_METHOD * method = TLS_method ( ) ;
SSL_CTX * ctx = SSL_CTX_new ( method ) ;
SSL_CTX_load_verify_locations ( ctx , " /etc/ssl/certs/ca-certificates.crt " , NULL ) ;
return ctx ;
}
2025-01-04 08:40:31 +01:00
SSL_CTX * create_context2 ( ) {
const SSL_METHOD * method = TLS_client_method ( ) ;
SSL_CTX * ctx = SSL_CTX_new ( method ) ;
if ( ! ctx ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
perror ( " Unable to create SSL context " ) ;
ERR_print_errors_fp ( stderr ) ;
exit ( EXIT_FAILURE ) ;
}
return ctx ;
}
2025-01-04 08:40:31 +01:00
int create_socket ( const char * hostname , int port ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
struct hostent * host ;
struct sockaddr_in addr ;
host = gethostbyname ( hostname ) ;
2025-01-04 08:40:31 +01:00
if ( ! host ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
perror ( " Unable to resolve host " ) ;
exit ( EXIT_FAILURE ) ;
}
int sock = socket ( AF_INET , SOCK_STREAM , 0 ) ;
2025-01-04 08:40:31 +01:00
if ( sock < 0 ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
perror ( " Unable to create socket " ) ;
exit ( EXIT_FAILURE ) ;
}
addr . sin_family = AF_INET ;
addr . sin_port = htons ( port ) ;
addr . sin_addr . s_addr = * ( long * ) ( host - > h_addr ) ;
2025-01-04 08:40:31 +01:00
if ( connect ( sock , ( struct sockaddr * ) & addr , sizeof ( addr ) ) ! = 0 ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
perror ( " Unable to connect to host " ) ;
close ( sock ) ;
exit ( EXIT_FAILURE ) ;
}
return sock ;
}
2025-01-04 08:40:31 +01:00
char * http_post ( const char * hostname , char * url , char * data ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
init_openssl ( ) ;
int port = 443 ;
SSL_CTX * ctx = create_context ( ) ;
int sock = create_socket ( hostname , port ) ;
SSL * ssl = SSL_new ( ctx ) ;
SSL_set_connect_state ( ssl ) ;
SSL_set_tlsext_host_name ( ssl , hostname ) ;
SSL_set_fd ( ssl , sock ) ;
2025-01-04 08:35:39 +01:00
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
int buffer_size = 4096 ;
2025-01-04 08:40:31 +01:00
char * buffer = malloc ( buffer_size ) ;
if ( SSL_connect ( ssl ) < = 0 ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
ERR_print_errors_fp ( stderr ) ;
2025-01-04 08:40:31 +01:00
} else {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
size_t len = strlen ( data ) ;
2025-01-04 08:40:31 +01:00
char * request = malloc ( len + 4096 ) ;
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
sprintf ( request ,
" POST %s HTTP/1.1 \r \n "
" Content-Length: %ld \r \n "
" Content-Type: application/json \r \n "
" Host: api.openai.com \r \n "
" Authorization: Bearer %s \r \n "
" Connection: close \r \n \r \n %s " ,
2025-01-04 16:54:48 +01:00
url , len , resolve_api_key ( ) , data ) ;
2025-01-04 08:35:39 +01:00
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
SSL_write ( ssl , request , strlen ( request ) ) ;
2025-01-04 08:35:39 +01:00
free ( request ) ;
2025-01-04 08:40:31 +01:00
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
int bytes ;
int bytes_total = 0 ;
2025-01-04 08:40:31 +01:00
while ( ( bytes = SSL_read ( ssl , buffer + bytes_total , buffer_size - 1 ) ) > 0 ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
bytes_total + = bytes ;
buffer = realloc ( buffer , bytes_total + buffer_size ) ;
buffer [ bytes_total ] = ' \0 ' ;
}
}
SSL_free ( ssl ) ;
close ( sock ) ;
SSL_CTX_free ( ctx ) ;
cleanup_openssl ( ) ;
2025-01-04 08:40:31 +01:00
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
return buffer ;
}
2025-01-04 08:40:31 +01:00
char * http_get ( const char * hostname , char * url ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
init_openssl ( ) ;
int port = 443 ;
SSL_CTX * ctx = create_context ( ) ;
int sock = create_socket ( hostname , port ) ;
SSL * ssl = SSL_new ( ctx ) ;
SSL_set_connect_state ( ssl ) ;
SSL_set_tlsext_host_name ( ssl , hostname ) ;
SSL_set_fd ( ssl , sock ) ;
int buffer_size = 4096 ;
2025-01-04 08:40:31 +01:00
char * buffer = malloc ( buffer_size ) ;
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
2025-01-04 08:40:31 +01:00
if ( SSL_connect ( ssl ) < = 0 ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
ERR_print_errors_fp ( stderr ) ;
2025-01-04 08:40:31 +01:00
} else {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
char request [ buffer_size ] ;
sprintf ( request ,
" GET %s HTTP/1.1 \r \n "
" Host: api.openai.com \r \n "
" Authorization: Bearer %s \r \n "
" Connection: close \r \n \r \n " ,
2025-01-04 16:54:48 +01:00
url , resolve_api_key ( ) ) ;
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
SSL_write ( ssl , request , strlen ( request ) ) ;
int bytes ;
int bytes_total = 0 ;
2025-01-04 08:40:31 +01:00
while ( ( bytes = SSL_read ( ssl , buffer + bytes_total , buffer_size - 1 ) ) > 0 ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
bytes_total + = bytes ;
buffer = realloc ( buffer , bytes_total + buffer_size ) ;
buffer [ bytes_total ] = ' \0 ' ;
}
}
SSL_free ( ssl ) ;
close ( sock ) ;
SSL_CTX_free ( ctx ) ;
cleanup_openssl ( ) ;
return buffer ;
}
2025-01-04 08:35:39 +01:00
# endif