Здравствуйте. Есть код небольшого backend сервера. Запросы приходят все нормально. Есть 2 вопроса. 1 - почему при обращению к домену не сразу приходят cookie(в логах будет видно)? 2 - почему после 1-3к запросов происходит ошибка сегментации? я не могу понять. Уже отладочную информацию выводил. Все равно вызывалась. Вот код:
Код:
//Стандартные библиотеки
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <thread>
//Классы
#include </etc/local/c++/server.class.cpp> //Класс сервера и инициализация объекта
#include </etc/local/c++/client.class.cpp> //Класс клиента
void Server::handler(int sock){
cout<<"Client \t";
Client client(sock);
}
using namespace std;
int main(){
return 0;
}
server.class.cpp
Код:
//Стандартные библиотеки
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <iostream>
using namespace std;
class Server{
public:
int sock;
static const unsigned short int count=50;
thread flows[Server::count];
Server(){
//Конфигурация
config.sin_family=AF_INET;
config.sin_port=htons(8080);
config.sin_addr.s_addr=inet_addr("127.0.0.5");
//Инициалзиация
sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int value=1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(int));
bind(sock, (struct sockaddr *)&config, sizeof(config));
listen(sock, 128);
for(int i=0; i<Server::count; i++){
flows[i]=thread(threads, sock);
}
}
~Server(){
for(int i=0; i<Server::count; i++){
flows[i].join();
}
shutdown(sock, 2);
close(sock);
}
static void threads(int sock){
listen(sock, 128);
int client_sock;
struct sockaddr_in config;
socklen_t *size=new socklen_t(sizeof(config));
thread potok[30];
unsigned short int j=0;
while(client_sock=accept(sock, (struct sockaddr *)&config, size)){
j=rand()%30;
potok[j]=thread(Server::handler, client_sock);
potok[j].detach();
}
delete size;
}
static void handler(int sock);
private:
struct sockaddr_in config;
} server;
client.class.cpp
Код:
//Стандартные библиотеки
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <iostream>
using namespace std;
class Client{
public:
//Информация о клинете
char *ip;
char *method;
char *path;
string host;
string cookie;
Client(int sock){
this->sock=sock;
read();
header_parse();
if(strcmp(method,"GET")==0){
//get
}else if(strcmp(method,"POST")==0){
//post
}else{
status="501";
state_name="Not Implemented";
}
cout<<host.c_str()<<'\t'<<cookie.c_str()<<endl;
}
~Client(){
delete request;
response_headers="HTTP/1.1 "+status+' '+state_name+"\r\n"
"Date: Wed, 09 Aug 2017 19:33:44 GMT\r\n"
"Server: LocalServer/1.0 (Ubuntu)\r\n"
"CVary: Accept-Encoding\r\n"
"Connection: Keep-aleave\r\n"
"Content-Type: text/html; charset=UTF-8\r\n";
response=response_headers+"\r\n"+response_body;
send(sock, response.c_str(), response.size(), 0);
shutdown(sock, 2);
close(sock);
}
void read(){
recv(sock, request, 1024, 0);
const unsigned short int len=strlen(request)-2;
char *part;
int i;
for(i=0; i<len; i++){
if(request[i]=='\r'){
headers_lenght++;
i++;
}
}
request_headers=new char*[headers_lenght];
part=strtok(request, "\r\n");
for(i=0; i<headers_lenght; i++){
request_headers[i]=part;
part=strtok(NULL, "\r\n");
}
}
void header_parse(){
method=strtok(request_headers[0], " ");
path=strtok(NULL, " ");
for(int i=1; i<headers_lenght-1; i++){
switch(request_headers[i][0]){
case 'H':
if(request_headers[i][1]=='o' && request_headers[i][2]=='s' && request_headers[i][3]=='t'){
host=request_headers[i]+6;
}
break;
case 'C':
if(request_headers[i][1]=='o' && request_headers[i][2]=='o'){
if(request_headers[i][3]=='k' && request_headers[i][4]=='i' && request_headers[i][5]=='e'){
cookie=request_headers[i]+8;
}
}
break;
}
}
}
void operator<<(char information[]){
response_body+=information;
}
void operator<<(const char information[]){
response_body+=information;
}
private:
int sock;
string status="200";
string state_name="OK";
//Данные из сокета
char *request=new char[1024];
char **request_headers;
unsigned short int headers_lenght=0;
string response_headers, response_body="<DOCTYPE html><html lang=\"ru\"><head><meta http-equiv=\"refresh\" content=\"0; url=/\"><title></title></head><body><h1>Hello world!</h1></body>", response;
};
Далее приведу некоторые логи.
Цитата:
root@server:/etc/local/c++# ./index
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru
Client js.%domain%.ru account=1; type=user; check_account=224006
Client js.%domain%.ru account=1; type=user; check_account=224006
Client js.%domain%.ru account=1; type=user; check_account=224006
Client js.%domain%.ru
Client js.%domain%.ru account=1; type=user; check_account=224006
Client js.%domain%.ru account=1; type=user; check_account=224006
Client js.%domain%.ru account=1; type=user; check_account=224006
Client js.%domain%.ru account=1; type=user; check_account=224006
Client js.%domain%.ru account=1; type=user; check_account=224006
|
почему первые запросы к под домену приходят без cookie? и от чего сегментация?