<commit_before>#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#include <libsoup/soup.h>
#include <glib.h>
#include <glib/gstdio.h>

#include "teleport-server.h"
#include "teleport-get.h"
#include "teleport-app.h"

static int port;
static SoupServer *glob_server;
//static const char *tls_cert_file, *tls_key_file;

static int
compare_strings (gconstpointer a, gconstpointer b)
{
  const char **sa = (const char **)a;
  const char **sb = (const char **)b;

  return strcmp (*sa, *sb);
}

static GString *
get_directory_listing (const char *path)
{
  GPtrArray *entries;
  GString *listing;
  char *escaped;
  GDir *dir;
  const gchar *d_name;
  int i;

  entries = g_ptr_array_new ();
  dir = g_dir_open (path, 0, NULL);
  if (dir) {
    while ((d_name = g_dir_read_name (dir))) {
      if (!strcmp (d_name, ".") ||
          (!strcmp (d_name, "..") &&
           !strcmp (path, "./")))
        continue;
      escaped = g_markup_escape_text (d_name, -1);
      g_ptr_array_add (entries, escaped);
    }
    g_dir_close (dir);
  }

  g_ptr_array_sort (entries, (GCompareFunc)compare_strings);

  listing = g_string_new ("<html>\r\n");
  escaped = g_markup_escape_text (strchr (path, '/'), -1);
  g_string_append_printf (listing, "<head><title>Index of %s</title></head>\r\n", escaped);
  g_string_append_printf (listing, "<body><h1>Index of %s</h1>\r\n<p>\r\n", escaped);
  g_free (escaped);
  for (i = 0; i < entries->len; i++) {
    g_string_append_printf (listing, "<a href=\"%s\">%s</a><br>\r\n",
                            (char *)entries->pdata[i], 
                            (char *)entries->pdata[i]);
    g_free (entries->pdata[i]);
  }
  g_string_append (listing, "</body>\r\n</html>\r\n");

  g_ptr_array_free (entries, TRUE);
  return listing;
}

static void
do_get (SoupServer *server, SoupMessage *msg, const char *path)
{
  char *slash;
  GStatBuf st;

  printf("paths: %s", path);
  if (g_stat (path, &st) == -1) {
    if (errno == EPERM)
      soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
    else if (errno == ENOENT)
      soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
    else
      soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    return;
  }

  if (g_file_test (path, G_FILE_TEST_IS_DIR)) {
    GString *listing;
    char *index_path;

    slash = strrchr (path, '/');
    if (!slash || slash[1]) {
      char *redir_uri;

      redir_uri = g_strdup_printf ("%s/", soup_message_get_uri (msg)->path);
      soup_message_set_redirect (msg, SOUP_STATUS_MOVED_PERMANENTLY,
                                 redir_uri);
      g_free (redir_uri);
      return;
    }

    index_path = g_strdup_printf ("%s/index.html", path);
    if (g_stat (path, &st) != -1) {
      do_get (server, msg, index_path);
      g_free (index_path);
      return;
    }
    g_free (index_path);

    listing = get_directory_listing (path);
    soup_message_set_response (msg, "text/html",
                               SOUP_MEMORY_TAKE,
                               listing->str, listing->len);
    soup_message_set_status (msg, SOUP_STATUS_OK);
    g_string_free (listing, FALSE);
    return;
  }<commit_msg>[refactor] remove not needed code from server<commit_after>#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#include <libsoup/soup.h>
#include <glib.h>
#include <glib/gstdio.h>

#include "teleport-server.h"
#include "teleport-get.h"
#include "teleport-app.h"

static int port;
static SoupServer *glob_server;
//static const char *tls_cert_file, *tls_key_file;

static void
do_get (SoupServer *server, SoupMessage *msg, const char *path)
{
  GStatBuf st;

  printf("paths: %s", path);
  if (g_stat (path, &st) == -1) {
    if (errno == EPERM)
      soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
    else if (errno == ENOENT)
      soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
    else
      soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    return;
  }
{'fraction_non_alphanumeric': 0.1485148514851485, 'fraction_numerical': 0.0024752475247524753, 'mean_word_length': 3.2492113564668768, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4515345', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1518, 'n_words': 333}