<commit_before>/*
 * IPK.2017
 *
 * TCP communication with REST API.
 *
 * <NAME> (xmrvam01)
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include <iostream>
#include <dirent.h>
#include <string>
#include <vector>
#include <sys/stat.h>

using namespace std;

int makeDir(string rest_path);
int removeDir(string rest_path);
string lsDir(string rest_path);
int rmFile(string file_path);
string setServerHttpHeader(int response, string data, string error_string);
const string currentDateTime();

void parseClientHeader(char *buff, int comm_socket, string root_path);
vector<string> split_string(const string& str, const string& delimiter);
int isInString(string s1, string s2);


int main (int argc, const char * argv[]) {
	int welcome_socket;
	struct sockaddr_in6 sa;
	struct sockaddr_in6 sa_client;
	char str[INET6_ADDRSTRLEN];
    int port_number;

    string root_path = "";

	if (argc == 1) {
		port_number = 6677;
		root_path = "";
	}
	else if (argc == 3){
		// have some root path on first place
		if (string(argv[1]) == "-r"){
			port_number = 6677;
			root_path = argv[2];
		}
		else if(string(argv[1]) == "-p") {
			port_number = atoi(argv[2]);
			root_path = "";
		}
		else {
			fprintf(stderr, "Unknown error.\n");
			exit(EXIT_FAILURE);
		}
	}
	else if (argc == 5){
		root_path = argv[2];
		port_number = atoi(argv[4]);
	}
	else {
		fprintf(stderr, "Unknown error.\n");
		exit(EXIT_FAILURE);
	}

	char *cstr = &root_path.back();
	if (strcmp(cstr, "/") == 0) {
		root_path.erase(root_path.end() - 1);
	}
    
	socklen_t sa_client_len=sizeof(sa_client);
	if ((welcome_socket = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
	{
		perror("ERROR: socket");
		exit(EXIT_FAILURE);
	}
	
	memset(&sa,0,sizeof(sa));
	sa.sin6_family = AF_INET6;
	sa.sin6_addr = in6addr_any;
	sa.sin6_port = htons(port_number);	

	if ((::bind(welcome_socket, (struct sockaddr*)&sa, sizeof(sa))) < 0)
	{
		perror("ERROR: bind");
		exit(EXIT_FAILURE);
	}
	if ((listen(welcome_socket, 1)) < 0)
	{
		perror("ERROR: listen");
		exit(EXIT_FAILURE);				
	}
	while(1)
	{
		int comm_socket = accept(welcome_socket, (struct sockaddr*)&sa_client, &sa_client_len);		
		if (comm_socket > 0)
		{
			inet_ntop(AF_INET6, &sa_client.sin6_addr, str, sizeof(str));
//				printf("INFO: New connection:\n");
//				printf("INFO: Client address is %s\n", str);
//				printf("INFO: Client port is %d\n", ntohs(sa_client.sin6_port));
//			}

			char buff[1024];
			recv(comm_socket, buff, 1024,0);
			parseClientHeader(buff, comm_socket, root_path);
		}

		//fprintf(stderr, "Connection to closed\n");
		close(comm_socket);
	}	
}

void parseClientHeader(char *buff, int comm_socket, string root_path){
	//cout << "[SERVER] toto parsujem" << endl;

	// convert char * to string
	string Str = string(buff);
	//cout << Str;

	// create list of header attributes (vector)
	vector<string> list_header_attributes = {};

	// push each splited value as one item of vector
	auto strings = split_string(Str, "\n");

	for (auto itr = strings.begin(); itr != strings.end(); itr++){
		list_header_attributes.push_back(*itr);
	}

	string rest_command = list_header_attributes.at(0);
	string host = list_header_attributes.at(1);
	string date = list_header_attributes.at(2);
	/*string accept = list_header_attributes.at(3);
	string accept_encoding = list_header_attributes.at(4);
	string content_type = list_header_attributes.at(5);
	string content_length = list_header_attributes.at(6);*/
