From a9ada4cb2805783cc203fa35ad248f3b3dcc63e0 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Thu, 4 Aug 2022 14:45:16 +0200 Subject: [PATCH] CRLF macro --- srcs/Client.cpp | 2 +- srcs/utils.hpp | 4 ++++ srcs/webserv/response.cpp | 16 ++++++++-------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/srcs/Client.cpp b/srcs/Client.cpp index 88aba00..8de09f4 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -35,7 +35,7 @@ void Client::parse_request() std::vector list; size_t pos; - pos = (raw_request).find("\r\n\r\n"); + pos = (raw_request).find(CRLF CRLF); sub = (raw_request).substr(0, pos); list = split(sub, '\n'); // request_line diff --git a/srcs/utils.hpp b/srcs/utils.hpp index fa22d09..7933552 100644 --- a/srcs/utils.hpp +++ b/srcs/utils.hpp @@ -7,6 +7,10 @@ # include # include // atoi +# define CR "\r" +# define LF "\n" +# define CRLF CR LF + // enum http_method // { // UNKNOWN = 0b00000000, diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index 359a327..0a09f28 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -43,12 +43,12 @@ void Webserv::_send_response(Client *client, ServerConfig &server) void Webserv::_append_base_headers(Client *client) { - client->response.append("Server: Webserv/0.1\r\n"); + client->response.append("Server: Webserv/0.1" CRLF); if (client->get_headers("Connection") == "close") - client->response.append("Connection: close\r\n"); + client->response.append("Connection: close" CRLF); else - client->response.append("Connection: keep-alive\r\n"); + client->response.append("Connection: keep-alive" CRLF); } void Webserv::_construct_response(Client *client, ServerConfig &server) @@ -94,7 +94,7 @@ void Webserv::_process_method(Client *client, ServerConfig &server, LocationConf client->status = 405; client->response.append("Allow: "); client->response.append(::http_methods_to_str(allow_methods)); - client->response.append("\r\n"); + client->response.append(CRLF); } } @@ -104,7 +104,7 @@ void Webserv::_insert_status_line(Client *client) status_line.append("HTTP/1.1 "); status_line.append(_http_status[client->status]); - status_line.append("\r\n"); + status_line.append(CRLF); client->response.insert(0, status_line); } @@ -195,14 +195,14 @@ void Webserv::_append_body(Client *client, const char *body, size_t body_size) http://nginx.org/en/docs/http/ngx_http_core_module.html#types Need to look "conf/mime.types" of nginx. Maybe make a map<> based on that. */ - client->response.append("Content-Type: text/html; charset=UTF-8\r\n"); + client->response.append("Content-Type: text/html; charset=UTF-8" CRLF); client->response.append("Content-Length: "); std::string tmp = ::itos(body_size); client->response.append(tmp); - client->response.append("\r\n"); + client->response.append(CRLF); - client->response.append("\r\n"); + client->response.append(CRLF); client->response.append(body); }