Skip to content

Anca04/PCOM_3_web-client

Repository files navigation

Homework 3 PCOM - Web Client. Communication with REST API

I started the implementation from the skelet of lab 9. I structured my skelet as it says in the homework. So, I will explain each part of the skelet.

buffer.c, buffer.h I took the implementation from lab 9, so I didn't change anything in the code. The buffer handles the requests and responses from HTTP. The buffer holds the data it receives, can add more data and can find delimiters.

helper.c, helper.h For this one too, I kept the implementation from the lab and add it some functions. I will explain the functions I have added it. I added the funtion void client_initialization(client_state_t *state); which initializes the client. I will talk later about the structure for the client, this function sets every state to NULL. The function void free_client(client_state_t *state); frees the memory allocated for my client. The function char *read_input(void); reads the input from the client. The functions int check_if_number(const char *s); int is_digit(char c); checks if I have no letter (22, 13). The function int check_if_double(const char *str); checks if it's a double.

requests.c, requests.h I kept the two function I have implemented in the lab and added other. The first one, that computes a GET request, I followed the steps from the lab, write the name, URL, request param and protocol type, then add the host, add headers, cookie and token, and lastly add new line. The second one, that computes a POST request, I do almost the same thing, I write the method name, URL, request params and protocol type, then add the host, add headers for Content-type, Content-length, cookie and token. At the end, add new line and add the JSON body to the message. I added a function that computes a DELETE request, it asks the server to delete something (like a user, a movie). I do the same thing, write name, URL, request params and protocol type, add host, add headers and add new line. The next one for PUT request, to update something (like a movie). The implementation follows the ones above. I have implemented a function to extract the cookie, I start by looking for "Set-Cookie: ", if it finds it, then the cookie exists, I jump over this substring, and I look for "session=", if this also exists, then I can finally extract the cookie, I jump over this substring, and look for the end of the cookie, which is ";", after this, I calculate the length of the cookie, and copy it. The last function checks if the response from HTTP is a success, by checking if the code is 200 or 201.

client.c, client.h I start by initializing the client, for which I use a structure to store it's state, if it's admin, logged in, to store the cookie and token. Now, for each command I have a function that handles what it needs to happen. For each the implementation is similar. I read from stdin the input, then compute the HTTP request depeding on what I have to do, open a connection to server and send the request and receive the answer, after this I check the result, and print what it needs to be printed. For others I extract the information from the JSON objects, from body etc. Depending on the response of the server, it will print a successful message, the one that I want or an error message.

I have used JSON library because it's simple to use and integrate. I have generated the body JSON using function from this library, like creating a new empty object, than extract the information and all fields into that.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors