<commit_before>// -*- c++ -*-
//
// Copyright 2010 <NAME> <<EMAIL>>
// Date: May 2010
//
// Updated: 08-JAN-2012 for Arduno IDE 1.0 by <<EMAIL>>
// Updated: 29-MAR-2013 replacing strtoul with parseHexChar by <<EMAIL>>
//
// TinyWebServer for Arduino.
//
// The DEBUG flag will enable serial console logging in this library
// By default Debugging to the Serial console is OFF.
// This ensures that any scripts using the Serial port are not corrupted
// by the tinywebserver libraries debugging messages.
//
// To ENABLE debugging set the following:
// DEBUG 1 and ENSURE that you have configured the serial port in the
// main Arduino script.
//
// There is an overall size increase of about 340 bytes in code size
// when the debugging is enabled and debugging lines are preceded by 'TWS:'

#define DEBUG 0

#include "Arduino.h"

extern "C" {

#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
}

#include <Ethernet.h>
#include <Flash.h>
#include <SD.h>

#include "TinyWebServer.h"

// Temporary buffer.
static char buffer[160];

FLASH_STRING(mime_types,
  "HTM*text/html|"
  "TXT*text/plain|"
  "CSS*text/css|"
  "XML*text/xml|"
  "JS*text/javascript|"

  "GIF*image/gif|"
  "JPG*image/jpeg|"
  "PNG*image/png|"
  "ICO*image/vnd.microsoft.icon|"

  "MP3*audio/mpeg|"
);

void *malloc_check(size_t size) {
  void* r = malloc(size);
#if DEBUG
  if (!r) {
    Serial << F("TWS:No space for malloc: " ); Serial.println(size, DEC);
  }
#endif
  return r;
}

// Offset for text/html in `mime_types' above.
static const TinyWebServer::MimeType text_html_content_type = 4;

TinyWebServer::TinyWebServer(PathHandler handlers[],
			     const char** headers,
                             const int port)
  : handlers_(handlers),
    server_(EthernetServer(port)),
    path_(NULL),
    request_type_(UNKNOWN_REQUEST),
    client_(EthernetClient(255)) {
  if (headers) {
    int size = 0;
    for (int i = 0; headers[i]; i++) {
      size++;
    }
    headers_ = (HeaderValue*)malloc_check(sizeof(HeaderValue) * (size + 1));
    if (headers_) {
      for (int i = 0; i < size; i++) {
        headers_[i].header = headers[i];
        headers_[i].value = NULL;
      }
      headers_[size].header = NULL;
    }
  }
}

void TinyWebServer::begin() {
  server_.begin();
}

