Commit 1529e4f7 authored by Laura Schlimmer's avatar Laura Schlimmer
Browse files

Merge branch 'fnordmetric2' of github.com:paulasmuth/fnordmetric-dev into adminui_fixes

parents c1e13a82 b245519d
Loading
Loading
Loading
Loading
−100 KiB

File deleted.

+7 −16
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ set(FNORDMETRIC_SOURCES
    stage/src/fnordmetric/io/pagemanager.cc
    stage/src/fnordmetric/net/udpserver.cc
    stage/src/fnordmetric/environment.cc
    stage/src/fnordmetric/http/httpinputstream.cc
    stage/src/fnordmetric/http/httpoutputstream.cc
    stage/src/fnordmetric/http/httpmessage.cc
    stage/src/fnordmetric/http/httprequest.cc
    stage/src/fnordmetric/http/httpresponse.cc
@@ -155,22 +157,6 @@ if(MYSQL_FOUND)
  target_link_libraries(fnord-static mysqlclient)
endif()

pkg_check_modules(XZERO "libxzero")
SET(XZERO_BUNDLED libxzero-0.11.0_pre20141105.tgz)
if(XZERO_FOUND)
  link_directories(${XZERO_LIBRARY_DIRS})
  include_directories(${XZERO_INCLUDE_DIRS})
  target_link_libraries(fnord-static ${XZERO_LIBRARIES})
else()
  set(XZERO_STAGE_DIR ${CMAKE_SOURCE_DIR}/stage/libxzero)
  execute_process(COMMAND mkdir -p ${XZERO_STAGE_DIR})
  execute_process(COMMAND tar -xz -C ${XZERO_STAGE_DIR} -f ${CMAKE_SOURCE_DIR}/../../3rdparty/${XZERO_BUNDLED})
  add_subdirectory(${XZERO_STAGE_DIR})
  link_directories(${XZERO_STAGE_DIR})
  include_directories(${XZERO_STAGE_DIR}/include)
  target_link_libraries(fnord-static xzero)
endif()

# fnordmetric
add_executable(fnordmetric-cli stage/src/fnordmetric/cli.cc)
target_link_libraries(fnordmetric-cli fnord-static)
@@ -194,6 +180,11 @@ add_executable(tests/test-uri
    stage/src/fnordmetric/util/uri_test.cc)
target_link_libraries(tests/test-uri fnord-static)

add_executable(tests/test-http
    stage/src/fnordmetric/http/http_test.cc)
target_link_libraries(tests/test-http fnord-static)

target_link_libraries(tests/test-uri fnord-static)
add_executable(tests/test-fnv
    stage/src/fnordmetric/util/fnv_test.cc)
target_link_libraries(tests/test-fnv fnord-static)
+0 −27
Original line number Diff line number Diff line
@@ -45,33 +45,6 @@ void CLI::parseArgs(Environment* env, const std::vector<std::string>& argv) {
      "Write output to a file",
      "<format>");

  // Start a user interface:
  flags->defineFlag(
      "repl",
      FlagParser::T_SWITCH,
      false,
      NULL,
      NULL,
      "Start an interactive readline shell",
      NULL);

  flags->defineFlag(
      "web",
      FlagParser::T_INTEGER,
      false,
      NULL,
      NULL,
      "Start a web interface on this port",
      "<port>");

  flags->defineFlag(
      "cgi",
      FlagParser::T_SWITCH,
      false,
      NULL,
      NULL,
      "Run as CGI script");

  flags->defineFlag(
      "verbose",
      FlagParser::T_SWITCH,
+27 −21
Original line number Diff line number Diff line
@@ -11,12 +11,18 @@
#include <stdio.h>
#include <string.h>
#include <fnordmetric/util/inputstream.h>
#include <fnordmetric/http/httpinputstream.h>
#include <fnordmetric/http/httpoutputstream.h>
#include <fnordmetric/http/httprequest.h>
#include <fnordmetric/http/httpresponse.h>
#include <fnordmetric/util/unittest.h>
#include <fnordmetric/util/runtimeexception.h>

using namespace fnord::http;
using fnord::http::HTTPInputStream;
using fnord::http::HTTPOutputStream;
using fnordmetric::util::StringInputStream;
using fnordmetric::util::StringOutputStream;

UNIT_TEST(HTTPTest);

@@ -24,9 +30,9 @@ TEST_CASE(HTTPTest, ParseHTTP1dot0Request, [] () {
  auto req = "GET / HTTP/1.0\r\n" \
             "\r\n";

  fnord::httputil::StringInputStream is(req);
  fnord::httphttp::HTTPInputStream http_is(&is);
  fnord::httphttp::HTTPRequest request;
  StringInputStream is(req);
  HTTPInputStream http_is(&is);
  HTTPRequest request;
  request.readFromInputStream(&http_is);

  EXPECT_EQ(request.getMethod(), "GET");
@@ -40,9 +46,9 @@ TEST_CASE(HTTPTest, ParseHTTP1dot0KeepaliveRequest, [] () {
             "Connection: keep-alive\r\n" \
             "\r\n";

  fnord::httputil::StringInputStream is(req);
  fnord::httphttp::HTTPInputStream http_is(&is);
  fnord::httphttp::HTTPRequest request;
  StringInputStream is(req);
  HTTPInputStream http_is(&is);
  HTTPRequest request;
  request.readFromInputStream(&http_is);

  EXPECT_EQ(request.getMethod(), "GET");
@@ -55,9 +61,9 @@ TEST_CASE(HTTPTest, ParseHTTP1dot1Request, [] () {
  auto req = "GET / HTTP/1.1\r\n" \
             "\r\n";

  fnord::httputil::StringInputStream is(req);
  fnord::httphttp::HTTPInputStream http_is(&is);
  fnord::httphttp::HTTPRequest request;
  StringInputStream is(req);
  HTTPInputStream http_is(&is);
  HTTPRequest request;
  request.readFromInputStream(&http_is);

  EXPECT_EQ(request.getMethod(), "GET");
@@ -70,13 +76,13 @@ TEST_CASE(HTTPTest, PopulateHTTPResponseFromHTTP1dot1Request, [] () {
  auto req = "GET / HTTP/1.1\r\n" \
             "\r\n";

  fnord::httputil::StringInputStream is(req);
  fnord::httphttp::HTTPInputStream http_is(&is);
  fnord::httphttp::HTTPRequest request;
  StringInputStream is(req);
  HTTPInputStream http_is(&is);
  HTTPRequest request;
  request.readFromInputStream(&http_is);
  EXPECT(request.keepalive() == true);

  fnord::httphttp::HTTPResponse response;
  HTTPResponse response;
  response.populateFromRequest(request);

  EXPECT_EQ(response.getVersion(), "HTTP/1.1");
@@ -86,12 +92,12 @@ TEST_CASE(HTTPTest, PopulateHTTPResponseFromHTTP1dot0Request, [] () {
  auto req = "GET / HTTP/1.0\r\n" \
             "\r\n";

  fnord::httputil::StringInputStream is(req);
  fnord::httphttp::HTTPInputStream http_is(&is);
  fnord::httphttp::HTTPRequest request;
  StringInputStream is(req);
  HTTPInputStream http_is(&is);
  HTTPRequest request;
  request.readFromInputStream(&http_is);

  fnord::httphttp::HTTPResponse response;
  HTTPResponse response;
  response.populateFromRequest(request);

  EXPECT_EQ(response.getVersion(), "HTTP/1.0");
@@ -103,12 +109,12 @@ TEST_CASE(HTTPTest, PopulateHTTPResponseFromHTTP1dot0KeepaliveRequest, [] () {
             "Connection: keep-alive\r\n" \
             "\r\n";

  fnord::httputil::StringInputStream is(req);
  fnord::httphttp::HTTPInputStream http_is(&is);
  fnord::httphttp::HTTPRequest request;
  StringInputStream is(req);
  HTTPInputStream http_is(&is);
  HTTPRequest request;
  request.readFromInputStream(&http_is);

  fnord::httphttp::HTTPResponse response;
  HTTPResponse response;
  response.populateFromRequest(request);

  EXPECT_EQ(response.getVersion(), "HTTP/1.0");
+225 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <fnordmetric/http/httpinputstream.h>
#include <fnordmetric/util/runtimeexception.h>

using fnordmetric::util::InputStream;

namespace fnord {
namespace http {

HTTPInputStream::HTTPInputStream(
    InputStream* input_stream) :
    input_(input_stream),
    state_(HTTP_STATE_METHOD) {}

HTTPInputStream::~HTTPInputStream() {
}

InputStream* HTTPInputStream::getInputStream() const {
  return input_;
}

void HTTPInputStream::readStatusLine(
    std::string* method,
    std::string* url,
    std::string* version) {
  while (state_ == HTTP_STATE_METHOD) {
    readNextByte(method);
  }

  while (state_ == HTTP_STATE_URI) {
    readNextByte(url);
  }

  while (state_ == HTTP_STATE_VERSION) {
    readNextByte(version);
  }
}

void HTTPInputStream::readHeaders(
    std::vector<std::pair<std::string, std::string>>* target) {
  std::pair<std::string, std::string>* cur_header;

  while (state_ == HTTP_STATE_HKEY || state_ == HTTP_STATE_HVAL) {
    if (target->size() == 0 || 
        (state_ == HTTP_STATE_HKEY && target->back().second.size() > 0)) {
      target->emplace_back("", "");
    }

    while (state_ == HTTP_STATE_HKEY) {
      readNextByte(&target->back().first);
      std::transform(
          target->back().first.begin(),
          target->back().first.end(),
          target->back().first.begin(),
          ::tolower);
    }

    while (state_ == HTTP_STATE_HVAL) {
      readNextByte(&target->back().second);
    }
  }
}

void HTTPInputStream::readNextByte(std::string* target) {
  char byte;

  if (!input_->readNextByte(&byte)) {
    RAISE(kRuntimeError, "unexpected EOF while reading HTTP header");
  }

  switch (byte) {

    case '\r':
      return;

    case ' ':
      switch (state_) {

        case HTTP_STATE_METHOD:
          state_ = HTTP_STATE_URI;
          return;

        case HTTP_STATE_URI:
          state_ = HTTP_STATE_VERSION;
          return;

        default:
          break;

      }
      break;

    case '\n':
      switch (state_) {

        case HTTP_STATE_METHOD:
          RAISE(kRuntimeError, "invalid HTTP header");

        case HTTP_STATE_URI:
          RAISE(kRuntimeError, "invalid HTTP header");

        case HTTP_STATE_VERSION:
          state_ = HTTP_STATE_HKEY;
          return;

        case HTTP_STATE_HVAL:
          state_ = HTTP_STATE_HKEY;
          return;

        case HTTP_STATE_HKEY:
          state_ = HTTP_STATE_BODY;
          return;

        default:
          break;

      }
      break;

    case ':':
      if (state_ == HTTP_STATE_HKEY) {
        state_ = HTTP_STATE_HVAL;
        return;
      }
      break;
  }

  if (byte == ' ' && target->size() == 0) {
    return;
  }

  *target += byte;
}

/*
int http_read_method(http_req_t* req, char* method, int len) {
  if (strncmp(method, "HEAD", len) == 0)
    req->method = HTTP_METHOD_HEAD;

  else if (strncmp(method, "GET", len) == 0)
    req->method = HTTP_METHOD_GET;

  else if (strncmp(method, "POST", len) == 0)
    req->method = HTTP_METHOD_POST;

  else
    return -1;

  req->state_ = HTTP_STATE_URI;
  return 0;
}

int http_read_uri(http_req_t* req, char* uri, int len) {
  char *end = uri + len;
  int  n;

  if (len >= (int) sizeof(req->uri))
    return -1;

  strncpy(req->uri, uri, len);
  req->uri[len] = 0;
  req->uri_len  = len;

  for (; *end == ' ' || *end == '/' ||*end == '?' ||
    *end == '&'; end--);

  for (n = 0; n < HTTP_URI_MAXARGS && uri < end; n++) {
    req->uri_argv[n] = uri;

    if (*uri == '&') *uri = '?';

    for (uri++; uri < end &&
      *uri != '/' && *uri != '?' && *uri != '&'; uri++);
  }

  req->uri_argv[n] = uri + 1;
  req->uri_argc = n;
  req->state_ = HTTP_STATE_VERSION;

  return 0;
}


int http_read_version(http_req_t* req, char* version, int len) {
  if (len < 8)
    return -1;

  if (strncmp(version + 5, "1.1", len - 5) == 0)
    req->keepalive = 1;

  else
    req->keepalive = 0;

  req->state_ = HTTP_STATE_HKEY;
  return 0;
}

void http_read_header(http_req_t* req, char* hkey, int hkey_len, char* hval, int hval_len) {
  //hkey[hkey_len] = 0; hval[hval_len] = 0;
  //printf("header: (%i) '%s' => (%i) '%s'\n", hkey_len, hkey, hval_len, hval);

  if (strncmp(hkey, "Connection", hkey_len) == 0)
    if (strncmp(hval, "Keep-Alive", hval_len) == 0)
      req->keepalive = 1;

}

*/


}
}
Loading