Skip to content

Commit b7e6354

Browse files
committed
Improve method logic & remove deprecated macros
1 parent e63786e commit b7e6354

File tree

1 file changed

+22
-52
lines changed

1 file changed

+22
-52
lines changed

single-HTTP/main.c

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -51,48 +51,36 @@
5151
#define HTTP_REQ_AMT 8
5252
#define REQLINE_TOKEN_AMT 4
5353

54-
#define MSG_LEN 4096
55-
#define PORT_LEN 5
56-
#define GET_REQ_LEN 3
57-
#define PHP_EXT_LEN 4
58-
#define REQLINE_LEN 1024
59-
#define CONF_EXT_LEN 5
60-
#define HTTP_VER_LEN 8
54+
#define GET_METHOD_LEN 3
55+
#define POST_METHOD_LEN 4
56+
6157
#define CODE_200_LEN 17
6258
#define CODE_400_LEN 26
6359
#define CODE_403_LEN 24
6460
#define CODE_404_LEN 24
6561
#define CODE_500_LEN 36
6662
#defineCODE_501_LEN 30
6763
#define CODE_505_LEN 41
68-
#define HTTP_METHOD_LEN 7
69-
#define DEFAULT_PAGE_LEN 22
70-
#define MDEFAULT_PAGE_LEN 2
64+
65+
#define MSG_LEN 4096
66+
#define PORT_LEN 5
67+
#define PHP_EXT_LEN 4
68+
#define REQLINE_LEN 1024
69+
#define CONF_EXT_LEN 5
70+
#define HTTP_VER_LEN 8
7171
#define CONNECTION_TEMPLATE_LEN 28
72-
#define IMPLEMENTED_HTTP_METHODS_LEN 2
72+
73+
#define BASE_TEN 10
74+
#define BASE_SIXTEEN 16
7375

7476
S_Ll _paths;
7577
char _port[PORT_LEN] = DEFAULT_PORT,
7678
_doc_root[PATH_MAX] = DEFAULT_ROOT;
7779

7880
bool _sigint_flag = true;
7981

80-
typedef enum {
81-
UNKNOWN,
82-
GET,
83-
POST
84-
} _methods; // Worth typedeffing?
85-
86-
static short get_method_type(const String restrict method) {
87-
if (strncmp("GET", method, 3) == 0)
88-
return GET;
89-
else if (strncmp("POST", method, 4) == 0)
90-
return POST;
91-
return UNKNOWN;
92-
}
93-
9482
static bool is_valid_port(void) { // Done
95-
const int port_num = atoi(_port);
83+
const int port_num = strtol(_port, NULL, BASE_TEN);
9684

9785
return ((PORT_MIN < port_num) && (port_num < PORT_MAX));
9886
}
@@ -313,7 +301,7 @@ static char decode_http_post(HashTable *ht, const String restrict data) {
313301
if (key[i] == '%') {
314302
tmp5[0] = key[i + 1];
315303
tmp5[1] = key[i + 2];
316-
tmp3[k] = decode_http_code(strtol(tmp5, NULL, 16));
304+
tmp3[k] = decode_http_code(strtol(tmp5, NULL, BASE_SIXTEEN));
317305
i += 2;
318306
continue;
319307
}
@@ -325,7 +313,7 @@ static char decode_http_post(HashTable *ht, const String restrict data) {
325313
if (value[i] == '%') {
326314
tmp5[0] = value[i + 1];
327315
tmp5[1] = value[i + 2];
328-
tmp4[k] = decode_http_code(strtol(tmp5, NULL, 16));
316+
tmp4[k] = decode_http_code(strtol(tmp5, NULL, BASE_SIXTEEN));
329317
i += 2;
330318
continue;
331319
}
@@ -517,34 +505,16 @@ static void respond(const int client_fd, String *const reqlines, const String pa
517505
return;
518506
}
519507

520-
const String restrict implemented_http_methods[IMPLEMENTED_HTTP_METHODS_LEN] = {"GET", "POST"};
521-
bool in = false;
522-
523-
for (unsigned int i = 0; i < IMPLEMENTED_HTTP_METHODS_LEN; i++)
524-
if (strncmp(reqlines[0], implemented_http_methods[i], HTTP_METHOD_LEN) == 0) {
525-
in = true;
526-
break;
527-
}
528-
529-
if (!in) {
508+
if (strncmp("GET", reqlines[0], GET_METHOD_LEN) == 0)
509+
serve_get_request(client_fd, reqlines, path);
510+
else if (strncmp("POST", reqlines[0], POST_METHOD_LEN) == 0)
511+
process_post_request(client_fd, reqlines, path);
512+
else {
530513
if (_verbose_flag)
531514
printf("%s %s [501 Not Implemented]\n", reqlines[0], reqlines[1]);
532515
send(client_fd, NOT_IMPLEMENTED, CODE_501_LEN, 0);
533516
send_file(client_fd, "partials/code-responses/501.html");
534-
return;
535517
}
536-
537-
const short method = get_method_type(reqlines[0]);
538-
539-
switch (method) {
540-
case GET:
541-
serve_get_request(client_fd, reqlines, path);
542-
break;
543-
case POST:
544-
process_post_request(client_fd, reqlines, path);
545-
break;
546-
}
547-
return;
548518
}
549519

550520
static String *get_req_lines(String msg) { // Done
@@ -842,7 +812,7 @@ int main(const int argc, String *const argv) {
842812

843813
if (recv(newfd, msg, MSG_LEN, 0) > 0) {
844814
if (_verbose_flag)
845-
printf(CYAN "MSG: %s\n" RESET, msg);
815+
printf(CYAN "MSG:\n%s\n" RESET, msg);
846816
process_request(newfd, msg, ipv6_address);
847817
}
848818
else {

0 commit comments

Comments
 (0)