diff options
author | Shav Kinderlehrer <[email protected]> | 2024-07-23 17:48:28 -0400 |
---|---|---|
committer | Shav Kinderlehrer <[email protected]> | 2024-07-23 17:48:28 -0400 |
commit | dc0f2ce9ba97ebb47e05b80a511da6eb29818b63 (patch) | |
tree | dc83035069f5a015047be1ca3da6f65781eb4695 /include | |
parent | f638f4bd1e3a03bc2bdd5f9dcd57d4830fd3c553 (diff) | |
download | molehole-ncurses.tar.gz molehole-ncurses.zip |
Merge old-moleholencurses
Diffstat (limited to 'include')
-rwxr-xr-x | include/color.h | 18 | ||||
-rwxr-xr-x | include/config.h | 56 | ||||
-rwxr-xr-x | include/connect.h | 44 | ||||
-rwxr-xr-x | include/molerat.h | 9 | ||||
-rwxr-xr-x | include/net.h | 24 | ||||
-rwxr-xr-x | include/request.h | 22 | ||||
-rwxr-xr-x | include/response.h | 50 | ||||
-rwxr-xr-x | include/status.h | 27 | ||||
-rwxr-xr-x | include/url.h | 42 | ||||
-rwxr-xr-x | include/util.h | 11 |
10 files changed, 303 insertions, 0 deletions
diff --git a/include/color.h b/include/color.h new file mode 100755 index 0000000..0fbad4f --- /dev/null +++ b/include/color.h @@ -0,0 +1,18 @@ +#ifndef _COLOR_H_ +#define _COLOR_H_ +#include <ncurses.h> + +/** + * Initialize color pairs for the rest of the application. + */ +void set_colors(void); + +enum StatusColorPairs { STATUS_MAIN = 1, STATUS_ERROR, STATUS_PROMPT }; + +enum StatusColors { + COLOR_GREY = 238, + COLOR_DIM_WHITE = 250, + COLOR_DIM_RED = 160 +}; + +#endif diff --git a/include/config.h b/include/config.h new file mode 100755 index 0000000..b90c5bd --- /dev/null +++ b/include/config.h @@ -0,0 +1,56 @@ +#ifndef _CONFIG_H_ +#define _CONFIG_H_ +#include <ncurses.h> +#include <openssl/ssl.h> + +#include "response.h" +#include "url.h" + +/** + * Molehole internal setting. + * + * Holds important information for the molehole runtime like terminal + * dimensions, and active ncurses windows. + */ +struct config_internal { + int height; + int width; + + WINDOW *page_win; + WINDOW *status_win; +}; + +/** + * Molehole current state information. + * + * Holds information pertaining to the content displayed such as the current + * page url. + */ +struct config_state { + char *url_string; + struct url *url; + struct connection *conn; + struct response *res; +}; + +/** + * Molehole configuration. + * + * Holds the state of the program including user configuration. + */ +struct config { + struct config_internal i; + struct config_state s; +}; + +/** + * Initializes a config with default parameters. + */ +void init_config(struct config *conf); + + +/** + * Cleans up a config state and internal. + */ +void conf_cleanup(struct config *conf); +#endif diff --git a/include/connect.h b/include/connect.h new file mode 100755 index 0000000..d733c5e --- /dev/null +++ b/include/connect.h @@ -0,0 +1,44 @@ +#ifndef _CONNECT_H_ +#define _CONNECT_H_ + +#include <openssl/ssl.h> + +#include "config.h" +#include "url.h" + +/* Error codes */ +enum ConnectError { + ERR_GETADDRINFO = -1, + ERR_SOCKET = -2, + ERR_CONNECT = -3, + ERR_SSL_CTX = -4, + ERR_SSL_SSL = -5, +}; + +/** + * Holds all needed information to manage an open TLS connection. + */ +struct connection { + SSL *ssl; + int sockfd; + bool used; +}; + +/** + * Connect to molerat server. + * + * Sets `*ssl` in `struct config *conf` to the current active connection. + */ +int tls_connect(struct config *conf, struct url url); + +/** + * Disconnect and free open connection + */ +void tls_cleanup(struct connection *conn); + +/** + * Allocate a new instance of a `struct connection` + */ +struct connection *init_connection(void); + +#endif diff --git a/include/molerat.h b/include/molerat.h new file mode 100755 index 0000000..4c07866 --- /dev/null +++ b/include/molerat.h @@ -0,0 +1,9 @@ +#ifndef _MOLERAT_H_ +#define _MOLERAT_H_ + +struct key { + char* key; + char* value; +}; + +#endif diff --git a/include/net.h b/include/net.h new file mode 100755 index 0000000..af6bf57 --- /dev/null +++ b/include/net.h @@ -0,0 +1,24 @@ +#ifndef _NET_H_ +#define _NET_H_ +#include "config.h" +#include "request.h" +#include "response.h" + +/** + * Sends a `struct request` to the current `struct connection` contained within + * `struct config *conf`. + */ +int send_request(struct config *conf, struct request *req); + +/** + * Reads a response from the current `struct connection` and parses it into + * `struct response *res` + */ +int read_response(struct config *conf, struct response *res); + +enum NetError { + SSL_SEND_ERROR = -1, + ALLOC_ERROR = -2, + RESPONSE_PARSE_ERROR = -3, +}; +#endif diff --git a/include/request.h b/include/request.h new file mode 100755 index 0000000..087b1c0 --- /dev/null +++ b/include/request.h @@ -0,0 +1,22 @@ +#ifndef _REQUEST_H_ +#define _REQUEST_H_ +#include "molerat.h" +#include "url.h" + +enum RequestKind { GET, PUT, DEL }; + +struct request { + enum RequestKind kind; + struct url url; + struct key *keys; +}; + +/** + * Convert a `struct request` to a string of chars that can be sent over the + * network. + * + * WARNING: Printing the screen won't work because the request ends in two + * \r\n's + */ +char *request_to_string(struct request *request); +#endif diff --git a/include/response.h b/include/response.h new file mode 100755 index 0000000..e4242a5 --- /dev/null +++ b/include/response.h @@ -0,0 +1,50 @@ +#ifndef _RESPONSE_H_ +#define _RESPONSE_H_ + +enum ResponseStatus { + SUCCESS = 10, + CONTENT_UNCHANGED = 11, + + PERMANENT_REDIRECT = 20, + TEMPORARY_REDIRECT = 21, + + MALFORMED_REQUEST = 30, + INVALID_REQUEST = 31, + NOT_AVAILABLE = 32, + + INTERNAL_ERROR = 40, + NOT_SUPPORTED = 41, + SLOW_DOWN = 42, + + CLIENT_CERTIFICATE_REQUIRED = 50 +}; + +struct mime_type { + char *type; + char *subtype; +}; + +struct response { + enum ResponseStatus status; + char *message; + struct mime_type type; + unsigned int length; + char *hash; + char *content; +}; + +/** + * Parses a string into a `struct response`. + */ +int parse_response(struct response *res, char *s); + +/** + * Deallocates `struct response *res`. + */ +void free_response(struct response *res); + +enum ResponseError { + INVALID_STATUS = -1, + UNKNOWN_KEY = -2, +}; +#endif diff --git a/include/status.h b/include/status.h new file mode 100755 index 0000000..45f6465 --- /dev/null +++ b/include/status.h @@ -0,0 +1,27 @@ +#ifndef _STATUS_H_ +#define _STATUS_H_ +#include "config.h" + +/** + * Allocate a status window at the bottom of the terminal + * + * Saves the status window into the `*conf` parameter. + */ +void init_status(struct config *conf); + +/** + * Set the message currently displayed in the status window. + */ +void update_status(struct config *conf, char *s); + +/** + * Set the status to prompt for a Molerat URL. The raw string recieved will be. + * placed in `*conf` + */ +void prompt_status_url(struct config *conf); + +/** + * Same as `update_status` but signify error. + */ +void error_status(struct config *conf, char *s); +#endif diff --git a/include/url.h b/include/url.h new file mode 100755 index 0000000..c1278ee --- /dev/null +++ b/include/url.h @@ -0,0 +1,42 @@ +#ifndef _URL_H_ +#define _URL_H_ + +#define MAX_URL_LENGTH 2048 + +/** + * Molerat URL. + * + * Holds all relevant for molehole to make requests. + */ +struct url { + char *scheme; + char *host; + int port; + char *path; + char *query; + char *fragment; +}; + +/** + * Parses a `*s` into `*url` as a molerat URL. + */ +int parse_url(struct url *url, char *s); + +/** + * Deallocates `struct url *url`. + */ +void free_url(struct url *url); + +/** + * Initializes a `struct url*` with sensible defaults. + */ +struct url *init_url(void); + +/** + * Get's the strlen of a `struct url*` + */ +int len_url(struct url *url); + +/* Error codes */ +enum UrlError { INVALID_CHARACTER = -1, MISSING_HOST = -2, MISSING_PORT = -3 }; +#endif diff --git a/include/util.h b/include/util.h new file mode 100755 index 0000000..9418824 --- /dev/null +++ b/include/util.h @@ -0,0 +1,11 @@ +#ifndef _UTIL_H_ +#define _UTIL_H_ +#include <stdnoreturn.h> + +/** + * Kill program by printing message `*s` and errno. + * + * Shuts down ncurses before killing program. + */ +noreturn void die(const char *s); +#endif |