<commit_before>#include <stdio.h>

#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

#include <pthread.h>

#include "urweb.h"

int uw_backlog = 10;
int uw_bufsize = 1024;

typedef struct node {
  int fd;
  struct node *next;
} *node;

static node front = NULL, back = NULL;

static int empty() {
  return front == NULL;
}

static void enqueue(int fd) {
  node n = malloc(sizeof(struct node));

  n->fd = fd;
  n->next = NULL;
  if (back)
    back->next = n;
  else
    front = n;
  back = n;
}

static int dequeue() {
  int ret = front->fd;

  front = front->next;
  if (!front)
    back = NULL;

  return ret;
}

static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;

#define MAX_RETRIES 5

int uw_db_begin(uw_context);
int uw_db_commit(uw_context);
int uw_db_rollback(uw_context);

static int try_rollback(uw_context ctx) {
  int r = uw_db_rollback(ctx);

  if (r) {
    printf("Error running SQL ROLLBACK\n");
    uw_reset(ctx);
    uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
    uw_write(ctx, "Content-type: text/plain\r\n\r\n");
    uw_write(ctx, "Error running SQL ROLLBACK\n");
  }

  return r;
}

static void *worker(void *data) {
  int me = *(int *)data, retries_left = MAX_RETRIES;
  uw_context ctx = uw_init(0, 1024, 0);
  
  while (1) {
    failure_kind fk = uw_begin_init(ctx);

    if (fk == SUCCESS) {
      uw_db_init(ctx);
      printf("Database connection initialized.\n");
      break;
    } else if (fk == BOUNDED_RETRY) {
      if (retries_left) {
        printf("Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
        --retries_left;
      } else {
        printf("Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
        uw_free(ctx);
        return NULL;
      }
    } else if (fk == UNLIMITED_RETRY)
      printf("Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
    else if (fk == FATAL) {
      printf("Fatal initialization error: %s\n", uw_error_message(ctx));
      uw_free(ctx);
      return NULL;
    } else {
      printf("Unknown uw_handle return code!\n");
      uw_free(ctx);
      return NULL;
    }
  }

  while (1) {
    char buf[uw_bufsize+1], *back = buf, *s;
    int sock;

    pthread_mutex_lock(&queue_mutex);
    while (empty())
      pthread_cond_wait(&queue_cond, &queue_mutex);
    sock = dequeue();
    pthread_mutex_unlock(&queue_mutex);

    printf("Handling connection with thread #%d.\n", me);

    while (1) {
      unsigned retries_left = MAX_RETRIES;
      int r = recv(sock, back, uw_bufsize - (back - buf), 0);

      if (r < 0) {
        fprintf(stderr, "Recv failed\n");
        break;
      }

      if (r == 0) {
        printf("Connection closed.\n");
        break;
      }

      printf("Received %d bytes.\n", r);

      back += r;
      *back = 0;
    
      if (s = strstr(buf, "\r\n\r\n")) {
        failure_kind fk;
        char *cmd, *path, *headers, path_copy[uw_bufsize+1], *inputs;

        s[2] = 0;

        if (!(s = strstr(buf, "\r\n"))) {
          fprintf(stderr, "No newline in buf\n");
          break;
        }

        *s = 0;
        headers = s + 2;
        cmd = s = buf;

        printf("Read: %s\n", buf);
      
        if (!strsep(&s, " ")) {
          fprintf(stderr, "No first space in HTTP command\n");
          break;
        }

        if (strcmp(cmd, "GET")) {
          fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
          break;
        }

        path = s;
        if (!strsep(&s, " ")) {
          fprintf(stderr, "No second space in HTTP command\n");
          break;
        }

        if (inputs = strchr(path, '?')) {
          char *name, *value;
          *inputs++ = 0;

          while (*inputs) {
            name = inputs;
            if (inputs = strchr(inputs, '&'))
              *inputs++ = 0;
            else
              inputs = strchr(name, 0);

            if (value = strchr(name, '=')) {
              *value++ = 0;
              uw_set_input(ctx, name, value);
            }
            else
              uw_set_input(ctx, name, "");
          }
        }

        printf("Serving URI %s....\n", path);

        uw_set_headers(ctx, headers);
        uw_write(ctx, "<html>");

        while (1) {
          if (uw_db_begin(ctx)) {
            printf("Error running SQL BEGIN\n");
            if (retries_left)
              --retries_left;
            else {
              fk = FATAL;
              uw_reset(ctx);
              uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
              uw_write_header(ctx, "Content-type: text/plain\r\n\r\n");
              uw_write(ctx, "Error running SQL BEGIN\n");

              break;
            }
          }
<commit_msg>Fix missing initial <html> after restarts<commit_after>#include <stdio.h>

#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

#include <pthread.h>

#include "urweb.h"

int uw_backlog = 10;
int uw_bufsize = 1024;

typedef struct node {
  int fd;
  struct node *next;
} *node;

static node front = NULL, back = NULL;

static int empty() {
  return front == NULL;
}

static void enqueue(int fd) {
  node n = malloc(sizeof(struct node));

  n->fd = fd;
  n->next = NULL;
  if (back)
    back->next = n;
  else
    front = n;
  back = n;
}

static int dequeue() {
  int ret = front->fd;

  front = front->next;
  if (!front)
    back = NULL;

  return ret;
}

static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;

#define MAX_RETRIES 5

int uw_db_begin(uw_context);
int uw_db_commit(uw_context);
int uw_db_rollback(uw_context);

static int try_rollback(uw_context ctx) {
  int r = uw_db_rollback(ctx);

  if (r) {
    printf("Error running SQL ROLLBACK\n");
    uw_reset(ctx);
    uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
    uw_write(ctx, "Content-type: text/plain\r\n\r\n");
    uw_write(ctx, "Error running SQL ROLLBACK\n");
  }

  return r;
}

static void *worker(void *data) {
  int me = *(int *)data, retries_left = MAX_RETRIES;
  uw_context ctx = uw_init(0, 1024, 0);
  
  while (1) {
    failure_kind fk = uw_begin_init(ctx);

    if (fk == SUCCESS) {
      uw_db_init(ctx);
      printf("Database connection initialized.\n");
      break;
    } else if (fk == BOUNDED_RETRY) {
      if (retries_left) {
        printf("Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
        --retries_left;
      } else {
        printf("Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
        uw_free(ctx);
        return NULL;
      }
    } else if (fk == UNLIMITED_RETRY)
      printf("Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
    else if (fk == FATAL) {
      printf("Fatal initialization error: %s\n", uw_error_message(ctx));
      uw_free(ctx);
      return NULL;
    } else {
      printf("Unknown uw_handle return code!\n");
      uw_free(ctx);
      return NULL;
    }
  }

  while (1) {
    char buf[uw_bufsize+1], *back = buf, *s;
    int sock;

    pthread_mutex_lock(&queue_mutex);
    while (empty())
      pthread_cond_wait(&queue_cond, &queue_mutex);
    sock = dequeue();
    pthread_mutex_unlock(&queue_mutex);

    printf("Handling connection with thread #%d.\n", me);

    while (1) {
      unsigned retries_left = MAX_RETRIES;
      int r = recv(sock, back, uw_bufsize - (back - buf), 0);

      if (r < 0) {
        fprintf(stderr, "Recv failed\n");
        break;
      }

      if (r == 0) {
        printf("Connection closed.\n");
        break;
      }

      printf("Received %d bytes.\n", r);

      back += r;
      *back = 0;
    
      if (s = strstr(buf, "\r\n\r\n")) {
        failure_kind fk;
        char *cmd, *path, *headers, path_copy[uw_bufsize+1], *inputs;

        s[2] = 0;

        if (!(s = strstr(buf, "\r\n"))) {
          fprintf(stderr, "No newline in buf\n");
          break;
        }

        *s = 0;
        headers = s + 2;
        cmd = s = buf;

        printf("Read: %s\n", buf);
      
        if (!strsep(&s, " ")) {
          fprintf(stderr, "No first space in HTTP command\n");
          break;
        }

        if (strcmp(cmd, "GET")) {
          fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
          break;
        }

        path = s;
        if (!strsep(&s, " ")) {
          fprintf(stderr, "No second space in HTTP command\n");
          break;
        }

        if (inputs = strchr(path, '?')) {
          char *name, *value;
          *inputs++ = 0;

          while (*inputs) {
            name = inputs;
            if (inputs = strchr(inputs, '&'))
              *inputs++ = 0;
            else
              inputs = strchr(name, 0);

            if (value = strchr(name, '=')) {
              *value++ = 0;
              uw_set_input(ctx, name, value);
            }
            else
              uw_set_input(ctx, name, "");
          }
        }

        printf("Serving URI %s....\n", path);

        uw_set_headers(ctx, headers);

        while (1) {
          uw_write(ctx, "<html>");

          if (uw_db_begin(ctx)) {
            printf("Error running SQL BEGIN\n");
            if (retries_left)
              --retries_left;
            else {
              fk = FATAL;
              uw_reset(ctx);
              uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
              uw_write_header(ctx, "Content-type: text/plain\r\n\r\n");
              uw_write(ctx, "Error running SQL BEGIN\n");

              break;
            }
          }

{'fraction_non_alphanumeric': 0.14363393864778906, 'fraction_numerical': 0.008412844977941931, 'mean_word_length': 2.0207623179423613, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 30, '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': '5747369', 'n_tokens_mistral': 3470, 'n_tokens_neox': 3374, 'n_words': 817}