Facebook
From Gruff Owl, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 78
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "buffer.h"
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include "requests.h"
  7. #include <netinet/in.h>
  8. #include <sys/socket.h>
  9. #include <netdb.h>  
  10. #include "helpers.h"
  11. #include "parson.h"
  12.  
  13. #define HOST "34.241.4.235"
  14. #define port 8080
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.     int sockfd;
  19.     char *cookies = malloc(sizeof(char) * 200);
  20.     char *token = malloc(sizeof(char) * 200);
  21.     strcpy(token, "Authorization: Bearer ");
  22.  
  23.    
  24.    
  25.     char read_in[1500];
  26.  
  27.     int sv_on = 1;
  28.     while(sv_on != 0)
  29.     {
  30.         memset(read_in, 0, 1500);
  31.         scanf("%s", read_in);
  32.         printf("-{%s}-", read_in);
  33.  
  34.         if(strcmp(read_in, "register") == 0)
  35.         {
  36.             sockfd = open_connection(HOST, port, AF_INET, SOCK_STREAM, 0); //deschid conexiunea cu serverul
  37.             char username[50];
  38.             char password[50];
  39.             printf("username: "); scanf("%s", username);
  40.             printf("password: "); scanf("%s", password);
  41.             printf("%s %s\n", username, password);
  42.             //fac JSON ul
  43.             JSON_Value *user  = json_value_init_object();
  44.             JSON_Object *user_cred = json_value_get_object(user);
  45.             json_object_set_string(user_cred, "username", username);
  46.             json_object_set_string(user_cred, "password", password);
  47.            
  48.             char *data[1]; //aici salvez username si parola
  49.             data[0] = malloc(sizeof(char) * 200);
  50.             strcpy(data[0], json_serialize_to_string(user));
  51.  
  52.  
  53.             //fac request la server
  54.             char *request = compute_post_request(HOST, "/api/v1/tema/auth/register", "application/json", data, 1, NULL, 0);
  55.             free(data[0]);
  56.          
  57.  
  58.             puts(request);
  59.             send_to_server(sockfd, request);//trimit mesajul la server
  60.             free(request);
  61.  
  62.             char *receive = receive_from_server(sockfd);
  63.             printf("%s", receive);
  64.             free(receive);
  65.             close(sockfd);
  66.             printf("\n");
  67.         }
  68.  
  69.         if(strcmp(read_in, "login") == 0)
  70.         {
  71.             sockfd = open_connection(HOST, port, AF_INET, SOCK_STREAM, 0); //deschid conexiunea cu serverul
  72.             char username[50];
  73.             char password[50];
  74.             printf("username: "); scanf("%s", username);
  75.             printf("password: "); scanf("%s", password);
  76.             printf("%s %s\n", username, password);
  77.             //fac JSON ul
  78.             JSON_Value *user  = json_value_init_object();
  79.             JSON_Object *user_cred = json_value_get_object(user);
  80.             json_object_set_string(user_cred, "username", username);
  81.             json_object_set_string(user_cred, "password", password);
  82.  
  83.             char *data[1]; //aici salvez username si parola
  84.             data[0] = malloc(sizeof(char) * 250);
  85.             strcpy(data[0], json_serialize_to_string(user));
  86.  
  87.  
  88.             //fac request la server
  89.             char *request = compute_post_request(HOST, "/api/v1/tema/auth/login", "application/json", data, 1, NULL, 0);
  90.             free(data[0]);
  91.  
  92.  
  93.  
  94.             puts(request);
  95.             send_to_server(sockfd, request);//trimit mesajul la server
  96.             free(request);
  97.  
  98.             char *receive = receive_from_server(sockfd);
  99.            
  100.            
  101.             char *cookie = malloc(sizeof(char) * 200);
  102.            
  103.            
  104.             char *line = strtok(receive, "\n");
  105.             while( line != NULL )
  106.             {
  107.                 if( strncmp(line, "Set-Cookie: ", 12) == 0 )
  108.                 {
  109.                     int i = 12;
  110.                     while(line[i - 1] != ';')
  111.                     {
  112.                         cookie[i - 12] = line[i];
  113.                         i++;
  114.                     }
  115.                     break;
  116.                 }
  117.                 line = strtok(NULL, "\n");
  118.             }
  119.             printf("%s", cookie);
  120.             strcpy(cookies, cookie);
  121.             close(sockfd);
  122.             printf("\n");
  123.         }
  124.  
  125.         if(strcmp(read_in, "access") == 0)
  126.         {
  127.             sockfd = open_connection(HOST, port, AF_INET, SOCK_STREAM, 0); //deschid conexiunea cu serverul
  128.             char *cookie[1]; //pt cookie
  129.             cookie[0] = malloc(sizeof(char) * 200);
  130.             strcpy(cookie[0], cookies);
  131.  
  132.             char *request_cookie = compute_get_request(HOST, "/api/v1/tema/library/access", NULL, cookie, 1);
  133.             free(cookie[0]);
  134.            
  135.             send_to_server(sockfd, request_cookie);
  136.            
  137.             char *receive_cookie = receive_from_server(sockfd);
  138.             puts(receive_cookie);
  139.             char *aux_token = malloc(sizeof(char) * 200);
  140.             strcpy(aux_token,strstr(receive_cookie, "token"));
  141.            
  142.             char *p;
  143.             p = strtok(aux_token, "\"");
  144.             p = strtok(NULL, "\"");
  145.             p = strtok(NULL, "\"");
  146.             strcat(token, p);
  147.  
  148.             printf("\n\n%s", p);
  149.             free(request_cookie);
  150.             free(receive_cookie);
  151.             close(sockfd);
  152.             printf("\n");
  153.         }
  154.  
  155.         if(strcmp(read_in, "get_books") == 0)
  156.         {  
  157.             printf("MUIE PSD");
  158.             sockfd = open_connection(HOST, port, AF_INET, SOCK_STREAM, 0); //deschid conexiunea cu serverul
  159.             printf("\n");
  160.             printf("%s", token);
  161.             char *books_details[1];
  162.             books_details[0] = malloc(sizeof(char) * 200);
  163.             strncpy(books_details[0], token, strlen(token));
  164.  
  165.             printf("%s", books_details[0]);
  166.             char *request_books = compute_get_request(HOST, "/api/v1/tema/library/books", NULL, books_details, 1);
  167.             puts(request_books);
  168.             send_to_server(sockfd, request_books);
  169.             char *receive_books = receive_from_server(sockfd);
  170.             puts(receive_books);
  171.  
  172.            
  173.             close(sockfd);
  174.             printf("\n");
  175.         }
  176.        
  177.  
  178.         if(strcmp(read_in, "exit") == 0)
  179.         { sv_on = 0; }
  180.     }
  181.    
  182.  
  183.    
  184. }