|
7 | 7 | #include "../include/sock.h"
|
8 | 8 | #include "../include/utils.h"
|
9 | 9 |
|
10 |
| -/* Networking constants */ |
11 | 10 | #define SERVER_IP "127.0.0.1"
|
12 | 11 | #define SERVER_PORT "8000"
|
13 | 12 |
|
| 13 | +/* Helper functions to assist with cleanup, I hate cleanup */ |
| 14 | +static void cleanup_char_array(char **array, int n_elem) { |
| 15 | + for (int i = 0; i < n_elem; i++) { |
| 16 | + free(array[i]); |
| 17 | + } |
| 18 | + free(array); |
| 19 | +} |
| 20 | + |
| 21 | +static void cleanup_fraction_array(fraction_t *array, int n_elem) { |
| 22 | + for (int i = 0; i < n_elem; i++) { |
| 23 | + fraction_free(&array[i]); |
| 24 | + } |
| 25 | + free(array); |
| 26 | +} |
| 27 | + |
14 | 28 | int main(void) {
|
15 | 29 | struct addrinfo hints, *ainfo;
|
16 |
| - int sfd; // socket file descriptor |
| 30 | + int sfd = -1; // to be extra professional |
17 | 31 | http_res_t http_fraction_res, http_post_res;
|
18 |
| - /* Setup socket and initiate connection with the server */ |
| 32 | + char **fraction_links = NULL; |
| 33 | + fraction_t *fractions = NULL; |
| 34 | + |
19 | 35 | setup_hints(&hints);
|
20 | 36 |
|
21 | 37 | if (h_getaddrinfo(SERVER_IP, SERVER_PORT, &hints, &ainfo) != 0) {
|
22 | 38 | fprintf(stderr, "Failed to resolve server address\n");
|
23 | 39 | return EXIT_FAILURE;
|
24 | 40 | }
|
25 | 41 |
|
26 |
| - |
27 | 42 | printf("Connecting to: %s:%s\n", SERVER_IP, SERVER_PORT);
|
28 | 43 | sfd = create_sock_and_conn(ainfo);
|
| 44 | + freeaddrinfo(ainfo); |
29 | 45 | if (sfd == -1) {
|
30 | 46 | fprintf(stderr, "Failed to create socket and connect\n");
|
31 | 47 | return EXIT_FAILURE;
|
32 | 48 | }
|
33 |
| - freeaddrinfo(ainfo); // ainfo no longer needed |
34 | 49 |
|
35 |
| - /* Get the fraction links */ |
36 | 50 | if (http_get(sfd, "/", &http_fraction_res) != HTTP_SUCCESS) {
|
37 | 51 | fprintf(stderr, "Failed to retrieve fraction links\n");
|
38 |
| - goto cleanup_socket; |
| 52 | + goto cleanup; |
39 | 53 | }
|
40 | 54 |
|
41 |
| - // Count number of links |
42 | 55 | int num_links = count_lines(http_fraction_res.data) + 1;
|
43 |
| - |
44 |
| - // Allocate memory for fraction links |
45 |
| - char **fraction_links = malloc(num_links * sizeof(char *)); |
| 56 | + fraction_links = malloc(num_links * sizeof(char *)); |
46 | 57 | if (!fraction_links) {
|
47 | 58 | fprintf(stderr, "Failed to allocate memory for fraction links\n");
|
48 | 59 | http_free(&http_fraction_res);
|
49 |
| - goto cleanup_socket; |
| 60 | + goto cleanup; |
50 | 61 | }
|
51 | 62 |
|
52 |
| - // Split the response data into lines |
53 | 63 | int lines_read =
|
54 | 64 | split_fraction_links(http_fraction_res.data, fraction_links, num_links);
|
55 | 65 | if (lines_read < 0) {
|
56 | 66 | fprintf(stderr, "Failed to split fraction links\n");
|
57 |
| - free(fraction_links); |
| 67 | + cleanup_char_array(fraction_links, num_links); |
58 | 68 | http_free(&http_fraction_res);
|
59 |
| - goto cleanup_socket; |
| 69 | + goto cleanup; |
60 | 70 | }
|
61 | 71 |
|
62 |
| - // Storing the fractions in a array |
63 |
| - fraction_t *fractions = malloc(lines_read * sizeof(fraction_t)); |
64 |
| - if (fractions == NULL) { |
65 |
| - fprintf(stderr, "Failed to malloc memory for fractions\n"); |
| 72 | + |
| 73 | + fractions = malloc(lines_read * sizeof(fraction_t)); |
| 74 | + if (!fractions) { |
| 75 | + fprintf(stderr, "Failed to allocate memory for fractions\n"); |
| 76 | + cleanup_char_array(fraction_links, num_links); |
| 77 | + http_free(&http_fraction_res); |
| 78 | + http_free(&http_post_res); |
| 79 | + goto cleanup; |
66 | 80 | }
|
67 | 81 |
|
68 |
| - for (int i=0; i<lines_read; i++) { |
| 82 | + for (int i = 0; i < lines_read; i++) { |
69 | 83 | if (download_fraction(sfd, fraction_links[i], &fractions[i]) != 0) {
|
70 |
| - fprintf(stderr, "Failed to parse fraction\n"); |
| 84 | + fprintf(stderr, "Failed to download fraction\n"); |
71 | 85 | }
|
72 | 86 | }
|
73 | 87 |
|
74 |
| - // Sort the fractions based on index |
75 | 88 | qsort(fractions, lines_read, sizeof(fraction_t), compare_fractions);
|
76 | 89 | for (int i = 0; i < lines_read; i++) {
|
77 | 90 | print_fraction(fractions[i]);
|
78 | 91 | }
|
79 | 92 |
|
80 |
| - check_fractions(fractions, lines_read); |
| 93 | + if (check_fractions(fractions, lines_read)) { // if this works, s0s4 and skelly is to blame! |
| 94 | + fprintf(stderr, "Fractions check failed\n"); |
| 95 | + cleanup_char_array(fraction_links, num_links); |
| 96 | + cleanup_fraction_array(fractions, lines_read); |
| 97 | + http_free(&http_fraction_res); |
| 98 | + http_free(&http_post_res); |
| 99 | + goto cleanup; |
| 100 | + } |
81 | 101 |
|
82 |
| - /* Notify the server that we successfully downloaded the fractions */ |
83 | 102 | if (http_post(sfd, "/deadbeef", "plain/text", "{'downloaded':true}",
|
84 | 103 | &http_post_res) != HTTP_SUCCESS) {
|
85 | 104 | fprintf(stderr, "Failed to send POST request\n");
|
86 |
| - free(fraction_links); |
| 105 | + cleanup_char_array(fraction_links, num_links); |
87 | 106 | http_free(&http_fraction_res);
|
88 |
| - http_free(&http_post_res); |
89 |
| - goto cleanup_socket; |
| 107 | + goto cleanup; |
90 | 108 | }
|
91 | 109 |
|
92 |
| - /* Cleanup */ |
93 | 110 | http_free(&http_fraction_res);
|
94 | 111 | http_free(&http_post_res);
|
95 |
| - |
96 |
| - // Free fractions and links |
97 |
| - for (int i = 0; i < lines_read; i++) { |
98 |
| - free(fraction_links[i]); |
99 |
| - fraction_free(&fractions[i]); |
100 |
| - } |
101 |
| - free(fraction_links); |
102 |
| - free(fractions); |
| 112 | + cleanup_char_array(fraction_links, num_links); |
| 113 | + cleanup_fraction_array(fractions, lines_read); |
103 | 114 |
|
104 | 115 | close(sfd);
|
105 | 116 | return EXIT_SUCCESS;
|
106 | 117 |
|
107 |
| - |
108 |
| - |
109 |
| - |
110 |
| -cleanup_socket: |
111 |
| - close(sfd); |
| 118 | +/* There's nothing to see here, move on*/ |
| 119 | +cleanup: // we accept NO comments on this. have a !nice day |
| 120 | + if (sfd != -1) { |
| 121 | + close(sfd); |
| 122 | + } |
| 123 | + if (fraction_links) { |
| 124 | + cleanup_char_array(fraction_links, num_links); |
| 125 | + } |
| 126 | + if (fractions) { |
| 127 | + cleanup_fraction_array(fractions, num_links); |
| 128 | + } |
| 129 | + if (http_fraction_res.data) { |
| 130 | + http_free(&http_fraction_res); |
| 131 | + } |
| 132 | + if (http_post_res.data) { |
| 133 | + http_free(&http_post_res); |
| 134 | + } |
112 | 135 | return EXIT_FAILURE;
|
113 | 136 | }
|
0 commit comments