// Process headers.
boolean TinyWebServer::process_headers() {
  if (headers_) {
    // First clear the header values from the previous HTTP request.
    for (int i = 0; headers_[i].header; i++) {
      if (headers_[i].value) {
        free(headers_[i].value);
        // Ensure the pointer is cleared once the memory is freed.
        headers_[i].value = NULL;
      }
    }
  }

  enum State {
    ERROR,
    START_LINE,
    HEADER_NAME,
    HEADER_VALUE,
    HEADER_VALUE_SKIP_INITIAL_SPACES,
    HEADER_IGNORE_VALUE,
    END_HEADERS,
  };
  State state = START_LINE;

  char ch;
  int pos;
  const char* header;
  while (1) {
    if (should_stop_processing()) {
      return false;
    }
    if (!read_next_char(client_, (uint8_t*)&ch)) {
      continue;
    }
#if DEBUG
    Serial.print(ch);
#endif
    switch (state) {
    case START_LINE:
      if (ch == '\r') {
	break;
      } else if (ch == '\n') {
	state = END_HEADERS;
      } else if (isalnum(ch) || ch == '-') {
	pos = 0;
	buffer[pos++] = ch;
	state = HEADER_NAME;
      } else {
	state = ERROR;
      }<commit_msg>Update TinyWebServer.cpp<commit_after>// -*- c++ -*-
//
// Copyright 2010 <NAME> <<EMAIL>>
// Date: May 2010
//
// Updated: 08-JAN-2012 for Arduno IDE 1.0 by <<EMAIL>>
// Updated: 29-MAR-2013 replacing strtoul with parseHexChar by <<EMAIL>>
//
// TinyWebServer for Arduino.
//
// The DEBUG flag will enable serial console logging in this library
// By default Debugging to the Serial console is OFF.
// This ensures that any scripts using the Serial port are not corrupted
// by the tinywebserver libraries debugging messages.
//
// To ENABLE debugging set the following:
// DEBUG 1 and ENSURE that you have configured the serial port in the
// main Arduino script.
//
// There is an overall size increase of about 340 bytes in code size
// when the debugging is enabled and debugging lines are preceded by 'TWS:'

#define DEBUG 0

// 10 milliseconds read timeout
#define READ_TIMEOUT 10

#include "Arduino.h"

extern "C" {

#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
}

#include <Ethernet.h>
#include <Flash.h>
#include <SD.h>

#include "TinyWebServer.h"

// Temporary buffer.
static char buffer[160];

FLASH_STRING(mime_types,
  "HTM*text/html|"
  "TXT*text/plain|"
  "CSS*text/css|"
  "XML*text/xml|"
  "JS*text/javascript|"

  "GIF*image/gif|"
  "JPG*image/jpeg|"
  "PNG*image/png|"
  "ICO*image/vnd.microsoft.icon|"

  "MP3*audio/mpeg|"
);

void *malloc_check(size_t size) {
  void* r = malloc(size);
#if DEBUG
  if (!r) {
    Serial << F("TWS:No space for malloc: " ); Serial.println(size, DEC);
  }
#endif
  return r;
}

// Offset for text/html in `mime_types' above.
static const TinyWebServer::MimeType text_html_content_type = 4;

TinyWebServer::TinyWebServer(PathHandler handlers[],
			     const char** headers,
                             const int port)
  : handlers_(handlers),
    server_(EthernetServer(port)),
    path_(NULL),
    request_type_(UNKNOWN_REQUEST),
    client_(EthernetClient(255)) {
  if (headers) {
    int size = 0;
    for (int i = 0; headers[i]; i++) {
      size++;
    }
    headers_ = (HeaderValue*)malloc_check(sizeof(HeaderValue) * (size + 1));
    if (headers_) {
      for (int i = 0; i < size; i++) {
        headers_[i].header = headers[i];
        headers_[i].value = NULL;
      }
      headers_[size].header = NULL;
    }
  }
}

void TinyWebServer::begin() {
  server_.begin();
}

// Process headers.
boolean TinyWebServer::process_headers() {
  if (headers_) {
    // First clear the header values from the previous HTTP request.
    for (int i = 0; headers_[i].header; i++) {
      if (headers_[i].value) {
        free(headers_[i].value);
        // Ensure the pointer is cleared once the memory is freed.
        headers_[i].value = NULL;
      }
    }
  }

  enum State {
    ERROR,
    START_LINE,
    HEADER_NAME,
    HEADER_VALUE,
    HEADER_VALUE_SKIP_INITIAL_SPACES,
    HEADER_IGNORE_VALUE,
    END_HEADERS,
  };
  State state = START_LINE;

  char ch;
  int pos;
  const char* header;
  uint32_t start_time = millis();
  while (1) {
    if (should_stop_processing()) {
      return false;
    }
    if (millis() - start_time > READ_TIMEOUT) {
      return false;
    }
    if (!read_next_char(client_, (uint8_t*)&ch)) {
      continue;
    }
    start_time = millis();
#if DEBUG
    Serial.print(ch);
#endif
    switch (state) {
    case START_LINE:
      if (ch == '\r') {
	break;
      } else if (ch == '\n') {
	state = END_HEADERS;
      } else if (isalnum(ch) || ch == '-') {
	pos = 0;
	buffer[pos++] = ch;
	state = HEADER_NAME;
      } else {
	state = ERROR;
      }
{'fraction_non_alphanumeric': 0.14092023654983413, 'fraction_numerical': 0.0132698687436896, 'mean_word_length': 2.85650723025584, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3552207', 'n_tokens_mistral': 2538, 'n_tokens_neox': 2377, 'n_words': 673}