#include #include #include "buffer.h" #include #include #include "requests.h" #include #include #include #include "helpers.h" #include "parson.h" #define HOST "34.241.4.235" #define port 8080 int main(int argc, char *argv[]) { int sockfd; char *cookies = malloc(sizeof(char) * 200); char *token = malloc(sizeof(char) * 200); strcpy(token, "Authorization: Bearer "); char read_in[1500]; int sv_on = 1; while(sv_on != 0) { memset(read_in, 0, 1500); scanf("%s", read_in); printf("-{%s}-", read_in); if(strcmp(read_in, "register") == 0) { sockfd = open_connection(HOST, port, AF_INET, SOCK_STREAM, 0); //deschid conexiunea cu serverul char username[50]; char password[50]; printf("username: "); scanf("%s", username); printf("password: "); scanf("%s", password); printf("%s %s\n", username, password); //fac JSON ul JSON_Value *user = json_value_init_object(); JSON_Object *user_cred = json_value_get_object(user); json_object_set_string(user_cred, "username", username); json_object_set_string(user_cred, "password", password); char *data[1]; //aici salvez username si parola data[0] = malloc(sizeof(char) * 200); strcpy(data[0], json_serialize_to_string(user)); //fac request la server char *request = compute_post_request(HOST, "/api/v1/tema/auth/register", "application/json", data, 1, NULL, 0); free(data[0]); puts(request); send_to_server(sockfd, request);//trimit mesajul la server free(request); char *receive = receive_from_server(sockfd); printf("%s", receive); free(receive); close(sockfd); printf("\n"); } if(strcmp(read_in, "login") == 0) { sockfd = open_connection(HOST, port, AF_INET, SOCK_STREAM, 0); //deschid conexiunea cu serverul char username[50]; char password[50]; printf("username: "); scanf("%s", username); printf("password: "); scanf("%s", password); printf("%s %s\n", username, password); //fac JSON ul JSON_Value *user = json_value_init_object(); JSON_Object *user_cred = json_value_get_object(user); json_object_set_string(user_cred, "username", username); json_object_set_string(user_cred, "password", password); char *data[1]; //aici salvez username si parola data[0] = malloc(sizeof(char) * 250); strcpy(data[0], json_serialize_to_string(user)); //fac request la server char *request = compute_post_request(HOST, "/api/v1/tema/auth/login", "application/json", data, 1, NULL, 0); free(data[0]); puts(request); send_to_server(sockfd, request);//trimit mesajul la server free(request); char *receive = receive_from_server(sockfd); char *cookie = malloc(sizeof(char) * 200); char *line = strtok(receive, "\n"); while( line != NULL ) { if( strncmp(line, "Set-Cookie: ", 12) == 0 ) { int i = 12; while(line[i - 1] != ';') { cookie[i - 12] = line[i]; i++; } break; } line = strtok(NULL, "\n"); } printf("%s", cookie); strcpy(cookies, cookie); close(sockfd); printf("\n"); } if(strcmp(read_in, "access") == 0) { sockfd = open_connection(HOST, port, AF_INET, SOCK_STREAM, 0); //deschid conexiunea cu serverul char *cookie[1]; //pt cookie cookie[0] = malloc(sizeof(char) * 200); strcpy(cookie[0], cookies); char *request_cookie = compute_get_request(HOST, "/api/v1/tema/library/access", NULL, cookie, 1); free(cookie[0]); send_to_server(sockfd, request_cookie); char *receive_cookie = receive_from_server(sockfd); puts(receive_cookie); char *aux_token = malloc(sizeof(char) * 200); strcpy(aux_token,strstr(receive_cookie, "token")); char *p; p = strtok(aux_token, "\""); p = strtok(NULL, "\""); p = strtok(NULL, "\""); strcat(token, p); printf("\n\n%s", p); free(request_cookie); free(receive_cookie); close(sockfd); printf("\n"); } if(strcmp(read_in, "get_books") == 0) { printf("MUIE PSD"); sockfd = open_connection(HOST, port, AF_INET, SOCK_STREAM, 0); //deschid conexiunea cu serverul printf("\n"); printf("%s", token); char *books_details[1]; books_details[0] = malloc(sizeof(char) * 200); strncpy(books_details[0], token, strlen(token)); printf("%s", books_details[0]); char *request_books = compute_get_request(HOST, "/api/v1/tema/library/books", NULL, books_details, 1); puts(request_books); send_to_server(sockfd, request_books); char *receive_books = receive_from_server(sockfd); puts(receive_books); close(sockfd); printf("\n"); } if(strcmp(read_in, "exit") == 0) { sv_on = 0; } } }