aboutsummaryrefslogtreecommitdiff
path: root/src/server.c
blob: 1176a66d7a25f68e889fbef5e62d27772192ad02 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "../beaker.h"        
#include "beaker_globals.h"   
#include <arpa/inet.h>        
#include <netinet/in.h>       
#include <stdbool.h>          
#include <stdio.h>            
#include <string.h>           
#include <sys/types.h>
#include <sys/socket.h>       
#include <unistd.h>           

static int initialize_server_socket(const char *ip, int port, int *server_fd_out,
                                    struct sockaddr_in *address_out) {

  if ((*server_fd_out = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket failed");
    fprintf(stderr, "[ERROR] initialize_server_socket: Failed to create socket.\n");
    return -1;
  }

  int opt = 1;

  if (setsockopt(*server_fd_out, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
    perror("setsockopt SO_REUSEADDR failed");
  }

  //Needed for FreeBSD Support
  #ifdef SO_REUSEPORT
  if (setsockopt(*server_fd_out, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt))) {
    perror("setsockopt SO_REUSEPORT failed");
    fprintf(stderr, "[ERROR] initialize_server_socket: Failed to set SO_REUSEPORT.\n");
    close(*server_fd_out);
    return -1;
  }
  #endif


  address_out->sin_family = AF_INET;           
  address_out->sin_addr.s_addr = inet_addr(ip); 
  address_out->sin_port = htons(port);         

  if (bind(*server_fd_out, (struct sockaddr *)address_out, sizeof(*address_out)) < 0) {
    perror("bind failed");
    fprintf(stderr, "[ERROR] initialize_server_socket: Failed to bind socket to %s:%d.\n", ip, port);
    close(*server_fd_out);
    return -1;
  }

  if (listen(*server_fd_out, 10) < 0) {
    perror("listen failed");
    fprintf(stderr, "[ERROR] initialize_server_socket: Failed to listen on socket.\n");
    close(*server_fd_out);
    return -1;
  }

  printf("Beaker server listening on %s:%d\n", ip, port);
  return 0;
}

static void handle_client_connection(int new_socket) {
  current_client_socket = new_socket; 
  char buffer[BUFFER_SIZE] = {0};     

  ssize_t bytes_read = read(new_socket, buffer, BUFFER_SIZE - 1);
  if (bytes_read < 0) {
    perror("read failed");
    fprintf(stderr, "[ERROR] handle_client_connection: Failed to read from client socket.\n");
    close(new_socket);
    return;
  }
  buffer[bytes_read] = '\0'; 

  strncpy(current_request_buffer, buffer, BUFFER_SIZE - 1);
  current_request_buffer[BUFFER_SIZE - 1] = '\0';

  char request_line[MAX_PATH_LEN + 64]; 
  char *first_line_end = strstr(buffer, "\r\n"); 

  if (first_line_end == NULL) {
    fprintf(stderr, "[ERROR] handle_client_connection: Invalid HTTP request: No CRLF found.\n");
    const char *bad_request = "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n";
    send(new_socket, bad_request, strlen(bad_request), 0);
    close(new_socket);
    return;
  }
  size_t request_line_len = first_line_end - buffer;
  if (request_line_len >= sizeof(request_line)) {
    fprintf(stderr, "[ERROR] handle_client_connection: Request line too long.\n");
    const char *bad_request = "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n";
    send(new_socket, bad_request, strlen(bad_request), 0);
    close(new_socket);
    return;
  }
  strncpy(request_line, buffer, request_line_len);
  request_line[request_line_len] = '\0'; 

  UrlParams request_params; 
  char *requested_path = parse_request_url(request_line, &request_params); 

  if (requested_path == NULL) {
    fprintf(stderr, "[ERROR] handle_client_connection: Could not parse request path. Sending 400 Bad Request.\n");
    const char *bad_request = "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n";
    send(new_socket, bad_request, strlen(bad_request), 0);
    close(new_socket);
    return;
  }
  printf("Accessing: %s\n", requested_path); 

  bool handled = false; 

  if (strncmp(requested_path, "/static/", strlen("/static/")) == 0) {

    if (serve_static_file(requested_path + strlen("/static/"))) {
      handled = true; 
    }
  }

  if (!handled) {
    int best_match_handler_index = -1; 
    size_t best_match_len = 0;         

    for (int i = 0; i < handler_count; i++) {
      size_t handler_path_len = strlen(handlers[i].path);

      if (strncmp(requested_path, handlers[i].path, handler_path_len) == 0) {

        if (handler_path_len == strlen(requested_path) ||
            requested_path[handler_path_len] == '/') {

          if (handler_path_len > best_match_len) {
            best_match_len = handler_path_len;
            best_match_handler_index = i;
          }
        }
      }
    }

    if (best_match_handler_index != -1) {
      handlers[best_match_handler_index].handler(&request_params);
      handled = true; 
    }
  }

  if (!handled) {
    fprintf(stderr,
            "[WARNING] handle_client_connection: No handler or static file found for path '%s'. Sending 404 Not Found.\n",
            requested_path);
    const char *not_found_html = "<h1>404 Not Found</h1><p>The requested URL "
                                 "was not located on this server.</p>";
    char not_found_response[BUFFER_SIZE];
    snprintf(not_found_response, sizeof(not_found_response),
             "HTTP/1.1 404 Not Found\r\n"
             "Content-Type: text/html; charset=UTF-8\r\n"
             "Content-Length: %zu\r\n"
             "Connection: close\r\n"
             "\r\n%s",
             strlen(not_found_html), not_found_html);
    send(new_socket, not_found_response, strlen(not_found_response), 0);
  }

  free(requested_path); 
  close(new_socket);    
  current_client_socket = -1; 
}

int beaker_run(const char *ip, int port) {
  int server_fd;            
  struct sockaddr_in address; 
  int addrlen = sizeof(address); 

  if (initialize_server_socket(ip, port, &server_fd, &address) != 0) {
    return -1; 
  }

  while (true) {
    int new_socket; 

    if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
                             (socklen_t *)&addrlen)) < 0) {
      perror("accept failed");
      fprintf(stderr, "[ERROR] beaker_run: Failed to accept connection.\n");
      continue; 
    }

    handle_client_connection(new_socket);
  }

  close(server_fd); 
  return 0;
}