<commit_before>/**
 * Yet Another Completely Useless Web Server
 *
 *
 */

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

#include <magic.h>

#include "utstring.h"

int PORT;
#define BUFSIZE 8192

#define DEBUG 0

#define STR(x) #x
#define TOSTR(x) STR(x)
#define dprint_str(str) if (DEBUG) printf(__FILE__ ":" TOSTR(__LINE__) "> " TOSTR(str) \
" = '%s'\n", str)

#define dprint_int(str) if (DEBUG) printf(__FILE__ ":" TOSTR(__LINE__) "> " TOSTR(str) \
" = %d\n", str)

#define dprint_char(str) if(DEBUG) printf(__FILE__ ":" TOSTR(__LINE__) "> " TOSTR(str) \
" = %c\n", str)



#define log_error(x) _log(x, 1)
#define log_debug(x) _log(x, 0)

const char* NOT_FOUND_NOT_FOUND = "HTTP/1.1 418 I'm a teapot (RFC 2324)\r\n\r\n<h1>Something is seriously wrong</h1><br>Even the file (./htdocs/404.html) with 'Not found' message was not found (and I might be a <a href='http://www.error418.org/'>teapot</a>).";

magic_t magic_cookie;

void _log(char *str, int err)
{
        if (err) {
                printf("yacuws: %s\n", str);
                perror(strerror(errno));
                exit(-1);
        } else {
                printf("=> yacuws: %s\n", str);
        }
}


/* A function that correctly sends given amount of data froma buffer
 * to a socket */
int send_buffer(int fd, const char* buffer, int len) {
        int i = 0, r;
        while (i < len) {
                r = send(fd, buffer + i, len - i, MSG_NOSIGNAL);
                if (r == -1) {
                        return -1;
                }
                i += r;
        }

        return 0;

}

/* A method that correctly closes given socket.*/
void close_socket(int fd) {
        char tmp[100];
        if (shutdown(fd, SHUT_RDWR) == -1) {
                log_error("Error closing socket (shutdown)");
        }
        while(read(fd, tmp, 100) > 0)
                ;

        if (close(fd)) {
                log_error("Error closing socket (close)");
        }
}

UT_string* build_dir_listing(char* directory)
{

        UT_string *out;
        char path[BUFSIZE];
        DIR *dir;
        struct dirent *ent;
        struct stat statbuf;

        utstring_new(out);
        utstring_printf(out, "<html><head><title>Dir listing of %s</title></head><body><h1>Dir listing of %s</h1><ul>", directory, directory);

        if ((dir = opendir (directory)) != NULL) {
                while ((ent = readdir (dir)) != NULL) {<commit_msg>typo fix<commit_after>/**
 * Yet Another Completely Useless Web Server
 *
 *
 */

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

#include <magic.h>

#include "utstring.h"

int PORT;
#define BUFSIZE 8192

#define DEBUG 0

#define STR(x) #x
#define TOSTR(x) STR(x)
#define dprint_str(str) if (DEBUG) printf(__FILE__ ":" TOSTR(__LINE__) "> " TOSTR(str) \
" = '%s'\n", str)

#define dprint_int(str) if (DEBUG) printf(__FILE__ ":" TOSTR(__LINE__) "> " TOSTR(str) \
" = %d\n", str)

#define dprint_char(str) if(DEBUG) printf(__FILE__ ":" TOSTR(__LINE__) "> " TOSTR(str) \
" = %c\n", str)



#define log_error(x) _log(x, 1)
#define log_debug(x) _log(x, 0)

const char* NOT_FOUND_NOT_FOUND = "HTTP/1.1 418 I'm a teapot (RFC 2324)\r\n\r\n<h1>Something is seriously wrong</h1><br>Even the file (./htdocs/404.html) with 'Not found' message was not found (and I might be a <a href='http://www.error418.org/'>teapot</a>).";

magic_t magic_cookie;

void _log(char *str, int err)
{
        if (err) {
                printf("yacuws: %s\n", str);
                perror(strerror(errno));
                exit(-1);
        } else {
                printf("=> yacuws: %s\n", str);
        }
}


/* A function that correctly sends given amount of data froma buffer
 * to a socket */
int send_buffer(int fd, const char* buffer, int len) {
        int i = 0, r;
        while (i < len) {
                r = send(fd, buffer + i, len - i, MSG_NOSIGNAL);
                if (r == -1) {
                        return -1;
                }
                i += r;
        }

        return 0;

}

/* A method that correctly closes given socket.*/
void close_socket(int fd) {
        char tmp[100];
        if (shutdown(fd, SHUT_WR) == -1) {
                log_error("Error closing socket (shutdown)");
        }
        while(read(fd, tmp, 100) > 0)
                ;

        if (close(fd)) {
                log_error("Error closing socket (close)");
        }
}

UT_string* build_dir_listing(char* directory)
{

        UT_string *out;
        char path[BUFSIZE];
        DIR *dir;
        struct dirent *ent;
        struct stat statbuf;

        utstring_new(out);
        utstring_printf(out, "<html><head><title>Dir listing of %s</title></head><body><h1>Dir listing of %s</h1><ul>", directory, directory);

        if ((dir = opendir (directory)) != NULL) {
                while ((ent = readdir (dir)) != NULL) {
{'fraction_non_alphanumeric': 0.16069031639501438, 'fraction_numerical': 0.0149568552253116, 'mean_word_length': 2.352185089974293, 'pattern_counts': {'":': 6, '<': 59, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1576053', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1853, 'n_words': 487}