<commit_msg>removed exits from server<commit_after>/*
 * IPK.2017
 *
 * TCP communication with REST API.
 *
 * <NAME> (xmrvam01)
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include <iostream>
#include <dirent.h>
#include <string>
#include <vector>
#include <sys/stat.h>

using namespace std;

int makeDir(string rest_path);
int removeDir(string rest_path);
string lsDir(string rest_path);
int rmFile(string file_path);
string setServerHttpHeader(int response, string data, string error_string);
const string currentDateTime();

void parseClientHeader(char *buff, int comm_socket, string root_path);
vector<string> split_string(const string& str, const string& delimiter);
int isInString(string s1, string s2);


int main (int argc, const char * argv[]) {
	int welcome_socket;
	struct sockaddr_in6 sa;
	struct sockaddr_in6 sa_client;
	char str[INET6_ADDRSTRLEN];
    int port_number;

    string root_path = "";

	if (argc == 1) {
		port_number = 6677;
		root_path = "";
	}
	else if (argc == 3){
		// have some root path on first place
		if (string(argv[1]) == "-r"){
			port_number = 6677;
			root_path = argv[2];
		}
		else if(string(argv[1]) == "-p") {
			port_number = atoi(argv[2]);
			root_path = "";
		}
		else {
			fprintf(stderr, "Unknown error.\n");
			//exit(//exit_FAILURE);
		}
	}
	else if (argc == 5){
		root_path = argv[2];
		port_number = atoi(argv[4]);
	}
	else {
		fprintf(stderr, "Unknown error.\n");
		//exit(//exit_FAILURE);
	}

	char *cstr = &root_path.back();
	if (strcmp(cstr, "/") == 0) {
		root_path.erase(root_path.end() - 1);
	}

	socklen_t sa_client_len=sizeof(sa_client);
	if ((welcome_socket = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
	{
		perror("ERROR: socket");
		//exit(//exit_FAILURE);
	}

	memset(&sa,0,sizeof(sa));
	sa.sin6_family = AF_INET6;
	sa.sin6_addr = in6addr_any;
	sa.sin6_port = htons(port_number);

	if ((::bind(welcome_socket, (struct sockaddr*)&sa, sizeof(sa))) < 0)
	{
		perror("ERROR: bind");
		//exit(//exit_FAILURE);
	}
	if ((listen(welcome_socket, 1)) < 0)
	{
		perror("ERROR: listen");
		//exit(EXIT_FAILURE);
	}
	while(1)
	{
		int comm_socket = accept(welcome_socket, (struct sockaddr*)&sa_client, &sa_client_len);
		if (comm_socket > 0)
		{
			inet_ntop(AF_INET6, &sa_client.sin6_addr, str, sizeof(str));
//				printf("INFO: New connection:\n");
//				printf("INFO: Client address is %s\n", str);
//				printf("INFO: Client port is %d\n", ntohs(sa_client.sin6_port));
//			}

			char buff[1024];
			recv(comm_socket, buff, 1024,0);
			parseClientHeader(buff, comm_socket, root_path);
		}

		//fprintf(stderr, "Connection to closed\n");
		close(comm_socket);
	}
}

void parseClientHeader(char *buff, int comm_socket, string root_path){
	//cout << "[SERVER] toto parsujem" << endl;

	// convert char * to string
	string Str = string(buff);
	//cout << Str;

	// create list of header attributes (vector)
	vector<string> list_header_attributes = {};

	// push each splited value as one item of vector
	auto strings = split_string(Str, "\n");

	for (auto itr = strings.begin(); itr != strings.end(); itr++){
		list_header_attributes.push_back(*itr);
	}

	string rest_command = list_header_attributes.at(0);
	string host = list_header_attributes.at(1);
	string date = list_header_attributes.at(2);
	/*string accept = list_header_attributes.at(3);
	string accept_encoding = list_header_attributes.at(4);
	string content_type = list_header_attributes.at(5);
	string content_length = list_header_attributes.at(6);*/

{'fraction_non_alphanumeric': 0.15807848065912583, 'fraction_numerical': 0.017595307917888565, 'mean_word_length': 4.389014296463507, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '282626', 'n_tokens_mistral': 2858, 'n_tokens_neox': 2689, 'n_words': 609}