Commit 9ce8679f authored by Paul Asmuth's avatar Paul Asmuth
Browse files

serve static assets

parent 38ed2ed9
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -60,9 +60,11 @@ set(FNORDMETRIC_CLI_SOURCES
    ${FNORDMETRIC_QUERY_SOURCES}
    stage/src/fnordmetric/cli/cli.cc
    stage/src/fnordmetric/cli/flagparser.cc
    stage/src/fnordmetric/web/assets.cc
    stage/src/fnordmetric/web/webinterface.cc)

include_directories(stage/src)
include_directories(stage/assets)
set(CMAKE_CXX_FLAGS "-std=c++0x -stdlib=libc++")

add_definitions("-Wno-predefined-identifier-outside-function")
@@ -95,3 +97,4 @@ add_executable(tests/test-csv-backend
add_executable(tests/test-http
    ${FNORDMETRIC_SOURCES}
    stage/src/fnordmetric/http/http_test.cc)
+6 −2
Original line number Diff line number Diff line
@@ -3,13 +3,17 @@
#
# Licensed under the MIT license (see LICENSE).

all:
all: assets
	mkdir -p target/tests
	mkdir -p stage/src
	test -e stage/src/fnordmetric || ln -s ../../../../src stage/src/fnordmetric || true
	test -e stage/src/3rdparty || ln -s ../../../../3rdparty stage/src/3rdparty || true
	(cd target && cmake .. && make)

assets:
	mkdir -p stage/assets
	(cd ../.. && xxd -i assets/index.html) > stage/assets/asset_index.html.h
	(cd ../.. && xxd -i assets/fnordmetric-web.css) > stage/assets/fnordmetric_web.css.h

test: all
	@find target/tests -iname "test-*" | while read t; do (cd ../../ && build/cmake/$$t) || exit 1; done

+0 −2
Original line number Diff line number Diff line
@@ -35,8 +35,6 @@ void HTTPResponse::populateFromRequest(const HTTPRequest& request) {
  setVersion(request.getVersion());

  if (request.keepalive()) {
    addHeader("Connection", "keep-alive");
  } else {
    addHeader("Connection", "close");
  }
}
+24 −7
Original line number Diff line number Diff line
@@ -34,26 +34,43 @@ void ThreadedHTTPServer::onConnection(int fd) {
void ThreadedHTTPServer::handleConnection(int fd) {
  bool keepalive = false;

  util::FileInputStream input_stream(fd, false);
  util::FileOutputStream output_stream(fd, true);

  do {
    HTTPRequest request;
    HTTPResponse response;

    util::FileInputStream input_stream(fd, false);
    try {
      HTTPInputStream http_input_stream(&input_stream);
      request.readFromInputStream(&http_input_stream);

      if (!request.keepalive()) {
        keepalive = false;
      }

      response.populateFromRequest(request);
    } catch (util::RuntimeException e) {
      e.debugPrint();
      response.setStatus(400);
      response.addHeader("Connection", "close");
      response.addBody("Bad Request");
      keepalive = false;
    }

    bool handled = false;
    for (const auto& handler : handlers_) {
      if (handler->handleHTTPRequest(&request, &response)) {
        handled = true;
        break;
      }
    }

    if (!request.keepalive()) {
      keepalive = false;
    if (!handled) {
      response.setStatus(404);
      response.addBody("Not Found");
    }

    util::FileOutputStream output_stream(fd, false);
    HTTPOutputStream http_output_stream(&output_stream);
    response.writeToOutputStream(&http_output_stream);
  } while (keepalive);

src/web/assets.cc

0 → 100644
+30 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2011-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 <fnordmetric/util/runtimeexception.h>
#include <fnordmetric/web/assets.h>
#include <asset_index.html.h>
#include <fnordmetric_web.css.h>

namespace fnordmetric {
namespace web {

#define TRY_FILE(F,T) \
  if (filename == F) { \
    return std::string((const char *) T, sizeof(T)); \
  }

std::string Assets::getAsset(const std::string& filename) {
  TRY_FILE("index.html", assets_index_html);
  TRY_FILE("fnordmetric-web.css", assets_fnordmetric_web_css);
  RAISE(util::RuntimeException, "asset not found: %s\n", filename.c_str());
}

}
}
Loading