Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ SRCDIR = src

# Find all .c files in the src directory
SRCS = $(wildcard $(SRCDIR)/*.c)
OBJECT = server
.PHONY: clean

# Build the executable
server: $(SRCS)
gcc -g $(SRCS) -o server
$(OBJECT): $(SRCS)
@gcc -g $(SRCS) -o $(OBJECT)

clean:
@rm -f $(OBJECT)
Binary file removed server
Binary file not shown.
9 changes: 7 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void service_client(const int master_socket_fd)


int sent_recv_bytes = 0;
int addr_len = sizeof(struct sockaddr);
socklen_t addr_len = sizeof(struct sockaddr);

fd_set readfds;

Expand Down Expand Up @@ -215,9 +215,14 @@ void service_client(const int master_socket_fd)
char *method = NULL;
char *URL = NULL;

char del[] = "\n";
const char del[] = "\n";

char *buffer_copy = strdup(DATA_BUFFER);
if( !buffer_copy ){
printf("Cannot duplicate buffer?\n");
close(comm_socket_fd);
break;
}

request_line = strtok(buffer_copy, del);

Expand Down
2 changes: 1 addition & 1 deletion src/queryparams.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ QueryParamNode_t * parse_query_string(const char *query_string)
tok=otok;

if (!value || !key) {
printf("Error with a ghost param! {%s:%s}\n", key, value);
printf("Error with a ghost param! {%s:%s}\n", key? key : "null", value? value : "null");
continue;
}

Expand Down
51 changes: 26 additions & 25 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ inote_t notes[DATABASESIZE] = {
{"Eric", "Eric", "Lock in and finish this program by the end of day", 2},
};

/* unused, but could be useful for debugging */
#if 0
void print_notes()
{
for (int i = 0; i < NOTES_SIZE; i++)
Expand All @@ -34,12 +36,14 @@ void print_notes()
);
}
}
#endif

// method and URL strings are passed by reference
void process_request_line(char *request_line, char **method, char **URL)
{
printf("processing request line: %s\n", request_line);
char del[] = " ";
const char del[] = " ";
/* FIXME: strok is not thread safe, but that's ok here */
*method = strtok(request_line, del); /*Tokenize the request line on the basis of space, and extract the first word*/
*URL = strtok(NULL, del); /*Extract the URL*/
}
Expand All @@ -49,8 +53,8 @@ void query_database(unsigned int *ids, const unsigned int *num_ids, QueryParamNo
// Save param and the value accordingly

unsigned int id_key = UINT_MAX;
char *to_key = NULL;
char *from_key = NULL;
const char *to_key = NULL;
const char *from_key = NULL;

// Populate datatypes from query params
QueryParamNode_t *curr = *head;
Expand Down Expand Up @@ -104,11 +108,9 @@ void query_database(unsigned int *ids, const unsigned int *num_ids, QueryParamNo
inote_t create_note(QueryParamNode_t **head)
{
// Save param and the value accordingly

unsigned int id_key = UINT_MAX;
char *to_key = NULL;
char *from_key = NULL;
char *note_key = NULL;
const char *to_key = NULL;
const char *from_key = NULL;
const char *note_key = NULL;

unsigned int count = 0;

Expand Down Expand Up @@ -159,7 +161,7 @@ inote_t create_note(QueryParamNode_t **head)

// Construct the thext response body
// returns the length of the response data
unsigned int construct_html_table(char** response, const size_t ARRSIZE, unsigned int *selected_ids)
unsigned int construct_html_table(char** response, const size_t ARRSIZE, const unsigned int *selected_ids)
{
// Construct table
strcpy(*response,
Expand Down Expand Up @@ -226,17 +228,15 @@ unsigned int construct_html_header(char** header, char** response)
return strlen(*header);
}

char * process_GET_request(char *URL, unsigned int *response_len)
char * process_GET_request(const char *URL, unsigned int *response_len)
{
char *strid = NULL, *to = NULL, *from = NULL;

// return a linked list of key, value pairs parsed from query string
QueryParamNode_t *head = parse_query_string(URL);

unsigned int selected_ids[NOTES_SIZE];

// Initialize all selected_ids to UINT_MAX (aka NULL)
memset(selected_ids, UINT_MAX, sizeof(selected_ids));
// Initialize all selected_ids to NUL
memset(selected_ids, 0, sizeof(selected_ids));

query_database(selected_ids, &NOTES_SIZE, &head);

Expand Down Expand Up @@ -283,8 +283,8 @@ char * process_POST_request(char *buffer, unsigned int *response_len)
const size_t ARRSIZE = sizeof(notes) / sizeof(inote_t);
unsigned int selected_ids[ARRSIZE];

// Initialize all selected_ids to UINT_MAX (aka NULL)
memset(selected_ids, UINT_MAX, sizeof(selected_ids));
// Initialize all selected_ids to NUL
memset(selected_ids, 0, sizeof(selected_ids));

inote_t new_note = create_note(&head);

Expand All @@ -304,16 +304,17 @@ char * process_POST_request(char *buffer, unsigned int *response_len)
free(head);
head = cur;
}
static const int max_resp_size = 248;

char *response = calloc(1, max_resp_size);

/* FIXME: check returned value, as if it's equal to max_resp_size, the answer was probably clipped */
/* Instead, let's nul-terminate response in case the note is larger than max_resp_size chars */
snprintf(response, max_resp_size, "Added note to database with id %04u.\n", new_note.id);
response[max_resp_size-1] = '\0';

char *response = calloc(1, 248);
strcpy(response, "Added note to database with id ");
char id_str[4];
sprintf(id_str, "%u", new_note.id);
strcat(response, id_str);
strcat(response, ".\n");
char *header = calloc(1, 248);
unsigned int content_len_str = construct_html_header(&header, &response);
*response_len = strlen(header);
char *header = calloc(1, max_resp_size);
*response_len = construct_html_header(&header, &response);
free(response);
return header;
}
8 changes: 1 addition & 7 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,5 @@ typedef struct inote{

// method and URL strings are passed by reference
void process_request_line(char *request_line, char **method, char **URL);
void string_space_trim(char *string);
inote_t create_note(QueryParamNode_t **head);
void query_database(unsigned int *ids, const unsigned int *num_ids, QueryParamNode_t **head);
void print_notes();
unsigned int construct_html_header(char** header, char** response);
unsigned int construct_html_table(char** response, const size_t ARRSIZE, unsigned int *selected_ids);
char * process_GET_request(char *URL, unsigned int *response_len);
char * process_GET_request(const char *URL, unsigned int *response_len);
char * process_POST_request(char *buffer, unsigned int *response_len);