#include <stdio.h> /* printf, sprintf / #include <stdlib.h> / exit, atoi, malloc, free /…
- User
#include <stdio.h> /* printf, sprintf / #include <stdlib.h> / exit, atoi, malloc, free / #include <unistd.h> / read, write, close / #include <string.h> / memcpy, memset / #include <sys/socket.h> / socket, connect / #include <netinet/in.h> / struct sockaddr_in, struct sockaddr / #include <netdb.h> / struct hostent, gethostbyname */ #include <arpa/inet.h> #include "helpers.h" #include "parson.h" #include "requests.h" #include <stdbool.h>
#define HOST "34.246.184.49" #define PORT 8080 #define CONTENT_TYPE "application/json"
char *create_response (char *url) { int sockfd; char *message = NULL; char *response = NULL; char *cookie; JSON_Value *root_value = json_value_init_object(); JSON_Object *root_object = json_value_get_object(root_value);
sockfd = open_connection(HOST, PORT, AF_INET, SOCK_STREAM, 0); char username[50], password[50]; printf("username="); fgets(username, 50, stdin); username[strcspn(username, "\n")] = 0; printf("password="); fgets(password, 50, stdin); password[strcspn(password, "\n")] = 0; json_object_set_string(root_object, "username", username); json_object_set_string(root_object, "password", password); char *serialized_string = json_serialize_to_string_pretty(root_value); message = compute_post_request(HOST, url, CONTENT_TYPE, &serialized_string, 1, NULL, 0, NULL); send_to_server(sockfd, message); response = receive_from_server(sockfd); close_connection(sockfd); if (strstr(response, "Set-Cookie: ")) { cookie = strtok(strstr(response, "Set-Cookie: ") + strlen("Set-Cookie: "), "\n"); return cookie; } return response;}
char *get_library (char *url, char **cookies, int cookies_len) { int sockfd; char *message = NULL; char *response = NULL;
sockfd = open_connection(HOST, PORT, AF_INET, SOCK_STREAM, 0); DIE(sockfd < 0, "open_connection"); message = compute_get_request(HOST, url, NULL, cookies, cookies_len, NULL); send_to_server(sockfd, message); response = receive_from_server(sockfd); close_connection(sockfd); return basic_extract_json_response(response);}
char *get_book(char *url, char **cookies, int cookies_len, char *token_jwt) { int sockfd; char *message = NULL; char *response = NULL; char *id = (char *)malloc(10 * sizeof(char)); char *new_url = (char *)malloc(100 * sizeof(char));
sockfd = open_connection(HOST, PORT, AF_INET, SOCK_STREAM, 0); DIE(sockfd < 0, "open_connection"); printf("id="); fgets(id, 10, stdin); id[strcspn(id, "\n")] = 0; strcpy(new_url, url); strcat(new_url, id); message = compute_get_request(HOST, new_url, NULL, cookies, cookies_len, token_jwt); send_to_server(sockfd, message); response = receive_from_server(sockfd); close_connection(sockfd); return response;}
char *get_books(char *url, char **cookies, int cookies_len, char *token_jwt) { int sockfd; char *message = NULL; char *response = NULL;
- Assistant
- User
else if (err == NULL){ loggedIn = true; char *p = strstr(response, "connect.sid"); if (p) { strcpy(cookies[cookies_len++], strtok(strdup(response), ";")); } printf("Login with success\n"); } continue; ce se intampla aici?
- Assistant
Blocul de cod pe care l-ai prezentat este parte din structura de control
else ifcare gestionează răspunsurile de la server după executarea unei operații de autentificare (login). Iată desfășurarea și explicația funcțiilor și a logicii acestui fragment: - User
explica functia get books char *get_books(char *url, char **cookies, int cookies_len, char *token_jwt) { int sockfd; char *message = NULL; char *response = NULL;
sockfd = open_connection(HOST, PORT, AF_INET, SOCK_STREAM, 0); DIE(sockfd < 0, "open_connection"); message = compute_get_request(HOST, url, NULL, cookies, cookies_len, token_jwt); send_to_server(sockfd, message); response = receive_from_server(sockfd); close_connection(sockfd); return response;}
- Assistant
- User
char *get_book(char *url, char **cookies, int cookies_len, char *token_jwt) { int sockfd; char *message = NULL; char *response = NULL; char *id = (char *)malloc(10 * sizeof(char)); char *new_url = (char *)malloc(100 * sizeof(char));
sockfd = open_connection(HOST, PORT, AF_INET, SOCK_STREAM, 0); DIE(sockfd < 0, "open_connection"); printf("id="); fgets(id, 10, stdin); id[strcspn(id, "\n")] = 0; strcpy(new_url, url); strcat(new_url, id); message = compute_get_request(HOST, new_url, NULL, cookies, cookies_len, token_jwt); send_to_server(sockfd, message); response = receive_from_server(sockfd); close_connection(sockfd); return response;} explica codul
- Assistant
- User
har *delete_book (char *url, char *token_jwt) { int sockfd; char *message = NULL; char *response = NULL; char *id = (char *)malloc(20 * sizeof(char)); char *new_url = (char *)malloc(100 * sizeof(char));
sockfd = open_connection(HOST, PORT, AF_INET, SOCK_STREAM, 0); DIE(sockfd < 0, "open_connection"); printf("id="); fgets(id, 20, stdin); id[strcspn(id, "\n")] = 0; strcpy(new_url, url); strcat(new_url, id); message = compute_delete_request(HOST, new_url, CONTENT_TYPE, token_jwt); send_to_server(sockfd, message); response = receive_from_server(sockfd); close_connection(sockfd); return response;} ar *compute_delete_request(char *host, char url, char content_type, char *token_jwt) { char *message = calloc(BUFLEN, sizeof(char)); char *line = calloc(LINELEN, sizeof(char));
sprintf(line, "DELETE %s HTTP/1.1", url); compute_message(message, line); sprintf(line, "Host: %s", host); compute_message(message, line); sprintf(line, "Authorization: Bearer %s", token_jwt); compute_message(message, line); compute_message(message, ""); free(line); return message;} explica codul
- Assistant
- User
ce face functia char *add_book(char *url, char **cookies, int cookies_len, char *token_jwt) { int sockfd; char *message = NULL; char *response = NULL;
sockfd = open_connection(HOST, 8080, AF_INET, SOCK_STREAM, 0); char title[200], author[200], genre[200], publisher[200], page_count[200]; printf("title="); fgets(title, 200, stdin); title[strcspn(title, "\n")] = 0; printf("author="); fgets(author, 200, stdin); author[strcspn(author, "\n")] = 0; printf("genre="); fgets(genre, 200, stdin); genre[strcspn(genre, "\n")] = 0; printf("publisher="); fgets(publisher, 200, stdin); publisher[strcspn(publisher, "\n")] = 0; printf("page_count="); fgets(page_count, 200, stdin); page_count[strcspn(page_count, "\n")] = 0; JSON_Value *root_value = json_value_init_object(); JSON_Object *root_object = json_value_get_object(root_value); json_object_set_string(root_object, "title", title); json_object_set_string(root_object, "author", author); json_object_set_string(root_object, "genre", genre); json_object_set_string(root_object, "publisher", publisher); json_object_set_string(root_object, "page_count", page_count); char *serialized_string = json_serialize_to_string_pretty(root_value); message = compute_post_request(HOST, url, CONTENT_TYPE, &serialized_string, 1, NULL, 0, token_jwt); send_to_server(sockfd, message); response = receive_from_server(sockfd); close_connection(sockfd); return response; - Assistant
- User
else if (strncmp(input, "logout", 6) == 0) { if (loggedIn == false || jwtAccess != 1) { printf("You are not logged_in\n"); continue; } int ok = 0; for (int i = 0; i < cookies_len; i++) { if (strstr(cookies[i], "connect.sid")) { ok = 1; } } if (ok == 1) { cookies_len = 0; jwtAccess = 0; loggedIn = true; free(token_jwt); token_jwt = NULL; printf("Logout with success\n"); } ce se intampla aici
- Assistant
- User
- Assistant
- User
- Assistant