Implemented register, login, enter_library and get_books operations.
This commit is contained in:
parent
c3a131eebc
commit
5e65f6cfc9
|
@ -0,0 +1,32 @@
|
|||
CLIENT=client
|
||||
SOURCES=$(wildcard *.c)
|
||||
LIBRARY=none
|
||||
INCPATHS=include
|
||||
LIBPATHS=
|
||||
LDFLAGS=
|
||||
CCFLAGS=-c -Wall -g3
|
||||
CC=gcc
|
||||
|
||||
# Automatic generation of some important lists
|
||||
|
||||
# tell the makefile to generate object files for each source file
|
||||
OBJECTS=$(SOURCES:.c=.o)
|
||||
INCFLAGS=$(foreach TMP,$(INCPATHS),-I$(TMP))
|
||||
LIBFLAGS=$(foreach TMP,$(LIBPATHS),-L$(TMP))
|
||||
|
||||
# Set up the output file names for the different output types
|
||||
|
||||
all: $(SOURCES) $(CLIENT)
|
||||
|
||||
$(CLIENT): $(OBJECTS)
|
||||
$(CC) $(LIBFLAGS) $(OBJECTS) $(LDFLAGS) -o $@
|
||||
|
||||
.c.o:
|
||||
$(CC) $(INCFLAGS) $(CCFLAGS) -fPIC $< -o $@
|
||||
|
||||
distclean: clean
|
||||
rm -f $(CLIENT)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJECTS)
|
||||
|
Binary file not shown.
|
@ -0,0 +1,88 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <termios.h>
|
||||
|
||||
#include <authentication.h>
|
||||
#include <error.h>
|
||||
#include <memory.h>
|
||||
|
||||
auth_info_t *get_auth_info() {
|
||||
/*
|
||||
* Read the username.
|
||||
*
|
||||
*/
|
||||
printf("username: ");
|
||||
fflush(stdout);
|
||||
|
||||
char username[MAX_USER_SZ] = {0};
|
||||
fgets(username, MAX_USER_SZ - 1, stdin);
|
||||
username[strlen(username) - 1] = 0x00;
|
||||
|
||||
/*
|
||||
* Disable terminal echoing for reading password.
|
||||
*
|
||||
*/
|
||||
struct termios current, modified;
|
||||
|
||||
// get current attributes
|
||||
int tcgetattr_ret = tcgetattr(STDIN_FILENO, ¤t);
|
||||
NONVOID_ERROR_HANDLER(tcgetattr_ret == -1, "get_auth_info: tcgetattr", NULL);
|
||||
|
||||
modified = current;
|
||||
modified.c_lflag &= ~ECHO;
|
||||
|
||||
int tcsetattr_ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, &modified);
|
||||
NONVOID_ERROR_HANDLER(tcsetattr_ret == -1, "get_auth_info: tcsetattr", NULL);
|
||||
|
||||
/*
|
||||
* Read the password.
|
||||
*
|
||||
*/
|
||||
printf("password: ");
|
||||
fflush(stdout);
|
||||
|
||||
char password[MAX_USER_SZ] = {0};
|
||||
fgets(password, MAX_PASS_SZ - 1, stdin);
|
||||
password[strlen(password) - 1] = 0x00;
|
||||
|
||||
puts("");
|
||||
|
||||
/*
|
||||
* Restore original settings for echoing.
|
||||
*
|
||||
*/
|
||||
tcsetattr_ret = tcsetattr(STDIN_FILENO, TCSANOW, ¤t);
|
||||
NONVOID_ERROR_HANDLER(tcsetattr_ret == -1, "get_auth_info: tcsetattr", NULL);
|
||||
|
||||
/*
|
||||
* Declare the return variable.
|
||||
*
|
||||
*/
|
||||
auth_info_t *auth_info;
|
||||
CALLOC(auth_info, sizeof(auth_info_t));
|
||||
|
||||
CALLOC(auth_info->username, strlen(username) + 1);
|
||||
strcpy(auth_info->username, username);
|
||||
|
||||
CALLOC(auth_info->password, strlen(password) + 1);
|
||||
strcpy(auth_info->password, password);
|
||||
|
||||
return auth_info;
|
||||
|
||||
}
|
||||
|
||||
void delete_auth_info(auth_info_t *auth_info) {
|
||||
|
||||
FREE(auth_info->username);
|
||||
FREE(auth_info->password);
|
||||
|
||||
FREE(auth_info);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
#include "buffer.h"
|
||||
#include <memory.h>
|
||||
|
||||
buffer buffer_init(void)
|
||||
{
|
||||
buffer buffer;
|
||||
|
||||
buffer.data = NULL;
|
||||
buffer.size = 0;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void buffer_destroy(buffer *buffer)
|
||||
{
|
||||
if (buffer->data != NULL) {
|
||||
FREE(buffer->data);
|
||||
buffer->data = NULL;
|
||||
}
|
||||
|
||||
buffer->size = 0;
|
||||
}
|
||||
|
||||
int buffer_is_empty(buffer *buffer)
|
||||
{
|
||||
return buffer->data == NULL;
|
||||
}
|
||||
|
||||
void buffer_add(buffer *buffer, const char *data, size_t data_size)
|
||||
{
|
||||
if (buffer->data != NULL) {
|
||||
buffer->data = realloc(buffer->data, (buffer->size + data_size) * sizeof(char));
|
||||
} else {
|
||||
CALLOC(buffer->data, data_size);
|
||||
}
|
||||
|
||||
memcpy(buffer->data + buffer->size, data, data_size);
|
||||
|
||||
buffer->size += data_size;
|
||||
}
|
||||
|
||||
int buffer_find(buffer *buffer, const char *data, size_t data_size)
|
||||
{
|
||||
if (data_size > buffer->size)
|
||||
return -1;
|
||||
|
||||
size_t last_pos = buffer->size - data_size + 1;
|
||||
|
||||
for (size_t i = 0; i < last_pos; ++i) {
|
||||
size_t j;
|
||||
|
||||
for (j = 0; j < data_size; ++j) {
|
||||
if (buffer->data[i + j] != data[j]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (j == data_size)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int buffer_find_insensitive(buffer *buffer, const char *data, size_t data_size)
|
||||
{
|
||||
if (data_size > buffer->size)
|
||||
return -1;
|
||||
|
||||
size_t last_pos = buffer->size - data_size + 1;
|
||||
|
||||
for (size_t i = 0; i < last_pos; ++i) {
|
||||
size_t j;
|
||||
|
||||
for (j = 0; j < data_size; ++j) {
|
||||
if (tolower(buffer->data[i + j]) != tolower(data[j])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (j == data_size)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -1,3 +1,139 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <error.h>
|
||||
#include <connection.h>
|
||||
#include <server.h>
|
||||
#include <dns.h>
|
||||
#include <memory.h>
|
||||
#include <commands.h>
|
||||
|
||||
#define RESTORE_SOCKFD(assertion, sockfd, server_ip) \
|
||||
do { \
|
||||
if (assertion) { \
|
||||
puts("[DEBUG] The server closed the connection, opening a new socket"); \
|
||||
sockfd = open_connection(server_ip, PORT, AF_INET, SOCK_STREAM, 0); \
|
||||
printf("[DEBUG] Socket is now: %d\n", sockfd); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
||||
int main() {
|
||||
return 0;
|
||||
/*
|
||||
* Get the IP of the server.
|
||||
*
|
||||
*/
|
||||
char *server_ip = get_ip(SERVER);
|
||||
printf("[DEBUG] Server IPv4 is: %s\n", server_ip);
|
||||
|
||||
/*
|
||||
* Open a connection with the server.
|
||||
*
|
||||
*/
|
||||
int sockfd = open_connection(server_ip, PORT, AF_INET, SOCK_STREAM, 0);
|
||||
printf("[DEBUG] Connection established on socket: %d\n", sockfd);
|
||||
|
||||
/*
|
||||
* Buffer for reading the input from the user.
|
||||
*
|
||||
*/
|
||||
char input_buffer[MAX_COMMAND_SZ] = {0};
|
||||
|
||||
/*
|
||||
* Login @cookie is computed in #login and @jwt_token is computed
|
||||
* in #enter_libary.
|
||||
*
|
||||
*/
|
||||
char *cookie = NULL;
|
||||
char *jwt_token = NULL;
|
||||
|
||||
while (1) {
|
||||
/*
|
||||
* Display promt.
|
||||
*
|
||||
*/
|
||||
printf("$ ");
|
||||
fflush(stdout);
|
||||
|
||||
/*
|
||||
* Fill the buffer with 0's and read the input.
|
||||
*
|
||||
*/
|
||||
memset(input_buffer, 0x00, MAX_COMMAND_SZ);
|
||||
fgets(input_buffer, MAX_COMMAND_SZ - 1, stdin);
|
||||
|
||||
if (strncmp(input_buffer, REGISTER, sizeof(REGISTER) - 1) == 0) {
|
||||
|
||||
int register_ret = op_register(sockfd);
|
||||
|
||||
/*
|
||||
* If the connection closed during the register operation then a new
|
||||
* socket must be opened for the communication with the server.
|
||||
*
|
||||
*/
|
||||
RESTORE_SOCKFD(register_ret == OPERATION_CONNECTION_CLOSED,
|
||||
sockfd, server_ip);
|
||||
|
||||
} else if (strncmp(input_buffer, LOGIN, sizeof(LOGIN) - 1) == 0) {
|
||||
|
||||
/*
|
||||
* If a cookie was already stored in the @cookie then
|
||||
* delete it because a new login will generate a new cookie.
|
||||
*
|
||||
*/
|
||||
FREE(cookie);
|
||||
|
||||
int login_ret = login(sockfd, &cookie);
|
||||
RESTORE_SOCKFD(login_ret == OPERATION_CONNECTION_CLOSED,
|
||||
sockfd, server_ip);
|
||||
|
||||
printf("The cookie is: %s\n", cookie);
|
||||
|
||||
/*
|
||||
* If the login was successful then the client must delete the previous
|
||||
* @jwt_token.
|
||||
*
|
||||
*/
|
||||
if (login_ret == OPERATION_SUCCESSFUL) {
|
||||
FREE(jwt_token);
|
||||
}
|
||||
|
||||
} else if (strncmp(input_buffer, ENTER_LIBRARY, sizeof(ENTER_LIBRARY) - 1) == 0) {
|
||||
|
||||
/*
|
||||
* If a jwt_token was already stored in the @jwt_token then
|
||||
* delete it because a new enter_library will generate a new jwt_token.
|
||||
*
|
||||
*/
|
||||
FREE(jwt_token);
|
||||
|
||||
int enter_library_ret = enter_library(sockfd, cookie, &jwt_token);
|
||||
RESTORE_SOCKFD(enter_library_ret == OPERATION_CONNECTION_CLOSED,
|
||||
sockfd, server_ip);
|
||||
|
||||
printf("The token is: %s\n", jwt_token);
|
||||
|
||||
} else if (strncmp(input_buffer, GET_BOOKS, sizeof(GET_BOOKS) - 1) == 0) {
|
||||
|
||||
int get_books_ret = get_books(sockfd, jwt_token);
|
||||
RESTORE_SOCKFD(get_books_ret == OPERATION_CONNECTION_CLOSED,
|
||||
sockfd, server_ip);
|
||||
|
||||
} else if (strncmp(input_buffer, EXIT, sizeof(EXIT) - 1) == 0) {
|
||||
|
||||
puts("[DEBUG] Closing the connection with the server, goodbye.");
|
||||
close(sockfd);
|
||||
|
||||
FREE(cookie);
|
||||
FREE(jwt_token);
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,401 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <commands.h>
|
||||
#include <cJSON.h>
|
||||
#include <requests.h>
|
||||
#include <error.h>
|
||||
#include <connection.h>
|
||||
#include <authentication.h>
|
||||
#include <json_parser.h>
|
||||
#include <memory.h>
|
||||
#include <http.h>
|
||||
|
||||
#include <server.h>
|
||||
|
||||
int op_register(int sockfd) {
|
||||
/*
|
||||
* Display the prompt for username and password and get them.
|
||||
*
|
||||
*/
|
||||
auth_info_t *auth_info = get_auth_info();
|
||||
NONVOID_ERROR_HANDLER(auth_info == NULL, "[ERROR] Could not get auth_info",
|
||||
OPERATION_FAILED);
|
||||
|
||||
/*
|
||||
* Generate the json payload for the POST request.
|
||||
*
|
||||
*/
|
||||
cJSON *register_json = convert_auth_info_to_cJSON(auth_info, USERNAME, PASSWORD);
|
||||
|
||||
/*
|
||||
* Convert the JSON object to char array.
|
||||
*
|
||||
*/
|
||||
char *register_json_string = cJSON_Print(register_json);
|
||||
|
||||
/*
|
||||
* Generate the POST request. If the post request computation fails then
|
||||
* an all the memory allocated previously will be FREEd and the function
|
||||
* will return a OPERATION_FAILED error code.
|
||||
*
|
||||
*/
|
||||
char *post_request = compute_post_request(SERVER, REGISTER_URL,
|
||||
JSON_CONTENT_TYPE, ®ister_json_string, 1, NULL, 0);
|
||||
|
||||
if (post_request == NULL) {
|
||||
cJSON_Delete(register_json);
|
||||
delete_auth_info(auth_info);
|
||||
|
||||
FREE(register_json_string);
|
||||
|
||||
NONVOID_ERROR_HANDLER(post_request == NULL, "[ERROR] Could not build POST request",
|
||||
OPERATION_FAILED);
|
||||
}
|
||||
|
||||
/*
|
||||
* Send the POST request to the server and print the response.
|
||||
* If the post request computation fails then an all the memory allocated
|
||||
* previously will be FREEd and the function will return a OPERATION_FAILED error code.
|
||||
*
|
||||
*/
|
||||
int send_to_server_ret = send_to_server(sockfd, post_request);
|
||||
if (send_to_server_ret == SEND_TO_SERVER_FAILED) {
|
||||
cJSON_Delete(register_json);
|
||||
delete_auth_info(auth_info);
|
||||
|
||||
FREE(register_json_string);
|
||||
FREE(post_request);
|
||||
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Could not send request to server",
|
||||
OPERATION_FAILED);
|
||||
}
|
||||
|
||||
/*
|
||||
* Receive a response. If #receive_from_server returns NULL it means an error occured
|
||||
* and the register operation was not successful.
|
||||
*
|
||||
*/
|
||||
char *response = receive_from_server(sockfd);
|
||||
if (response == NULL || contains_status_code(response, BAD_REQUEST)) {
|
||||
cJSON_Delete(register_json);
|
||||
delete_auth_info(auth_info);
|
||||
|
||||
FREE(register_json_string);
|
||||
FREE(post_request);
|
||||
|
||||
/*
|
||||
* Decide what status code to return and what error code to print.
|
||||
*
|
||||
*/
|
||||
if (response == NULL) {
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Could not receive a response from server",
|
||||
OPERATION_CONNECTION_CLOSED);
|
||||
} else if (contains_status_code(response, BAD_REQUEST)) {
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Bad request",
|
||||
OPERATION_FAILED);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
puts(response);
|
||||
|
||||
/*
|
||||
* FREE the memory.
|
||||
*
|
||||
*/
|
||||
delete_auth_info(auth_info);
|
||||
|
||||
cJSON_Delete(register_json);
|
||||
FREE(register_json_string);
|
||||
|
||||
FREE(post_request);
|
||||
FREE(response);
|
||||
|
||||
return OPERATION_SUCCESSFUL;
|
||||
}
|
||||
|
||||
int login(int sockfd, char **cookie) {
|
||||
NONVOID_ERROR_HANDLER(cookie == NULL, "[ERROR] Cookie argument must be"
|
||||
" a valid address", OPERATION_FAILED);
|
||||
|
||||
/*
|
||||
* Display the prompt for username and password and get them.
|
||||
*
|
||||
*/
|
||||
auth_info_t *auth_info = get_auth_info();
|
||||
NONVOID_ERROR_HANDLER(auth_info == NULL, "[ERROR] Could not get auth_info",
|
||||
OPERATION_FAILED);
|
||||
|
||||
/*
|
||||
* Generate the json payload for the POST request.
|
||||
*
|
||||
*/
|
||||
cJSON *register_json = convert_auth_info_to_cJSON(auth_info, USERNAME, PASSWORD);
|
||||
|
||||
/*
|
||||
* Convert the JSON object to char array.
|
||||
*
|
||||
*/
|
||||
char *register_json_string = cJSON_Print(register_json);
|
||||
|
||||
/*
|
||||
* Generate the POST request. If the post request computation fails then
|
||||
* an all the memory allocated previously will be FREEd and the function
|
||||
* will return a OPERATION_FAILED error code.
|
||||
*
|
||||
*/
|
||||
char *post_request = compute_post_request(SERVER, LOGIN_URL,
|
||||
JSON_CONTENT_TYPE, ®ister_json_string, 1, NULL, 0);
|
||||
|
||||
if (post_request == NULL) {
|
||||
cJSON_Delete(register_json);
|
||||
delete_auth_info(auth_info);
|
||||
|
||||
FREE(register_json_string);
|
||||
|
||||
NONVOID_ERROR_HANDLER(post_request == NULL, "[ERROR] Could not build POST request",
|
||||
OPERATION_FAILED);
|
||||
}
|
||||
|
||||
/*
|
||||
* Send the POST request to the server and print the response.
|
||||
* If the post request computation fails then an all the memory allocated
|
||||
* previously will be FREEd and the function will return a OPERATION_FAILED error code.
|
||||
*
|
||||
*/
|
||||
int send_to_server_ret = send_to_server(sockfd, post_request);
|
||||
if (send_to_server_ret == SEND_TO_SERVER_FAILED) {
|
||||
cJSON_Delete(register_json);
|
||||
delete_auth_info(auth_info);
|
||||
|
||||
FREE(register_json_string);
|
||||
FREE(post_request);
|
||||
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Could not send request to server",
|
||||
OPERATION_FAILED);
|
||||
}
|
||||
|
||||
/*
|
||||
* Receive a response. If #receive_from_server returns NULL it means an error occured
|
||||
* and the login operation was not successful.
|
||||
*
|
||||
* Also if the response contains a BAD_REQUEST status code then the response is
|
||||
* not valid.
|
||||
*
|
||||
*/
|
||||
char *response = receive_from_server(sockfd);
|
||||
if (response == NULL || contains_status_code(response, BAD_REQUEST)) {
|
||||
cJSON_Delete(register_json);
|
||||
delete_auth_info(auth_info);
|
||||
|
||||
FREE(register_json_string);
|
||||
FREE(post_request);
|
||||
|
||||
/*
|
||||
* Decide what status code to return and what error code to print.
|
||||
*
|
||||
*/
|
||||
if (response == NULL) {
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Could not receive a response from server",
|
||||
OPERATION_CONNECTION_CLOSED);
|
||||
} else if (contains_status_code(response, BAD_REQUEST)) {
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Bad request",
|
||||
OPERATION_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Extract the cookie from the response and put its value in the @cookie argument.
|
||||
*
|
||||
*/
|
||||
char *cookie_line = basic_extract_cookie_response(response);
|
||||
char *cookie_line_end = basic_extract_crlf_response(cookie_line);
|
||||
|
||||
cookie_line[cookie_line_end - cookie_line] = 0x00;
|
||||
|
||||
CALLOC(*cookie, strlen(cookie_line) + 1);
|
||||
strcpy(*cookie, cookie_line);
|
||||
|
||||
/*
|
||||
* FREE the memory.
|
||||
*
|
||||
*/
|
||||
delete_auth_info(auth_info);
|
||||
|
||||
cJSON_Delete(register_json);
|
||||
FREE(register_json_string);
|
||||
|
||||
FREE(post_request);
|
||||
FREE(response);
|
||||
|
||||
return OPERATION_SUCCESSFUL;
|
||||
}
|
||||
|
||||
int enter_library(int sockfd, char *cookie, char **jwt_token) {
|
||||
|
||||
/*
|
||||
* Put guards on @cookie and @jwt_token before beginning this operation.
|
||||
*
|
||||
*/
|
||||
NONVOID_ERROR_HANDLER(cookie == NULL, "[ERROR] cookie must be a"
|
||||
" non-null address", OPERATION_FAILED);
|
||||
|
||||
NONVOID_ERROR_HANDLER(jwt_token == NULL, "[ERROR] jwt_token must be a"
|
||||
" non-null address", OPERATION_FAILED);
|
||||
|
||||
/*
|
||||
* Compute a get request using the @cookie.
|
||||
*
|
||||
*/
|
||||
char *get_request = compute_get_request(SERVER, ENTER_LIBRARY_URL, NULL, &cookie, 1);
|
||||
NONVOID_ERROR_HANDLER(get_request == NULL, "[ERROR] Could not build GET request",
|
||||
OPERATION_FAILED);
|
||||
|
||||
/*
|
||||
* Send the POST request to the server and print the response.
|
||||
* If the post request computation fails then an all the memory allocated
|
||||
* previously will be FREEd and the function will return a OPERATION_FAILED error code.
|
||||
*
|
||||
*/
|
||||
int send_to_server_ret = send_to_server(sockfd, get_request);
|
||||
if (send_to_server_ret == SEND_TO_SERVER_FAILED) {
|
||||
FREE(get_request);
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Could not send request to server",
|
||||
OPERATION_FAILED);
|
||||
}
|
||||
|
||||
/*
|
||||
* Receive a response. If #receive_from_server returns NULL it means an error occured
|
||||
* and the login operation was not successful.
|
||||
*
|
||||
* Also if the response contains a BAD_REQUEST status code then the response is
|
||||
* not valid.
|
||||
*
|
||||
*/
|
||||
char *response = receive_from_server(sockfd);
|
||||
if (response == NULL || contains_status_code(response, BAD_REQUEST)) {
|
||||
FREE(get_request);
|
||||
|
||||
/*
|
||||
* Decide what status code to return and what error code to print.
|
||||
*
|
||||
*/
|
||||
if (response == NULL) {
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Could not receive a response from server",
|
||||
OPERATION_CONNECTION_CLOSED);
|
||||
} else if (contains_status_code(response, BAD_REQUEST)) {
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Bad request",
|
||||
OPERATION_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
char *json_body = basic_extract_json_response(response);
|
||||
|
||||
/*
|
||||
* Parse the JWT token received in the HTTP response.
|
||||
*
|
||||
*/
|
||||
cJSON *json_received_object = cJSON_Parse(json_body);
|
||||
if (json_received_object == NULL) {
|
||||
FREE(get_request);
|
||||
FREE(response);
|
||||
|
||||
NONVOID_ERROR_HANDLER(true, cJSON_GetErrorPtr(), OPERATION_FAILED);
|
||||
}
|
||||
|
||||
cJSON *cJSON_token = cJSON_GetObjectItemCaseSensitive(json_received_object,
|
||||
JWT_TOKEN);
|
||||
|
||||
/*
|
||||
* Put the token in @jwt_token.
|
||||
*
|
||||
*/
|
||||
CALLOC(*jwt_token, strlen(cJSON_token->valuestring) + 1);
|
||||
strcpy(*jwt_token, cJSON_token->valuestring);
|
||||
|
||||
/*
|
||||
* FREE the memory.
|
||||
*
|
||||
*/
|
||||
cJSON_Delete(json_received_object);
|
||||
|
||||
FREE(get_request);
|
||||
FREE(response);
|
||||
|
||||
return OPERATION_SUCCESSFUL;
|
||||
}
|
||||
|
||||
int get_books(int sockfd, char *jwt_token) {
|
||||
/*
|
||||
* Guard for input.
|
||||
*
|
||||
*/
|
||||
NONVOID_ERROR_HANDLER(jwt_token == NULL, "[ERROR] jwt_token must be a"
|
||||
" non-null address", OPERATION_FAILED);
|
||||
|
||||
/*
|
||||
* Add the jwt token to the GET request.
|
||||
*
|
||||
*/
|
||||
char *get_request = compute_get_request(SERVER, GET_BOOKS_URL, NULL, NULL, 0);
|
||||
get_request = add_access_token(get_request, jwt_token);
|
||||
|
||||
/*
|
||||
* Send the POST request to the server and print the response.
|
||||
* If the post request computation fails then an all the memory allocated
|
||||
* previously will be FREEd and the function will return a OPERATION_FAILED error code.
|
||||
*
|
||||
*/
|
||||
int send_to_server_ret = send_to_server(sockfd, get_request);
|
||||
if (send_to_server_ret == SEND_TO_SERVER_FAILED) {
|
||||
FREE(get_request);
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Could not send request to server",
|
||||
OPERATION_FAILED);
|
||||
}
|
||||
|
||||
/*
|
||||
* Receive a response. If #receive_from_server returns NULL it means an error occured
|
||||
* and the login operation was not successful.
|
||||
*
|
||||
* Also if the response contains a BAD_REQUEST status code then the response is
|
||||
* not valid.
|
||||
*
|
||||
*/
|
||||
char *response = receive_from_server(sockfd);
|
||||
if (response == NULL || contains_status_code(response, BAD_REQUEST)
|
||||
|| contains_status_code(response, FORBIDDEN)) {
|
||||
FREE(get_request);
|
||||
|
||||
/*
|
||||
* Decide what status code to return and what error code to print.
|
||||
*
|
||||
*/
|
||||
if (response == NULL) {
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Could not receive a response from server",
|
||||
OPERATION_CONNECTION_CLOSED);
|
||||
} else if (contains_status_code(response, BAD_REQUEST)) {
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Bad request",
|
||||
OPERATION_FAILED);
|
||||
} else if (contains_status_code(response, FORBIDDEN)) {
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Forbidden",
|
||||
OPERATION_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
puts(get_request);
|
||||
puts(response);
|
||||
|
||||
return OPERATION_SUCCESSFUL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,150 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <connection.h>
|
||||
#include <error.h>
|
||||
#include <buffer.h>
|
||||
|
||||
void compute_message(char *message, const char *line) {
|
||||
|
||||
strcat(message, line);
|
||||
strcat(message, "\r\n");
|
||||
|
||||
}
|
||||
|
||||
int open_connection(char *host_ip, int portno, int ip_type, int socket_type, int flag) {
|
||||
/*
|
||||
* Open the socket.
|
||||
*
|
||||
*/
|
||||
int sockfd = socket(ip_type, socket_type, flag);
|
||||
NONVOID_ERROR_HANDLER(sockfd < 0, "[ERROR] open_connection: socket", OPEN_CONNECTION_FAILED);
|
||||
|
||||
/*
|
||||
* Fill the information about the server in a struct sockaddr_in.
|
||||
*
|
||||
*/
|
||||
struct sockaddr_in serv_addr;
|
||||
memset(&serv_addr, 0x00, sizeof(serv_addr));
|
||||
|
||||
serv_addr.sin_family = ip_type;
|
||||
serv_addr.sin_port = htons(portno);
|
||||
inet_aton(host_ip, &serv_addr.sin_addr);
|
||||
|
||||
/*
|
||||
* Connect to server.
|
||||
*
|
||||
*/
|
||||
int connect_ret = connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
|
||||
NONVOID_ERROR_HANDLER(connect_ret < 0, "[ERROR] open_connection: socket", OPEN_CONNECTION_FAILED);
|
||||
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
void close_connection(int sockfd) {
|
||||
|
||||
close(sockfd);
|
||||
|
||||
}
|
||||
|
||||
int send_to_server(int sockfd, char *message) {
|
||||
int bytes, sent = 0;
|
||||
int total = strlen(message);
|
||||
|
||||
do {
|
||||
bytes = write(sockfd, message + sent, total - sent);
|
||||
NONVOID_ERROR_HANDLER(bytes < 0, "[ERROR] send_to_server: write ", SEND_TO_SERVER_FAILED);
|
||||
|
||||
if (bytes == 0) {
|
||||
puts("[DEBUG] Message sent successfully!");
|
||||
break;
|
||||
}
|
||||
|
||||
sent += bytes;
|
||||
} while (sent < total);
|
||||
|
||||
return SEND_TO_SERVER_SUCCESSFUL;
|
||||
}
|
||||
|
||||
char *receive_from_server(int sockfd) {
|
||||
|
||||
char response[BUFLEN];
|
||||
buffer buffer = buffer_init();
|
||||
int header_end = 0;
|
||||
int content_length = 0;
|
||||
|
||||
/*
|
||||
* Read the header.
|
||||
*
|
||||
*/
|
||||
do {
|
||||
int bytes = read(sockfd, response, BUFLEN);
|
||||
|
||||
if (bytes < 0){
|
||||
buffer_destroy(&buffer);
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] receive_from_server: read ", NULL);
|
||||
}
|
||||
|
||||
if (bytes == 0) {
|
||||
buffer_destroy(&buffer);
|
||||
NONVOID_ERROR_HANDLER(true, "[DEBUG] The server closed the connection.", NULL);
|
||||
}
|
||||
|
||||
buffer_add(&buffer, response, (size_t) bytes);
|
||||
|
||||
header_end = buffer_find(&buffer, HEADER_TERMINATOR, HEADER_TERMINATOR_SIZE);
|
||||
|
||||
if (header_end >= 0) {
|
||||
header_end += HEADER_TERMINATOR_SIZE;
|
||||
|
||||
int content_length_start = buffer_find_insensitive(&buffer, CONTENT_LENGTH, CONTENT_LENGTH_SIZE);
|
||||
|
||||
if (content_length_start < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
content_length_start += CONTENT_LENGTH_SIZE;
|
||||
content_length = strtol(buffer.data + content_length_start, NULL, 10);
|
||||
break;
|
||||
}
|
||||
} while (1);
|
||||
|
||||
/*
|
||||
* Read the content.
|
||||
*
|
||||
*/
|
||||
size_t total = content_length + (size_t) header_end;
|
||||
|
||||
while (buffer.size < total) {
|
||||
int bytes = read(sockfd, response, BUFLEN);
|
||||
|
||||
if (bytes < 0){
|
||||
buffer_destroy(&buffer);
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] receive_from_server: read ", NULL);
|
||||
}
|
||||
|
||||
if (bytes == 0) {
|
||||
buffer_destroy(&buffer);
|
||||
NONVOID_ERROR_HANDLER(true, "[DEBUG] The server closed the connection.", NULL);
|
||||
}
|
||||
|
||||
buffer_add(&buffer, response, (size_t) bytes);
|
||||
}
|
||||
|
||||
buffer_add(&buffer, "", 1);
|
||||
return buffer.data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include <dns.h>
|
||||
#include <error.h>
|
||||
|
||||
char *get_ip(char *name) {
|
||||
|
||||
/*
|
||||
* Set the hints for getaddrinfo(3).
|
||||
*
|
||||
*/
|
||||
struct addrinfo hints = {
|
||||
.ai_flags = 0,
|
||||
.ai_family = AF_INET,
|
||||
.ai_socktype = SOCK_DGRAM,
|
||||
.ai_protocol = 0,
|
||||
|
||||
.ai_addrlen = 0,
|
||||
.ai_addr = NULL,
|
||||
.ai_canonname = NULL,
|
||||
.ai_next = NULL,
|
||||
};
|
||||
|
||||
/*
|
||||
* Call getaddrinfo(3) and store the info in @result.
|
||||
*
|
||||
*/
|
||||
struct addrinfo *result;
|
||||
int getaddrinfo_ret = getaddrinfo(name, NULL, &hints, &result);
|
||||
NONVOID_ERROR_HANDLER(getaddrinfo_ret < 0, "[ERROR] get_ip: getaddrinfo ",
|
||||
NULL);
|
||||
|
||||
/*
|
||||
* Get the IPv4 address from @result.
|
||||
*
|
||||
*/
|
||||
struct sockaddr_in *ipv4_addr = (struct sockaddr_in *) result->ai_addr;
|
||||
uint32_t ip_addr = ipv4_addr->sin_addr.s_addr;
|
||||
|
||||
freeaddrinfo(result);
|
||||
|
||||
/*
|
||||
* Return the IPv4 address converted to char array.
|
||||
*
|
||||
*/
|
||||
return inet_ntoa((struct in_addr) { .s_addr = ip_addr});
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,26 @@
|
|||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <http.h>
|
||||
|
||||
bool contains_status_code(char *response, char *status_code) {
|
||||
|
||||
if (strstr(response, status_code) != NULL) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
char *basic_extract_json_response(char *response) {
|
||||
return strstr(response, "{\"");
|
||||
}
|
||||
|
||||
char *basic_extract_cookie_response(char *response) {
|
||||
return strstr(response, SET_COOKIE) + SET_COOKIE_SIZE;
|
||||
}
|
||||
|
||||
char *basic_extract_crlf_response(char *response) {
|
||||
return strstr(response, CRLF);
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef AUTHENTICATION_H_
|
||||
#define AUTHENTICATION_H_
|
||||
|
||||
#define MAX_USER_SZ 100
|
||||
#define MAX_PASS_SZ 100
|
||||
|
||||
/*
|
||||
* Number of components implied in a register operation:
|
||||
* username
|
||||
* psasword
|
||||
*
|
||||
*/
|
||||
#define REGISTER_ELEM_NO 2
|
||||
#define LOGIN_ELEM_NO 2
|
||||
|
||||
typedef struct auth_info {
|
||||
char *username;
|
||||
char *password;
|
||||
} auth_info_t;
|
||||
|
||||
/*
|
||||
* Display a prompt with username and password, let the user enter the
|
||||
* info and return it in a auth_info_t data type.
|
||||
*
|
||||
*/
|
||||
auth_info_t *get_auth_info();
|
||||
|
||||
/*
|
||||
* Frees the memory for a auth_info_t object.
|
||||
*
|
||||
*/
|
||||
void delete_auth_info(auth_info_t *auth_info);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,28 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
typedef struct {
|
||||
char *data;
|
||||
size_t size;
|
||||
} buffer;
|
||||
|
||||
// initializes a buffer
|
||||
buffer buffer_init(void);
|
||||
|
||||
// destroys a buffer
|
||||
void buffer_destroy(buffer *buffer);
|
||||
|
||||
// adds data of size data_size to a buffer
|
||||
void buffer_add(buffer *buffer, const char *data, size_t data_size);
|
||||
|
||||
// checks if a buffer is empty
|
||||
int buffer_is_empty(buffer *buffer);
|
||||
|
||||
// finds data of size data_size in a buffer and returns its position
|
||||
int buffer_find(buffer *buffer, const char *data, size_t data_size);
|
||||
|
||||
// finds data of size data_size in a buffer in a
|
||||
// case-insensitive fashion and returns its position
|
||||
int buffer_find_insensitive(buffer *buffer, const char *data, size_t data_size);
|
|
@ -0,0 +1,293 @@
|
|||
/*
|
||||
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef cJSON__h
|
||||
#define cJSON__h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
|
||||
#define __WINDOWS__
|
||||
#endif
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
|
||||
/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options:
|
||||
|
||||
CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
|
||||
CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
|
||||
CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
|
||||
|
||||
For *nix builds that support visibility attribute, you can define similar behavior by
|
||||
|
||||
setting default visibility to hidden by adding
|
||||
-fvisibility=hidden (for gcc)
|
||||
or
|
||||
-xldscope=hidden (for sun cc)
|
||||
to CFLAGS
|
||||
|
||||
then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
|
||||
|
||||
*/
|
||||
|
||||
#define CJSON_CDECL __cdecl
|
||||
#define CJSON_STDCALL __stdcall
|
||||
|
||||
/* export symbols by default, this is necessary for copy pasting the C and header file */
|
||||
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_EXPORT_SYMBOLS
|
||||
#endif
|
||||
|
||||
#if defined(CJSON_HIDE_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) type CJSON_STDCALL
|
||||
#elif defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
|
||||
#elif defined(CJSON_IMPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
|
||||
#endif
|
||||
#else /* !__WINDOWS__ */
|
||||
#define CJSON_CDECL
|
||||
#define CJSON_STDCALL
|
||||
|
||||
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
|
||||
#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
|
||||
#else
|
||||
#define CJSON_PUBLIC(type) type
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* project version */
|
||||
#define CJSON_VERSION_MAJOR 1
|
||||
#define CJSON_VERSION_MINOR 7
|
||||
#define CJSON_VERSION_PATCH 13
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* cJSON Types: */
|
||||
#define cJSON_Invalid (0)
|
||||
#define cJSON_False (1 << 0)
|
||||
#define cJSON_True (1 << 1)
|
||||
#define cJSON_NULL (1 << 2)
|
||||
#define cJSON_Number (1 << 3)
|
||||
#define cJSON_String (1 << 4)
|
||||
#define cJSON_Array (1 << 5)
|
||||
#define cJSON_Object (1 << 6)
|
||||
#define cJSON_Raw (1 << 7) /* raw json */
|
||||
|
||||
#define cJSON_IsReference 256
|
||||
#define cJSON_StringIsConst 512
|
||||
|
||||
/* The cJSON structure: */
|
||||
typedef struct cJSON
|
||||
{
|
||||
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
|
||||
struct cJSON *next;
|
||||
struct cJSON *prev;
|
||||
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
|
||||
struct cJSON *child;
|
||||
|
||||
/* The type of the item, as above. */
|
||||
int type;
|
||||
|
||||
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
|
||||
char *valuestring;
|
||||
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
|
||||
int valueint;
|
||||
/* The item's number, if type==cJSON_Number */
|
||||
double valuedouble;
|
||||
|
||||
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
|
||||
char *string;
|
||||
} cJSON;
|
||||
|
||||
typedef struct cJSON_Hooks
|
||||
{
|
||||
/* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
|
||||
void *(CJSON_CDECL *malloc_fn)(size_t sz);
|
||||
void (CJSON_CDECL *free_fn)(void *ptr);
|
||||
} cJSON_Hooks;
|
||||
|
||||
typedef int cJSON_bool;
|
||||
|
||||
/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
|
||||
* This is to prevent stack overflows. */
|
||||
#ifndef CJSON_NESTING_LIMIT
|
||||
#define CJSON_NESTING_LIMIT 1000
|
||||
#endif
|
||||
|
||||
/* returns the version of cJSON as a string */
|
||||
CJSON_PUBLIC(const char*) cJSON_Version(void);
|
||||
|
||||
/* Supply malloc, realloc and free functions to cJSON */
|
||||
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
|
||||
|
||||
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
|
||||
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length);
|
||||
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
|
||||
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated);
|
||||
|
||||
/* Render a cJSON entity to text for transfer/storage. */
|
||||
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
|
||||
/* Render a cJSON entity to text for transfer/storage without any formatting. */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
|
||||
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
|
||||
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
|
||||
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
|
||||
/* Delete a cJSON entity and all subentities. */
|
||||
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);
|
||||
|
||||
/* Returns the number of items in an array (or object). */
|
||||
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
|
||||
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
|
||||
/* Get item "string" from object. Case insensitive. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
|
||||
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
|
||||
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
|
||||
|
||||
/* Check item type and return its value */
|
||||
CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);
|
||||
CJSON_PUBLIC(double) cJSON_GetNumberValue(cJSON *item);
|
||||
|
||||
/* These functions check the type of an item */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
|
||||
|
||||
/* These calls create a cJSON item of the appropriate type. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
|
||||
/* raw json */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
|
||||
|
||||
/* Create a string where valuestring references a string so
|
||||
* it will not be freed by cJSON_Delete */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
|
||||
/* Create an object/array that only references it's elements so
|
||||
* they will not be freed by cJSON_Delete */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
|
||||
|
||||
/* These utilities create an Array of count items.
|
||||
* The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count);
|
||||
|
||||
/* Append item to the specified array/object. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
|
||||
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
|
||||
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
|
||||
* writing to `item->string` */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
|
||||
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
|
||||
|
||||
/* Remove/Detach items from Arrays/Objects. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
|
||||
|
||||
/* Update array items. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
|
||||
|
||||
/* Duplicate a cJSON item */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
|
||||
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
|
||||
* need to be released. With recurse!=0, it will duplicate any children connected to the item.
|
||||
* The item->next and ->prev pointers are always zero on return from Duplicate. */
|
||||
/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
|
||||
* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
|
||||
|
||||
/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings.
|
||||
* The input pointer json cannot point to a read-only address area, such as a string constant,
|
||||
* but should point to a readable and writable adress area. */
|
||||
CJSON_PUBLIC(void) cJSON_Minify(char *json);
|
||||
|
||||
/* Helper functions for creating and adding items to an object at the same time.
|
||||
* They return the added item or NULL on failure. */
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
|
||||
|
||||
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
|
||||
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
|
||||
/* helper for the cJSON_SetNumberValue macro */
|
||||
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
|
||||
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
|
||||
/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */
|
||||
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
|
||||
|
||||
/* Macro for iterating over an array or object */
|
||||
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
|
||||
|
||||
/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
|
||||
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
|
||||
CJSON_PUBLIC(void) cJSON_free(void *object);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,75 @@
|
|||
#ifndef COMMANDS_H_
|
||||
#define COMMANDS_H_
|
||||
|
||||
/*
|
||||
* Maximum sizes of different inputs.
|
||||
*
|
||||
*/
|
||||
|
||||
#define MAX_COMMAND_SZ 20
|
||||
|
||||
/*
|
||||
* Format of commands given by the user.
|
||||
*
|
||||
*/
|
||||
|
||||
#define REGISTER "register"
|
||||
#define LOGIN "login"
|
||||
#define ENTER_LIBRARY "enter_library"
|
||||
#define GET_BOOKS "get_books"
|
||||
#define EXIT "exit"
|
||||
|
||||
|
||||
#define USERNAME "username"
|
||||
#define PASSWORD "password"
|
||||
|
||||
/*
|
||||
* Status codes for all commands listed below.
|
||||
*
|
||||
*/
|
||||
|
||||
#define OPERATION_SUCCESSFUL 0
|
||||
#define OPERATION_FAILED 1
|
||||
#define OPERATION_CONNECTION_CLOSED 2
|
||||
|
||||
/*
|
||||
* Registers an account to the server. The connection
|
||||
* with the server must be established on @sockfd.
|
||||
*
|
||||
* If the server closed the connection with the client then
|
||||
* OPERATION_CONNECTION_CLOSED error code will be returned. If any other
|
||||
* error occured then OPERATION_FAILED will be returned.
|
||||
*
|
||||
* On success, OPERATION_SUCCESSFUL is returned.
|
||||
*
|
||||
*/
|
||||
|
||||
int op_register(int sockfd);
|
||||
|
||||
/*
|
||||
* Logins to the server and returns a session cookie.
|
||||
*
|
||||
* If this function fails then it will return NULL.
|
||||
*
|
||||
*/
|
||||
|
||||
int login(int sockfd, char **cookie);
|
||||
|
||||
/*
|
||||
* Using a @cookie, send a GET request to the library access URL and return
|
||||
* a JWT Token in @jwt_token.
|
||||
*
|
||||
*/
|
||||
int enter_library(int sockfd, char *cookie, char **jwt_token);
|
||||
|
||||
/*
|
||||
* Using a @jwt_token, send a GET request to the library URL and print the
|
||||
* received list of JSON object.
|
||||
*
|
||||
*/
|
||||
int get_books(int sockfd, char *jwt_token);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
#ifndef CONNECTION_H_
|
||||
#define CONNECTION_H_
|
||||
|
||||
#define BUFLEN 4096
|
||||
#define LINELEN 1000
|
||||
|
||||
#define HEADER_TERMINATOR "\r\n\r\n"
|
||||
#define HEADER_TERMINATOR_SIZE (sizeof(HEADER_TERMINATOR) - 1)
|
||||
|
||||
#define CONTENT_LENGTH "Content-Length: "
|
||||
#define CONTENT_LENGTH_SIZE (sizeof(CONTENT_LENGTH) - 1)
|
||||
|
||||
/*
|
||||
* Adds a line to a string message.
|
||||
*
|
||||
*/
|
||||
void compute_message(char *message, const char *line);
|
||||
|
||||
/*
|
||||
* Opens a connection with the server at @host_ip on port @portno.
|
||||
*
|
||||
* If the connection is successful then it returns a new socket file descriptor,
|
||||
* else it returns OPEN_CONNECTION_FAILED.
|
||||
*
|
||||
*/
|
||||
#define OPEN_CONNECTION_FAILED -1
|
||||
|
||||
int open_connection(char *host_ip, int portno, int ip_type, int socket_type, int flag);
|
||||
|
||||
/*
|
||||
* Closes the connection with the server on socket sockfd.
|
||||
*
|
||||
*/
|
||||
void close_connection(int sockfd);
|
||||
|
||||
/*
|
||||
* Sends a message to a server on socket sockfd.
|
||||
*
|
||||
* Returns SEND_TO_SERVER_SUCCESSFUL on success and
|
||||
* SEND_TO_SERVER_FAILED on failure.
|
||||
*
|
||||
*/
|
||||
#define SEND_TO_SERVER_SUCCESSFUL 0
|
||||
#define SEND_TO_SERVER_FAILED 1
|
||||
|
||||
int send_to_server(int sockfd, char *message);
|
||||
|
||||
/*
|
||||
* Receives and returns a message from server.
|
||||
*
|
||||
*/
|
||||
char *receive_from_server(int sockfd);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,18 @@
|
|||
#ifndef DNS_H_
|
||||
#define DNS_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
/*
|
||||
* Get the IPv4 address in integer format.
|
||||
* Returns NULL on error.
|
||||
*
|
||||
*/
|
||||
|
||||
char *get_ip(char *name);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef ERROR_H_
|
||||
#define ERROR_H_
|
||||
|
||||
#define VOID_ERROR_HANDLER(assertion, call_description) \
|
||||
do { \
|
||||
if (assertion) { \
|
||||
fprintf(stderr, "(%s, %d): ", \
|
||||
__FILE__, __LINE__); \
|
||||
perror(call_description); \
|
||||
return; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define NONVOID_ERROR_HANDLER(assertion, call_description, return_value) \
|
||||
do { \
|
||||
if (assertion) { \
|
||||
fprintf(stderr, "(%s, %d): ", \
|
||||
__FILE__, __LINE__); \
|
||||
perror(call_description); \
|
||||
return return_value; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#endif
|
|
@ -0,0 +1,31 @@
|
|||
#ifndef _HELPERS_
|
||||
#define _HELPERS_
|
||||
|
||||
#define BUFLEN 4096
|
||||
#define LINELEN 1000
|
||||
|
||||
// shows the current error
|
||||
void error(const char *msg);
|
||||
|
||||
// adds a line to a string message
|
||||
void compute_message(char *message, const char *line);
|
||||
|
||||
// opens a connection with server host_ip on port portno, returns a socket
|
||||
int open_connection(char *host_ip, int portno, int ip_type, int socket_type, int flag);
|
||||
|
||||
// closes a server connection on socket sockfd
|
||||
void close_connection(int sockfd);
|
||||
|
||||
// send a message to a server
|
||||
#define SEND_TO_SERVER_SUCCESSFUL 0
|
||||
#define SEND_TO_SERVER_FAILED 1
|
||||
|
||||
int send_to_server(int sockfd, char *message);
|
||||
|
||||
// receives and returns the message from a server
|
||||
char *receive_from_server(int sockfd);
|
||||
|
||||
// extracts and returns a JSON from a server response
|
||||
char *basic_extract_json_response(char *str);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,38 @@
|
|||
#ifndef HTTP_H_
|
||||
#define HTTP_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
/*
|
||||
* Name of a JWT Token in then JSON object received in #enter_library.
|
||||
*
|
||||
*/
|
||||
#define JWT_TOKEN "token"
|
||||
|
||||
#define BAD_REQUEST "400"
|
||||
#define FORBIDDEN "403"
|
||||
|
||||
/*
|
||||
* Function that checks if HTTP @response contains @status_code.
|
||||
*
|
||||
*/
|
||||
bool contains_status_code(char *response, char *status_code);
|
||||
|
||||
/*
|
||||
* Functions that extract data from a HTTP response.
|
||||
*
|
||||
*/
|
||||
|
||||
#define CRLF "\r\n"
|
||||
#define CRLF_SIZE (sizeof(CRLF) - 1)
|
||||
|
||||
#define SET_COOKIE "Set-Cookie: "
|
||||
#define SET_COOKIE_SIZE (sizeof(SET_COOKIE) - 1)
|
||||
|
||||
char *basic_extract_json_response(char *response);
|
||||
|
||||
char *basic_extract_cookie_response(char *respose);
|
||||
|
||||
char *basic_extract_crlf_response(char *response);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef JSON_PARSER_H_
|
||||
#define JSON_PARSER_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <authentication.h>
|
||||
|
||||
#include <cJSON.h>
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *value;
|
||||
} rigid_json_t;
|
||||
|
||||
cJSON *array_to_cJSON_Object(rigid_json_t *array, size_t array_sz);
|
||||
cJSON *convert_auth_info_to_cJSON(auth_info_t *auth_info,
|
||||
char *username, char *password);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* This header file deals with memory allocation.
|
||||
*
|
||||
*/
|
||||
|
||||
#define ERROR_ALLOC_MESSAGE "Insufficient memory"
|
||||
|
||||
#define MALLOC(p, n) \
|
||||
do \
|
||||
{ \
|
||||
if ( !( (p) = malloc(sizeof(*(p)) * (n)) ) ) \
|
||||
{ \
|
||||
fprintf(stderr, ERROR_ALLOC_MESSAGE); \
|
||||
exit(EXIT_FAILURE); \
|
||||
} \
|
||||
} \
|
||||
while(0)
|
||||
|
||||
#define CALLOC(p, n) \
|
||||
do \
|
||||
{ \
|
||||
if ( !( (p) = calloc((n), sizeof(*(p))) ) ) \
|
||||
{ \
|
||||
fprintf(stderr, ERROR_ALLOC_MESSAGE); \
|
||||
exit(EXIT_FAILURE); \
|
||||
} \
|
||||
} \
|
||||
while(0)
|
||||
|
||||
#define REALLOC(p, n) \
|
||||
do \
|
||||
{ \
|
||||
if ( !( (p) = realloc((p), sizeof(*(p)) * (n)) ) ) \
|
||||
{ \
|
||||
fprintf(stderr, ERROR_ALLOC_MESSAGE); \
|
||||
exit(EXIT_FAILURE); \
|
||||
} \
|
||||
} \
|
||||
while(0)
|
||||
|
||||
#define FREE(p) \
|
||||
do \
|
||||
{ \
|
||||
free(p); \
|
||||
p = NULL; \
|
||||
} \
|
||||
while(0)
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef REQUESTS_H_
|
||||
#define REQUESTS_H_
|
||||
|
||||
/*
|
||||
* Computes a GET request addressed to the @url hosted by @host.
|
||||
*
|
||||
* @query_params and @cookies can be set to NULL if not needed.
|
||||
*
|
||||
* @return: a char array containing the GET request.
|
||||
* NULL if the operation faield (input was invalid)
|
||||
*
|
||||
*/
|
||||
char *compute_get_request(char *host, char *url, char *query_params,
|
||||
char **cookies, int cookies_count);
|
||||
|
||||
/*
|
||||
* Computes a POST request addressed to the @url hosted by @host.
|
||||
*
|
||||
* @cookies can be set to NULL if not needed.
|
||||
* @body_data is the data contained in the POST request.
|
||||
*
|
||||
* @return: a char array containing the POST request.
|
||||
*
|
||||
*/
|
||||
char *compute_post_request(char *host, char *url, char* content_type, char **body_data,
|
||||
int body_data_fields_count, char** cookies, int cookies_count);
|
||||
|
||||
/*
|
||||
* Adds a x-access-token field in the header of the GET request.
|
||||
*
|
||||
*/
|
||||
char *add_access_token(char *request, char *jwt_token);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,26 @@
|
|||
#ifndef SERVER_H_
|
||||
#define SERVER_H_
|
||||
|
||||
/*
|
||||
* Info about the server.
|
||||
*
|
||||
*/
|
||||
#define SERVER "ec2-3-8-116-10.eu-west-2.compute.amazonaws.com"
|
||||
#define PORT 8080
|
||||
|
||||
/*
|
||||
* Content types.
|
||||
*
|
||||
*/
|
||||
#define JSON_CONTENT_TYPE "application/json"
|
||||
|
||||
/*
|
||||
* URLs.
|
||||
*
|
||||
*/
|
||||
#define REGISTER_URL "/api/v1/tema/auth/register"
|
||||
#define LOGIN_URL "/api/v1/tema/auth/login"
|
||||
#define ENTER_LIBRARY_URL "/api/v1/tema/library/access"
|
||||
#define GET_BOOKS_URL "/api/v1/tema/library/books"
|
||||
|
||||
#endif
|
|
@ -0,0 +1,32 @@
|
|||
#include <json_parser.h>
|
||||
|
||||
cJSON *array_to_cJSON_Object(rigid_json_t *array, size_t array_sz) {
|
||||
|
||||
cJSON *json_object = cJSON_CreateObject();
|
||||
|
||||
for (size_t i = 0; i < array_sz; i++) {
|
||||
cJSON *item = cJSON_CreateString(array[i].value);
|
||||
cJSON_AddItemToObject(json_object, array[i].name, item);
|
||||
}
|
||||
|
||||
return json_object;
|
||||
}
|
||||
|
||||
cJSON *convert_auth_info_to_cJSON(auth_info_t *auth_info,
|
||||
char *username, char *password) {
|
||||
|
||||
/*
|
||||
* Create an array of rigid_json_t's and convert them using
|
||||
* #array_to_cJSON_Object.
|
||||
*
|
||||
*/
|
||||
rigid_json_t json_array_content[REGISTER_ELEM_NO];
|
||||
|
||||
json_array_content[0] = (rigid_json_t)
|
||||
{ .name = username, .value = auth_info->username };
|
||||
json_array_content[1] = (rigid_json_t)
|
||||
{ .name = password, .value = auth_info->password };
|
||||
|
||||
return array_to_cJSON_Object(json_array_content, REGISTER_ELEM_NO);
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -0,0 +1,240 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "connection.h"
|
||||
#include "requests.h"
|
||||
#include <error.h>
|
||||
#include <memory.h>
|
||||
|
||||
char *compute_get_request(char *host, char *url, char *query_params,
|
||||
char **cookies, int cookies_count)
|
||||
{
|
||||
char *message;
|
||||
CALLOC(message, BUFLEN);
|
||||
|
||||
char *line;
|
||||
CALLOC(line, LINELEN);
|
||||
|
||||
/*
|
||||
* Append queries to the first line of the request.
|
||||
*
|
||||
*/
|
||||
if (query_params != NULL) {
|
||||
sprintf(line, "GET %s?%s HTTP/1.1", url, query_params);
|
||||
} else {
|
||||
sprintf(line, "GET %s HTTP/1.1", url);
|
||||
}
|
||||
|
||||
compute_message(message, line);
|
||||
|
||||
if (host == NULL) {
|
||||
|
||||
FREE(message);
|
||||
FREE(line);
|
||||
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Add a host to GET request", NULL);
|
||||
}
|
||||
|
||||
memset(line, 0x00, LINELEN);
|
||||
sprintf(line, "Host: %s", host);
|
||||
|
||||
compute_message(message, line);
|
||||
|
||||
/*
|
||||
* Add headers and/or cookies, according to the protocol format
|
||||
*
|
||||
*/
|
||||
if (cookies != NULL) {
|
||||
|
||||
memset(line, 0x00, LINELEN);
|
||||
sprintf(line, "Cookie: %s", cookies[0]);
|
||||
|
||||
for (size_t i = 1; i < cookies_count; i++) {
|
||||
sprintf(line, "%s; %s", line, cookies[i]);
|
||||
}
|
||||
|
||||
compute_message(message, line);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Add a final newline.
|
||||
*
|
||||
*/
|
||||
compute_message(message, "");
|
||||
|
||||
/*
|
||||
* FREE the memory and return.
|
||||
*
|
||||
*/
|
||||
FREE(line);
|
||||
return message;
|
||||
}
|
||||
|
||||
char *compute_post_request(char *host, char *url, char* content_type, char **body_data,
|
||||
int body_data_fields_count, char **cookies, int cookies_count)
|
||||
{
|
||||
char *message;
|
||||
CALLOC(message, BUFLEN);
|
||||
|
||||
char *line;
|
||||
CALLOC(line, LINELEN);
|
||||
|
||||
char *body_data_buffer;
|
||||
CALLOC(body_data_buffer, LINELEN);
|
||||
|
||||
/*
|
||||
* Write the method name, URL and protocol type.
|
||||
*
|
||||
*/
|
||||
sprintf(line, "POST %s HTTP/1.1", url);
|
||||
compute_message(message, line);
|
||||
|
||||
/*
|
||||
* Add the host.
|
||||
*
|
||||
*/
|
||||
if (host == NULL) {
|
||||
|
||||
FREE(message);
|
||||
FREE(line);
|
||||
FREE(body_data_buffer);
|
||||
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] Add a host to POST request", NULL);
|
||||
}
|
||||
|
||||
memset(line, 0x00, LINELEN);
|
||||
sprintf(line, "Host: %s", host);
|
||||
|
||||
compute_message(message, line);
|
||||
|
||||
/*
|
||||
* Add Content-Type to the request.
|
||||
*
|
||||
*/
|
||||
memset(line, 0x00, LINELEN);
|
||||
sprintf(line, "Content-Type: %s", content_type);
|
||||
|
||||
compute_message(message, line);
|
||||
|
||||
|
||||
if (body_data_fields_count == 0) {
|
||||
FREE(message);
|
||||
FREE(line);
|
||||
FREE(body_data_buffer);
|
||||
|
||||
NONVOID_ERROR_HANDLER(true, "[ERROR] body_data missing in POST request", NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute Content-Length.
|
||||
*
|
||||
*/
|
||||
size_t content_length = 0;
|
||||
for (size_t i = 0; i < body_data_fields_count; i++) {
|
||||
content_length += strlen(body_data[i]);
|
||||
}
|
||||
|
||||
// add the number of &'s from body_Data to length
|
||||
content_length += (body_data_fields_count - 1);
|
||||
|
||||
memset(line, 0x00, LINELEN);
|
||||
sprintf(line, "Content-Length: %ld", content_length);
|
||||
|
||||
compute_message(message, line);
|
||||
|
||||
/*
|
||||
* Add the cookies.
|
||||
*
|
||||
*/
|
||||
if (cookies != NULL) {
|
||||
|
||||
memset(line, 0x00, LINELEN);
|
||||
sprintf(line, "Cookie: %s", cookies[0]);
|
||||
|
||||
for (size_t i = 1; i < cookies_count; i++) {
|
||||
sprintf(line, "%s; %s", line, cookies[i]);
|
||||
}
|
||||
|
||||
compute_message(message, line);
|
||||
|
||||
}
|
||||
/*
|
||||
* Add a newline at the final of the header.
|
||||
*/
|
||||
compute_message(message, "");
|
||||
|
||||
/*
|
||||
* Add the payload of the POST request.
|
||||
*
|
||||
*/
|
||||
memset(body_data_buffer, 0x00, LINELEN);
|
||||
sprintf(body_data_buffer, "%s", body_data[0]);
|
||||
|
||||
for (size_t i = 1; i < body_data_fields_count; i++) {
|
||||
sprintf(body_data_buffer, "%s&%s", body_data_buffer, body_data[i]);
|
||||
}
|
||||
|
||||
compute_message(message, body_data_buffer);
|
||||
|
||||
/*
|
||||
* FREE the memory and return.
|
||||
*
|
||||
*/
|
||||
FREE(line);
|
||||
FREE(body_data_buffer);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
char *add_access_token(char *request, char *jwt_token) {
|
||||
/*
|
||||
* Add guards for input.
|
||||
*
|
||||
*/
|
||||
NONVOID_ERROR_HANDLER(request == NULL, "[ERROR] request must be a"
|
||||
" non-null address", request);
|
||||
|
||||
NONVOID_ERROR_HANDLER(jwt_token == NULL, "[ERROR] jwt_token must be a"
|
||||
" non-null address", request);
|
||||
|
||||
/*
|
||||
* Delete the final \CR\LF from the request.
|
||||
*
|
||||
*/
|
||||
request[strlen(request) - 1] = 0x00;
|
||||
request[strlen(request) - 1] = 0x00;
|
||||
|
||||
/*
|
||||
* Add the jwt_token and the final \CR\LF.
|
||||
*
|
||||
*/
|
||||
char *access_token;
|
||||
CALLOC(access_token, LINELEN);
|
||||
sprintf(access_token, "Authorization: Bearer %s", jwt_token);
|
||||
|
||||
compute_message(request, access_token);
|
||||
compute_message(request, "");
|
||||
|
||||
/*
|
||||
* Free the memory.
|
||||
*
|
||||
*/
|
||||
FREE(access_token);
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Binary file not shown.
|
@ -0,0 +1,5 @@
|
|||
#include <stdio.h>
|
||||
int main() {
|
||||
perror("{ufasda");
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
refactor the cookies in compute get request
|
Loading…
Reference in New Issue