add linux version
This commit is contained in:
parent
4c89da26c0
commit
816fcf8833
@ -1,2 +1,8 @@
|
|||||||
default_target:
|
default_target:
|
||||||
g++ socket_up.cpp socket_handler.cpp routes.cpp main.cpp -o main -lws2_32 && main.exe
|
g++ socket_up.cpp socket_handler.cpp routes.cpp main.cpp -o main.exe -lws2_32 && main.exe
|
||||||
|
|
||||||
|
win:
|
||||||
|
g++ socket_up.cpp socket_handler.cpp routes.cpp main.cpp -o main.exe -lws2_32 && main.exe
|
||||||
|
|
||||||
|
unix:
|
||||||
|
g++ socket_up.cpp socket_handler.cpp routes.cpp main.cpp -o main && ./main
|
@ -1,57 +1,19 @@
|
|||||||
#include "socket_up.h"
|
#include "socket_up.h"
|
||||||
#include "socket_handler.h"
|
#include "socket_handler.h"
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
|
|
||||||
#include <iterator>
|
|
||||||
#include <regex>
|
|
||||||
|
|
||||||
|
|
||||||
struct request_info {
|
|
||||||
std::string method, path, params;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::string get_string_from_regex(std::string text, std::regex pattern) {
|
|
||||||
auto parts_begin = std::sregex_iterator(
|
|
||||||
text.begin(),
|
|
||||||
text.end(),
|
|
||||||
pattern
|
|
||||||
);
|
|
||||||
auto parts_end = std::sregex_iterator();
|
|
||||||
if (std::distance(parts_begin, parts_end)) {
|
|
||||||
std::smatch part_match = *parts_begin;
|
|
||||||
return part_match.str();
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
struct request_info parse_request(const std::string& request) {
|
|
||||||
std::regex request_line_regex(
|
|
||||||
"(GET|POST|PUT|DELETE) .* HTTP/",
|
|
||||||
std::regex_constants::ECMAScript | std::regex_constants::icase
|
|
||||||
);
|
|
||||||
std::string request_line = get_string_from_regex(request, request_line_regex);
|
|
||||||
|
|
||||||
std::regex method_regex("^[A-Z]+");
|
|
||||||
std::regex path_regex("/[^? ]*");
|
|
||||||
std::regex params_regex(R"(\?[^ ]*)");
|
|
||||||
|
|
||||||
struct request_info this_request;
|
|
||||||
|
|
||||||
this_request.method = get_string_from_regex(request_line, method_regex);
|
|
||||||
this_request.path = get_string_from_regex(request_line, path_regex);
|
|
||||||
this_request.params = get_string_from_regex(request_line, params_regex);
|
|
||||||
|
|
||||||
return this_request;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const int BUFFER_SIZE = 2048;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
#ifdef _WIN32
|
||||||
|
SOCKET listen_socket = SocketUp::config_socket_windows();
|
||||||
|
SocketHandler::working_with_client_windows(listen_socket);
|
||||||
|
closesocket(listen_socket);
|
||||||
|
WSACleanup();
|
||||||
|
#else
|
||||||
|
int server_socket = SocketUp::config_socket_unix();
|
||||||
|
SocketHandler::working_with_client_unix(server_socket);
|
||||||
|
close(server_socket);
|
||||||
|
#endif
|
||||||
|
|
||||||
SOCKET listenSocket = SocketUp::config_socket_windows();
|
|
||||||
SocketHandler::working_with_client_windows(listenSocket);
|
|
||||||
|
|
||||||
closesocket(listenSocket);
|
|
||||||
WSACleanup();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -17,7 +17,9 @@ std::string RouteHandler::get_string_from_regex(std::string text, std::regex pat
|
|||||||
struct request_info RouteHandler::parse_request(const std::string& request) {
|
struct request_info RouteHandler::parse_request(const std::string& request) {
|
||||||
struct request_info this_request;
|
struct request_info this_request;
|
||||||
|
|
||||||
std::regex request_line_regex("(GET|POST|PUT|DELETE) .* HTTP/");
|
std::regex request_line_regex(
|
||||||
|
"(GET|POST|PUT|DELETE|HEAD|TRACE|OPTIONS|CONNECT) .* HTTP/"
|
||||||
|
);
|
||||||
std::regex method_regex("^[A-Z]+");
|
std::regex method_regex("^[A-Z]+");
|
||||||
std::regex path_regex("/[^? ]*");
|
std::regex path_regex("/[^? ]*");
|
||||||
std::regex params_regex(R"(\?[^ ]*)");
|
std::regex params_regex(R"(\?[^ ]*)");
|
||||||
|
@ -6,13 +6,13 @@
|
|||||||
const int BUFFER_SIZE = 4096;
|
const int BUFFER_SIZE = 4096;
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
int SocketHandler::working_with_client_windows(SOCKET listenSocket) {
|
int SocketHandler::working_with_client_windows(SOCKET listen_socket) {
|
||||||
while (1) {
|
while (1) {
|
||||||
// 6. Принятие входящего соединения
|
// 6. Принятие входящего соединения
|
||||||
sockaddr_in clientAddr;
|
sockaddr_in clientAddr;
|
||||||
int clientAddrSize = sizeof(clientAddr);
|
int clientAddrSize = sizeof(clientAddr);
|
||||||
SOCKET clientSocket = accept(listenSocket, (struct sockaddr*)&clientAddr, &clientAddrSize);
|
SOCKET clientSocket = accept(listen_socket, (struct sockaddr*)&clientAddr, &clientAddrSize);
|
||||||
if (clientSocket == INVALID_SOCKET) {
|
if (clientSocket == INVALID_SOCKET) {
|
||||||
std::cerr << "Accept failed with error: " << WSAGetLastError() << "\n";
|
std::cerr << "Accept failed with error: " << WSAGetLastError() << "\n";
|
||||||
continue;
|
continue;
|
||||||
@ -38,17 +38,36 @@ int SocketHandler::working_with_client_windows(SOCKET listenSocket) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
|
||||||
int SocketHandler::working_with_client_unix() {
|
int SocketHandler::working_with_client_unix(int listen_socket) {
|
||||||
return 0;
|
while (1) {
|
||||||
|
// Принятие соединения
|
||||||
|
sockaddr_in clientAddr;
|
||||||
|
socklen_t clientAddrSize = sizeof(clientAddr);
|
||||||
|
int clientSocket = accept(listen_socket, (struct sockaddr*)&clientAddr, &clientAddrSize);
|
||||||
|
if (clientSocket == -1) {
|
||||||
|
perror("Accept failed");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
std::cout << "Client connected.\n";
|
||||||
|
|
||||||
|
// Чтение данных
|
||||||
|
char buffer[BUFFER_SIZE] = {0};
|
||||||
|
ssize_t bytesReceived = read(clientSocket, buffer, BUFFER_SIZE);
|
||||||
|
if (bytesReceived > 0) {
|
||||||
|
std::string request(buffer, bytesReceived);
|
||||||
|
std::cout << "Received request:\n" << request << "\n";
|
||||||
|
|
||||||
|
std::string response = RouteHandler::get_response_by_request(request);
|
||||||
|
std::cout << "Response:\n" << response << "\n\n";
|
||||||
|
|
||||||
|
// Отправка ответа
|
||||||
|
send(clientSocket, response.c_str(), response.size(), 0);
|
||||||
|
}
|
||||||
|
close(clientSocket);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,6 +5,11 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#else
|
#else
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <netinet/ip.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <cstring>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@ -13,7 +18,10 @@
|
|||||||
|
|
||||||
class SocketHandler {
|
class SocketHandler {
|
||||||
public:
|
public:
|
||||||
static int working_with_client_windows(SOCKET listenSocket);
|
#ifdef _WIN32
|
||||||
static int working_with_client_unix();
|
static int working_with_client_windows(SOCKET listen_socket);
|
||||||
|
#else
|
||||||
|
static int working_with_client_unix(int listen_socket);
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "socket_up.h"
|
#include "socket_up.h"
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
// // Линковка библиотеки Winsock
|
// // Линковка библиотеки Winsock
|
||||||
// #pragma comment(lib, "ws2_32.lib")
|
// #pragma comment(lib, "ws2_32.lib")
|
||||||
// don't need but I think may shoot
|
// don't need but I think may shoot
|
||||||
@ -15,8 +15,8 @@ SOCKET SocketUp::config_socket_windows() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2. Создание сокета
|
// 2. Создание сокета
|
||||||
SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, 0);
|
SOCKET listen_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (listenSocket == INVALID_SOCKET) {
|
if (listen_socket == INVALID_SOCKET) {
|
||||||
std::cerr << "Socket creation failed with error: " << WSAGetLastError() << "\n";
|
std::cerr << "Socket creation failed with error: " << WSAGetLastError() << "\n";
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
return 1;
|
return 1;
|
||||||
@ -30,36 +30,64 @@ SOCKET SocketUp::config_socket_windows() {
|
|||||||
serverAddr.sin_addr.s_addr = INADDR_ANY; // Привязка к любому интерфейсу
|
serverAddr.sin_addr.s_addr = INADDR_ANY; // Привязка к любому интерфейсу
|
||||||
|
|
||||||
// 4. Привязка сокета к адресу
|
// 4. Привязка сокета к адресу
|
||||||
if (bind(listenSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
|
if (bind(listen_socket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
|
||||||
std::cerr << "Bind failed with error: " << WSAGetLastError() << "\n";
|
std::cerr << "Bind failed with error: " << WSAGetLastError() << "\n";
|
||||||
closesocket(listenSocket);
|
closesocket(listen_socket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Перевод сокета в режим прослушивания
|
// 5. Перевод сокета в режим прослушивания
|
||||||
if (listen(listenSocket, SOMAXCONN) == SOCKET_ERROR) {
|
if (listen(listen_socket, SOMAXCONN) == SOCKET_ERROR) {
|
||||||
std::cerr << "Listen failed with error: " << WSAGetLastError() << "\n";
|
std::cerr << "Listen failed with error: " << WSAGetLastError() << "\n";
|
||||||
closesocket(listenSocket);
|
closesocket(listen_socket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
std::cout << "Server is listening on port 8080...\n";
|
std::cout << "Server is listening on port 8080...\n";
|
||||||
|
|
||||||
|
|
||||||
return listenSocket;
|
return listen_socket;
|
||||||
|
|
||||||
// 8. Закрытие клиентского сокета
|
// 8. Закрытие клиентского сокета
|
||||||
// closesocket(clientSocket);
|
// closesocket(clientSocket);
|
||||||
|
|
||||||
// // 9. Закрытие серверного сокета и завершение Winsock
|
// // 9. Закрытие серверного сокета и завершение Winsock
|
||||||
// closesocket(listenSocket);
|
// closesocket(listen_socket);
|
||||||
// WSACleanup();
|
// WSACleanup();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
int SocketUp::config_socket_unix() {
|
int SocketUp::config_socket_unix() {
|
||||||
return 0;
|
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (server_socket == -1) {
|
||||||
|
perror("server socket can't be created");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
sockaddr_in serverAddr;
|
||||||
|
memset(&serverAddr, 0, sizeof(serverAddr));
|
||||||
|
serverAddr.sin_family = AF_INET;
|
||||||
|
serverAddr.sin_port = htons(8080); // Порт для привязки
|
||||||
|
serverAddr.sin_addr.s_addr = INADDR_ANY; // Привязка к любому интерфейсу
|
||||||
|
|
||||||
|
if (bind(server_socket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == -1) {
|
||||||
|
perror("Bind failed");
|
||||||
|
close(server_socket);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (listen(server_socket, SOMAXCONN) == -1) {
|
||||||
|
perror("Listen failed");
|
||||||
|
close(server_socket);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
std::cout << "Server is listening on port 8080...\n";
|
||||||
|
|
||||||
|
return server_socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -5,6 +5,11 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#else
|
#else
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <netinet/ip.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <cstring>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@ -13,8 +18,10 @@
|
|||||||
|
|
||||||
class SocketUp {
|
class SocketUp {
|
||||||
public:
|
public:
|
||||||
|
#ifdef _WIN32
|
||||||
static SOCKET config_socket_windows();
|
static SOCKET config_socket_windows();
|
||||||
static int config_socket_unix();
|
#else
|
||||||
|
static int config_socket_unix();
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user