aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 26090ffcb14ff7918d6e5a317c295b3d989ae655 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <ncurses.h>
#include <openssl/ssl.h>
#include <stdlib.h>

#include "color.h"
#include "config.h"
#include "connect.h"
#include "net.h"
#include "request.h"
#include "response.h"
#include "status.h"
#include "url.h"
#include "util.h"

void exit_cleanup(void) { endwin(); }

void init_ncurses(void) {
  initscr();
  cbreak();
  noecho();

  curs_set(0);
  keypad(stdscr, TRUE);
  scrollok(stdscr, TRUE);

  if (!has_colors())
    die("Terminal does not support colors");

  start_color();
  set_colors();

  refresh();
}

int main(void) {
  struct config conf;
  init_config(&conf);

  init_ncurses();
  atexit(exit_cleanup);

  getmaxyx(stdscr, conf.i.height, conf.i.width);

  init_status(&conf);
  conf.s.url = init_url();
  conf.s.conn = init_connection();

  prompt_status_url(&conf);
  update_status(&conf, conf.s.url_string);

  int rc = parse_url(conf.s.url, conf.s.url_string);
  if (rc < 0) {
    error_status(&conf, "Invalid URL");
    conf_cleanup(&conf);
    return 1;
  }

  rc = tls_connect(&conf, *conf.s.url);
  if (rc < 0) {
    conf_cleanup(&conf);
    return 1;
  }

  struct request req;
  req.kind = GET;
  req.url = *conf.s.url;
  send_request(&conf, &req);

  struct response res;
  rc = read_response(&conf, &res);
  if (rc < 0) {
    conf_cleanup(&conf);
    return rc * -1;
  }
  conf.s.res = &res;

  printw("%s\n", conf.s.res->content);

  getch();

  conf_cleanup(&conf);
  return 0;
}