<commit_before>/*
 * System_httpServer.cpp
 *
 *  Created on: Jun 15, 2014
 *      Author: Pimenta
 */

// this
#include "System.hpp"

// standard
#include <cstring>
#include <cstdio>
#include <string>

// local
#include "Defines.hpp"
#include "Concurrency.hpp"
#include "Network.hpp"
#include "Helpers.hpp"
#include "FileSystem.hpp"

using namespace std;
using namespace concurrency;
using namespace network;
using namespace helpers;

// static variables
static TCPConnection* client = nullptr;
static ByteQueue fileData;

static void recvFile() {
  fileData.resize(0);
  string tmp;

  while(true){
    tmp = "";
    for (char c; (c = client->recv<char>()) != '\n'; tmp += c);
    if(tmp.find("Tamanho") != string::npos){
      fileData.resize(fromString<int>(tmp.substr(tmp.find(":") + 1, tmp.size() - 2).c_str()));
    } else if (tmp == "\r"){
      break;
    }
  }
  while(true){
    tmp = "";
    for (char c; (c = client->recv<char>()) != '\n'; tmp += c);
    if (tmp == "\r"){
      break;
    }
  }
  client->recv(fileData);
  client->recv<char>();
  client->recv<char>();
  for (char c; (c = client->recv<char>()) != '\n';);
}

void System::httpServer() {
  client = httpTCPServer.accept();
  if (client == nullptr)
    return;
  
  string requestLine;

  for (char c; (c = client->recv<char>()) != '\n'; requestLine += c); // receive the request line
  for (; requestLine[0] != ' '; requestLine = requestLine.substr(1, requestLine.size())); // remove method
  requestLine = requestLine.substr(1, requestLine.size()); // remove space after method
  for (; requestLine[requestLine.size() - 1] != ' '; requestLine = requestLine.substr(0, requestLine.size() - 1)); // remove http version
  requestLine = requestLine.substr(0, requestLine.size() - 1);// remove space before http version
  if (requestLine.find("Cfile") != string::npos)
    recvFile();
  else { // if the request is NOT for file upload
    fileData.resize(SIZE_HTTPSERVER_MAXBUF);
    client->recv(fileData); // actually, this is the request body... discarding
  }
  
  if (requestLine.find("?") != string::npos) {
    httpServer_dataRequest(requestLine);
  } else {
    if (requestLine == "/")
    requestLine += "/index.html";
    FILE* fp = fopen((string("./www") + requestLine).c_str(), "rb");
    if (fp) {
      if (requestLine.find(".html") != string::npos) {
        const char* header = 
          "HTTP/1.1 200 OK\r\n"
          "Connection: close\r\r"
          "Content-Type: text/html\r\n"
          "\r\n"
        ;
        client->send(header, strlen(header));
      }
      else if (requestLine.find(".css") != string::npos) {<commit_msg>Fixing motherfucking bug of dump browser requests<commit_after>/*
 * System_httpServer.cpp
 *
 *  Created on: Jun 15, 2014
 *      Author: Pimenta
 */

// this
#include "System.hpp"

// standard
#include <cstring>
#include <cstdio>
#include <string>

// local
#include "Defines.hpp"
#include "Concurrency.hpp"
#include "Network.hpp"
#include "Helpers.hpp"
#include "FileSystem.hpp"

using namespace std;
using namespace concurrency;
using namespace network;
using namespace helpers;

// static variables
static TCPConnection* client = nullptr;
static ByteQueue fileData;

static void recvFile() {
  fileData.resize(0);
  string tmp;

  while(true){
    tmp = "";
    for (char c; (c = client->recv<char>()) != '\n'; tmp += c);
    if(tmp.find("Tamanho") != string::npos){
      fileData.resize(fromString<int>(tmp.substr(tmp.find(":") + 1, tmp.size() - 2).c_str()));
    } else if (tmp == "\r"){
      break;
    }
  }
  while(true){
    tmp = "";
    for (char c; (c = client->recv<char>()) != '\n'; tmp += c);
    if (tmp == "\r"){
      break;
    }
  }
  client->recv(fileData);
  client->recv<char>();
  client->recv<char>();
  for (char c; (c = client->recv<char>()) != '\n';);
}

void System::httpServer() {
  client = httpTCPServer.accept();
  if (client == nullptr)
    return;
  
  string requestLine;
  
  for (char c; (c = client->recv<char>()) != '\n'; requestLine += c); // receive the request line
  if (!requestLine.size()) return; // for dumb requests
  for (; requestLine[0] != ' '; requestLine = requestLine.substr(1, requestLine.size())); // remove method
  requestLine = requestLine.substr(1, requestLine.size()); // remove space after method
  for (; requestLine[requestLine.size() - 1] != ' '; requestLine = requestLine.substr(0, requestLine.size() - 1)); // remove http version
  requestLine = requestLine.substr(0, requestLine.size() - 1);// remove space before http version
  if (requestLine.find("Cfile") != string::npos)
    recvFile();
  else { // if the request is NOT for file upload
    fileData.resize(SIZE_HTTPSERVER_MAXBUF);
    client->recv(fileData); // actually, this is the request body... discarding
  }
  
  if (requestLine.find("?") != string::npos) {
    httpServer_dataRequest(requestLine);
  } else {
    if (requestLine == "/")
    requestLine += "/index.html";
    FILE* fp = fopen((string("./www") + requestLine).c_str(), "rb");
    if (fp) {
      if (requestLine.find(".html") != string::npos) {
        const char* header = 
          "HTTP/1.1 200 OK\r\n"
          "Connection: close\r\r"
          "Content-Type: text/html\r\n"
          "\r\n"
        ;
        client->send(header, strlen(header));
      }
      else if (requestLine.find(".css") != string::npos) {
{'fraction_non_alphanumeric': 0.1830590875093493, 'fraction_numerical': 0.008227374719521317, 'mean_word_length': 3.1497284716834755, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 41, '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': '631706', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1764, 'n_words': 440}