<commit_before>#include <arpa/inet.h>    // inet_ntoa
#include <fstream>
#include <iostream>
#include <netdb.h>        // gethostbyname
#include <netinet/in.h>   // htonl, htons, inet_ntoa
#include <netinet/tcp.h>  // SO_REUSEADDR
#include <pthread.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <strings.h>      // bzero
#include <sys/types.h>    // socket, bind
#include <sys/socket.h>   // socket, bind, listen, inet_ntoa
#include <sys/uio.h>      // writev
#include <sys/time.h>
#include <unistd.h>       // read, write, close


using namespace std;

struct threadData
{
  int sd;
};

#define BUF_SIZE 16384
#define PORT 9649
//
// IF_FALSE_RETURN
//
// Macro to convieniently add basic error handling
//
#define IF_FALSE_RETURN(test, msg) \
  if (!(test)) \
  { \
    printf("%s\n", msg); \
    return -1; \
  }

// ConvertParameterToInt
//
// several of our parameters are expected to be well formed numbers
// since no negative values are expected, return -1 to indicate an error.
//
int ConvertParameterToInt(char* value)
{
  char* endptr = NULL;
  long int result = strtol(value, &endptr, 0);
  if (*value == '\0' || *endptr != '\0')
    return -1;

  return (int)result;
}

void* openAndSendFile(void* whatever)
{
  

  //  If file requested does not exist, return 404 Not Found code with custom File Not Found page
  //
  //
  //
  //  If HTTP request is for SecretFile.html, return 401 Unauthorized
  //
  //
  //
  //  If request is for file that is above the directory structure where web server is running ( for example, "GET ../../../etc/passwd" ), return 403 Forbidden
  //
  //
  //
  //  if server cannot understand request return 400 Bad Request
  //
  //
  //

  printf("Begin thread:\n");
  threadData* data = (threadData*)whatever;
  int sd = data->sd;
  delete data;
  // After receiving GET request...
  char databuf[BUF_SIZE];
  stringstream ssrequest;

  // Repeat reading data from the client into databuf[BUF_SIZE]. 
  // Note that the read system call may return without reading 
  // the entire data if the network is slow.
  int readed = read(sd, &databuf, BUF_SIZE);

  while(readed > 0)
  {
    printf("Reading data...\n");
    ssrequest.write(databuf, readed);
    readed = read(sd, &databuf, BUF_SIZE); 
  }

  printf("Printing ssrequest:\n");
  cout << ssrequest.str();

  // open file requested...

/*
  // and send with HTTP 200 OK code.
  int written = 0;
  while (written < sizeof(count))
  {
    int bytesWritten = write(sd, &count, sizeof(count));
    if (bytesWritten == -1)
    {
      printf("write failed\n");
      exit(1);
    }

    written += bytesWritten;
  }


  if (close(sd) != 0)
  {<commit_msg>Changed hedaer-reading loop to not hang on empty read.<commit_after>#include <arpa/inet.h>    // inet_ntoa
#include <fstream>
#include <iostream>
#include <netdb.h>        // gethostbyname
#include <netinet/in.h>   // htonl, htons, inet_ntoa
#include <netinet/tcp.h>  // SO_REUSEADDR
#include <pthread.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <strings.h>      // bzero
#include <sys/types.h>    // socket, bind
#include <sys/socket.h>   // socket, bind, listen, inet_ntoa
#include <sys/uio.h>      // writev
#include <sys/time.h>
#include <unistd.h>       // read, write, close


using namespace std;

struct threadData
{
  int sd;
};

#define BUF_SIZE 16384
#define PORT 9649
//
// IF_FALSE_RETURN
//
// Macro to convieniently add basic error handling
//
#define IF_FALSE_RETURN(test, msg) \
  if (!(test)) \
  { \
    printf("%s\n", msg); \
    return -1; \
  }

// ConvertParameterToInt
//
// several of our parameters are expected to be well formed numbers
// since no negative values are expected, return -1 to indicate an error.
//
int ConvertParameterToInt(char* value)
{
  char* endptr = NULL;
  long int result = strtol(value, &endptr, 0);
  if (*value == '\0' || *endptr != '\0')
    return -1;

  return (int)result;
}

void* openAndSendFile(void* whatever)
{
  

  //  If file requested does not exist, return 404 Not Found code with custom File Not Found page
  //
  //
  //
  //  If HTTP request is for SecretFile.html, return 401 Unauthorized
  //
  //
  //
  //  If request is for file that is above the directory structure where web server is running ( for example, "GET ../../../etc/passwd" ), return 403 Forbidden
  //
  //
  //
  //  if server cannot understand request return 400 Bad Request
  //
  //
  //

  printf("Begin thread:\n");
  threadData* data = (threadData*)whatever;
  int sd = data->sd;
  delete data;
  // After receiving GET request...
  char databuf[BUF_SIZE];
  stringstream ssrequest;

  // Repeat reading data from the client into databuf[BUF_SIZE]. 
  // Note that the read system call may return without reading 
  // the entire data if the network is slow.
  int readed = read(sd, &databuf, BUF_SIZE);

  while(readed > 0)
  {
    printf("Reading data...\n");
    ssrequest.write(databuf, readed);
    if(readed >= BUF_SIZE)
    {
      readed = read(sd, &databuf, BUF_SIZE);
    }
    else
    {
      readed = 0;
    }
  }

  printf("Printing ssrequest:\n");
  cout << ssrequest.str();

  // open file requested...

/*
  // and send with HTTP 200 OK code.
  int written = 0;
  while (written < sizeof(count))
  {
    int bytesWritten = write(sd, &count, sizeof(count));
    if (bytesWritten == -1)
    {
      printf("write failed\n");
      exit(1);
    }

    written += bytesWritten;
  }


  if (close(sd) != 0)
  {
{'fraction_non_alphanumeric': 0.14207848837209303, 'fraction_numerical': 0.012899709302325582, 'mean_word_length': 2.6872069658405895, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 42, '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': '590402', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1866, 'n_words': 579}