This commit is contained in:
2025-03-21 10:59:34 +10:00
commit 32e2a09b8d
7 changed files with 1062 additions and 0 deletions

81
dbclient.h Normal file
View File

@@ -0,0 +1,81 @@
#ifndef DBCLIENT_H
#define DBCLIENT_H
#include <csse2310a4.h>
#include <ctype.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define GET_REQUEST "GET /public/%s HTTP/1.1\r\n\r\n"
#define PUT_REQUEST "PUT /public/%s HTTP/1.1\r\nContent-Length: %lu\r\n\r\n%s"
// Program exit codes
enum ExitCodes {
EXIT_OK = 0,
EXIT_ERR = 1,
EXIT_PORT_ERR = 2,
EXIT_GET_ERR = 3,
EXIT_PUT_ERR = 4
};
/* construct_header()
* ------------------
* Constructs the needed header to be sent
*
* key: the to check or update
* value: the value to assign to the key
*
* Returns: the header
*/
char *construct_header(char *key, char *value);
/* handle_connect()
* ----------------
* Handles the initial connection to the port given, updates the given
* file descriptors pointer to the created one
*
* port: the port number or service to connect to
* fd: the file descriptor that will be connected for the connection
*
* Returns: 1 if the address info couldnt be found, 2 if could not connect to
* port, 0 if successful
*/
int handle_connect(char *port, int *fd);
/* handle_response()
* -----------------
* Handles the response given from the connected server
*
* recv: the fd that will be used for reading
* valueSet: if the value argument was set
*
* Returns: 3 if GET failed, 4 if PUT failed, 0 if successful
*/
int handle_response(FILE *recv, int valueSet);
/* handle_connection()
* -------------------
* Handles the connection to the given port, sends a GET or PUT request
* depending on if the value argument is not NULL
*
* port: the port to connect to
* key: the key to retrieve or update
* value: the value to update the supplied key to
*/
int handle_connection(char *port, char *key, char *value);
/* is_valid_key()
* --------------
* Checks if the given key is valid according to the spec
*
* key: the key to check if valid
*
* Returns: 0 if not valid, 1 if valid
*/
int is_valid_key(char *key);
#endif